blob: 5a4ed677d5ee799bdda297377df7e837bb920c8e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar9937a052019-06-15 15:45:06 +020031static char *e_cannot_mod = N_("E995: Cannot modify existing variable");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020032#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020033static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020034#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000035
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010036#define NAMESPACE_CHAR (char_u *)"abglstvw"
37
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020038static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000039#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000042 * Old Vim variables such as "v:version" are also available without the "v:".
43 * Also in functions. We need a special hashtable for them.
44 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000045static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000046
47/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000048 * When recursively copying lists and dicts we need to remember which ones we
49 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000050 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000051 */
52static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020053
Bram Moolenaard9fba312005-06-26 22:34:35 +000054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000055 * Array to hold the hashtab with variables local to each sourced script.
56 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000058typedef struct
59{
60 dictitem_T sv_var;
61 dict_T sv_dict;
62} scriptvar_T;
63
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020064static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
65#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
66#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68static int echo_attr = 0; /* attributes used for ":echo" */
69
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000070/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000071static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000074 * Info used by a ":for" loop.
75 */
Bram Moolenaar33570922005-01-25 22:26:29 +000076typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000077{
78 int fi_semicolon; /* TRUE if ending in '; var]' */
79 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000080 listwatch_T fi_lw; /* keep an eye on the item used. */
81 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010082 int fi_bi; /* index of blob */
83 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000084} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000085
Bram Moolenaara7043832005-01-21 11:56:39 +000086
87/*
Bram Moolenaar33570922005-01-25 22:26:29 +000088 * Array to hold the value of v: variables.
89 * The value is in a dictitem, so that it can also be used in the v: scope.
90 * The reason to use this table anyway is for very quick access to the
91 * variables with the VV_ defines.
92 */
Bram Moolenaar33570922005-01-25 22:26:29 +000093
94/* values for vv_flags: */
95#define VV_COMPAT 1 /* compatible, also used without "v:" */
96#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000097#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000098
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010099#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000100
101static struct vimvar
102{
103 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100104 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000105 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
106} vimvars[VV_LEN] =
107{
108 /*
109 * The order here must match the VV_ defines in vim.h!
110 * Initializing a union does not work, leave tv.vval empty to get zero's.
111 */
112 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
113 {VV_NAME("count1", VAR_NUMBER), VV_RO},
114 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
115 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
116 {VV_NAME("warningmsg", VAR_STRING), 0},
117 {VV_NAME("statusmsg", VAR_STRING), 0},
118 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
119 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
120 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
121 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
122 {VV_NAME("termresponse", VAR_STRING), VV_RO},
123 {VV_NAME("fname", VAR_STRING), VV_RO},
124 {VV_NAME("lang", VAR_STRING), VV_RO},
125 {VV_NAME("lc_time", VAR_STRING), VV_RO},
126 {VV_NAME("ctype", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
128 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
129 {VV_NAME("fname_in", VAR_STRING), VV_RO},
130 {VV_NAME("fname_out", VAR_STRING), VV_RO},
131 {VV_NAME("fname_new", VAR_STRING), VV_RO},
132 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
133 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
134 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
136 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
137 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
138 {VV_NAME("progname", VAR_STRING), VV_RO},
139 {VV_NAME("servername", VAR_STRING), VV_RO},
140 {VV_NAME("dying", VAR_NUMBER), VV_RO},
141 {VV_NAME("exception", VAR_STRING), VV_RO},
142 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
143 {VV_NAME("register", VAR_STRING), VV_RO},
144 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
145 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000146 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
147 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000148 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000149 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
150 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200153 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000154 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
156 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000157 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000158 {VV_NAME("swapname", VAR_STRING), VV_RO},
159 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000160 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200161 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000162 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200163 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000164 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
165 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000166 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000167 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100168 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000169 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200170 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200171 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200172 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200173 {VV_NAME("option_new", VAR_STRING), VV_RO},
174 {VV_NAME("option_old", VAR_STRING), VV_RO},
Bram Moolenaard7c96872019-06-15 17:12:48 +0200175 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
176 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
177 {VV_NAME("option_command", VAR_STRING), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200178 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100179 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100180 {VV_NAME("false", VAR_SPECIAL), VV_RO},
181 {VV_NAME("true", VAR_SPECIAL), VV_RO},
182 {VV_NAME("null", VAR_SPECIAL), VV_RO},
183 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100184 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200185 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200186 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
193 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
194 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
195 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100196 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200197 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
198 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200199 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200200 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
201 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
202 {VV_NAME("event", VAR_DICT), VV_RO},
203 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000204};
205
206/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000207#define vv_type vv_di.di_tv.v_type
208#define vv_nr vv_di.di_tv.vval.v_number
209#define vv_float vv_di.di_tv.vval.v_float
210#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000211#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200212#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100213#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000214#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000215
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200216static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define vimvarht vimvardict.dv_hashtab
218
Bram Moolenaar9937a052019-06-15 15:45:06 +0200219static void ex_let_const(exarg_T *eap, int is_const);
220static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
222static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void list_glob_vars(int *first);
224static void list_buf_vars(int *first);
225static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void list_vim_vars(int *first);
228static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100229static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200230static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
231static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
234static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
235static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
236static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000237
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int eval2(char_u **arg, typval_T *rettv, int evaluate);
239static int eval3(char_u **arg, typval_T *rettv, int evaluate);
240static int eval4(char_u **arg, typval_T *rettv, int evaluate);
241static int eval5(char_u **arg, typval_T *rettv, int evaluate);
242static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
243static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000244
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
246static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200251static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100252static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100253static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100254static void list_one_var(dictitem_T *v, char *prefix, int *first);
255static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200256static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100257static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100258static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260/* for VIM_VERSION_ defines */
261#include "version.h"
262
Bram Moolenaare21c1582019-03-02 11:57:09 +0100263/*
264 * Return "n1" divided by "n2", taking care of dividing by zero.
265 */
266 static varnumber_T
267num_divide(varnumber_T n1, varnumber_T n2)
268{
269 varnumber_T result;
270
271 if (n2 == 0) // give an error message?
272 {
273 if (n1 == 0)
274 result = VARNUM_MIN; // similar to NaN
275 else if (n1 < 0)
276 result = -VARNUM_MAX;
277 else
278 result = VARNUM_MAX;
279 }
280 else
281 result = n1 / n2;
282
283 return result;
284}
285
286/*
287 * Return "n1" modulus "n2", taking care of dividing by zero.
288 */
289 static varnumber_T
290num_modulus(varnumber_T n1, varnumber_T n2)
291{
292 // Give an error when n2 is 0?
293 return (n2 == 0) ? 0 : (n1 % n2);
294}
295
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100296
297#if defined(EBCDIC) || defined(PROTO)
298/*
299 * Compare struct fst by function name.
300 */
301 static int
302compare_func_name(const void *s1, const void *s2)
303{
304 struct fst *p1 = (struct fst *)s1;
305 struct fst *p2 = (struct fst *)s2;
306
307 return STRCMP(p1->f_name, p2->f_name);
308}
309
310/*
311 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100312 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100313 * On machines using EBCDIC we have to sort it.
314 */
315 static void
316sortFunctions(void)
317{
318 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
319
320 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
321}
322#endif
323
324
Bram Moolenaar33570922005-01-25 22:26:29 +0000325/*
326 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000327 */
328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000330{
Bram Moolenaar33570922005-01-25 22:26:29 +0000331 int i;
332 struct vimvar *p;
333
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200334 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
335 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200336 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000337 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200338 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000339
340 for (i = 0; i < VV_LEN; ++i)
341 {
342 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200343 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200346 getout(1);
347 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 STRCPY(p->vv_di.di_key, p->vv_name);
349 if (p->vv_flags & VV_RO)
350 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
351 else if (p->vv_flags & VV_RO_SBX)
352 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
353 else
354 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000355
356 /* add to v: scope dict, unless the value is not always available */
357 if (p->vv_type != VAR_UNKNOWN)
358 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000359 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000360 /* add to compat scope dict */
361 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100363 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200364 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100365
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100368 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100369 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100370 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371
372 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
373 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
374 set_vim_var_nr(VV_NONE, VVAL_NONE);
375 set_vim_var_nr(VV_NULL, VVAL_NULL);
376
Bram Moolenaarf562e722016-07-19 17:25:25 +0200377 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
378 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
379 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
380 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
381 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
382 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
383 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
384 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
385 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
386 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100387 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200388
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200389 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200390
391#ifdef EBCDIC
392 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100393 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200394 */
395 sortFunctions();
396#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000397}
398
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000399#if defined(EXITFREE) || defined(PROTO)
400 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100401eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000402{
403 int i;
404 struct vimvar *p;
405
406 for (i = 0; i < VV_LEN; ++i)
407 {
408 p = &vimvars[i];
409 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100410 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000411 else if (p->vv_di.di_tv.v_type == VAR_LIST)
412 {
413 list_unref(p->vv_list);
414 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000415 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000416 }
417 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000418 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000419 hash_clear(&compat_hashtab);
420
Bram Moolenaard9fba312005-06-26 22:34:35 +0000421 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100422# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200423 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100424# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000425
426 /* global variables */
427 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000428
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000429 /* autoloaded script names */
430 ga_clear_strings(&ga_loaded);
431
Bram Moolenaarcca74132013-09-25 21:00:28 +0200432 /* Script-local variables. First clear all the variables and in a second
433 * loop free the scriptvar_T, because a variable in one script might hold
434 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200435 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200436 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200437 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200438 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200439 ga_clear(&ga_scripts);
440
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200441 // unreferenced lists and dicts
442 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200443
444 // functions not garbage collected
445 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000446}
447#endif
448
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 * Set an internal variable to a string value. Creates the variable if it does
452 * not already exist.
453 */
454 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100455set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000457 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000458 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
460 val = vim_strsave(value);
461 if (val != NULL)
462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000463 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000464 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000466 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000467 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 }
469 }
470}
471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200473#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000474static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000475static char_u *redir_endp = NULL;
476static char_u *redir_varname = NULL;
477
478/*
479 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100480 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000481 * Returns OK if successfully completed the setup. FAIL otherwise.
482 */
483 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100484var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000485{
486 int save_emsg;
487 int err;
488 typval_T tv;
489
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000490 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000491 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100493 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 return FAIL;
495 }
496
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 redir_varname = vim_strsave(name);
499 if (redir_varname == NULL)
500 return FAIL;
501
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200502 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000503 if (redir_lval == NULL)
504 {
505 var_redir_stop();
506 return FAIL;
507 }
508
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000509 /* The output is stored in growarray "redir_ga" until redirection ends. */
510 ga_init2(&redir_ga, (int)sizeof(char), 500);
511
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100513 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000514 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
516 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200517 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518 if (redir_endp != NULL && *redir_endp != NUL)
519 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100520 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100522 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000523 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 var_redir_stop();
525 return FAIL;
526 }
527
528 /* check if we can write to the variable: set it to or append an empty
529 * string */
530 save_emsg = did_emsg;
531 did_emsg = FALSE;
532 tv.v_type = VAR_STRING;
533 tv.vval.v_string = (char_u *)"";
534 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200535 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200537 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200538 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000540 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541 if (err)
542 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000543 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000544 var_redir_stop();
545 return FAIL;
546 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000547
548 return OK;
549}
550
551/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000552 * Append "value[value_len]" to the variable set by var_redir_start().
553 * The actual appending is postponed until redirection ends, because the value
554 * appended may in fact be the string we write to, changing it may cause freed
555 * memory to be used:
556 * :redir => foo
557 * :let foo
558 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000559 */
560 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100561var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000562{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000563 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000564
565 if (redir_lval == NULL)
566 return;
567
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000568 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000569 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000571 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000573 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574 {
575 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000576 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000577 }
578 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000580}
581
582/*
583 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000584 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585 */
586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000588{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000589 typval_T tv;
590
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200591 if (EVALCMD_BUSY)
592 {
593 redir_lval = NULL;
594 return;
595 }
596
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000597 if (redir_lval != NULL)
598 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000599 /* If there was no error: assign the text to the variable. */
600 if (redir_endp != NULL)
601 {
602 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
603 tv.v_type = VAR_STRING;
604 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200605 /* Call get_lval() again, if it's inside a Dict or List it may
606 * have changed. */
607 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100608 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200609 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200610 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
611 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200612 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000613 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000614
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000615 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100616 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000617
Bram Moolenaard23a8232018-02-10 18:45:26 +0100618 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000619 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100620 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100624eval_charconvert(
625 char_u *enc_from,
626 char_u *enc_to,
627 char_u *fname_from,
628 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 int err = FALSE;
631
632 set_vim_var_string(VV_CC_FROM, enc_from, -1);
633 set_vim_var_string(VV_CC_TO, enc_to, -1);
634 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
635 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
636 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
637 err = TRUE;
638 set_vim_var_string(VV_CC_FROM, NULL, -1);
639 set_vim_var_string(VV_CC_TO, NULL, -1);
640 set_vim_var_string(VV_FNAME_IN, NULL, -1);
641 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
642
643 if (err)
644 return FAIL;
645 return OK;
646}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
649 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100650eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int err = FALSE;
653
654 set_vim_var_string(VV_FNAME_IN, fname, -1);
655 set_vim_var_string(VV_CMDARG, args, -1);
656 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
657 err = TRUE;
658 set_vim_var_string(VV_FNAME_IN, NULL, -1);
659 set_vim_var_string(VV_CMDARG, NULL, -1);
660
661 if (err)
662 {
663 mch_remove(fname);
664 return FAIL;
665 }
666 return OK;
667}
668# endif
669
670# if defined(FEAT_DIFF) || defined(PROTO)
671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100672eval_diff(
673 char_u *origfile,
674 char_u *newfile,
675 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
677 int err = FALSE;
678
679 set_vim_var_string(VV_FNAME_IN, origfile, -1);
680 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
681 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
682 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
683 set_vim_var_string(VV_FNAME_IN, NULL, -1);
684 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
685 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
686}
687
688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100689eval_patch(
690 char_u *origfile,
691 char_u *difffile,
692 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
694 int err;
695
696 set_vim_var_string(VV_FNAME_IN, origfile, -1);
697 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
698 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
699 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
700 set_vim_var_string(VV_FNAME_IN, NULL, -1);
701 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
702 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
703}
704# endif
705
706/*
707 * Top level evaluation function, returning a boolean.
708 * Sets "error" to TRUE if there was an error.
709 * Return TRUE or FALSE.
710 */
711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100712eval_to_bool(
713 char_u *arg,
714 int *error,
715 char_u **nextcmd,
716 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
Bram Moolenaar33570922005-01-25 22:26:29 +0000718 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200719 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721 if (skip)
722 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 else
726 {
727 *error = FALSE;
728 if (!skip)
729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100730 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000731 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 }
734 if (skip)
735 --emsg_skip;
736
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200737 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100740/*
741 * Call eval1() and give an error message if not done at a lower level.
742 */
743 static int
744eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
745{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100746 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100747 int ret;
748 int did_emsg_before = did_emsg;
749 int called_emsg_before = called_emsg;
750
751 ret = eval1(arg, rettv, evaluate);
752 if (ret == FAIL)
753 {
754 // Report the invalid expression unless the expression evaluation has
755 // been cancelled due to an aborting error, an interrupt, or an
756 // exception, or we already gave a more specific error.
757 // Also check called_emsg for when using assert_fails().
758 if (!aborting() && did_emsg == did_emsg_before
759 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100760 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100761 }
762 return ret;
763}
764
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200765 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100766eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
767{
768 char_u *s;
769 int dummy;
770 char_u buf[NUMBUFLEN];
771
772 if (expr->v_type == VAR_FUNC)
773 {
774 s = expr->vval.v_string;
775 if (s == NULL || *s == NUL)
776 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200777 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100778 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
779 return FAIL;
780 }
781 else if (expr->v_type == VAR_PARTIAL)
782 {
783 partial_T *partial = expr->vval.v_partial;
784
785 s = partial_name(partial);
786 if (s == NULL || *s == NUL)
787 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200788 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100789 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
790 return FAIL;
791 }
792 else
793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100794 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100795 if (s == NULL)
796 return FAIL;
797 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100798 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100799 return FAIL;
800 if (*s != NUL) /* check for trailing chars after expr */
801 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200802 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100804 return FAIL;
805 }
806 }
807 return OK;
808}
809
810/*
811 * Like eval_to_bool() but using a typval_T instead of a string.
812 * Works for string, funcref and partial.
813 */
814 int
815eval_expr_to_bool(typval_T *expr, int *error)
816{
817 typval_T rettv;
818 int res;
819
820 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
821 {
822 *error = TRUE;
823 return FALSE;
824 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100825 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100826 clear_tv(&rettv);
827 return res;
828}
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830/*
831 * Top level evaluation function, returning a string. If "skip" is TRUE,
832 * only parsing to "nextcmd" is done, without reporting errors. Return
833 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
834 */
835 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836eval_to_string_skip(
837 char_u *arg,
838 char_u **nextcmd,
839 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 char_u *retval;
843
844 if (skip)
845 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 retval = NULL;
848 else
849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100850 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 if (skip)
854 --emsg_skip;
855
856 return retval;
857}
858
859/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000860 * Skip over an expression at "*pp".
861 * Return FAIL for an error, OK otherwise.
862 */
863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100864skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000867
868 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000870}
871
872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000874 * When "convert" is TRUE convert a List into a sequence of lines and convert
875 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Return pointer to allocated memory, or NULL for failure.
877 */
878 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100879eval_to_string(
880 char_u *arg,
881 char_u **nextcmd,
882 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000886 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000887#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000888 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 retval = NULL;
893 else
894 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000895 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000896 {
897 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000898 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200899 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200900 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200901 if (tv.vval.v_list->lv_len > 0)
902 ga_append(&ga, NL);
903 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000904 ga_append(&ga, NUL);
905 retval = (char_u *)ga.ga_data;
906 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000907#ifdef FEAT_FLOAT
908 else if (convert && tv.v_type == VAR_FLOAT)
909 {
910 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
911 retval = vim_strsave(numbuf);
912 }
913#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000914 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100915 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
919 return retval;
920}
921
922/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000923 * Call eval_to_string() without using current local variables and using
924 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
926 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100927eval_to_string_safe(
928 char_u *arg,
929 char_u **nextcmd,
930 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200933 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200935 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000936 if (use_sandbox)
937 ++sandbox;
938 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000939 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000940 if (use_sandbox)
941 --sandbox;
942 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200943 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 return retval;
945}
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947/*
948 * Top level evaluation function, returning a number.
949 * Evaluates "expr" silently.
950 * Returns -1 for an error.
951 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200952 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100953eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200956 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000957 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
959 ++emsg_off;
960
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000961 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 retval = -1;
963 else
964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100965 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 --emsg_off;
969
970 return retval;
971}
972
Bram Moolenaara40058a2005-07-11 22:42:07 +0000973/*
974 * Prepare v: variable "idx" to be used.
975 * Save the current typeval in "save_tv".
976 * When not used yet add the variable to the v: hashtable.
977 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100979prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000980{
981 *save_tv = vimvars[idx].vv_tv;
982 if (vimvars[idx].vv_type == VAR_UNKNOWN)
983 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
984}
985
986/*
987 * Restore v: variable "idx" to typeval "save_tv".
988 * When no longer defined, remove the variable from the v: hashtable.
989 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100991restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000992{
993 hashitem_T *hi;
994
Bram Moolenaara40058a2005-07-11 22:42:07 +0000995 vimvars[idx].vv_tv = *save_tv;
996 if (vimvars[idx].vv_type == VAR_UNKNOWN)
997 {
998 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
999 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001000 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001001 else
1002 hash_remove(&vimvarht, hi);
1003 }
1004}
1005
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001006#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001007/*
1008 * Evaluate an expression to a list with suggestions.
1009 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001010 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001011 */
1012 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001014{
1015 typval_T save_val;
1016 typval_T rettv;
1017 list_T *list = NULL;
1018 char_u *p = skipwhite(expr);
1019
1020 /* Set "v:val" to the bad word. */
1021 prepare_vimvar(VV_VAL, &save_val);
1022 vimvars[VV_VAL].vv_type = VAR_STRING;
1023 vimvars[VV_VAL].vv_str = badword;
1024 if (p_verbose == 0)
1025 ++emsg_off;
1026
1027 if (eval1(&p, &rettv, TRUE) == OK)
1028 {
1029 if (rettv.v_type != VAR_LIST)
1030 clear_tv(&rettv);
1031 else
1032 list = rettv.vval.v_list;
1033 }
1034
1035 if (p_verbose == 0)
1036 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001037 restore_vimvar(VV_VAL, &save_val);
1038
1039 return list;
1040}
1041
1042/*
1043 * "list" is supposed to contain two items: a word and a number. Return the
1044 * word in "pp" and the number as the return value.
1045 * Return -1 if anything isn't right.
1046 * Used to get the good word and score from the eval_spell_expr() result.
1047 */
1048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001049get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001050{
1051 listitem_T *li;
1052
1053 li = list->lv_first;
1054 if (li == NULL)
1055 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001056 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001057
1058 li = li->li_next;
1059 if (li == NULL)
1060 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001062}
1063#endif
1064
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001065/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001066 * Top level evaluation function.
1067 * Returns an allocated typval_T with the result.
1068 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069 */
1070 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001071eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001072{
1073 typval_T *tv;
1074
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001075 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001076 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001077 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001078
1079 return tv;
1080}
1081
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001084 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001085 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1086 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001087 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001090call_vim_function(
1091 char_u *func,
1092 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001093 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001094 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001097 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001100 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001102 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001103 if (ret == FAIL)
1104 clear_tv(rettv);
1105
1106 return ret;
1107}
1108
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001109/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001110 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001111 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001112 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1113 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001114 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001115 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001116call_func_retnr(
1117 char_u *func,
1118 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001119 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001120{
1121 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001122 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001123
Bram Moolenaarded27a12018-08-01 19:06:03 +02001124 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001125 return -1;
1126
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001127 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001128 clear_tv(&rettv);
1129 return retval;
1130}
1131
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001132#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001133 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1134
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001135# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001136/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001137 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001138 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001139 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1140 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141 */
1142 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001143call_func_retstr(
1144 char_u *func,
1145 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001146 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001147{
1148 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001149 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001150
Bram Moolenaarded27a12018-08-01 19:06:03 +02001151 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001152 return NULL;
1153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001154 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 return retval;
1157}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001158# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001159
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001160/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001161 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001162 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1163 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001164 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001165 */
1166 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001167call_func_retlist(
1168 char_u *func,
1169 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001170 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001171{
1172 typval_T rettv;
1173
Bram Moolenaarded27a12018-08-01 19:06:03 +02001174 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001175 return NULL;
1176
1177 if (rettv.v_type != VAR_LIST)
1178 {
1179 clear_tv(&rettv);
1180 return NULL;
1181 }
1182
1183 return rettv.vval.v_list;
1184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186
Bram Moolenaar05159a02005-02-26 23:04:13 +00001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#ifdef FEAT_FOLDING
1189/*
1190 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1191 * it in "*cp". Doesn't give error messages.
1192 */
1193 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001194eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar33570922005-01-25 22:26:29 +00001196 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001197 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001199 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1200 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001203 if (use_sandbox)
1204 ++sandbox;
1205 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = 0;
1209 else
1210 {
1211 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 if (tv.v_type == VAR_NUMBER)
1213 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001214 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 retval = 0;
1216 else
1217 {
1218 /* If the result is a string, check if there is a non-digit before
1219 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (!VIM_ISDIGIT(*s) && *s != '-')
1222 *cp = *s++;
1223 retval = atol((char *)s);
1224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001228 if (use_sandbox)
1229 --sandbox;
1230 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001232 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233}
1234#endif
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001237 * Get a list of lines from a HERE document. The here document is a list of
1238 * lines surrounded by a marker.
1239 * cmd << {marker}
1240 * {line1}
1241 * {line2}
1242 * ....
1243 * {marker}
1244 *
1245 * The {marker} is a string. If the optional 'trim' word is supplied before the
1246 * marker, then the leading indentation before the lines (matching the
1247 * indentation in the 'cmd' line) is stripped.
1248 * Returns a List with {lines} or NULL.
1249 */
1250 static list_T *
1251heredoc_get(exarg_T *eap, char_u *cmd)
1252{
1253 char_u *theline;
1254 char_u *marker;
1255 list_T *l;
1256 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001257 int marker_indent_len = 0;
1258 int text_indent_len = 0;
1259 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001260
1261 if (eap->getline == NULL)
1262 {
1263 emsg(_("E991: cannot use =<< here"));
1264 return NULL;
1265 }
1266
1267 // Check for the optional 'trim' word before the marker
1268 cmd = skipwhite(cmd);
1269 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1270 {
1271 cmd = skipwhite(cmd + 4);
1272
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001273 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001274 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001275 // the first line after the :let command line. To find the end marker
1276 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001277 p = *eap->cmdlinep;
1278 while (VIM_ISWHITE(*p))
1279 {
1280 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001281 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001282 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001283 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001284 }
1285
Bram Moolenaar24582002019-07-21 14:14:26 +02001286 // The marker is the next word.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001287 if (*cmd != NUL && *cmd != '"')
1288 {
1289 marker = skipwhite(cmd);
1290 p = skiptowhite(marker);
1291 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1292 {
1293 emsg(_(e_trailing));
1294 return NULL;
1295 }
1296 *p = NUL;
Bram Moolenaar24582002019-07-21 14:14:26 +02001297 if (vim_islower(*marker))
1298 {
1299 emsg(_("E221: Marker cannot start with lower case letter"));
1300 return NULL;
1301 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001302 }
1303 else
Bram Moolenaar24582002019-07-21 14:14:26 +02001304 {
1305 emsg(_("E172: Missing marker"));
1306 return NULL;
1307 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001308
1309 l = list_alloc();
1310 if (l == NULL)
1311 return NULL;
1312
1313 for (;;)
1314 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001315 int mi = 0;
1316 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001317
Bram Moolenaare96a2492019-06-25 04:12:16 +02001318 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001319 if (theline == NULL)
1320 {
1321 semsg(_("E990: Missing end marker '%s'"), marker);
1322 break;
1323 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001324
1325 // with "trim": skip the indent matching the :let line to find the
1326 // marker
1327 if (marker_indent_len > 0
1328 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1329 mi = marker_indent_len;
1330 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001331 {
1332 vim_free(theline);
1333 break;
1334 }
1335
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001336 if (text_indent_len == -1 && *theline != NUL)
1337 {
1338 // set the text indent from the first line.
1339 p = theline;
1340 text_indent_len = 0;
1341 while (VIM_ISWHITE(*p))
1342 {
1343 p++;
1344 text_indent_len++;
1345 }
1346 text_indent = vim_strnsave(theline, text_indent_len);
1347 }
1348 // with "trim": skip the indent matching the first line
1349 if (text_indent != NULL)
1350 for (ti = 0; ti < text_indent_len; ++ti)
1351 if (theline[ti] != text_indent[ti])
1352 break;
1353
1354 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001355 break;
1356 vim_free(theline);
1357 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001358 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001359
1360 return l;
1361}
1362
1363/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 * ":let" list all variable values
1365 * ":let var1 var2" list variable values
1366 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001367 * ":let var += expr" assignment command.
1368 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001369 * ":let var *= expr" assignment command.
1370 * ":let var /= expr" assignment command.
1371 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001373 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 */
1376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001377ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001379 ex_let_const(eap, FALSE);
1380}
1381
1382/*
1383 * ":const" list all variable values
1384 * ":const var1 var2" list variable values
1385 * ":const var = expr" assignment command.
1386 * ":const [var1, var2] = expr" unpack list.
1387 */
1388 void
1389ex_const(exarg_T *eap)
1390{
1391 ex_let_const(eap, TRUE);
1392}
1393
1394 static void
1395ex_let_const(exarg_T *eap, int is_const)
1396{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001398 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 int var_count = 0;
1402 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001404 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001405 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001406 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaardb552d602006-03-23 22:59:57 +00001408 argend = skip_var_list(arg, &var_count, &semicolon);
1409 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001411 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001412 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001413 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001414 concat = expr[0] == '.'
1415 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1416 || (expr[1] == '.' && expr[2] == '='));
1417 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1418 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001420 /*
1421 * ":let" without "=": list variables
1422 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001424 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001425 else if (expr[0] == '.')
1426 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001429 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001433 list_glob_vars(&first);
1434 list_buf_vars(&first);
1435 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001436 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001437 list_script_vars(&first);
1438 list_func_vars(&first);
1439 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 eap->nextcmd = check_nextcmd(arg);
1442 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001443 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1444 {
1445 list_T *l;
1446
1447 // HERE document
1448 l = heredoc_get(eap, expr + 3);
1449 if (l != NULL)
1450 {
1451 rettv_list_set(&rettv, l);
1452 op[0] = '=';
1453 op[1] = NUL;
1454 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001455 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001456 clear_tv(&rettv);
1457 }
1458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 else
1460 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001461 op[0] = '=';
1462 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001463 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001465 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001466 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001467 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001468 if (expr[0] == '.' && expr[1] == '.') // ..=
1469 ++expr;
1470 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001471 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001473 else
1474 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (eap->skip)
1477 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001478 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (eap->skip)
1480 {
1481 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 --emsg_skip;
1484 }
1485 else if (i != FAIL)
1486 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001488 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 }
1492}
1493
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001494/*
1495 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1496 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar61343f02019-07-20 21:11:13 +02001497 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1499 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500 * Returns OK or FAIL;
1501 */
1502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503ex_let_vars(
1504 char_u *arg_start,
1505 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001506 int copy, // copy values from "tv", don't move
1507 int semicolon, // from skip_var_list()
1508 int var_count, // from skip_var_list()
1509 int is_const, // lock variables for const
Bram Moolenaar61343f02019-07-20 21:11:13 +02001510 char_u *op)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001511{
1512 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001513 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001515 listitem_T *item;
1516 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517
1518 if (*arg != '[')
1519 {
1520 /*
1521 * ":let var = expr" or ":for var in list"
1522 */
Bram Moolenaar61343f02019-07-20 21:11:13 +02001523 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001524 return FAIL;
1525 return OK;
1526 }
1527
1528 /*
1529 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1530 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001531 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001533 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001534 return FAIL;
1535 }
1536
1537 i = list_len(l);
1538 if (semicolon == 0 && var_count < i)
1539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001540 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001541 return FAIL;
1542 }
1543 if (var_count - semicolon > i)
1544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546 return FAIL;
1547 }
1548
1549 item = l->lv_first;
1550 while (*arg != ']')
1551 {
1552 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001553 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001554 (char_u *)",;]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 item = item->li_next;
1556 if (arg == NULL)
1557 return FAIL;
1558
1559 arg = skipwhite(arg);
1560 if (*arg == ';')
1561 {
1562 /* Put the rest of the list (may be empty) in the var after ';'.
1563 * Create a new list for this. */
1564 l = list_alloc();
1565 if (l == NULL)
1566 return FAIL;
1567 while (item != NULL)
1568 {
1569 list_append_tv(l, &item->li_tv);
1570 item = item->li_next;
1571 }
1572
1573 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001574 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 ltv.vval.v_list = l;
1576 l->lv_refcount = 1;
1577
Bram Moolenaar9937a052019-06-15 15:45:06 +02001578 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001579 (char_u *)"]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580 clear_tv(&ltv);
1581 if (arg == NULL)
1582 return FAIL;
1583 break;
1584 }
1585 else if (*arg != ',' && *arg != ']')
1586 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001587 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001588 return FAIL;
1589 }
1590 }
1591
1592 return OK;
1593}
1594
1595/*
1596 * Skip over assignable variable "var" or list of variables "[var, var]".
1597 * Used for ":let varvar = expr" and ":for varvar in expr".
1598 * For "[var, var]" increment "*var_count" for each variable.
1599 * for "[var, var; var]" set "semicolon".
1600 * Return NULL for an error.
1601 */
1602 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001603skip_var_list(
1604 char_u *arg,
1605 int *var_count,
1606 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607{
1608 char_u *p, *s;
1609
1610 if (*arg == '[')
1611 {
1612 /* "[var, var]": find the matching ']'. */
1613 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615 {
1616 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1617 s = skip_var_one(p);
1618 if (s == p)
1619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001620 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 return NULL;
1622 }
1623 ++*var_count;
1624
1625 p = skipwhite(s);
1626 if (*p == ']')
1627 break;
1628 else if (*p == ';')
1629 {
1630 if (*semicolon == 1)
1631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001632 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633 return NULL;
1634 }
1635 *semicolon = 1;
1636 }
1637 else if (*p != ',')
1638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640 return NULL;
1641 }
1642 }
1643 return p + 1;
1644 }
1645 else
1646 return skip_var_one(arg);
1647}
1648
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001649/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001650 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001651 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001652 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001654skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001656 if (*arg == '@' && arg[1] != NUL)
1657 return arg + 2;
1658 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1659 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660}
1661
Bram Moolenaara7043832005-01-21 11:56:39 +00001662/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001663 * List variables for hashtab "ht" with prefix "prefix".
1664 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001665 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001667list_hashtable_vars(
1668 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001669 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001670 int empty,
1671 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001672{
Bram Moolenaar33570922005-01-25 22:26:29 +00001673 hashitem_T *hi;
1674 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001675 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001676 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001677
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001678 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001679 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1680 {
1681 if (!HASHITEM_EMPTY(hi))
1682 {
1683 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001684 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001685
1686 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001687 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1688 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001689 if (message_filtered(buf))
1690 continue;
1691
Bram Moolenaar33570922005-01-25 22:26:29 +00001692 if (empty || di->di_tv.v_type != VAR_STRING
1693 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001694 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001695 }
1696 }
1697}
1698
1699/*
1700 * List global variables.
1701 */
1702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001704{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001705 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001706}
1707
1708/*
1709 * List buffer variables.
1710 */
1711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001713{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001714 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001715}
1716
1717/*
1718 * List window variables.
1719 */
1720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001721list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001722{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001723 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001724}
1725
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726/*
1727 * List tab page variables.
1728 */
1729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001730list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001731{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001732 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001733}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001734
Bram Moolenaara7043832005-01-21 11:56:39 +00001735/*
1736 * List Vim variables.
1737 */
1738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001739list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001741 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742}
1743
1744/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001745 * List script-local variables, if there is a script.
1746 */
1747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001748list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001749{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001750 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1751 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001752 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001753}
1754
1755/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001756 * List variables in "arg".
1757 */
1758 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760{
1761 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001762 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001764 char_u *name_start;
1765 char_u *arg_subsc;
1766 char_u *tofree;
1767 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768
1769 while (!ends_excmd(*arg) && !got_int)
1770 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001771 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001773 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001774 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001775 {
1776 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001777 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001778 break;
1779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001782 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001783 /* get_name_len() takes care of expanding curly braces */
1784 name_start = name = arg;
1785 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1786 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001788 /* This is mainly to keep test 49 working: when expanding
1789 * curly braces fails overrule the exception error message. */
1790 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001792 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001793 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001794 break;
1795 }
1796 error = TRUE;
1797 }
1798 else
1799 {
1800 if (tofree != NULL)
1801 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001802 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 else
1805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001806 /* handle d.key, l[idx], f(expr) */
1807 arg_subsc = arg;
1808 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001809 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001811 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001812 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001814 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001815 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001816 case 'g': list_glob_vars(first); break;
1817 case 'b': list_buf_vars(first); break;
1818 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001819 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001820 case 'v': list_vim_vars(first); break;
1821 case 's': list_script_vars(first); break;
1822 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001824 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001825 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001826 }
1827 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001828 {
1829 char_u numbuf[NUMBUFLEN];
1830 char_u *tf;
1831 int c;
1832 char_u *s;
1833
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001834 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001835 c = *arg;
1836 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001837 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001838 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001839 tv.v_type,
1840 s == NULL ? (char_u *)"" : s,
1841 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001842 *arg = c;
1843 vim_free(tf);
1844 }
1845 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 }
1848 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001849
1850 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001852
1853 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 }
1855
1856 return arg;
1857}
1858
1859/*
1860 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1861 * Returns a pointer to the char just after the var name.
1862 * Returns NULL if there is an error.
1863 */
1864 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001866 char_u *arg, // points to variable name
1867 typval_T *tv, // value to assign to variable
1868 int copy, // copy value from "tv"
1869 int is_const, // lock variable for const
1870 char_u *endchars, // valid chars after variable name or NULL
1871 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872{
1873 int c1;
1874 char_u *name;
1875 char_u *p;
1876 char_u *arg_end = NULL;
1877 int len;
1878 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880
1881 /*
1882 * ":let $VAR = expr": Set environment variable.
1883 */
1884 if (*arg == '$')
1885 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001886 if (is_const)
1887 {
1888 emsg(_("E996: Cannot lock an environment variable"));
1889 return NULL;
1890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 /* Find the end of the name. */
1892 ++arg;
1893 name = arg;
1894 len = get_env_len(&arg);
1895 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001896 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 else
1898 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001899 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001900 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001903 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001904 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 {
1906 c1 = name[len];
1907 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001908 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001909 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 {
1911 int mustfree = FALSE;
1912 char_u *s = vim_getenv(name, &mustfree);
1913
1914 if (s != NULL)
1915 {
1916 p = tofree = concat_str(s, p);
1917 if (mustfree)
1918 vim_free(s);
1919 }
1920 }
1921 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001922 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001924 if (STRICMP(name, "HOME") == 0)
1925 init_homedir();
1926 else if (didset_vim && STRICMP(name, "VIM") == 0)
1927 didset_vim = FALSE;
1928 else if (didset_vimruntime
1929 && STRICMP(name, "VIMRUNTIME") == 0)
1930 didset_vimruntime = FALSE;
1931 arg_end = arg;
1932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 }
1936 }
1937 }
1938
1939 /*
1940 * ":let &option = expr": Set option value.
1941 * ":let &l:option = expr": Set local option value.
1942 * ":let &g:option = expr": Set global option value.
1943 */
1944 else if (*arg == '&')
1945 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001946 if (is_const)
1947 {
1948 emsg(_("E996: Cannot lock an option"));
1949 return NULL;
1950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 /* Find the end of the name. */
1952 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 if (p == NULL || (endchars != NULL
1954 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001955 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 else
1957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 long n;
1959 int opt_type;
1960 long numval;
1961 char_u *stringval = NULL;
1962 char_u *s;
1963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 c1 = *p;
1965 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001966
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001967 n = (long)tv_get_number(tv);
1968 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 {
1971 opt_type = get_option_value(arg, &numval,
1972 &stringval, opt_flags);
1973 if ((opt_type == 1 && *op == '.')
1974 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001976 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001977 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001979 else
1980 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001981 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001983 switch (*op)
1984 {
1985 case '+': n = numval + n; break;
1986 case '-': n = numval - n; break;
1987 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001988 case '/': n = (long)num_divide(numval, n); break;
1989 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001990 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001992 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 {
1994 s = concat_str(stringval, s);
1995 vim_free(stringval);
1996 stringval = s;
1997 }
1998 }
1999 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002000 if (s != NULL)
2001 {
2002 set_option_value(arg, n, s, opt_flags);
2003 arg_end = p;
2004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 }
2008 }
2009
2010 /*
2011 * ":let @r = expr": Set register contents.
2012 */
2013 else if (*arg == '@')
2014 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002015 if (is_const)
2016 {
2017 emsg(_("E996: Cannot lock a register"));
2018 return NULL;
2019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002021 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002022 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002025 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 else
2027 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002028 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002029 char_u *s;
2030
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002031 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002034 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 if (s != NULL)
2036 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002037 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 vim_free(s);
2039 }
2040 }
2041 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002042 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002044 arg_end = arg + 1;
2045 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002046 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 }
2048 }
2049
2050 /*
2051 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002053 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002054 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002055 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002056 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002058 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002062 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 else
2064 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002065 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 arg_end = p;
2067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 }
2071
2072 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074
2075 return arg_end;
2076}
2077
2078/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002079 * Get an lval: variable, Dict item or List item that can be assigned a value
2080 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2081 * "name.key", "name.key[expr]" etc.
2082 * Indexing only works if "name" is an existing List or Dictionary.
2083 * "name" points to the start of the name.
2084 * If "rettv" is not NULL it points to the value to be assigned.
2085 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2086 * wrong; must end in space or cmd separator.
2087 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002088 * flags:
2089 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002090 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002091 * GLV_NO_AUTOLOAD: do not use script autoloading
2092 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002094 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002097 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098get_lval(
2099 char_u *name,
2100 typval_T *rettv,
2101 lval_T *lp,
2102 int unlet,
2103 int skip,
2104 int flags, /* GLV_ values */
2105 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 char_u *expr_start, *expr_end;
2109 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 dictitem_T *v;
2111 typval_T var1;
2112 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002116 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002118 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122
2123 if (skip)
2124 {
2125 /* When skipping just find the end of the name. */
2126 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002127 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 }
2129
2130 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002131 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 if (expr_start != NULL)
2133 {
2134 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002135 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 && *p != '[' && *p != '.')
2137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002138 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
2140 }
2141
2142 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2143 if (lp->ll_exp_name == NULL)
2144 {
2145 /* Report an invalid expression in braces, unless the
2146 * expression evaluation has been cancelled due to an
2147 * aborting error, an interrupt, or an exception. */
2148 if (!aborting() && !quiet)
2149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002150 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002151 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002152 return NULL;
2153 }
2154 }
2155 lp->ll_name = lp->ll_exp_name;
2156 }
2157 else
2158 lp->ll_name = name;
2159
2160 /* Without [idx] or .key we are done. */
2161 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2162 return p;
2163
2164 cc = *p;
2165 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002166 /* Only pass &ht when we would write to the variable, it prevents autoload
2167 * as well. */
2168 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2169 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002171 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 if (v == NULL)
2174 return NULL;
2175
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 /*
2177 * Loop until no more [idx] or .key is following.
2178 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002180 var1.v_type = VAR_UNKNOWN;
2181 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2185 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002186 && lp->ll_tv->vval.v_dict != NULL)
2187 && !(lp->ll_tv->v_type == VAR_BLOB
2188 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002191 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002200
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 len = -1;
2202 if (*p == '.')
2203 {
2204 key = p + 1;
2205 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2206 ;
2207 if (len == 0)
2208 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 }
2213 p = key + len;
2214 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002215 else
2216 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002218 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 if (*p == ':')
2220 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002221 else
2222 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 empty1 = FALSE;
2224 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002226 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002227 {
2228 /* not a number or string */
2229 clear_tv(&var1);
2230 return NULL;
2231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002232 }
2233
2234 /* Optionally get the second index [ :expr]. */
2235 if (*p == ':')
2236 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002238 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002240 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002241 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002243 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002244 if (rettv != NULL
2245 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002246 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002247 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002248 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002252 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 }
2255 p = skipwhite(p + 1);
2256 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002258 else
2259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002261 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2262 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002263 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002266 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 {
2268 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002269 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 clear_tv(&var2);
2271 return NULL;
2272 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002273 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002275 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002276 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002278
Bram Moolenaar8c711452005-01-14 21:53:12 +00002279 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002282 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002283 clear_tv(&var1);
2284 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002286 }
2287
2288 /* Skip to past ']'. */
2289 ++p;
2290 }
2291
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002292 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002293 {
2294 if (len == -1)
2295 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002297 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002298 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002299 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002300 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002302 }
2303 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 lp->ll_list = NULL;
2305 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002306 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002307
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002308 /* When assigning to a scope dictionary check that a function and
2309 * variable name is valid (only variable name unless it is l: or
2310 * g: dictionary). Disallow overwriting a builtin function. */
2311 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002312 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002313 int prevval;
2314 int wrong;
2315
2316 if (len != -1)
2317 {
2318 prevval = key[len];
2319 key[len] = NUL;
2320 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002321 else
2322 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002323 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2324 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002325 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002326 || !valid_varname(key);
2327 if (len != -1)
2328 key[len] = prevval;
2329 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002330 return NULL;
2331 }
2332
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002334 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002335 // Can't add "v:" or "a:" variable.
2336 if (lp->ll_dict == &vimvardict
2337 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002339 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002340 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002341 return NULL;
2342 }
2343
Bram Moolenaar31b81602019-02-10 22:14:27 +01002344 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002348 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002349 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 }
2352 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002356 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002358 p = NULL;
2359 break;
2360 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002361 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002362 else if ((flags & GLV_READ_ONLY) == 0
2363 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002364 {
2365 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002366 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002367 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002368
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002369 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002372 else if (lp->ll_tv->v_type == VAR_BLOB)
2373 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002374 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2375
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002376 /*
2377 * Get the number and item for the only or first index of the List.
2378 */
2379 if (empty1)
2380 lp->ll_n1 = 0;
2381 else
2382 // is number or string
2383 lp->ll_n1 = (long)tv_get_number(&var1);
2384 clear_tv(&var1);
2385
2386 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002387 || lp->ll_n1 > bloblen
2388 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002389 {
2390 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002391 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002392 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002393 return NULL;
2394 }
2395 if (lp->ll_range && !lp->ll_empty2)
2396 {
2397 lp->ll_n2 = (long)tv_get_number(&var2);
2398 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002399 if (lp->ll_n2 < 0
2400 || lp->ll_n2 >= bloblen
2401 || lp->ll_n2 < lp->ll_n1)
2402 {
2403 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002404 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002405 return NULL;
2406 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002407 }
2408 lp->ll_blob = lp->ll_tv->vval.v_blob;
2409 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002410 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002411 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 else
2413 {
2414 /*
2415 * Get the number and item for the only or first index of the List.
2416 */
2417 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002420 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002421 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002422 clear_tv(&var1);
2423
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 lp->ll_list = lp->ll_tv->vval.v_list;
2426 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2427 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002429 if (lp->ll_n1 < 0)
2430 {
2431 lp->ll_n1 = 0;
2432 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2433 }
2434 }
2435 if (lp->ll_li == NULL)
2436 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002437 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002438 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002439 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 }
2442
2443 /*
2444 * May need to find the item or absolute index for the second
2445 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 * When no index given: "lp->ll_empty2" is TRUE.
2447 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002451 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002452 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002458 {
2459 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002460 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 }
2465
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2467 if (lp->ll_n1 < 0)
2468 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2469 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002470 {
2471 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002472 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002474 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002475 }
2476
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002481 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 return p;
2483}
2484
2485/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002489clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490{
2491 vim_free(lp->ll_exp_name);
2492 vim_free(lp->ll_newkey);
2493}
2494
2495/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002496 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002498 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2499 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 */
2501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002502set_var_lval(
2503 lval_T *lp,
2504 char_u *endp,
2505 typval_T *rettv,
2506 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002507 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002508 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509{
2510 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002511 listitem_T *ri;
2512 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513
2514 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002516 cc = *endp;
2517 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002518 if (lp->ll_blob != NULL)
2519 {
2520 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002521
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002522 if (op != NULL && *op != '=')
2523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002524 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002525 return;
2526 }
2527
2528 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2529 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002530 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002531
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002532 if (lp->ll_empty2)
2533 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2534
2535 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002538 return;
2539 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002540 if (lp->ll_empty2)
2541 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002542
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002543 ir = 0;
2544 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2545 blob_set(lp->ll_blob, il,
2546 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002547 }
2548 else
2549 {
2550 val = (int)tv_get_number_chk(rettv, &error);
2551 if (!error)
2552 {
2553 garray_T *gap = &lp->ll_blob->bv_ga;
2554
2555 // Allow for appending a byte. Setting a byte beyond
2556 // the end is an error otherwise.
2557 if (lp->ll_n1 < gap->ga_len
2558 || (lp->ll_n1 == gap->ga_len
2559 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2560 {
2561 blob_set(lp->ll_blob, lp->ll_n1, val);
2562 if (lp->ll_n1 == gap->ga_len)
2563 ++gap->ga_len;
2564 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002565 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002566 }
2567 }
2568 }
2569 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002571 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002572
Bram Moolenaar9937a052019-06-15 15:45:06 +02002573 if (is_const)
2574 {
2575 emsg(_(e_cannot_mod));
2576 *endp = cc;
2577 return;
2578 }
2579
Bram Moolenaarff697e62019-02-12 22:28:33 +01002580 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002581 di = NULL;
2582 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2583 &tv, &di, TRUE, FALSE) == OK)
2584 {
2585 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002586 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2587 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002588 && tv_op(&tv, rettv, op) == OK)
2589 set_var(lp->ll_name, &tv, FALSE);
2590 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002593 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002594 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002595 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002597 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002598 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002599 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002600 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 else if (lp->ll_range)
2602 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002603 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002604 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002605
Bram Moolenaar9937a052019-06-15 15:45:06 +02002606 if (is_const)
2607 {
2608 emsg(_("E996: Cannot lock a range"));
2609 return;
2610 }
2611
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002612 /*
2613 * Check whether any of the list items is locked
2614 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002615 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002616 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002617 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002618 return;
2619 ri = ri->li_next;
2620 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2621 break;
2622 ll_li = ll_li->li_next;
2623 ++ll_n1;
2624 }
2625
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 /*
2627 * Assign the List values to the list items.
2628 */
2629 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002631 if (op != NULL && *op != '=')
2632 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2633 else
2634 {
2635 clear_tv(&lp->ll_li->li_tv);
2636 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 ri = ri->li_next;
2639 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2640 break;
2641 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002644 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 ri = NULL;
2647 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 lp->ll_li = lp->ll_li->li_next;
2651 ++lp->ll_n1;
2652 }
2653 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002654 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655 else if (lp->ll_empty2
2656 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002658 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 }
2660 else
2661 {
2662 /*
2663 * Assign to a List or Dictionary item.
2664 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002665 if (is_const)
2666 {
2667 emsg(_("E996: Cannot lock a list or dict"));
2668 return;
2669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (lp->ll_newkey != NULL)
2671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 if (op != NULL && *op != '=')
2673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002675 return;
2676 }
2677
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002679 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (di == NULL)
2681 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002682 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2683 {
2684 vim_free(di);
2685 return;
2686 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 else if (op != NULL && *op != '=')
2690 {
2691 tv_op(lp->ll_tv, rettv, op);
2692 return;
2693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 /*
2698 * Assign the value to the variable or list item.
2699 */
2700 if (copy)
2701 copy_tv(rettv, lp->ll_tv);
2702 else
2703 {
2704 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002705 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 }
2708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002709}
2710
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002711/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002712 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2713 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 * Returns OK or FAIL.
2715 */
2716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002719 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 char_u numbuf[NUMBUFLEN];
2721 char_u *s;
2722
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002723 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2724 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2725 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002726 {
2727 switch (tv1->v_type)
2728 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002729 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002730 case VAR_DICT:
2731 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002732 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002733 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002734 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002735 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 break;
2737
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002738 case VAR_BLOB:
2739 if (*op != '+' || tv2->v_type != VAR_BLOB)
2740 break;
2741 // BLOB += BLOB
2742 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2743 {
2744 blob_T *b1 = tv1->vval.v_blob;
2745 blob_T *b2 = tv2->vval.v_blob;
2746 int i, len = blob_len(b2);
2747 for (i = 0; i < len; i++)
2748 ga_append(&b1->bv_ga, blob_get(b2, i));
2749 }
2750 return OK;
2751
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 case VAR_LIST:
2753 if (*op != '+' || tv2->v_type != VAR_LIST)
2754 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002755 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2757 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2758 return OK;
2759
2760 case VAR_NUMBER:
2761 case VAR_STRING:
2762 if (tv2->v_type == VAR_LIST)
2763 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002764 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002766 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002767 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002768#ifdef FEAT_FLOAT
2769 if (tv2->v_type == VAR_FLOAT)
2770 {
2771 float_T f = n;
2772
Bram Moolenaarff697e62019-02-12 22:28:33 +01002773 if (*op == '%')
2774 break;
2775 switch (*op)
2776 {
2777 case '+': f += tv2->vval.v_float; break;
2778 case '-': f -= tv2->vval.v_float; break;
2779 case '*': f *= tv2->vval.v_float; break;
2780 case '/': f /= tv2->vval.v_float; break;
2781 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002782 clear_tv(tv1);
2783 tv1->v_type = VAR_FLOAT;
2784 tv1->vval.v_float = f;
2785 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002786 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002787#endif
2788 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002789 switch (*op)
2790 {
2791 case '+': n += tv_get_number(tv2); break;
2792 case '-': n -= tv_get_number(tv2); break;
2793 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002794 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2795 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002796 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002797 clear_tv(tv1);
2798 tv1->v_type = VAR_NUMBER;
2799 tv1->vval.v_number = n;
2800 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002801 }
2802 else
2803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002804 if (tv2->v_type == VAR_FLOAT)
2805 break;
2806
Bram Moolenaarff697e62019-02-12 22:28:33 +01002807 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002808 s = tv_get_string(tv1);
2809 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 clear_tv(tv1);
2811 tv1->v_type = VAR_STRING;
2812 tv1->vval.v_string = s;
2813 }
2814 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002815
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002816 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002817#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002818 {
2819 float_T f;
2820
Bram Moolenaarff697e62019-02-12 22:28:33 +01002821 if (*op == '%' || *op == '.'
2822 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002823 && tv2->v_type != VAR_NUMBER
2824 && tv2->v_type != VAR_STRING))
2825 break;
2826 if (tv2->v_type == VAR_FLOAT)
2827 f = tv2->vval.v_float;
2828 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002829 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002830 switch (*op)
2831 {
2832 case '+': tv1->vval.v_float += f; break;
2833 case '-': tv1->vval.v_float -= f; break;
2834 case '*': tv1->vval.v_float *= f; break;
2835 case '/': tv1->vval.v_float /= f; break;
2836 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002837 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002838#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002839 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840 }
2841 }
2842
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002843 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 return FAIL;
2845}
2846
2847/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 * Evaluate the expression used in a ":for var in expr" command.
2849 * "arg" points to "var".
2850 * Set "*errp" to TRUE for an error, FALSE otherwise;
2851 * Return a pointer that holds the info. Null when there is an error.
2852 */
2853 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002854eval_for_line(
2855 char_u *arg,
2856 int *errp,
2857 char_u **nextcmdp,
2858 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002859{
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002861 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 typval_T tv;
2863 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002864
2865 *errp = TRUE; /* default: there is an error */
2866
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002867 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002868 if (fi == NULL)
2869 return NULL;
2870
2871 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2872 if (expr == NULL)
2873 return fi;
2874
2875 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002876 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002878 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002879 return fi;
2880 }
2881
2882 if (skip)
2883 ++emsg_skip;
2884 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2885 {
2886 *errp = FALSE;
2887 if (!skip)
2888 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002889 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002890 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002891 l = tv.vval.v_list;
2892 if (l == NULL)
2893 {
2894 // a null list is like an empty list: do nothing
2895 clear_tv(&tv);
2896 }
2897 else
2898 {
2899 // No need to increment the refcount, it's already set for
2900 // the list being used in "tv".
2901 fi->fi_list = l;
2902 list_add_watch(l, &fi->fi_lw);
2903 fi->fi_lw.lw_item = l->lv_first;
2904 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002905 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002906 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002907 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002908 fi->fi_bi = 0;
2909 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002910 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002911 typval_T btv;
2912
2913 // Make a copy, so that the iteration still works when the
2914 // blob is changed.
2915 blob_copy(&tv, &btv);
2916 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002917 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002918 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002919 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920 else
2921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002922 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002923 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 }
2925 }
2926 }
2927 if (skip)
2928 --emsg_skip;
2929
2930 return fi;
2931}
2932
2933/*
2934 * Use the first item in a ":for" list. Advance to the next.
2935 * Assign the values to the variable (list). "arg" points to the first one.
2936 * Return TRUE when a valid item was found, FALSE when at end of list or
2937 * something wrong.
2938 */
2939 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002940next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002942 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002946 if (fi->fi_blob != NULL)
2947 {
2948 typval_T tv;
2949
2950 if (fi->fi_bi >= blob_len(fi->fi_blob))
2951 return FALSE;
2952 tv.v_type = VAR_NUMBER;
2953 tv.v_lock = VAR_FIXED;
2954 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2955 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002956 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2957 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002958 }
2959
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002960 item = fi->fi_lw.lw_item;
2961 if (item == NULL)
2962 result = FALSE;
2963 else
2964 {
2965 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002966 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2967 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968 }
2969 return result;
2970}
2971
2972/*
2973 * Free the structure used to store info used by ":for".
2974 */
2975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002976free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977{
Bram Moolenaar33570922005-01-25 22:26:29 +00002978 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002980 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002981 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002983 list_unref(fi->fi_list);
2984 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002985 if (fi != NULL && fi->fi_blob != NULL)
2986 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987 vim_free(fi);
2988}
2989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2991
2992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002993set_context_for_expression(
2994 expand_T *xp,
2995 char_u *arg,
2996 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 int got_eq = FALSE;
2999 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 if (cmdidx == CMD_let)
3003 {
3004 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003005 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 {
3007 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003008 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 {
3010 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003011 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003012 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 break;
3014 }
3015 return;
3016 }
3017 }
3018 else
3019 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3020 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 while ((xp->xp_pattern = vim_strpbrk(arg,
3022 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3023 {
3024 c = *xp->xp_pattern;
3025 if (c == '&')
3026 {
3027 c = xp->xp_pattern[1];
3028 if (c == '&')
3029 {
3030 ++xp->xp_pattern;
3031 xp->xp_context = cmdidx != CMD_let || got_eq
3032 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3033 }
3034 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003037 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3038 xp->xp_pattern += 2;
3039
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042 else if (c == '$')
3043 {
3044 /* environment variable */
3045 xp->xp_context = EXPAND_ENV_VARS;
3046 }
3047 else if (c == '=')
3048 {
3049 got_eq = TRUE;
3050 xp->xp_context = EXPAND_EXPRESSION;
3051 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003052 else if (c == '#'
3053 && xp->xp_context == EXPAND_EXPRESSION)
3054 {
3055 /* Autoload function/variable contains '#'. */
3056 break;
3057 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003058 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 && xp->xp_context == EXPAND_FUNCTIONS
3060 && vim_strchr(xp->xp_pattern, '(') == NULL)
3061 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003062 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 break;
3064 }
3065 else if (cmdidx != CMD_let || got_eq)
3066 {
3067 if (c == '"') /* string */
3068 {
3069 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3070 if (c == '\\' && xp->xp_pattern[1] != NUL)
3071 ++xp->xp_pattern;
3072 xp->xp_context = EXPAND_NOTHING;
3073 }
3074 else if (c == '\'') /* literal string */
3075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003076 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3078 /* skip */ ;
3079 xp->xp_context = EXPAND_NOTHING;
3080 }
3081 else if (c == '|')
3082 {
3083 if (xp->xp_pattern[1] == '|')
3084 {
3085 ++xp->xp_pattern;
3086 xp->xp_context = EXPAND_EXPRESSION;
3087 }
3088 else
3089 xp->xp_context = EXPAND_COMMANDS;
3090 }
3091 else
3092 xp->xp_context = EXPAND_EXPRESSION;
3093 }
3094 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095 /* Doesn't look like something valid, expand as an expression
3096 * anyway. */
3097 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 arg = xp->xp_pattern;
3099 if (*arg != NUL)
3100 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3101 /* skip */ ;
3102 }
3103 xp->xp_pattern = arg;
3104}
3105
3106#endif /* FEAT_CMDL_COMPL */
3107
3108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * ":unlet[!] var1 ... " command.
3110 */
3111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003112ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003114 ex_unletlock(eap, eap->arg, 0);
3115}
3116
3117/*
3118 * ":lockvar" and ":unlockvar" commands
3119 */
3120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003121ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003122{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003124 int deep = 2;
3125
3126 if (eap->forceit)
3127 deep = -1;
3128 else if (vim_isdigit(*arg))
3129 {
3130 deep = getdigits(&arg);
3131 arg = skipwhite(arg);
3132 }
3133
3134 ex_unletlock(eap, arg, deep);
3135}
3136
3137/*
3138 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3139 */
3140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003141ex_unletlock(
3142 exarg_T *eap,
3143 char_u *argstart,
3144 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003145{
3146 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003149 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 do
3152 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003153 if (*arg == '$')
3154 {
3155 char_u *name = ++arg;
3156
3157 if (get_env_len(&arg) == 0)
3158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003159 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003160 return;
3161 }
3162 vim_unsetenv(name);
3163 arg = skipwhite(arg);
3164 continue;
3165 }
3166
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003168 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003169 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003170 if (lv.ll_name == NULL)
3171 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003172 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003173 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003175 if (name_end != NULL)
3176 {
3177 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003178 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003179 }
3180 if (!(eap->skip || error))
3181 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 break;
3183 }
3184
3185 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003186 {
3187 if (eap->cmdidx == CMD_unlet)
3188 {
3189 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3190 error = TRUE;
3191 }
3192 else
3193 {
3194 if (do_lock_var(&lv, name_end, deep,
3195 eap->cmdidx == CMD_lockvar) == FAIL)
3196 error = TRUE;
3197 }
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 if (!eap->skip)
3201 clear_lval(&lv);
3202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 arg = skipwhite(name_end);
3204 } while (!ends_excmd(*arg));
3205
3206 eap->nextcmd = check_nextcmd(arg);
3207}
3208
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210do_unlet_var(
3211 lval_T *lp,
3212 char_u *name_end,
3213 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 int ret = OK;
3216 int cc;
3217
3218 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 cc = *name_end;
3221 *name_end = NUL;
3222
3223 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003224 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003226 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003227 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003228 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003229 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003230 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003231 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003232 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 else if (lp->ll_range)
3234 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003236 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003237 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003238
3239 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3240 {
3241 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003242 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003243 return FAIL;
3244 ll_li = li;
3245 ++ll_n1;
3246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003247
3248 /* Delete a range of List items. */
3249 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3250 {
3251 li = lp->ll_li->li_next;
3252 listitem_remove(lp->ll_list, lp->ll_li);
3253 lp->ll_li = li;
3254 ++lp->ll_n1;
3255 }
3256 }
3257 else
3258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 /* unlet a List item. */
3261 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003263 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003264 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 }
3266
3267 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003268}
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270/*
3271 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003272 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
3274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003275do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 hashtab_T *ht;
3278 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003279 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003280 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003281 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 ht = find_var_ht(name, &varname);
3284 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003287 if (d == NULL)
3288 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 if (ht == &globvarht)
3290 d = &globvardict;
3291 else if (ht == &compat_hashtab)
3292 d = &vimvardict;
3293 else
3294 {
3295 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3296 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3297 }
3298 if (d == NULL)
3299 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003300 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003301 return FAIL;
3302 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003303 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003305 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003306 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003307 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003308 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003309 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003310 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003311 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003312 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003313 return FAIL;
3314
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003315 delete_var(ht, hi);
3316 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003319 if (forceit)
3320 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003321 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 return FAIL;
3323}
3324
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003325/*
3326 * Lock or unlock variable indicated by "lp".
3327 * "deep" is the levels to go (-1 for unlimited);
3328 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3329 */
3330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003331do_lock_var(
3332 lval_T *lp,
3333 char_u *name_end,
3334 int deep,
3335 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003336{
3337 int ret = OK;
3338 int cc;
3339 dictitem_T *di;
3340
3341 if (deep == 0) /* nothing to do */
3342 return OK;
3343
3344 if (lp->ll_tv == NULL)
3345 {
3346 cc = *name_end;
3347 *name_end = NUL;
3348
3349 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003350 di = find_var(lp->ll_name, NULL, TRUE);
3351 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003352 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003353 else if ((di->di_flags & DI_FLAGS_FIX)
3354 && di->di_tv.v_type != VAR_DICT
3355 && di->di_tv.v_type != VAR_LIST)
3356 /* For historic reasons this error is not given for a list or dict.
3357 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003358 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003359 else
3360 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003361 if (lock)
3362 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003363 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003364 di->di_flags &= ~DI_FLAGS_LOCK;
3365 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 }
3367 *name_end = cc;
3368 }
3369 else if (lp->ll_range)
3370 {
3371 listitem_T *li = lp->ll_li;
3372
3373 /* (un)lock a range of List items. */
3374 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3375 {
3376 item_lock(&li->li_tv, deep, lock);
3377 li = li->li_next;
3378 ++lp->ll_n1;
3379 }
3380 }
3381 else if (lp->ll_list != NULL)
3382 /* (un)lock a List item. */
3383 item_lock(&lp->ll_li->li_tv, deep, lock);
3384 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003385 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003386 item_lock(&lp->ll_di->di_tv, deep, lock);
3387
3388 return ret;
3389}
3390
3391/*
3392 * Lock or unlock an item. "deep" is nr of levels to go.
3393 */
3394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003395item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003396{
3397 static int recurse = 0;
3398 list_T *l;
3399 listitem_T *li;
3400 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003401 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003402 hashitem_T *hi;
3403 int todo;
3404
3405 if (recurse >= DICT_MAXNEST)
3406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003407 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003408 return;
3409 }
3410 if (deep == 0)
3411 return;
3412 ++recurse;
3413
3414 /* lock/unlock the item itself */
3415 if (lock)
3416 tv->v_lock |= VAR_LOCKED;
3417 else
3418 tv->v_lock &= ~VAR_LOCKED;
3419
3420 switch (tv->v_type)
3421 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003422 case VAR_UNKNOWN:
3423 case VAR_NUMBER:
3424 case VAR_STRING:
3425 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003426 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003427 case VAR_FLOAT:
3428 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003429 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003430 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003431 break;
3432
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003433 case VAR_BLOB:
3434 if ((b = tv->vval.v_blob) != NULL)
3435 {
3436 if (lock)
3437 b->bv_lock |= VAR_LOCKED;
3438 else
3439 b->bv_lock &= ~VAR_LOCKED;
3440 }
3441 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003442 case VAR_LIST:
3443 if ((l = tv->vval.v_list) != NULL)
3444 {
3445 if (lock)
3446 l->lv_lock |= VAR_LOCKED;
3447 else
3448 l->lv_lock &= ~VAR_LOCKED;
3449 if (deep < 0 || deep > 1)
3450 /* recursive: lock/unlock the items the List contains */
3451 for (li = l->lv_first; li != NULL; li = li->li_next)
3452 item_lock(&li->li_tv, deep - 1, lock);
3453 }
3454 break;
3455 case VAR_DICT:
3456 if ((d = tv->vval.v_dict) != NULL)
3457 {
3458 if (lock)
3459 d->dv_lock |= VAR_LOCKED;
3460 else
3461 d->dv_lock &= ~VAR_LOCKED;
3462 if (deep < 0 || deep > 1)
3463 {
3464 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003465 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003466 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3467 {
3468 if (!HASHITEM_EMPTY(hi))
3469 {
3470 --todo;
3471 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3472 }
3473 }
3474 }
3475 }
3476 }
3477 --recurse;
3478}
3479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3481/*
3482 * Delete all "menutrans_" variables.
3483 */
3484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003485del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003488 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar33570922005-01-25 22:26:29 +00003490 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003491 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003492 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003493 {
3494 if (!HASHITEM_EMPTY(hi))
3495 {
3496 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003497 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3498 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003499 }
3500 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502}
3503#endif
3504
3505#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3506
3507/*
3508 * Local string buffer for the next two functions to store a variable name
3509 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3510 * get_user_var_name().
3511 */
3512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513static char_u *varnamebuf = NULL;
3514static int varnamebuflen = 0;
3515
3516/*
3517 * Function to concatenate a prefix and a variable name.
3518 */
3519 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 int len;
3523
3524 len = (int)STRLEN(name) + 3;
3525 if (len > varnamebuflen)
3526 {
3527 vim_free(varnamebuf);
3528 len += 10; /* some additional space */
3529 varnamebuf = alloc(len);
3530 if (varnamebuf == NULL)
3531 {
3532 varnamebuflen = 0;
3533 return NULL;
3534 }
3535 varnamebuflen = len;
3536 }
3537 *varnamebuf = prefix;
3538 varnamebuf[1] = ':';
3539 STRCPY(varnamebuf + 2, name);
3540 return varnamebuf;
3541}
3542
3543/*
3544 * Function given to ExpandGeneric() to obtain the list of user defined
3545 * (global/buffer/window/built-in) variable names.
3546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003550 static long_u gdone;
3551 static long_u bdone;
3552 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003553 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003554 static int vidx;
3555 static hashitem_T *hi;
3556 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
3558 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003559 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003560 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003561 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003562 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003563
3564 /* Global variables */
3565 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003567 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003568 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003569 else
3570 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003571 while (HASHITEM_EMPTY(hi))
3572 ++hi;
3573 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3574 return cat_prefix_varname('g', hi->hi_key);
3575 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003577
3578 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003579 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003582 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003584 else
3585 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003586 while (HASHITEM_EMPTY(hi))
3587 ++hi;
3588 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003590
3591 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003592 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003595 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003597 else
3598 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003599 while (HASHITEM_EMPTY(hi))
3600 ++hi;
3601 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003603
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003604 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003605 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003606 if (tdone < ht->ht_used)
3607 {
3608 if (tdone++ == 0)
3609 hi = ht->ht_array;
3610 else
3611 ++hi;
3612 while (HASHITEM_EMPTY(hi))
3613 ++hi;
3614 return cat_prefix_varname('t', hi->hi_key);
3615 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003616
Bram Moolenaar33570922005-01-25 22:26:29 +00003617 /* v: variables */
3618 if (vidx < VV_LEN)
3619 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaard23a8232018-02-10 18:45:26 +01003621 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 varnamebuflen = 0;
3623 return NULL;
3624}
3625
3626#endif /* FEAT_CMDL_COMPL */
3627
3628/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003629 * Return TRUE if "pat" matches "text".
3630 * Does not use 'cpo' and always uses 'magic'.
3631 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003632 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003633pattern_match(char_u *pat, char_u *text, int ic)
3634{
3635 int matches = FALSE;
3636 char_u *save_cpo;
3637 regmatch_T regmatch;
3638
3639 /* avoid 'l' flag in 'cpoptions' */
3640 save_cpo = p_cpo;
3641 p_cpo = (char_u *)"";
3642 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3643 if (regmatch.regprog != NULL)
3644 {
3645 regmatch.rm_ic = ic;
3646 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3647 vim_regfree(regmatch.regprog);
3648 }
3649 p_cpo = save_cpo;
3650 return matches;
3651}
3652
3653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3657 */
3658
3659/*
3660 * Handle zero level expression.
3661 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003662 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003663 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 * Return OK or FAIL.
3665 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667eval0(
3668 char_u *arg,
3669 typval_T *rettv,
3670 char_u **nextcmd,
3671 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 int ret;
3674 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003675 int did_emsg_before = did_emsg;
3676 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003679 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (ret == FAIL || !ends_excmd(*p))
3681 {
3682 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003683 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /*
3685 * Report the invalid expression unless the expression evaluation has
3686 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003687 * exception, or we already gave a more specific error.
3688 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003690 if (!aborting() && did_emsg == did_emsg_before
3691 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003692 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 ret = FAIL;
3694 }
3695 if (nextcmd != NULL)
3696 *nextcmd = check_nextcmd(p);
3697
3698 return ret;
3699}
3700
3701/*
3702 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003703 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 *
3705 * "arg" must point to the first non-white of the expression.
3706 * "arg" is advanced to the next non-white after the recognized expression.
3707 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003708 * Note: "rettv.v_lock" is not set.
3709 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 * Return OK or FAIL.
3711 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003713eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
3715 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 /*
3719 * Get the first variable.
3720 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 return FAIL;
3723
3724 if ((*arg)[0] == '?')
3725 {
3726 result = FALSE;
3727 if (evaluate)
3728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 int error = FALSE;
3730
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003731 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 if (error)
3735 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
3738 /*
3739 * Get the second variable.
3740 */
3741 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 return FAIL;
3744
3745 /*
3746 * Check for the ":".
3747 */
3748 if ((*arg)[0] != ':')
3749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003750 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 return FAIL;
3754 }
3755
3756 /*
3757 * Get the third variable.
3758 */
3759 *arg = skipwhite(*arg + 1);
3760 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3761 {
3762 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return FAIL;
3765 }
3766 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769
3770 return OK;
3771}
3772
3773/*
3774 * Handle first level expression:
3775 * expr2 || expr2 || expr2 logical OR
3776 *
3777 * "arg" must point to the first non-white of the expression.
3778 * "arg" is advanced to the next non-white after the recognized expression.
3779 *
3780 * Return OK or FAIL.
3781 */
3782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003783eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 long result;
3787 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 /*
3791 * Get the first variable.
3792 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795
3796 /*
3797 * Repeat until there is no following "||".
3798 */
3799 first = TRUE;
3800 result = FALSE;
3801 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3802 {
3803 if (evaluate && first)
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 first = FALSE;
3811 }
3812
3813 /*
3814 * Get the second variable.
3815 */
3816 *arg = skipwhite(*arg + 2);
3817 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3818 return FAIL;
3819
3820 /*
3821 * Compute the result.
3822 */
3823 if (evaluate && !result)
3824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003825 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 if (error)
3829 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 if (evaluate)
3832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 rettv->v_type = VAR_NUMBER;
3834 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 }
3837
3838 return OK;
3839}
3840
3841/*
3842 * Handle second level expression:
3843 * expr3 && expr3 && expr3 logical AND
3844 *
3845 * "arg" must point to the first non-white of the expression.
3846 * "arg" is advanced to the next non-white after the recognized expression.
3847 *
3848 * Return OK or FAIL.
3849 */
3850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003851eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 long result;
3855 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
3858 /*
3859 * Get the first variable.
3860 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 return FAIL;
3863
3864 /*
3865 * Repeat until there is no following "&&".
3866 */
3867 first = TRUE;
3868 result = TRUE;
3869 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3870 {
3871 if (evaluate && first)
3872 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003873 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003876 if (error)
3877 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 first = FALSE;
3879 }
3880
3881 /*
3882 * Get the second variable.
3883 */
3884 *arg = skipwhite(*arg + 2);
3885 if (eval4(arg, &var2, evaluate && result) == FAIL)
3886 return FAIL;
3887
3888 /*
3889 * Compute the result.
3890 */
3891 if (evaluate && result)
3892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003896 if (error)
3897 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 if (evaluate)
3900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 rettv->v_type = VAR_NUMBER;
3902 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904 }
3905
3906 return OK;
3907}
3908
3909/*
3910 * Handle third level expression:
3911 * var1 == var2
3912 * var1 =~ var2
3913 * var1 != var2
3914 * var1 !~ var2
3915 * var1 > var2
3916 * var1 >= var2
3917 * var1 < var2
3918 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003919 * var1 is var2
3920 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 *
3922 * "arg" must point to the first non-white of the expression.
3923 * "arg" is advanced to the next non-white after the recognized expression.
3924 *
3925 * Return OK or FAIL.
3926 */
3927 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003928eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 char_u *p;
3932 int i;
3933 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003934 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 /*
3939 * Get the first variable.
3940 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 return FAIL;
3943
3944 p = *arg;
3945 switch (p[0])
3946 {
3947 case '=': if (p[1] == '=')
3948 type = TYPE_EQUAL;
3949 else if (p[1] == '~')
3950 type = TYPE_MATCH;
3951 break;
3952 case '!': if (p[1] == '=')
3953 type = TYPE_NEQUAL;
3954 else if (p[1] == '~')
3955 type = TYPE_NOMATCH;
3956 break;
3957 case '>': if (p[1] != '=')
3958 {
3959 type = TYPE_GREATER;
3960 len = 1;
3961 }
3962 else
3963 type = TYPE_GEQUAL;
3964 break;
3965 case '<': if (p[1] != '=')
3966 {
3967 type = TYPE_SMALLER;
3968 len = 1;
3969 }
3970 else
3971 type = TYPE_SEQUAL;
3972 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003973 case 'i': if (p[1] == 's')
3974 {
3975 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3976 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003977 i = p[len];
3978 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003979 {
3980 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3981 type_is = TRUE;
3982 }
3983 }
3984 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986
3987 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003988 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 */
3990 if (type != TYPE_UNKNOWN)
3991 {
3992 /* extra question mark appended: ignore case */
3993 if (p[len] == '?')
3994 {
3995 ic = TRUE;
3996 ++len;
3997 }
3998 /* extra '#' appended: match case */
3999 else if (p[len] == '#')
4000 {
4001 ic = FALSE;
4002 ++len;
4003 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004004 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 else
4006 ic = p_ic;
4007
4008 /*
4009 * Get the second variable.
4010 */
4011 *arg = skipwhite(p + len);
4012 if (eval5(arg, &var2, evaluate) == FAIL)
4013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return FAIL;
4016 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004017 if (evaluate)
4018 {
4019 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4020
4021 clear_tv(&var2);
4022 return ret;
4023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025
4026 return OK;
4027}
4028
4029/*
4030 * Handle fourth level expression:
4031 * + number addition
4032 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004033 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004034 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 *
4036 * "arg" must point to the first non-white of the expression.
4037 * "arg" is advanced to the next non-white after the recognized expression.
4038 *
4039 * Return OK or FAIL.
4040 */
4041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004042eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 typval_T var2;
4045 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004047 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004048#ifdef FEAT_FLOAT
4049 float_T f1 = 0, f2 = 0;
4050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u *s1, *s2;
4052 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4053 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004054 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056 /*
4057 * Get the first variable.
4058 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004059 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return FAIL;
4061
4062 /*
4063 * Repeat computing, until no '+', '-' or '.' is following.
4064 */
4065 for (;;)
4066 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004067 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004069 concat = op == '.'
4070 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4071 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 break;
4073
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004074 if ((op != '+' || (rettv->v_type != VAR_LIST
4075 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076#ifdef FEAT_FLOAT
4077 && (op == '.' || rettv->v_type != VAR_FLOAT)
4078#endif
4079 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004080 {
4081 /* For "list + ...", an illegal use of the first operand as
4082 * a number cannot be determined before evaluating the 2nd
4083 * operand: if this is also a list, all is ok.
4084 * For "something . ...", "something - ..." or "non-list + ...",
4085 * we know that the first operand needs to be a string or number
4086 * without evaluating the 2nd operand. So check before to avoid
4087 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004088 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 {
4090 clear_tv(rettv);
4091 return FAIL;
4092 }
4093 }
4094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 /*
4096 * Get the second variable.
4097 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004098 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4099 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004101 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 return FAIL;
4105 }
4106
4107 if (evaluate)
4108 {
4109 /*
4110 * Compute the result.
4111 */
4112 if (op == '.')
4113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004114 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4115 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004116 if (s2 == NULL) /* type error ? */
4117 {
4118 clear_tv(rettv);
4119 clear_tv(&var2);
4120 return FAIL;
4121 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004122 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
4124 rettv->v_type = VAR_STRING;
4125 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004127 else if (op == '+' && rettv->v_type == VAR_BLOB
4128 && var2.v_type == VAR_BLOB)
4129 {
4130 blob_T *b1 = rettv->vval.v_blob;
4131 blob_T *b2 = var2.vval.v_blob;
4132 blob_T *b = blob_alloc();
4133 int i;
4134
4135 if (b != NULL)
4136 {
4137 for (i = 0; i < blob_len(b1); i++)
4138 ga_append(&b->bv_ga, blob_get(b1, i));
4139 for (i = 0; i < blob_len(b2); i++)
4140 ga_append(&b->bv_ga, blob_get(b2, i));
4141
4142 clear_tv(rettv);
4143 rettv_blob_set(rettv, b);
4144 }
4145 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004146 else if (op == '+' && rettv->v_type == VAR_LIST
4147 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004148 {
4149 /* concatenate Lists */
4150 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4151 &var3) == FAIL)
4152 {
4153 clear_tv(rettv);
4154 clear_tv(&var2);
4155 return FAIL;
4156 }
4157 clear_tv(rettv);
4158 *rettv = var3;
4159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 else
4161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 int error = FALSE;
4163
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004164#ifdef FEAT_FLOAT
4165 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004167 f1 = rettv->vval.v_float;
4168 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004170 else
4171#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004173 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004174 if (error)
4175 {
4176 /* This can only happen for "list + non-list". For
4177 * "non-list + ..." or "something - ...", we returned
4178 * before evaluating the 2nd operand. */
4179 clear_tv(rettv);
4180 return FAIL;
4181 }
4182#ifdef FEAT_FLOAT
4183 if (var2.v_type == VAR_FLOAT)
4184 f1 = n1;
4185#endif
4186 }
4187#ifdef FEAT_FLOAT
4188 if (var2.v_type == VAR_FLOAT)
4189 {
4190 f2 = var2.vval.v_float;
4191 n2 = 0;
4192 }
4193 else
4194#endif
4195 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004196 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004197 if (error)
4198 {
4199 clear_tv(rettv);
4200 clear_tv(&var2);
4201 return FAIL;
4202 }
4203#ifdef FEAT_FLOAT
4204 if (rettv->v_type == VAR_FLOAT)
4205 f2 = n2;
4206#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004209
4210#ifdef FEAT_FLOAT
4211 /* If there is a float on either side the result is a float. */
4212 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4213 {
4214 if (op == '+')
4215 f1 = f1 + f2;
4216 else
4217 f1 = f1 - f2;
4218 rettv->v_type = VAR_FLOAT;
4219 rettv->vval.v_float = f1;
4220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004222#endif
4223 {
4224 if (op == '+')
4225 n1 = n1 + n2;
4226 else
4227 n1 = n1 - n2;
4228 rettv->v_type = VAR_NUMBER;
4229 rettv->vval.v_number = n1;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 }
4235 return OK;
4236}
4237
4238/*
4239 * Handle fifth level expression:
4240 * * number multiplication
4241 * / number division
4242 * % number modulo
4243 *
4244 * "arg" must point to the first non-white of the expression.
4245 * "arg" is advanced to the next non-white after the recognized expression.
4246 *
4247 * Return OK or FAIL.
4248 */
4249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250eval6(
4251 char_u **arg,
4252 typval_T *rettv,
4253 int evaluate,
4254 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255{
Bram Moolenaar33570922005-01-25 22:26:29 +00004256 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004258 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004259#ifdef FEAT_FLOAT
4260 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004261 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004262#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
4265 /*
4266 * Get the first variable.
4267 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004268 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return FAIL;
4270
4271 /*
4272 * Repeat computing, until no '*', '/' or '%' is following.
4273 */
4274 for (;;)
4275 {
4276 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004277 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 break;
4279
4280 if (evaluate)
4281 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004282#ifdef FEAT_FLOAT
4283 if (rettv->v_type == VAR_FLOAT)
4284 {
4285 f1 = rettv->vval.v_float;
4286 use_float = TRUE;
4287 n1 = 0;
4288 }
4289 else
4290#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004291 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 if (error)
4294 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 else
4297 n1 = 0;
4298
4299 /*
4300 * Get the second variable.
4301 */
4302 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004303 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return FAIL;
4305
4306 if (evaluate)
4307 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004308#ifdef FEAT_FLOAT
4309 if (var2.v_type == VAR_FLOAT)
4310 {
4311 if (!use_float)
4312 {
4313 f1 = n1;
4314 use_float = TRUE;
4315 }
4316 f2 = var2.vval.v_float;
4317 n2 = 0;
4318 }
4319 else
4320#endif
4321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004322 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004323 clear_tv(&var2);
4324 if (error)
4325 return FAIL;
4326#ifdef FEAT_FLOAT
4327 if (use_float)
4328 f2 = n2;
4329#endif
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 /*
4333 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004336#ifdef FEAT_FLOAT
4337 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004339 if (op == '*')
4340 f1 = f1 * f2;
4341 else if (op == '/')
4342 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004343# ifdef VMS
4344 /* VMS crashes on divide by zero, work around it */
4345 if (f2 == 0.0)
4346 {
4347 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004348 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004349 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004350 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004351 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004352 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004353 }
4354 else
4355 f1 = f1 / f2;
4356# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 /* We rely on the floating point library to handle divide
4358 * by zero to result in "inf" and not a crash. */
4359 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004360# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004364 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004365 return FAIL;
4366 }
4367 rettv->v_type = VAR_FLOAT;
4368 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004373 if (op == '*')
4374 n1 = n1 * n2;
4375 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004376 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004378 n1 = num_modulus(n1, n2);
4379
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004380 rettv->v_type = VAR_NUMBER;
4381 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 }
4385
4386 return OK;
4387}
4388
4389/*
4390 * Handle sixth level expression:
4391 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004392 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004393 * "string" string constant
4394 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 * &option-name option value
4396 * @r register contents
4397 * identifier variable value
4398 * function() function call
4399 * $VAR environment variable
4400 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004401 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004402 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004403 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 *
4405 * Also handle:
4406 * ! in front logical NOT
4407 * - in front unary minus
4408 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004409 * trailing [] subscript in String or List
4410 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 *
4412 * "arg" must point to the first non-white of the expression.
4413 * "arg" is advanced to the next non-white after the recognized expression.
4414 *
4415 * Return OK or FAIL.
4416 */
4417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004418eval7(
4419 char_u **arg,
4420 typval_T *rettv,
4421 int evaluate,
4422 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004424 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 int len;
4426 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 char_u *start_leader, *end_leader;
4428 int ret = OK;
4429 char_u *alias;
4430
4431 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004438 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 */
4440 start_leader = *arg;
4441 while (**arg == '!' || **arg == '-' || **arg == '+')
4442 *arg = skipwhite(*arg + 1);
4443 end_leader = *arg;
4444
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004445 if (**arg == '.' && (!isdigit(*(*arg + 1))
4446#ifdef FEAT_FLOAT
4447 || current_sctx.sc_version < 2
4448#endif
4449 ))
4450 {
4451 semsg(_(e_invexpr2), *arg);
4452 ++*arg;
4453 return FAIL;
4454 }
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 switch (**arg)
4457 {
4458 /*
4459 * Number constant.
4460 */
4461 case '0':
4462 case '1':
4463 case '2':
4464 case '3':
4465 case '4':
4466 case '5':
4467 case '6':
4468 case '7':
4469 case '8':
4470 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004471 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004472 {
4473#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004474 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004475 int get_float = FALSE;
4476
4477 /* We accept a float when the format matches
4478 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004479 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004480 * With script version 2 and later the leading digit can be
4481 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004482 * Don't look for a float after the "." operator, so that
4483 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004484 if (**arg == '.')
4485 p = *arg;
4486 else
4487 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004488 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004490 get_float = TRUE;
4491 p = skipdigits(p + 2);
4492 if (*p == 'e' || *p == 'E')
4493 {
4494 ++p;
4495 if (*p == '-' || *p == '+')
4496 ++p;
4497 if (!vim_isdigit(*p))
4498 get_float = FALSE;
4499 else
4500 p = skipdigits(p + 1);
4501 }
4502 if (ASCII_ISALPHA(*p) || *p == '.')
4503 get_float = FALSE;
4504 }
4505 if (get_float)
4506 {
4507 float_T f;
4508
4509 *arg += string2float(*arg, &f);
4510 if (evaluate)
4511 {
4512 rettv->v_type = VAR_FLOAT;
4513 rettv->vval.v_float = f;
4514 }
4515 }
4516 else
4517#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004518 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004519 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004520 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004521 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004522
4523 // Blob constant: 0z0123456789abcdef
4524 if (evaluate)
4525 blob = blob_alloc();
4526 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4527 {
4528 if (!vim_isxdigit(bp[1]))
4529 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004530 if (blob != NULL)
4531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004532 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004533 ga_clear(&blob->bv_ga);
4534 VIM_CLEAR(blob);
4535 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004536 ret = FAIL;
4537 break;
4538 }
4539 if (blob != NULL)
4540 ga_append(&blob->bv_ga,
4541 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004542 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4543 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004544 }
4545 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004546 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004547 *arg = bp;
4548 }
4549 else
4550 {
4551 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004552 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4553 if (len == 0)
4554 {
4555 semsg(_(e_invexpr2), *arg);
4556 ret = FAIL;
4557 break;
4558 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004559 *arg += len;
4560 if (evaluate)
4561 {
4562 rettv->v_type = VAR_NUMBER;
4563 rettv->vval.v_number = n;
4564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 /*
4570 * String constant: "string".
4571 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 break;
4574
4575 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004576 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 break;
4580
4581 /*
4582 * List: [expr, expr]
4583 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 break;
4586
4587 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004588 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004589 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004590 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004591 {
4592 ++*arg;
4593 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4594 }
4595 else
4596 ret = NOTDONE;
4597 break;
4598
4599 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004600 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004601 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004603 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4604 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004605 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 break;
4607
4608 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004609 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004611 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 break;
4613
4614 /*
4615 * Environment variable: $VAR.
4616 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 break;
4619
4620 /*
4621 * Register contents: @r.
4622 */
4623 case '@': ++*arg;
4624 if (evaluate)
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004627 rettv->vval.v_string = get_reg_contents(**arg,
4628 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 if (**arg != NUL)
4631 ++*arg;
4632 break;
4633
4634 /*
4635 * nested expression: (expression).
4636 */
4637 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004638 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (**arg == ')')
4640 ++*arg;
4641 else if (ret == OK)
4642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004643 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 ret = FAIL;
4646 }
4647 break;
4648
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 break;
4651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652
4653 if (ret == NOTDONE)
4654 {
4655 /*
4656 * Must be a variable or function name.
4657 * Can also be a curly-braces kind of name: {expr}.
4658 */
4659 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004660 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661 if (alias != NULL)
4662 s = alias;
4663
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004664 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 ret = FAIL;
4666 else
4667 {
4668 if (**arg == '(') /* recursive! */
4669 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004670 partial_T *partial;
4671
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004672 if (!evaluate)
4673 check_vars(s, len);
4674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 /* If "s" is the name of a variable of type VAR_FUNC
4676 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004677 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004679 /* Need to make a copy, in case evaluating the arguments makes
4680 * the name invalid. */
4681 s = vim_strsave(s);
4682 if (s == NULL)
4683 ret = FAIL;
4684 else
4685 /* Invoke the function. */
4686 ret = get_func_tv(s, len, rettv, arg,
4687 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4688 &len, evaluate, partial, NULL);
4689 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004690
4691 /* If evaluate is FALSE rettv->v_type was not set in
4692 * get_func_tv, but it's needed in handle_subscript() to parse
4693 * what follows. So set it here. */
4694 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4695 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004696 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004697 rettv->v_type = VAR_FUNC;
4698 }
4699
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 /* Stop the expression evaluation when immediately
4701 * aborting on error, or when an interrupt occurred or
4702 * an exception was thrown but not caught. */
Bram Moolenaar60640732019-06-07 23:15:22 +02004703 if (evaluate && aborting())
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 {
4705 if (ret == OK)
4706 clear_tv(rettv);
4707 ret = FAIL;
4708 }
4709 }
4710 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004711 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004712 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004713 {
4714 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004715 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004718 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004719 }
4720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 *arg = skipwhite(*arg);
4722
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004723 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4724 * expr(expr). */
4725 if (ret == OK)
4726 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 /*
4729 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4730 */
4731 if (ret == OK && evaluate && end_leader > start_leader)
4732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004733 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004734 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735#ifdef FEAT_FLOAT
4736 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004738 if (rettv->v_type == VAR_FLOAT)
4739 f = rettv->vval.v_float;
4740 else
4741#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004742 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004743 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 clear_tv(rettv);
4746 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 else
4749 {
4750 while (end_leader > start_leader)
4751 {
4752 --end_leader;
4753 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754 {
4755#ifdef FEAT_FLOAT
4756 if (rettv->v_type == VAR_FLOAT)
4757 f = !f;
4758 else
4759#endif
4760 val = !val;
4761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004763 {
4764#ifdef FEAT_FLOAT
4765 if (rettv->v_type == VAR_FLOAT)
4766 f = -f;
4767 else
4768#endif
4769 val = -val;
4770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772#ifdef FEAT_FLOAT
4773 if (rettv->v_type == VAR_FLOAT)
4774 {
4775 clear_tv(rettv);
4776 rettv->vval.v_float = f;
4777 }
4778 else
4779#endif
4780 {
4781 clear_tv(rettv);
4782 rettv->v_type = VAR_NUMBER;
4783 rettv->vval.v_number = val;
4784 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787
4788 return ret;
4789}
4790
4791/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004792 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4793 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004794 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4795 */
4796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004797eval_index(
4798 char_u **arg,
4799 typval_T *rettv,
4800 int evaluate,
4801 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802{
4803 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004804 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004805 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807 long len = -1;
4808 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811
Bram Moolenaara03f2332016-02-06 18:09:59 +01004812 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004814 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004815 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004816 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004817 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004818 return FAIL;
4819 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004820#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004821 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004822 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004823 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004824#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004825 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004826 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004827 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004828 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004829 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004830 return FAIL;
4831 case VAR_UNKNOWN:
4832 if (evaluate)
4833 return FAIL;
4834 /* FALLTHROUGH */
4835
4836 case VAR_STRING:
4837 case VAR_NUMBER:
4838 case VAR_LIST:
4839 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004840 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004841 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004842 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004844 init_tv(&var1);
4845 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004846 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004848 /*
4849 * dict.name
4850 */
4851 key = *arg + 1;
4852 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4853 ;
4854 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004856 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004857 }
4858 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004860 /*
4861 * something[idx]
4862 *
4863 * Get the (first) variable from inside the [].
4864 */
4865 *arg = skipwhite(*arg + 1);
4866 if (**arg == ':')
4867 empty1 = TRUE;
4868 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4869 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004870 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004871 {
4872 /* not a number or string */
4873 clear_tv(&var1);
4874 return FAIL;
4875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004876
4877 /*
4878 * Get the second variable from inside the [:].
4879 */
4880 if (**arg == ':')
4881 {
4882 range = TRUE;
4883 *arg = skipwhite(*arg + 1);
4884 if (**arg == ']')
4885 empty2 = TRUE;
4886 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004888 if (!empty1)
4889 clear_tv(&var1);
4890 return FAIL;
4891 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004892 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004893 {
4894 /* not a number or string */
4895 if (!empty1)
4896 clear_tv(&var1);
4897 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 return FAIL;
4899 }
4900 }
4901
4902 /* Check for the ']'. */
4903 if (**arg != ']')
4904 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004905 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004906 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004907 clear_tv(&var1);
4908 if (range)
4909 clear_tv(&var2);
4910 return FAIL;
4911 }
4912 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 }
4914
4915 if (evaluate)
4916 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004917 n1 = 0;
4918 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004920 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 }
4923 if (range)
4924 {
4925 if (empty2)
4926 n2 = -1;
4927 else
4928 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004929 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004931 }
4932 }
4933
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004936 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004937 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004938 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004939 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004940 case VAR_SPECIAL:
4941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004942 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004943 break; /* not evaluating, skipping over subscript */
4944
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 case VAR_NUMBER:
4946 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004947 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 len = (long)STRLEN(s);
4949 if (range)
4950 {
4951 /* The resulting variable is a substring. If the indexes
4952 * are out of range the result is empty. */
4953 if (n1 < 0)
4954 {
4955 n1 = len + n1;
4956 if (n1 < 0)
4957 n1 = 0;
4958 }
4959 if (n2 < 0)
4960 n2 = len + n2;
4961 else if (n2 >= len)
4962 n2 = len;
4963 if (n1 >= len || n2 < 0 || n1 > n2)
4964 s = NULL;
4965 else
4966 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4967 }
4968 else
4969 {
4970 /* The resulting variable is a string of a single
4971 * character. If the index is too big or negative the
4972 * result is empty. */
4973 if (n1 >= len || n1 < 0)
4974 s = NULL;
4975 else
4976 s = vim_strnsave(s + n1, 1);
4977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 clear_tv(rettv);
4979 rettv->v_type = VAR_STRING;
4980 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 break;
4982
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004983 case VAR_BLOB:
4984 len = blob_len(rettv->vval.v_blob);
4985 if (range)
4986 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004987 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004988 // are out of range the result is empty.
4989 if (n1 < 0)
4990 {
4991 n1 = len + n1;
4992 if (n1 < 0)
4993 n1 = 0;
4994 }
4995 if (n2 < 0)
4996 n2 = len + n2;
4997 else if (n2 >= len)
4998 n2 = len - 1;
4999 if (n1 >= len || n2 < 0 || n1 > n2)
5000 {
5001 clear_tv(rettv);
5002 rettv->v_type = VAR_BLOB;
5003 rettv->vval.v_blob = NULL;
5004 }
5005 else
5006 {
5007 blob_T *blob = blob_alloc();
5008
5009 if (blob != NULL)
5010 {
5011 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5012 {
5013 blob_free(blob);
5014 return FAIL;
5015 }
5016 blob->bv_ga.ga_len = n2 - n1 + 1;
5017 for (i = n1; i <= n2; i++)
5018 blob_set(blob, i - n1,
5019 blob_get(rettv->vval.v_blob, i));
5020
5021 clear_tv(rettv);
5022 rettv_blob_set(rettv, blob);
5023 }
5024 }
5025 }
5026 else
5027 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005028 // The resulting variable is a byte value.
5029 // If the index is too big or negative that is an error.
5030 if (n1 < 0)
5031 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005032 if (n1 < len && n1 >= 0)
5033 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005034 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005035
5036 clear_tv(rettv);
5037 rettv->v_type = VAR_NUMBER;
5038 rettv->vval.v_number = v;
5039 }
5040 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005041 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005042 }
5043 break;
5044
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005045 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005047 if (n1 < 0)
5048 n1 = len + n1;
5049 if (!empty1 && (n1 < 0 || n1 >= len))
5050 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005051 /* For a range we allow invalid values and return an empty
5052 * list. A list index out of range is an error. */
5053 if (!range)
5054 {
5055 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005056 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005057 return FAIL;
5058 }
5059 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 }
5061 if (range)
5062 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005063 list_T *l;
5064 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005065
5066 if (n2 < 0)
5067 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005068 else if (n2 >= len)
5069 n2 = len - 1;
5070 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005071 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005072 l = list_alloc();
5073 if (l == NULL)
5074 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005076 n1 <= n2; ++n1)
5077 {
5078 if (list_append_tv(l, &item->li_tv) == FAIL)
5079 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005080 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005081 return FAIL;
5082 }
5083 item = item->li_next;
5084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005086 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005087 }
5088 else
5089 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005090 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 clear_tv(rettv);
5092 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005093 }
5094 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095
5096 case VAR_DICT:
5097 if (range)
5098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005099 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005100 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 if (len == -1)
5102 clear_tv(&var1);
5103 return FAIL;
5104 }
5105 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005106 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107
5108 if (len == -1)
5109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005110 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005111 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 clear_tv(&var1);
5114 return FAIL;
5115 }
5116 }
5117
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005118 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005120 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005121 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 if (len == -1)
5123 clear_tv(&var1);
5124 if (item == NULL)
5125 return FAIL;
5126
5127 copy_tv(&item->di_tv, &var1);
5128 clear_tv(rettv);
5129 *rettv = var1;
5130 }
5131 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005132 }
5133 }
5134
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 return OK;
5136}
5137
5138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 * Get an option value.
5140 * "arg" points to the '&' or '+' before the option name.
5141 * "arg" is advanced to character after the option name.
5142 * Return OK or FAIL.
5143 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005144 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005145get_option_tv(
5146 char_u **arg,
5147 typval_T *rettv, /* when NULL, only check if option exists */
5148 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149{
5150 char_u *option_end;
5151 long numval;
5152 char_u *stringval;
5153 int opt_type;
5154 int c;
5155 int working = (**arg == '+'); /* has("+option") */
5156 int ret = OK;
5157 int opt_flags;
5158
5159 /*
5160 * Isolate the option name and find its value.
5161 */
5162 option_end = find_option_end(arg, &opt_flags);
5163 if (option_end == NULL)
5164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005166 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 return FAIL;
5168 }
5169
5170 if (!evaluate)
5171 {
5172 *arg = option_end;
5173 return OK;
5174 }
5175
5176 c = *option_end;
5177 *option_end = NUL;
5178 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181 if (opt_type == -3) /* invalid name */
5182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005184 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 ret = FAIL;
5186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 if (opt_type == -2) /* hidden string option */
5190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 rettv->v_type = VAR_STRING;
5192 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 else if (opt_type == -1) /* hidden number option */
5195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 rettv->v_type = VAR_NUMBER;
5197 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 else if (opt_type == 1) /* number option */
5200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 rettv->v_type = VAR_NUMBER;
5202 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204 else /* string option */
5205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 rettv->v_type = VAR_STRING;
5207 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
5209 }
5210 else if (working && (opt_type == -2 || opt_type == -1))
5211 ret = FAIL;
5212
5213 *option_end = c; /* put back for error messages */
5214 *arg = option_end;
5215
5216 return ret;
5217}
5218
5219/*
5220 * Allocate a variable for a string constant.
5221 * Return OK or FAIL.
5222 */
5223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005224get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 char_u *p;
5227 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 int extra = 0;
5229
5230 /*
5231 * Find the end of the string, skipping backslashed characters.
5232 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005233 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
5235 if (*p == '\\' && p[1] != NUL)
5236 {
5237 ++p;
5238 /* A "\<x>" form occupies at least 4 characters, and produces up
5239 * to 6 characters: reserve space for 2 extra */
5240 if (*p == '<')
5241 extra += 2;
5242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244
5245 if (*p != '"')
5246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005247 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 return FAIL;
5249 }
5250
5251 /* If only parsing, set *arg and return here */
5252 if (!evaluate)
5253 {
5254 *arg = p + 1;
5255 return OK;
5256 }
5257
5258 /*
5259 * Copy the string into allocated memory, handling backslashed
5260 * characters.
5261 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005262 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (name == NULL)
5264 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 rettv->v_type = VAR_STRING;
5266 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 if (*p == '\\')
5271 {
5272 switch (*++p)
5273 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 case 'b': *name++ = BS; ++p; break;
5275 case 'e': *name++ = ESC; ++p; break;
5276 case 'f': *name++ = FF; ++p; break;
5277 case 'n': *name++ = NL; ++p; break;
5278 case 'r': *name++ = CAR; ++p; break;
5279 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
5281 case 'X': /* hex: "\x1", "\x12" */
5282 case 'x':
5283 case 'u': /* Unicode: "\u0023" */
5284 case 'U':
5285 if (vim_isxdigit(p[1]))
5286 {
5287 int n, nr;
5288 int c = toupper(*p);
5289
5290 if (c == 'X')
5291 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005292 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005294 else
5295 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 nr = 0;
5297 while (--n >= 0 && vim_isxdigit(p[1]))
5298 {
5299 ++p;
5300 nr = (nr << 4) + hex2nr(*p);
5301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 /* For "\u" store the number according to
5304 * 'encoding'. */
5305 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 break;
5311
5312 /* octal: "\1", "\12", "\123" */
5313 case '0':
5314 case '1':
5315 case '2':
5316 case '3':
5317 case '4':
5318 case '5':
5319 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 case '7': *name = *p++ - '0';
5321 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 *name = (*name << 3) + *p++ - '0';
5324 if (*p >= '0' && *p <= '7')
5325 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 break;
5329
5330 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005331 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (extra != 0)
5333 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 break;
5336 }
5337 /* FALLTHROUGH */
5338
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 break;
5341 }
5342 }
5343 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005348 if (*p != NUL) /* just in case */
5349 ++p;
5350 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 return OK;
5353}
5354
5355/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 * Return OK or FAIL.
5358 */
5359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005360get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361{
5362 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005363 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005364 int reduce = 0;
5365
5366 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005368 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005369 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005370 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005372 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005374 break;
5375 ++reduce;
5376 ++p;
5377 }
5378 }
5379
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005382 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005383 return FAIL;
5384 }
5385
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005387 if (!evaluate)
5388 {
5389 *arg = p + 1;
5390 return OK;
5391 }
5392
5393 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005395 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005396 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005397 if (str == NULL)
5398 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 rettv->v_type = VAR_STRING;
5400 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005401
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005403 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005407 break;
5408 ++p;
5409 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005411 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005413 *arg = p + 1;
5414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005415 return OK;
5416}
5417
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005418/*
5419 * Return the function name of the partial.
5420 */
5421 char_u *
5422partial_name(partial_T *pt)
5423{
5424 if (pt->pt_name != NULL)
5425 return pt->pt_name;
5426 return pt->pt_func->uf_name;
5427}
5428
Bram Moolenaarddecc252016-04-06 22:59:37 +02005429 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005430partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005431{
5432 int i;
5433
5434 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005435 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005436 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005437 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005438 if (pt->pt_name != NULL)
5439 {
5440 func_unref(pt->pt_name);
5441 vim_free(pt->pt_name);
5442 }
5443 else
5444 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005445 vim_free(pt);
5446}
5447
5448/*
5449 * Unreference a closure: decrement the reference count and free it when it
5450 * becomes zero.
5451 */
5452 void
5453partial_unref(partial_T *pt)
5454{
5455 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005456 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005457}
5458
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005459static int tv_equal_recurse_limit;
5460
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005461 static int
5462func_equal(
5463 typval_T *tv1,
5464 typval_T *tv2,
5465 int ic) /* ignore case */
5466{
5467 char_u *s1, *s2;
5468 dict_T *d1, *d2;
5469 int a1, a2;
5470 int i;
5471
5472 /* empty and NULL function name considered the same */
5473 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005474 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005475 if (s1 != NULL && *s1 == NUL)
5476 s1 = NULL;
5477 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005478 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005479 if (s2 != NULL && *s2 == NUL)
5480 s2 = NULL;
5481 if (s1 == NULL || s2 == NULL)
5482 {
5483 if (s1 != s2)
5484 return FALSE;
5485 }
5486 else if (STRCMP(s1, s2) != 0)
5487 return FALSE;
5488
5489 /* empty dict and NULL dict is different */
5490 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5491 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5492 if (d1 == NULL || d2 == NULL)
5493 {
5494 if (d1 != d2)
5495 return FALSE;
5496 }
5497 else if (!dict_equal(d1, d2, ic, TRUE))
5498 return FALSE;
5499
5500 /* empty list and no list considered the same */
5501 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5502 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5503 if (a1 != a2)
5504 return FALSE;
5505 for (i = 0; i < a1; ++i)
5506 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5507 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5508 return FALSE;
5509
5510 return TRUE;
5511}
5512
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005513/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005514 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005515 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005516 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005517 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005519tv_equal(
5520 typval_T *tv1,
5521 typval_T *tv2,
5522 int ic, /* ignore case */
5523 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005524{
5525 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005526 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005527 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005528 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005529
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005530 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005531 * recursiveness to a limit. We guess they are equal then.
5532 * A fixed limit has the problem of still taking an awful long time.
5533 * Reduce the limit every time running into it. That should work fine for
5534 * deeply linked structures that are not recursively linked and catch
5535 * recursiveness quickly. */
5536 if (!recursive)
5537 tv_equal_recurse_limit = 1000;
5538 if (recursive_cnt >= tv_equal_recurse_limit)
5539 {
5540 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005541 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005542 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005543
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005544 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5545 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005546 if ((tv1->v_type == VAR_FUNC
5547 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5548 && (tv2->v_type == VAR_FUNC
5549 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5550 {
5551 ++recursive_cnt;
5552 r = func_equal(tv1, tv2, ic);
5553 --recursive_cnt;
5554 return r;
5555 }
5556
5557 if (tv1->v_type != tv2->v_type)
5558 return FALSE;
5559
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005560 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005561 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005562 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005563 ++recursive_cnt;
5564 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5565 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005566 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005567
5568 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005569 ++recursive_cnt;
5570 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5571 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005572 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005573
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005574 case VAR_BLOB:
5575 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5576
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005577 case VAR_NUMBER:
5578 return tv1->vval.v_number == tv2->vval.v_number;
5579
5580 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005581 s1 = tv_get_string_buf(tv1, buf1);
5582 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005583 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005584
5585 case VAR_SPECIAL:
5586 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005587
5588 case VAR_FLOAT:
5589#ifdef FEAT_FLOAT
5590 return tv1->vval.v_float == tv2->vval.v_float;
5591#endif
5592 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005593#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005594 return tv1->vval.v_job == tv2->vval.v_job;
5595#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005596 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005597#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005598 return tv1->vval.v_channel == tv2->vval.v_channel;
5599#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005600 case VAR_FUNC:
5601 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005602 case VAR_UNKNOWN:
5603 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005604 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005605
Bram Moolenaara03f2332016-02-06 18:09:59 +01005606 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5607 * does not equal anything, not even itself. */
5608 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005609}
5610
5611/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005612 * Return the next (unique) copy ID.
5613 * Used for serializing nested structures.
5614 */
5615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005616get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005617{
5618 current_copyID += COPYID_INC;
5619 return current_copyID;
5620}
5621
5622/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005623 * Garbage collection for lists and dictionaries.
5624 *
5625 * We use reference counts to be able to free most items right away when they
5626 * are no longer used. But for composite items it's possible that it becomes
5627 * unused while the reference count is > 0: When there is a recursive
5628 * reference. Example:
5629 * :let l = [1, 2, 3]
5630 * :let d = {9: l}
5631 * :let l[1] = d
5632 *
5633 * Since this is quite unusual we handle this with garbage collection: every
5634 * once in a while find out which lists and dicts are not referenced from any
5635 * variable.
5636 *
5637 * Here is a good reference text about garbage collection (refers to Python
5638 * but it applies to all reference-counting mechanisms):
5639 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005640 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005641
5642/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005643 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005644 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005645 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005646 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005647 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005648garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005649{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005650 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005651 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005652 buf_T *buf;
5653 win_T *wp;
5654 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005655 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005656 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005657
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005658 if (!testing)
5659 {
5660 /* Only do this once. */
5661 want_garbage_collect = FALSE;
5662 may_garbage_collect = FALSE;
5663 garbage_collect_at_exit = FALSE;
5664 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005665
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005666 /* We advance by two because we add one for items referenced through
5667 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005668 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005669
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005670 /*
5671 * 1. Go through all accessible variables and mark all lists and dicts
5672 * with copyID.
5673 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005674
5675 /* Don't free variables in the previous_funccal list unless they are only
5676 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005677 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005679
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005680 /* script-local variables */
5681 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005682 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005683
5684 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005685 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005686 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5687 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005688
5689 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005690 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005691 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5692 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005693 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005694 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5695 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005696#ifdef FEAT_TEXT_PROP
5697 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5698 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5699 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005700 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005701 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005702 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5703 NULL, NULL);
5704#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005705
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005706 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005707 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005708 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5709 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005710 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005711 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005712
5713 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005715
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005716 /* named functions (matters for closures) */
5717 abort = abort || set_ref_in_functions(copyID);
5718
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005719 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005721
Bram Moolenaard812df62008-11-09 12:46:09 +00005722 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005723 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005724
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005725 // callbacks in buffers
5726 abort = abort || set_ref_in_buffers(copyID);
5727
Bram Moolenaar1dced572012-04-05 16:54:08 +02005728#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005729 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005730#endif
5731
Bram Moolenaardb913952012-06-29 12:54:53 +02005732#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005733 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005734#endif
5735
5736#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005737 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005738#endif
5739
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005740#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005741 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005742 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005743#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005744#ifdef FEAT_NETBEANS_INTG
5745 abort = abort || set_ref_in_nb_channel(copyID);
5746#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005747
Bram Moolenaare3188e22016-05-31 21:13:04 +02005748#ifdef FEAT_TIMERS
5749 abort = abort || set_ref_in_timer(copyID);
5750#endif
5751
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005752#ifdef FEAT_QUICKFIX
5753 abort = abort || set_ref_in_quickfix(copyID);
5754#endif
5755
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005756#ifdef FEAT_TERMINAL
5757 abort = abort || set_ref_in_term(copyID);
5758#endif
5759
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005760#ifdef FEAT_TEXT_PROP
5761 abort = abort || set_ref_in_popups(copyID);
5762#endif
5763
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005764 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005765 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005766 /*
5767 * 2. Free lists and dictionaries that are not referenced.
5768 */
5769 did_free = free_unref_items(copyID);
5770
5771 /*
5772 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005773 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005774 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005775 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005776 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005777 else if (p_verbose > 0)
5778 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005779 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005780 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005781
5782 return did_free;
5783}
5784
5785/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005786 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005787 */
5788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005789free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005790{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005791 int did_free = FALSE;
5792
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005793 /* Let all "free" functions know that we are here. This means no
5794 * dictionaries, lists, channels or jobs are to be freed, because we will
5795 * do that here. */
5796 in_free_unref_items = TRUE;
5797
5798 /*
5799 * PASS 1: free the contents of the items. We don't free the items
5800 * themselves yet, so that it is possible to decrement refcount counters
5801 */
5802
Bram Moolenaarcd524592016-07-17 14:57:05 +02005803 /* Go through the list of dicts and free items without the copyID. */
5804 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005805
Bram Moolenaarda861d62016-07-17 15:46:27 +02005806 /* Go through the list of lists and free items without the copyID. */
5807 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005808
5809#ifdef FEAT_JOB_CHANNEL
5810 /* Go through the list of jobs and free items without the copyID. This
5811 * must happen before doing channels, because jobs refer to channels, but
5812 * the reference from the channel to the job isn't tracked. */
5813 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5814
5815 /* Go through the list of channels and free items without the copyID. */
5816 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5817#endif
5818
5819 /*
5820 * PASS 2: free the items themselves.
5821 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005822 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005823 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005824
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005825#ifdef FEAT_JOB_CHANNEL
5826 /* Go through the list of jobs and free items without the copyID. This
5827 * must happen before doing channels, because jobs refer to channels, but
5828 * the reference from the channel to the job isn't tracked. */
5829 free_unused_jobs(copyID, COPYID_MASK);
5830
5831 /* Go through the list of channels and free items without the copyID. */
5832 free_unused_channels(copyID, COPYID_MASK);
5833#endif
5834
5835 in_free_unref_items = FALSE;
5836
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005837 return did_free;
5838}
5839
5840/*
5841 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005842 * "list_stack" is used to add lists to be marked. Can be NULL.
5843 *
5844 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005845 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005847set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005848{
5849 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005850 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005851 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005852 hashtab_T *cur_ht;
5853 ht_stack_T *ht_stack = NULL;
5854 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005855
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 cur_ht = ht;
5857 for (;;)
5858 {
5859 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005860 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005861 /* Mark each item in the hashtab. If the item contains a hashtab
5862 * it is added to ht_stack, if it contains a list it is added to
5863 * list_stack. */
5864 todo = (int)cur_ht->ht_used;
5865 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5866 if (!HASHITEM_EMPTY(hi))
5867 {
5868 --todo;
5869 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5870 &ht_stack, list_stack);
5871 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005872 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005873
5874 if (ht_stack == NULL)
5875 break;
5876
5877 /* take an item from the stack */
5878 cur_ht = ht_stack->ht;
5879 tempitem = ht_stack;
5880 ht_stack = ht_stack->prev;
5881 free(tempitem);
5882 }
5883
5884 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005885}
5886
5887/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005888 * Mark a dict and its items with "copyID".
5889 * Returns TRUE if setting references failed somehow.
5890 */
5891 int
5892set_ref_in_dict(dict_T *d, int copyID)
5893{
5894 if (d != NULL && d->dv_copyID != copyID)
5895 {
5896 d->dv_copyID = copyID;
5897 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5898 }
5899 return FALSE;
5900}
5901
5902/*
5903 * Mark a list and its items with "copyID".
5904 * Returns TRUE if setting references failed somehow.
5905 */
5906 int
5907set_ref_in_list(list_T *ll, int copyID)
5908{
5909 if (ll != NULL && ll->lv_copyID != copyID)
5910 {
5911 ll->lv_copyID = copyID;
5912 return set_ref_in_list_items(ll, copyID, NULL);
5913 }
5914 return FALSE;
5915}
5916
5917/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005918 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005919 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5920 *
5921 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005922 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005923 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005924set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005925{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005926 listitem_T *li;
5927 int abort = FALSE;
5928 list_T *cur_l;
5929 list_stack_T *list_stack = NULL;
5930 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005932 cur_l = l;
5933 for (;;)
5934 {
5935 if (!abort)
5936 /* Mark each item in the list. If the item contains a hashtab
5937 * it is added to ht_stack, if it contains a list it is added to
5938 * list_stack. */
5939 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5940 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5941 ht_stack, &list_stack);
5942 if (list_stack == NULL)
5943 break;
5944
5945 /* take an item from the stack */
5946 cur_l = list_stack->list;
5947 tempitem = list_stack;
5948 list_stack = list_stack->prev;
5949 free(tempitem);
5950 }
5951
5952 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953}
5954
5955/*
5956 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005957 * "list_stack" is used to add lists to be marked. Can be NULL.
5958 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5959 *
5960 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005961 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005963set_ref_in_item(
5964 typval_T *tv,
5965 int copyID,
5966 ht_stack_T **ht_stack,
5967 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005968{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005969 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005970
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005971 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005972 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005973 dict_T *dd = tv->vval.v_dict;
5974
Bram Moolenaara03f2332016-02-06 18:09:59 +01005975 if (dd != NULL && dd->dv_copyID != copyID)
5976 {
5977 /* Didn't see this dict yet. */
5978 dd->dv_copyID = copyID;
5979 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005980 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005981 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5982 }
5983 else
5984 {
5985 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5986 if (newitem == NULL)
5987 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005988 else
5989 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005990 newitem->ht = &dd->dv_hashtab;
5991 newitem->prev = *ht_stack;
5992 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005993 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005994 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005995 }
5996 }
5997 else if (tv->v_type == VAR_LIST)
5998 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005999 list_T *ll = tv->vval.v_list;
6000
Bram Moolenaara03f2332016-02-06 18:09:59 +01006001 if (ll != NULL && ll->lv_copyID != copyID)
6002 {
6003 /* Didn't see this list yet. */
6004 ll->lv_copyID = copyID;
6005 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006006 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006007 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006008 }
6009 else
6010 {
6011 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006012 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006013 if (newitem == NULL)
6014 abort = TRUE;
6015 else
6016 {
6017 newitem->list = ll;
6018 newitem->prev = *list_stack;
6019 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006020 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006021 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006022 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006023 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006024 else if (tv->v_type == VAR_FUNC)
6025 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006026 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006027 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006028 else if (tv->v_type == VAR_PARTIAL)
6029 {
6030 partial_T *pt = tv->vval.v_partial;
6031 int i;
6032
6033 /* A partial does not have a copyID, because it cannot contain itself.
6034 */
6035 if (pt != NULL)
6036 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006037 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006038
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006039 if (pt->pt_dict != NULL)
6040 {
6041 typval_T dtv;
6042
6043 dtv.v_type = VAR_DICT;
6044 dtv.vval.v_dict = pt->pt_dict;
6045 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6046 }
6047
6048 for (i = 0; i < pt->pt_argc; ++i)
6049 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6050 ht_stack, list_stack);
6051 }
6052 }
6053#ifdef FEAT_JOB_CHANNEL
6054 else if (tv->v_type == VAR_JOB)
6055 {
6056 job_T *job = tv->vval.v_job;
6057 typval_T dtv;
6058
6059 if (job != NULL && job->jv_copyID != copyID)
6060 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006061 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006062 if (job->jv_channel != NULL)
6063 {
6064 dtv.v_type = VAR_CHANNEL;
6065 dtv.vval.v_channel = job->jv_channel;
6066 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6067 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006068 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006069 {
6070 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006071 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006072 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6073 }
6074 }
6075 }
6076 else if (tv->v_type == VAR_CHANNEL)
6077 {
6078 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006079 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006080 typval_T dtv;
6081 jsonq_T *jq;
6082 cbq_T *cq;
6083
6084 if (ch != NULL && ch->ch_copyID != copyID)
6085 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006086 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006087 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006088 {
6089 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6090 jq = jq->jq_next)
6091 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6092 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6093 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006094 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006095 {
6096 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006097 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006098 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6099 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006100 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006101 {
6102 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006103 dtv.vval.v_partial =
6104 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006105 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6106 }
6107 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006108 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006109 {
6110 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006111 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006112 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6113 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006114 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006115 {
6116 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006117 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006118 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6119 }
6120 }
6121 }
6122#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006123 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006124}
6125
Bram Moolenaar17a13432016-01-24 14:22:10 +01006126 static char *
6127get_var_special_name(int nr)
6128{
6129 switch (nr)
6130 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006131 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006132 case VVAL_TRUE: return "v:true";
6133 case VVAL_NONE: return "v:none";
6134 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006135 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006136 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006137 return "42";
6138}
6139
Bram Moolenaar8c711452005-01-14 21:53:12 +00006140/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141 * Return a string with the string representation of a variable.
6142 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006144 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006145 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6146 * stings as "string()", otherwise does not put quotes around strings, as
6147 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006148 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6149 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006150 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006152 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006153echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006154 typval_T *tv,
6155 char_u **tofree,
6156 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006157 int copyID,
6158 int echo_style,
6159 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006160 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162 static int recurse = 0;
6163 char_u *r = NULL;
6164
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006166 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006167 if (!did_echo_string_emsg)
6168 {
6169 /* Only give this message once for a recursive call to avoid
6170 * flooding the user with errors. And stop iterating over lists
6171 * and dicts. */
6172 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006173 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006174 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006175 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006176 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006177 }
6178 ++recurse;
6179
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 switch (tv->v_type)
6181 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006182 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006183 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006184 {
6185 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006186 r = tv->vval.v_string;
6187 if (r == NULL)
6188 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006189 }
6190 else
6191 {
6192 *tofree = string_quote(tv->vval.v_string, FALSE);
6193 r = *tofree;
6194 }
6195 break;
6196
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006198 if (echo_style)
6199 {
6200 *tofree = NULL;
6201 r = tv->vval.v_string;
6202 }
6203 else
6204 {
6205 *tofree = string_quote(tv->vval.v_string, TRUE);
6206 r = *tofree;
6207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006209
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006210 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006211 {
6212 partial_T *pt = tv->vval.v_partial;
6213 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006214 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006215 garray_T ga;
6216 int i;
6217 char_u *tf;
6218
6219 ga_init2(&ga, 1, 100);
6220 ga_concat(&ga, (char_u *)"function(");
6221 if (fname != NULL)
6222 {
6223 ga_concat(&ga, fname);
6224 vim_free(fname);
6225 }
6226 if (pt != NULL && pt->pt_argc > 0)
6227 {
6228 ga_concat(&ga, (char_u *)", [");
6229 for (i = 0; i < pt->pt_argc; ++i)
6230 {
6231 if (i > 0)
6232 ga_concat(&ga, (char_u *)", ");
6233 ga_concat(&ga,
6234 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6235 vim_free(tf);
6236 }
6237 ga_concat(&ga, (char_u *)"]");
6238 }
6239 if (pt != NULL && pt->pt_dict != NULL)
6240 {
6241 typval_T dtv;
6242
6243 ga_concat(&ga, (char_u *)", ");
6244 dtv.v_type = VAR_DICT;
6245 dtv.vval.v_dict = pt->pt_dict;
6246 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6247 vim_free(tf);
6248 }
6249 ga_concat(&ga, (char_u *)")");
6250
6251 *tofree = ga.ga_data;
6252 r = *tofree;
6253 break;
6254 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006255
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006256 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006257 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006258 break;
6259
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006261 if (tv->vval.v_list == NULL)
6262 {
6263 *tofree = NULL;
6264 r = NULL;
6265 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006266 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6267 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006268 {
6269 *tofree = NULL;
6270 r = (char_u *)"[...]";
6271 }
6272 else
6273 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006274 int old_copyID = tv->vval.v_list->lv_copyID;
6275
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006276 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006277 *tofree = list2string(tv, copyID, restore_copyID);
6278 if (restore_copyID)
6279 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006280 r = *tofree;
6281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006282 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006283
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006285 if (tv->vval.v_dict == NULL)
6286 {
6287 *tofree = NULL;
6288 r = NULL;
6289 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006290 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6291 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006292 {
6293 *tofree = NULL;
6294 r = (char_u *)"{...}";
6295 }
6296 else
6297 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006298 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006299 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006300 *tofree = dict2string(tv, copyID, restore_copyID);
6301 if (restore_copyID)
6302 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006303 r = *tofree;
6304 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006306
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006307 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006308 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006309 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006310 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006311 break;
6312
Bram Moolenaar835dc632016-02-07 14:27:38 +01006313 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006314 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006315 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006316 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006317 if (composite_val)
6318 {
6319 *tofree = string_quote(r, FALSE);
6320 r = *tofree;
6321 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006323
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006324 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006325#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006326 *tofree = NULL;
6327 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6328 r = numbuf;
6329 break;
6330#endif
6331
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006332 case VAR_SPECIAL:
6333 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006334 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006335 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006336 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006337
Bram Moolenaar8502c702014-06-17 12:51:16 +02006338 if (--recurse == 0)
6339 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006340 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006341}
6342
6343/*
6344 * Return a string with the string representation of a variable.
6345 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6346 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006347 * Does not put quotes around strings, as ":echo" displays values.
6348 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6349 * May return NULL.
6350 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006351 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006352echo_string(
6353 typval_T *tv,
6354 char_u **tofree,
6355 char_u *numbuf,
6356 int copyID)
6357{
6358 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6359}
6360
6361/*
6362 * Return a string with the string representation of a variable.
6363 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6364 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006365 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006366 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006367 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006369tv2string(
6370 typval_T *tv,
6371 char_u **tofree,
6372 char_u *numbuf,
6373 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006374{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006375 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376}
6377
6378/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 * Return string "str" in ' quotes, doubling ' characters.
6380 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006382 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006383 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006384string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006385{
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006387 char_u *p, *r, *s;
6388
Bram Moolenaar33570922005-01-25 22:26:29 +00006389 len = (function ? 13 : 3);
6390 if (str != NULL)
6391 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006392 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006393 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 if (*p == '\'')
6395 ++len;
6396 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006397 s = r = alloc(len);
6398 if (r != NULL)
6399 {
6400 if (function)
6401 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006403 r += 10;
6404 }
6405 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 if (str != NULL)
6408 for (p = str; *p != NUL; )
6409 {
6410 if (*p == '\'')
6411 *r++ = '\'';
6412 MB_COPY_CHAR(p, r);
6413 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006415 if (function)
6416 *r++ = ')';
6417 *r++ = NUL;
6418 }
6419 return s;
6420}
6421
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006422#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006423/*
6424 * Convert the string "text" to a floating point number.
6425 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6426 * this always uses a decimal point.
6427 * Returns the length of the text that was consumed.
6428 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006430string2float(
6431 char_u *text,
6432 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006433{
6434 char *s = (char *)text;
6435 float_T f;
6436
Bram Moolenaar62473612017-01-08 19:25:40 +01006437 /* MS-Windows does not deal with "inf" and "nan" properly. */
6438 if (STRNICMP(text, "inf", 3) == 0)
6439 {
6440 *value = INFINITY;
6441 return 3;
6442 }
6443 if (STRNICMP(text, "-inf", 3) == 0)
6444 {
6445 *value = -INFINITY;
6446 return 4;
6447 }
6448 if (STRNICMP(text, "nan", 3) == 0)
6449 {
6450 *value = NAN;
6451 return 3;
6452 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006453 f = strtod(s, &s);
6454 *value = f;
6455 return (int)((char_u *)s - text);
6456}
6457#endif
6458
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * Get the value of an environment variable.
6461 * "arg" is pointing to the '$'. It is advanced to after the name.
6462 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006463 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 */
6465 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006466get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 char_u *string = NULL;
6469 int len;
6470 int cc;
6471 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006472 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
6474 ++*arg;
6475 name = *arg;
6476 len = get_env_len(arg);
6477 if (evaluate)
6478 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006479 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006480 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006481
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006482 cc = name[len];
6483 name[len] = NUL;
6484 /* first try vim_getenv(), fast for normal environment vars */
6485 string = vim_getenv(name, &mustfree);
6486 if (string != NULL && *string != NUL)
6487 {
6488 if (!mustfree)
6489 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006491 else
6492 {
6493 if (mustfree)
6494 vim_free(string);
6495
6496 /* next try expanding things like $VIM and ${HOME} */
6497 string = expand_env_save(name - 1);
6498 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006499 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006500 }
6501 name[len] = cc;
6502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503 rettv->v_type = VAR_STRING;
6504 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 }
6506
6507 return OK;
6508}
6509
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006510
6511
6512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006514 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517var2fpos(
6518 typval_T *varp,
6519 int dollar_lnum, /* TRUE when $ is last line */
6520 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006522 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006524 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
Bram Moolenaara5525202006-03-02 22:52:09 +00006526 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006527 if (varp->v_type == VAR_LIST)
6528 {
6529 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006530 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006531 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006532 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006533
6534 l = varp->vval.v_list;
6535 if (l == NULL)
6536 return NULL;
6537
6538 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006539 pos.lnum = list_find_nr(l, 0L, &error);
6540 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006541 return NULL; /* invalid line number */
6542
6543 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006544 pos.col = list_find_nr(l, 1L, &error);
6545 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006546 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006547 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006548
6549 /* We accept "$" for the column number: last column. */
6550 li = list_find(l, 1L);
6551 if (li != NULL && li->li_tv.v_type == VAR_STRING
6552 && li->li_tv.vval.v_string != NULL
6553 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6554 pos.col = len + 1;
6555
Bram Moolenaara5525202006-03-02 22:52:09 +00006556 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006557 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006558 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006559 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006560
Bram Moolenaara5525202006-03-02 22:52:09 +00006561 /* Get the virtual offset. Defaults to zero. */
6562 pos.coladd = list_find_nr(l, 2L, &error);
6563 if (error)
6564 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006565
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006566 return &pos;
6567 }
6568
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006569 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006570 if (name == NULL)
6571 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006572 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006574 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6575 {
6576 if (VIsual_active)
6577 return &VIsual;
6578 return &curwin->w_cursor;
6579 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006580 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006582 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6584 return NULL;
6585 return pp;
6586 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006587
Bram Moolenaara5525202006-03-02 22:52:09 +00006588 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006589
Bram Moolenaar477933c2007-07-17 14:32:23 +00006590 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006591 {
6592 pos.col = 0;
6593 if (name[1] == '0') /* "w0": first visible line */
6594 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006595 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006596 /* In silent Ex mode topline is zero, but that's not a valid line
6597 * number; use one instead. */
6598 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006599 return &pos;
6600 }
6601 else if (name[1] == '$') /* "w$": last visible line */
6602 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006603 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006604 /* In silent Ex mode botline is zero, return zero then. */
6605 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006606 return &pos;
6607 }
6608 }
6609 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006611 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 {
6613 pos.lnum = curbuf->b_ml.ml_line_count;
6614 pos.col = 0;
6615 }
6616 else
6617 {
6618 pos.lnum = curwin->w_cursor.lnum;
6619 pos.col = (colnr_T)STRLEN(ml_get_curline());
6620 }
6621 return &pos;
6622 }
6623 return NULL;
6624}
6625
6626/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006627 * Convert list in "arg" into a position and optional file number.
6628 * When "fnump" is NULL there is no file number, only 3 items.
6629 * Note that the column is passed on as-is, the caller may want to decrement
6630 * it to use 1 for the first column.
6631 * Return FAIL when conversion is not possible, doesn't check the position for
6632 * validity.
6633 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006635list2fpos(
6636 typval_T *arg,
6637 pos_T *posp,
6638 int *fnump,
6639 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006640{
6641 list_T *l = arg->vval.v_list;
6642 long i = 0;
6643 long n;
6644
Bram Moolenaar493c1782014-05-28 14:34:46 +02006645 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6646 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006647 if (arg->v_type != VAR_LIST
6648 || l == NULL
6649 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006650 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006651 return FAIL;
6652
6653 if (fnump != NULL)
6654 {
6655 n = list_find_nr(l, i++, NULL); /* fnum */
6656 if (n < 0)
6657 return FAIL;
6658 if (n == 0)
6659 n = curbuf->b_fnum; /* current buffer */
6660 *fnump = n;
6661 }
6662
6663 n = list_find_nr(l, i++, NULL); /* lnum */
6664 if (n < 0)
6665 return FAIL;
6666 posp->lnum = n;
6667
6668 n = list_find_nr(l, i++, NULL); /* col */
6669 if (n < 0)
6670 return FAIL;
6671 posp->col = n;
6672
Bram Moolenaar493c1782014-05-28 14:34:46 +02006673 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006674 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006675 posp->coladd = 0;
6676 else
6677 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006678
Bram Moolenaar493c1782014-05-28 14:34:46 +02006679 if (curswantp != NULL)
6680 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6681
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006682 return OK;
6683}
6684
6685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 * Get the length of an environment variable name.
6687 * Advance "arg" to the first character after the name.
6688 * Return 0 for error.
6689 */
6690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006691get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 char_u *p;
6694 int len;
6695
6696 for (p = *arg; vim_isIDc(*p); ++p)
6697 ;
6698 if (p == *arg) /* no name found */
6699 return 0;
6700
6701 len = (int)(p - *arg);
6702 *arg = p;
6703 return len;
6704}
6705
6706/*
6707 * Get the length of the name of a function or internal variable.
6708 * "arg" is advanced to the first non-white character after the name.
6709 * Return 0 if something is wrong.
6710 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006712get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 char_u *p;
6715 int len;
6716
6717 /* Find the end of the name. */
6718 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006719 {
6720 if (*p == ':')
6721 {
6722 /* "s:" is start of "s:var", but "n:" is not and can be used in
6723 * slice "[n:]". Also "xx:" is not a namespace. */
6724 len = (int)(p - *arg);
6725 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6726 || len > 1)
6727 break;
6728 }
6729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (p == *arg) /* no name found */
6731 return 0;
6732
6733 len = (int)(p - *arg);
6734 *arg = skipwhite(p);
6735
6736 return len;
6737}
6738
6739/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006740 * Get the length of the name of a variable or function.
6741 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006743 * Return -1 if curly braces expansion failed.
6744 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 * If the name contains 'magic' {}'s, expand them and return the
6746 * expanded name in an allocated string via 'alias' - caller must free.
6747 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006749get_name_len(
6750 char_u **arg,
6751 char_u **alias,
6752 int evaluate,
6753 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 char_u *p;
6757 char_u *expr_start;
6758 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760 *alias = NULL; /* default to no alias */
6761
6762 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6763 && (*arg)[2] == (int)KE_SNR)
6764 {
6765 /* hard coded <SNR>, already translated */
6766 *arg += 3;
6767 return get_id_len(arg) + 3;
6768 }
6769 len = eval_fname_script(*arg);
6770 if (len > 0)
6771 {
6772 /* literal "<SID>", "s:" or "<SNR>" */
6773 *arg += len;
6774 }
6775
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006777 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006779 p = find_name_end(*arg, &expr_start, &expr_end,
6780 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (expr_start != NULL)
6782 {
6783 char_u *temp_string;
6784
6785 if (!evaluate)
6786 {
6787 len += (int)(p - *arg);
6788 *arg = skipwhite(p);
6789 return len;
6790 }
6791
6792 /*
6793 * Include any <SID> etc in the expanded string:
6794 * Thus the -len here.
6795 */
6796 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6797 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006798 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 *alias = temp_string;
6800 *arg = skipwhite(p);
6801 return (int)STRLEN(temp_string);
6802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006805 // Only give an error when there is something, otherwise it will be
6806 // reported at a higher level.
6807 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006808 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 return len;
6811}
6812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813/*
6814 * Find the end of a variable or function name, taking care of magic braces.
6815 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6816 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006817 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006818 * Return a pointer to just after the name. Equal to "arg" if there is no
6819 * valid name.
6820 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006821 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822find_name_end(
6823 char_u *arg,
6824 char_u **expr_start,
6825 char_u **expr_end,
6826 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006828 int mb_nest = 0;
6829 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006831 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835 *expr_start = NULL;
6836 *expr_end = NULL;
6837 }
6838
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006839 /* Quick check for valid starting character. */
6840 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6841 return arg;
6842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006843 for (p = arg; *p != NUL
6844 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006846 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006847 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006848 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006849 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006850 if (*p == '\'')
6851 {
6852 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006853 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006854 ;
6855 if (*p == NUL)
6856 break;
6857 }
6858 else if (*p == '"')
6859 {
6860 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006861 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006862 if (*p == '\\' && p[1] != NUL)
6863 ++p;
6864 if (*p == NUL)
6865 break;
6866 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006867 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6868 {
6869 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006870 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006871 len = (int)(p - arg);
6872 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006873 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006874 break;
6875 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006877 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006879 if (*p == '[')
6880 ++br_nest;
6881 else if (*p == ']')
6882 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006885 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006887 if (*p == '{')
6888 {
6889 mb_nest++;
6890 if (expr_start != NULL && *expr_start == NULL)
6891 *expr_start = p;
6892 }
6893 else if (*p == '}')
6894 {
6895 mb_nest--;
6896 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6897 *expr_end = p;
6898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
6901
6902 return p;
6903}
6904
6905/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006906 * Expands out the 'magic' {}'s in a variable/function name.
6907 * Note that this can call itself recursively, to deal with
6908 * constructs like foo{bar}{baz}{bam}
6909 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6910 * "in_start" ^
6911 * "expr_start" ^
6912 * "expr_end" ^
6913 * "in_end" ^
6914 *
6915 * Returns a new allocated string, which the caller must free.
6916 * Returns NULL for failure.
6917 */
6918 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919make_expanded_name(
6920 char_u *in_start,
6921 char_u *expr_start,
6922 char_u *expr_end,
6923 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006924{
6925 char_u c1;
6926 char_u *retval = NULL;
6927 char_u *temp_result;
6928 char_u *nextcmd = NULL;
6929
6930 if (expr_end == NULL || in_end == NULL)
6931 return NULL;
6932 *expr_start = NUL;
6933 *expr_end = NUL;
6934 c1 = *in_end;
6935 *in_end = NUL;
6936
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006937 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006938 if (temp_result != NULL && nextcmd == NULL)
6939 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006940 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6941 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006942 if (retval != NULL)
6943 {
6944 STRCPY(retval, in_start);
6945 STRCAT(retval, temp_result);
6946 STRCAT(retval, expr_end + 1);
6947 }
6948 }
6949 vim_free(temp_result);
6950
6951 *in_end = c1; /* put char back for error messages */
6952 *expr_start = '{';
6953 *expr_end = '}';
6954
6955 if (retval != NULL)
6956 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006957 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006958 if (expr_start != NULL)
6959 {
6960 /* Further expansion! */
6961 temp_result = make_expanded_name(retval, expr_start,
6962 expr_end, temp_result);
6963 vim_free(retval);
6964 retval = temp_result;
6965 }
6966 }
6967
6968 return retval;
6969}
6970
6971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006976eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006978 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6979}
6980
6981/*
6982 * Return TRUE if character "c" can be used as the first character in a
6983 * variable or function name (excluding '{' and '}').
6984 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006987{
6988 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989}
6990
6991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 * Set number v: variable to "val".
6993 */
6994 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006995set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006997 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
7000/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02007001 * Get typval_T v: variable value.
7002 */
7003 typval_T *
7004get_vim_var_tv(int idx)
7005{
7006 return &vimvars[idx].vv_tv;
7007}
7008
7009/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007010 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007012 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007013get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016}
7017
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007018/*
7019 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007020 * If the String variable has never been set, return an empty string.
7021 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007022 */
7023 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007024get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007025{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007026 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007027}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007028
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007030 * Get List v: variable value. Caller must take care of reference count when
7031 * needed.
7032 */
7033 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007035{
7036 return vimvars[idx].vv_list;
7037}
7038
7039/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007040 * Get Dict v: variable value. Caller must take care of reference count when
7041 * needed.
7042 */
7043 dict_T *
7044get_vim_var_dict(int idx)
7045{
7046 return vimvars[idx].vv_dict;
7047}
7048
7049/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007050 * Set v:char to character "c".
7051 */
7052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007053set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007054{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007055 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007056
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007057 if (has_mbyte)
7058 buf[(*mb_char2bytes)(c, buf)] = NUL;
7059 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007060 {
7061 buf[0] = c;
7062 buf[1] = NUL;
7063 }
7064 set_vim_var_string(VV_CHAR, buf, -1);
7065}
7066
7067/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007068 * Set v:count to "count" and v:count1 to "count1".
7069 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 */
7071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072set_vcount(
7073 long count,
7074 long count1,
7075 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007077 if (set_prevcount)
7078 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007079 vimvars[VV_COUNT].vv_nr = count;
7080 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081}
7082
7083/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007084 * Save variables that might be changed as a side effect. Used when executing
7085 * a timer callback.
7086 */
7087 void
7088save_vimvars(vimvars_save_T *vvsave)
7089{
7090 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7091 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7092 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7093}
7094
7095/*
7096 * Restore variables saved by save_vimvars().
7097 */
7098 void
7099restore_vimvars(vimvars_save_T *vvsave)
7100{
7101 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7102 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7103 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7104}
7105
7106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 * Set string v: variable to a copy of "val".
7108 */
7109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110set_vim_var_string(
7111 int idx,
7112 char_u *val,
7113 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
Bram Moolenaara542c682016-01-31 16:28:04 +01007115 clear_tv(&vimvars[idx].vv_di.di_tv);
7116 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007120 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007122 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123}
7124
7125/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007126 * Set List v: variable to "val".
7127 */
7128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007129set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007130{
Bram Moolenaara542c682016-01-31 16:28:04 +01007131 clear_tv(&vimvars[idx].vv_di.di_tv);
7132 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007133 vimvars[idx].vv_list = val;
7134 if (val != NULL)
7135 ++val->lv_refcount;
7136}
7137
7138/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007139 * Set Dictionary v: variable to "val".
7140 */
7141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007142set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007143{
Bram Moolenaara542c682016-01-31 16:28:04 +01007144 clear_tv(&vimvars[idx].vv_di.di_tv);
7145 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007146 vimvars[idx].vv_dict = val;
7147 if (val != NULL)
7148 {
7149 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007150 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007151 }
7152}
7153
7154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 * Set v:register if needed.
7156 */
7157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
7160 char_u regname;
7161
7162 if (c == 0 || c == ' ')
7163 regname = '"';
7164 else
7165 regname = c;
7166 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 set_vim_var_string(VV_REG, &regname, 1);
7169}
7170
7171/*
7172 * Get or set v:exception. If "oldval" == NULL, return the current value.
7173 * Otherwise, restore the value to "oldval" and return NULL.
7174 * Must always be called in pairs to save and restore v:exception! Does not
7175 * take care of memory allocations.
7176 */
7177 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 return NULL;
7185}
7186
7187/*
7188 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7189 * Otherwise, restore the value to "oldval" and return NULL.
7190 * Must always be called in pairs to save and restore v:throwpoint! Does not
7191 * take care of memory allocations.
7192 */
7193 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 return NULL;
7201}
7202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203/*
7204 * Set v:cmdarg.
7205 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7206 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7207 * Must always be called in pairs!
7208 */
7209 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007210set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 char_u *oldval;
7213 char_u *newval;
7214 unsigned len;
7215
Bram Moolenaare9a41262005-01-15 22:18:47 +00007216 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007217 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007219 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007221 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007224 if (eap->force_bin == FORCE_BIN)
7225 len = 6;
7226 else if (eap->force_bin == FORCE_NOBIN)
7227 len = 8;
7228 else
7229 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007230
7231 if (eap->read_edit)
7232 len += 7;
7233
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007234 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007235 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007236 if (eap->force_enc != 0)
7237 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007238 if (eap->bad_char != 0)
7239 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007240
7241 newval = alloc(len + 1);
7242 if (newval == NULL)
7243 return NULL;
7244
7245 if (eap->force_bin == FORCE_BIN)
7246 sprintf((char *)newval, " ++bin");
7247 else if (eap->force_bin == FORCE_NOBIN)
7248 sprintf((char *)newval, " ++nobin");
7249 else
7250 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007251
7252 if (eap->read_edit)
7253 STRCAT(newval, " ++edit");
7254
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007255 if (eap->force_ff != 0)
7256 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007257 eap->force_ff == 'u' ? "unix"
7258 : eap->force_ff == 'd' ? "dos"
7259 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007260 if (eap->force_enc != 0)
7261 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7262 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007263 if (eap->bad_char == BAD_KEEP)
7264 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7265 else if (eap->bad_char == BAD_DROP)
7266 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7267 else if (eap->bad_char != 0)
7268 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007269 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007270 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
7273/*
7274 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007275 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007278get_var_tv(
7279 char_u *name,
7280 int len, /* length of "name" */
7281 typval_T *rettv, /* NULL when only checking existence */
7282 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7283 int verbose, /* may give error message */
7284 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
7291 /* truncate the name, so that we can use strcmp() */
7292 cc = name[len];
7293 name[len] = NUL;
7294
7295 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 * Check for user-defined variables.
7297 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007298 v = find_var(name, NULL, no_autoload);
7299 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007301 tv = &v->di_tv;
7302 if (dip != NULL)
7303 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
7305
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007308 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007309 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 ret = FAIL;
7311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
7315 name[len] = cc;
7316
7317 return ret;
7318}
7319
7320/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007321 * Check if variable "name[len]" is a local variable or an argument.
7322 * If so, "*eval_lavars_used" is set to TRUE.
7323 */
7324 static void
7325check_vars(char_u *name, int len)
7326{
7327 int cc;
7328 char_u *varname;
7329 hashtab_T *ht;
7330
7331 if (eval_lavars_used == NULL)
7332 return;
7333
7334 /* truncate the name, so that we can use strcmp() */
7335 cc = name[len];
7336 name[len] = NUL;
7337
7338 ht = find_var_ht(name, &varname);
7339 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7340 {
7341 if (find_var(name, NULL, TRUE) != NULL)
7342 *eval_lavars_used = TRUE;
7343 }
7344
7345 name[len] = cc;
7346}
7347
7348/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007349 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7350 * Also handle function call with Funcref variable: func(expr)
7351 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7352 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354handle_subscript(
7355 char_u **arg,
7356 typval_T *rettv,
7357 int evaluate, /* do more than finding the end */
7358 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007359{
7360 int ret = OK;
7361 dict_T *selfdict = NULL;
7362 char_u *s;
7363 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007364 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007365
Bram Moolenaar61343f02019-07-20 21:11:13 +02007366 // "." is ".name" lookup when we found a dict or when evaluating and
7367 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007368 while (ret == OK
7369 && (**arg == '['
Bram Moolenaar61343f02019-07-20 21:11:13 +02007370 || (**arg == '.' && (rettv->v_type == VAR_DICT
7371 || (!evaluate
7372 && (*arg)[1] != '.'
7373 && current_sctx.sc_version >= 2)))
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007374 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7375 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007376 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007377 {
7378 if (**arg == '(')
7379 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007380 partial_T *pt = NULL;
7381
Bram Moolenaard9fba312005-06-26 22:34:35 +00007382 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007383 if (evaluate)
7384 {
7385 functv = *rettv;
7386 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007387
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007388 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007389 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007390 {
7391 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007392 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007393 }
7394 else
7395 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007396 }
7397 else
7398 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007399 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007400 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007401 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007402
7403 /* Clear the funcref afterwards, so that deleting it while
7404 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007405 if (evaluate)
7406 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007407
7408 /* Stop the expression evaluation when immediately aborting on
7409 * error, or when an interrupt occurred or an exception was thrown
7410 * but not caught. */
7411 if (aborting())
7412 {
7413 if (ret == OK)
7414 clear_tv(rettv);
7415 ret = FAIL;
7416 }
7417 dict_unref(selfdict);
7418 selfdict = NULL;
7419 }
7420 else /* **arg == '[' || **arg == '.' */
7421 {
7422 dict_unref(selfdict);
7423 if (rettv->v_type == VAR_DICT)
7424 {
7425 selfdict = rettv->vval.v_dict;
7426 if (selfdict != NULL)
7427 ++selfdict->dv_refcount;
7428 }
7429 else
7430 selfdict = NULL;
7431 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7432 {
7433 clear_tv(rettv);
7434 ret = FAIL;
7435 }
7436 }
7437 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007438
Bram Moolenaar1d429612016-05-24 15:44:17 +02007439 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7440 * Don't do this when "Func" is already a partial that was bound
7441 * explicitly (pt_auto is FALSE). */
7442 if (selfdict != NULL
7443 && (rettv->v_type == VAR_FUNC
7444 || (rettv->v_type == VAR_PARTIAL
7445 && (rettv->vval.v_partial->pt_auto
7446 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007447 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007448
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007449 dict_unref(selfdict);
7450 return ret;
7451}
7452
7453/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007454 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007455 * value).
7456 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007457 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007458alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007460 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461}
7462
7463/*
7464 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 * The string "s" must have been allocated, it is consumed.
7466 * Return NULL for out of memory, the variable otherwise.
7467 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007469alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007473 rettv = alloc_tv();
7474 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 rettv->v_type = VAR_STRING;
7477 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479 else
7480 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007481 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482}
7483
7484/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007485 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 if (varp != NULL)
7491 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007492 switch (varp->v_type)
7493 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007494 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007495 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007496 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007497 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007498 vim_free(varp->vval.v_string);
7499 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007500 case VAR_PARTIAL:
7501 partial_unref(varp->vval.v_partial);
7502 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007503 case VAR_BLOB:
7504 blob_unref(varp->vval.v_blob);
7505 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007506 case VAR_LIST:
7507 list_unref(varp->vval.v_list);
7508 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007509 case VAR_DICT:
7510 dict_unref(varp->vval.v_dict);
7511 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007512 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007513#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007514 job_unref(varp->vval.v_job);
7515 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007516#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007517 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007518#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007519 channel_unref(varp->vval.v_channel);
7520 break;
7521#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007522 case VAR_NUMBER:
7523 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007524 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007525 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007526 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 vim_free(varp);
7529 }
7530}
7531
7532/*
7533 * Free the memory for a variable value and set the value to NULL or 0.
7534 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 if (varp != NULL)
7539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007540 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007542 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007543 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007544 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007545 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007546 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007547 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007548 case VAR_PARTIAL:
7549 partial_unref(varp->vval.v_partial);
7550 varp->vval.v_partial = NULL;
7551 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007552 case VAR_BLOB:
7553 blob_unref(varp->vval.v_blob);
7554 varp->vval.v_blob = NULL;
7555 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007556 case VAR_LIST:
7557 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007559 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 case VAR_DICT:
7561 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007565 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 varp->vval.v_number = 0;
7567 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007568 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007569#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570 varp->vval.v_float = 0.0;
7571 break;
7572#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007573 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007574#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007575 job_unref(varp->vval.v_job);
7576 varp->vval.v_job = NULL;
7577#endif
7578 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007579 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007580#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007581 channel_unref(varp->vval.v_channel);
7582 varp->vval.v_channel = NULL;
7583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 case VAR_UNKNOWN:
7585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007587 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 }
7589}
7590
7591/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592 * Set the value of a variable to NULL without freeing items.
7593 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007595init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596{
7597 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007599}
7600
7601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 * Get the number value of a variable.
7603 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007604 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007605 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007606 * caller of incompatible types: it sets *denote to TRUE if "denote"
7607 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007609 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007610tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007612 int error = FALSE;
7613
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007614 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007615}
7616
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007617 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007618tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007619{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007620 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007625 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007626 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007627#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007628 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007629 break;
7630#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007632 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 break;
7635 case VAR_STRING:
7636 if (varp->vval.v_string != NULL)
7637 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007638 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007639 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007640 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007641 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007642 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007643 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007644 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007645 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007646 case VAR_SPECIAL:
7647 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7648 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007649 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007650#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007651 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007652 break;
7653#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007654 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007655#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007657 break;
7658#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007659 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007660 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007661 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007662 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007663 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007664 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007666 if (denote == NULL) /* useful for values that must be unsigned */
7667 n = -1;
7668 else
7669 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671}
7672
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007673#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007675tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007676{
7677 switch (varp->v_type)
7678 {
7679 case VAR_NUMBER:
7680 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007681 case VAR_FLOAT:
7682 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007683 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007684 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007685 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007686 break;
7687 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007688 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007689 break;
7690 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007691 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007692 break;
7693 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007694 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007695 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007696 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007697 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007698 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007699 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007700# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007701 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007702 break;
7703# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007704 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007705# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007706 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007707 break;
7708# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007709 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007710 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007711 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007712 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007713 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007714 break;
7715 }
7716 return 0;
7717}
7718#endif
7719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 * Get the string value of a variable.
7722 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007723 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7724 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 * If the String variable has never been set, return an empty string.
7726 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007727 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007728 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007730 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007731tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 static char_u mybuf[NUMBUFLEN];
7734
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007735 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007738 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007739tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007741 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742
7743 return res != NULL ? res : (char_u *)"";
7744}
7745
Bram Moolenaar7d647822014-04-05 21:28:56 +02007746/*
7747 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7748 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007749 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007750tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007751{
7752 static char_u mybuf[NUMBUFLEN];
7753
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007754 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755}
7756
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007757 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007758tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007759{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007760 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007762 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007763 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007764 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007765 return buf;
7766 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007767 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007768 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 break;
7770 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007771 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772 break;
7773 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007774 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007776 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007777#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007778 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007779 break;
7780#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 case VAR_STRING:
7782 if (varp->vval.v_string != NULL)
7783 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007784 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007785 case VAR_SPECIAL:
7786 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7787 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007788 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007789 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007790 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007791 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007793 {
7794 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007795 char *status;
7796
7797 if (job == NULL)
7798 return (char_u *)"no process";
7799 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007800 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007801 : "run";
7802# ifdef UNIX
7803 vim_snprintf((char *)buf, NUMBUFLEN,
7804 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007805# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007806 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007807 "process %ld %s",
7808 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007809 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007810# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007811 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7813# endif
7814 return buf;
7815 }
7816#endif
7817 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007818 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007819#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007820 {
7821 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007822 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007823
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007824 if (channel == NULL)
7825 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7826 else
7827 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007828 "channel %d %s", channel->ch_id, status);
7829 return buf;
7830 }
7831#endif
7832 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007833 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007834 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007837 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
7840/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007841 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7842 * string() on Dict, List, etc.
7843 */
7844 char_u *
7845tv_stringify(typval_T *varp, char_u *buf)
7846{
7847 if (varp->v_type == VAR_LIST
7848 || varp->v_type == VAR_DICT
7849 || varp->v_type == VAR_FUNC
7850 || varp->v_type == VAR_PARTIAL
7851 || varp->v_type == VAR_FLOAT)
7852 {
7853 typval_T tmp;
7854
7855 f_string(varp, &tmp);
7856 tv_get_string_buf(&tmp, buf);
7857 clear_tv(varp);
7858 *varp = tmp;
7859 return tmp.vval.v_string;
7860 }
7861 return tv_get_string_buf(varp, buf);
7862}
7863
7864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 * Find variable "name" in the list of variables.
7866 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007868 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007869 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007871 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007872find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007876 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877
Bram Moolenaara7043832005-01-21 11:56:39 +00007878 ht = find_var_ht(name, &varname);
7879 if (htp != NULL)
7880 *htp = ht;
7881 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007883 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7884 if (ret != NULL)
7885 return ret;
7886
7887 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007888 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007892 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007893 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896find_var_in_ht(
7897 hashtab_T *ht,
7898 int htname,
7899 char_u *varname,
7900 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007901{
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 hashitem_T *hi;
7903
7904 if (*varname == NUL)
7905 {
7906 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007907 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007908 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007909 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 case 'g': return &globvars_var;
7911 case 'v': return &vimvars_var;
7912 case 'b': return &curbuf->b_bufvar;
7913 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007914 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007915 case 'l': return get_funccal_local_var();
7916 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 }
7918 return NULL;
7919 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007920
7921 hi = hash_find(ht, varname);
7922 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007923 {
7924 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007925 * worked find the variable again. Don't auto-load a script if it was
7926 * loaded already, otherwise it would be loaded every time when
7927 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007928 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007929 {
7930 /* Note: script_autoload() may make "hi" invalid. It must either
7931 * be obtained again or not used. */
7932 if (!script_autoload(varname, FALSE) || aborting())
7933 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007934 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007935 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007936 if (HASHITEM_EMPTY(hi))
7937 return NULL;
7938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007940}
7941
7942/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007944 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007945 * Set "varname" to the start of name without ':'.
7946 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007947 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007948find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007950 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007951 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007952
Bram Moolenaar73627d02015-08-11 15:46:09 +02007953 if (name[0] == NUL)
7954 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (name[1] != ':')
7956 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007957 /* The name must not start with a colon or #. */
7958 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 return NULL;
7960 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007961
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007962 // "version" is "v:version" in all scopes if scriptversion < 3.
7963 // Same for a few other variables marked with VV_COMPAT.
7964 if (current_sctx.sc_version < 3)
7965 {
7966 hi = hash_find(&compat_hashtab, name);
7967 if (!HASHITEM_EMPTY(hi))
7968 return &compat_hashtab;
7969 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007970
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007971 ht = get_funccal_local_ht();
7972 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007974 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 }
7976 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007977 if (*name == 'g') /* global variable */
7978 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02007979 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007980 if (vim_strchr(name + 2, ':') != NULL
7981 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007982 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007984 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007986 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007987 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007988 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 if (*name == 'v') /* v: variable */
7990 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007991 if (*name == 'a') /* a: function argument */
7992 return get_funccal_args_ht();
7993 if (*name == 'l') /* l: local function variable */
7994 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007996 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7997 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 return NULL;
7999}
8000
8001/*
8002 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008003 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 * Returns NULL when it doesn't exist.
8005 */
8006 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008007get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
Bram Moolenaar33570922005-01-25 22:26:29 +00008009 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008011 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if (v == NULL)
8013 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008014 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015}
8016
8017/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 * sourcing this script and when executing functions defined in the script.
8020 */
8021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008022new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
Bram Moolenaara7043832005-01-21 11:56:39 +00008024 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008025 hashtab_T *ht;
8026 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008027
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8029 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008030 /* Re-allocating ga_data means that an ht_array pointing to
8031 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008032 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008033 for (i = 1; i <= ga_scripts.ga_len; ++i)
8034 {
8035 ht = &SCRIPT_VARS(i);
8036 if (ht->ht_mask == HT_INIT_SIZE - 1)
8037 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008038 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008040 }
8041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 while (ga_scripts.ga_len < id)
8043 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008044 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008045 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008046 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 }
8049 }
8050}
8051
8052/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8054 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 */
8056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008057init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008060 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008061 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008062 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008063 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 dict_var->di_tv.vval.v_dict = dict;
8065 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008066 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8068 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069}
8070
8071/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008072 * Unreference a dictionary initialized by init_var_dict().
8073 */
8074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008075unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008076{
8077 /* Now the dict needs to be freed if no one else is using it, go back to
8078 * normal reference counting. */
8079 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8080 dict_unref(dict);
8081}
8082
8083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 * Frees all allocated variables and the value they contain.
8086 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 */
8088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008089vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008090{
8091 vars_clear_ext(ht, TRUE);
8092}
8093
8094/*
8095 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8096 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008098vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
Bram Moolenaara7043832005-01-21 11:56:39 +00008100 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 hashitem_T *hi;
8102 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
Bram Moolenaar33570922005-01-25 22:26:29 +00008104 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008105 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008106 for (hi = ht->ht_array; todo > 0; ++hi)
8107 {
8108 if (!HASHITEM_EMPTY(hi))
8109 {
8110 --todo;
8111
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008113 * ht_array might change then. hash_clear() takes care of it
8114 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008115 v = HI2DI(hi);
8116 if (free_val)
8117 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008118 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008120 }
8121 }
8122 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008123 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124}
8125
Bram Moolenaara7043832005-01-21 11:56:39 +00008126/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 * Delete a variable from hashtab "ht" at item "hi".
8128 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008131delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008134
8135 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 clear_tv(&di->di_tv);
8137 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138}
8139
8140/*
8141 * List the value of one internal variable.
8142 */
8143 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008144list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 char_u *tofree;
8147 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008148 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008150 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008151 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008152 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008153 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154}
8155
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008157list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008158 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008159 char_u *name,
8160 int type,
8161 char_u *string,
8162 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
Bram Moolenaar31859182007-08-14 20:41:13 +00008164 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8165 msg_start();
8166 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008168 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 msg_putchar(' ');
8170 msg_advance(22);
8171 if (type == VAR_NUMBER)
8172 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008173 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008174 msg_putchar('*');
8175 else if (type == VAR_LIST)
8176 {
8177 msg_putchar('[');
8178 if (*string == '[')
8179 ++string;
8180 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008181 else if (type == VAR_DICT)
8182 {
8183 msg_putchar('{');
8184 if (*string == '{')
8185 ++string;
8186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 else
8188 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008191
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008192 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008193 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008194 if (*first)
8195 {
8196 msg_clr_eos();
8197 *first = FALSE;
8198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199}
8200
8201/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008202 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 * If the variable already exists, the value is updated.
8204 * Otherwise the variable is created.
8205 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008207set_var(
8208 char_u *name,
8209 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008210 int copy) // make copy of value in "tv"
8211{
8212 set_var_const(name, tv, copy, FALSE);
8213}
8214
8215/*
8216 * Set variable "name" to value in "tv".
8217 * If the variable already exists and "is_const" is FALSE the value is updated.
8218 * Otherwise the variable is created.
8219 */
8220 static void
8221set_var_const(
8222 char_u *name,
8223 typval_T *tv,
8224 int copy, // make copy of value in "tv"
8225 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226{
Bram Moolenaar33570922005-01-25 22:26:29 +00008227 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008231 ht = find_var_ht(name, &varname);
8232 if (ht == NULL || *varname == NUL)
8233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008234 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008235 return;
8236 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008237 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008238
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008239 /* Search in parent scope which is possible to reference from lambda */
8240 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008241 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008242
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008243 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8244 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008245 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008246
Bram Moolenaar33570922005-01-25 22:26:29 +00008247 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008249 if (is_const)
8250 {
8251 emsg(_(e_cannot_mod));
8252 return;
8253 }
8254
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008256 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008257 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008258 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008259
8260 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008261 * Handle setting internal v: variables separately where needed to
8262 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 */
8264 if (ht == &vimvarht)
8265 {
8266 if (v->di_tv.v_type == VAR_STRING)
8267 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008268 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008270 {
8271 char_u *val = tv_get_string(tv);
8272
8273 // Careful: when assigning to v:errmsg and tv_get_string()
8274 // causes an error message the variable will alrady be set.
8275 if (v->di_tv.vval.v_string == NULL)
8276 v->di_tv.vval.v_string = vim_strsave(val);
8277 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008278 else
8279 {
8280 /* Take over the string to avoid an extra alloc/free. */
8281 v->di_tv.vval.v_string = tv->vval.v_string;
8282 tv->vval.v_string = NULL;
8283 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008284 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008286 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008288 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008289 if (STRCMP(varname, "searchforward") == 0)
8290 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008291#ifdef FEAT_SEARCH_EXTRA
8292 else if (STRCMP(varname, "hlsearch") == 0)
8293 {
8294 no_hlsearch = !v->di_tv.vval.v_number;
8295 redraw_all_later(SOME_VALID);
8296 }
8297#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008298 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008299 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008300 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008303 return;
8304 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 }
8306
8307 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 }
8309 else /* add a new variable */
8310 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008311 // Can't add "v:" or "a:" variable.
8312 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008314 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008315 return;
8316 }
8317
Bram Moolenaar31b81602019-02-10 22:14:27 +01008318 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008319 if (!valid_varname(varname))
8320 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008321
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008322 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008323 if (v == NULL)
8324 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008326 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008328 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 return;
8330 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008331 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008332 if (is_const)
8333 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008335
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008336 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008338 else
8339 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008341 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008342 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008343 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008344
8345 if (is_const)
8346 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347}
8348
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008349/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008350 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 * Also give an error message.
8352 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008354var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008355{
8356 if (flags & DI_FLAGS_RO)
8357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008358 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 return TRUE;
8360 }
8361 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008363 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 return TRUE;
8365 }
8366 return FALSE;
8367}
8368
8369/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008370 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8371 * Also give an error message.
8372 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008374var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008375{
8376 if (flags & DI_FLAGS_FIX)
8377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008378 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008379 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008380 return TRUE;
8381 }
8382 return FALSE;
8383}
8384
8385/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008386 * Check if a funcref is assigned to a valid variable name.
8387 * Return TRUE and give an error if not.
8388 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008390var_check_func_name(
8391 char_u *name, /* points to start of variable name */
8392 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008393{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008394 /* Allow for w: b: s: and t:. */
8395 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008396 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8397 ? name[2] : name[0]))
8398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008399 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008400 name);
8401 return TRUE;
8402 }
8403 /* Don't allow hiding a function. When "v" is not NULL we might be
8404 * assigning another function to the same var, the type is checked
8405 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008406 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008407 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008408 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008409 name);
8410 return TRUE;
8411 }
8412 return FALSE;
8413}
8414
8415/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008416 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008417 * Also give an error message, using "name" or _("name") when use_gettext is
8418 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008419 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008420 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008421var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008422{
8423 if (lock & VAR_LOCKED)
8424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008425 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008426 name == NULL ? (char_u *)_("Unknown")
8427 : use_gettext ? (char_u *)_(name)
8428 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008429 return TRUE;
8430 }
8431 if (lock & VAR_FIXED)
8432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008433 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008434 name == NULL ? (char_u *)_("Unknown")
8435 : use_gettext ? (char_u *)_(name)
8436 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008437 return TRUE;
8438 }
8439 return FALSE;
8440}
8441
8442/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008443 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8444 * Also give an error message, using "name" or _("name") when use_gettext is
8445 * TRUE.
8446 */
8447 static int
8448tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8449{
8450 int lock = 0;
8451
8452 switch (tv->v_type)
8453 {
8454 case VAR_BLOB:
8455 if (tv->vval.v_blob != NULL)
8456 lock = tv->vval.v_blob->bv_lock;
8457 break;
8458 case VAR_LIST:
8459 if (tv->vval.v_list != NULL)
8460 lock = tv->vval.v_list->lv_lock;
8461 break;
8462 case VAR_DICT:
8463 if (tv->vval.v_dict != NULL)
8464 lock = tv->vval.v_dict->dv_lock;
8465 break;
8466 default:
8467 break;
8468 }
8469 return var_check_lock(tv->v_lock, name, use_gettext)
8470 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8471}
8472
8473/*
8474 * Check if a variable name is valid.
8475 * Return FALSE and give an error if not.
8476 */
8477 int
8478valid_varname(char_u *varname)
8479{
8480 char_u *p;
8481
8482 for (p = varname; *p != NUL; ++p)
8483 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8484 && *p != AUTOLOAD_CHAR)
8485 {
8486 semsg(_(e_illvar), varname);
8487 return FALSE;
8488 }
8489 return TRUE;
8490}
8491
8492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008494 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008495 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008496 * It is OK for "from" and "to" to point to the same item. This is used to
8497 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008498 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008500copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008502 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008503 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 switch (from->v_type)
8505 {
8506 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008507 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 to->vval.v_number = from->vval.v_number;
8509 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008510 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008511#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008512 to->vval.v_float = from->vval.v_float;
8513 break;
8514#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008515 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008517 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008518 if (to->vval.v_job != NULL)
8519 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008520 break;
8521#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008522 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008524 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008525 if (to->vval.v_channel != NULL)
8526 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008527 break;
8528#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 case VAR_STRING:
8530 case VAR_FUNC:
8531 if (from->vval.v_string == NULL)
8532 to->vval.v_string = NULL;
8533 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008534 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008536 if (from->v_type == VAR_FUNC)
8537 func_ref(to->vval.v_string);
8538 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008540 case VAR_PARTIAL:
8541 if (from->vval.v_partial == NULL)
8542 to->vval.v_partial = NULL;
8543 else
8544 {
8545 to->vval.v_partial = from->vval.v_partial;
8546 ++to->vval.v_partial->pt_refcount;
8547 }
8548 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008549 case VAR_BLOB:
8550 if (from->vval.v_blob == NULL)
8551 to->vval.v_blob = NULL;
8552 else
8553 {
8554 to->vval.v_blob = from->vval.v_blob;
8555 ++to->vval.v_blob->bv_refcount;
8556 }
8557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008558 case VAR_LIST:
8559 if (from->vval.v_list == NULL)
8560 to->vval.v_list = NULL;
8561 else
8562 {
8563 to->vval.v_list = from->vval.v_list;
8564 ++to->vval.v_list->lv_refcount;
8565 }
8566 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 case VAR_DICT:
8568 if (from->vval.v_dict == NULL)
8569 to->vval.v_dict = NULL;
8570 else
8571 {
8572 to->vval.v_dict = from->vval.v_dict;
8573 ++to->vval.v_dict->dv_refcount;
8574 }
8575 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008576 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008577 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 break;
8579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580}
8581
8582/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 * Make a copy of an item.
8584 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008585 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8586 * reference to an already copied list/dict can be used.
8587 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008588 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008589 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008590item_copy(
8591 typval_T *from,
8592 typval_T *to,
8593 int deep,
8594 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595{
8596 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008597 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008598
Bram Moolenaar33570922005-01-25 22:26:29 +00008599 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008601 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008602 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 }
8604 ++recurse;
8605
8606 switch (from->v_type)
8607 {
8608 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008609 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008610 case VAR_STRING:
8611 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008612 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008613 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008614 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008615 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008616 copy_tv(from, to);
8617 break;
8618 case VAR_LIST:
8619 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008620 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008621 if (from->vval.v_list == NULL)
8622 to->vval.v_list = NULL;
8623 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8624 {
8625 /* use the copy made earlier */
8626 to->vval.v_list = from->vval.v_list->lv_copylist;
8627 ++to->vval.v_list->lv_refcount;
8628 }
8629 else
8630 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8631 if (to->vval.v_list == NULL)
8632 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008634 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008635 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008636 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008637 case VAR_DICT:
8638 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008639 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008640 if (from->vval.v_dict == NULL)
8641 to->vval.v_dict = NULL;
8642 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8643 {
8644 /* use the copy made earlier */
8645 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8646 ++to->vval.v_dict->dv_refcount;
8647 }
8648 else
8649 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8650 if (to->vval.v_dict == NULL)
8651 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008652 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008653 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008654 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008655 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008656 }
8657 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008658 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008659}
8660
8661/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 * This function is used by f_input() and f_inputdialog() functions. The third
8663 * argument to f_input() specifies the type of completion to use at the
8664 * prompt. The third argument to f_inputdialog() specifies the value to return
8665 * when the user cancels the prompt.
8666 */
8667 void
8668get_user_input(
8669 typval_T *argvars,
8670 typval_T *rettv,
8671 int inputdialog,
8672 int secret)
8673{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008674 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 char_u *p = NULL;
8676 int c;
8677 char_u buf[NUMBUFLEN];
8678 int cmd_silent_save = cmd_silent;
8679 char_u *defstr = (char_u *)"";
8680 int xp_type = EXPAND_NOTHING;
8681 char_u *xp_arg = NULL;
8682
8683 rettv->v_type = VAR_STRING;
8684 rettv->vval.v_string = NULL;
8685
8686#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008687 /* While starting up, there is no place to enter text. When running tests
8688 * with --not-a-term we assume feedkeys() will be used. */
8689 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 return;
8691#endif
8692
8693 cmd_silent = FALSE; /* Want to see the prompt. */
8694 if (prompt != NULL)
8695 {
8696 /* Only the part of the message after the last NL is considered as
8697 * prompt for the command line */
8698 p = vim_strrchr(prompt, '\n');
8699 if (p == NULL)
8700 p = prompt;
8701 else
8702 {
8703 ++p;
8704 c = *p;
8705 *p = NUL;
8706 msg_start();
8707 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008708 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 msg_didout = FALSE;
8710 msg_starthere();
8711 *p = c;
8712 }
8713 cmdline_row = msg_row;
8714
8715 if (argvars[1].v_type != VAR_UNKNOWN)
8716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008717 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 if (defstr != NULL)
8719 stuffReadbuffSpec(defstr);
8720
8721 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8722 {
8723 char_u *xp_name;
8724 int xp_namelen;
8725 long argt;
8726
8727 /* input() with a third argument: completion */
8728 rettv->vval.v_string = NULL;
8729
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008730 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 if (xp_name == NULL)
8732 return;
8733
8734 xp_namelen = (int)STRLEN(xp_name);
8735
8736 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8737 &xp_arg) == FAIL)
8738 return;
8739 }
8740 }
8741
8742 if (defstr != NULL)
8743 {
8744 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008746 ex_normal_busy = 0;
8747 rettv->vval.v_string =
8748 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8749 xp_type, xp_arg);
8750 ex_normal_busy = save_ex_normal_busy;
8751 }
8752 if (inputdialog && rettv->vval.v_string == NULL
8753 && argvars[1].v_type != VAR_UNKNOWN
8754 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008755 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 &argvars[2], buf));
8757
8758 vim_free(xp_arg);
8759
8760 /* since the user typed this, no need to wait for return */
8761 need_wait_return = FALSE;
8762 msg_didout = FALSE;
8763 }
8764 cmd_silent = cmd_silent_save;
8765}
8766
8767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 * ":echo expr1 ..." print each argument separated with a space, add a
8769 * newline at the end.
8770 * ":echon expr1 ..." print each argument plain.
8771 */
8772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008773ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008777 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 char_u *p;
8779 int needclr = TRUE;
8780 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008781 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008782 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008783 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
8785 if (eap->skip)
8786 ++emsg_skip;
8787 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008789 /* If eval1() causes an error message the text from the command may
8790 * still need to be cleared. E.g., "echo 22,44". */
8791 need_clr_eos = needclr;
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 {
8796 /*
8797 * Report the invalid expression unless the expression evaluation
8798 * has been cancelled due to an aborting error, an interrupt, or an
8799 * exception.
8800 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008801 if (!aborting() && did_emsg == did_emsg_before
8802 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008803 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008804 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 break;
8806 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008807 need_clr_eos = FALSE;
8808
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (!eap->skip)
8810 {
8811 if (atstart)
8812 {
8813 atstart = FALSE;
8814 /* Call msg_start() after eval1(), evaluating the expression
8815 * may cause a message to appear. */
8816 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008817 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008818 /* Mark the saved text as finishing the line, so that what
8819 * follows is displayed on a new line when scrolling back
8820 * at the more prompt. */
8821 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 }
8825 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008826 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008827 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008828 if (p != NULL)
8829 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008831 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008833 if (*p != TAB && needclr)
8834 {
8835 /* remove any text still there from the command */
8836 msg_clr_eos();
8837 needclr = FALSE;
8838 }
8839 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 }
8841 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008842 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843 if (has_mbyte)
8844 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008845 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008846
8847 (void)msg_outtrans_len_attr(p, i, echo_attr);
8848 p += i - 1;
8849 }
8850 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008851 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 arg = skipwhite(arg);
8858 }
8859 eap->nextcmd = check_nextcmd(arg);
8860
8861 if (eap->skip)
8862 --emsg_skip;
8863 else
8864 {
8865 /* remove text that may still be there from the command */
8866 if (needclr)
8867 msg_clr_eos();
8868 if (eap->cmdidx == CMD_echo)
8869 msg_end();
8870 }
8871}
8872
8873/*
8874 * ":echohl {name}".
8875 */
8876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008879 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880}
8881
8882/*
8883 * ":execute expr1 ..." execute the result of an expression.
8884 * ":echomsg expr1 ..." Print a message
8885 * ":echoerr expr1 ..." Print an error
8886 * Each gets spaces around each argument and a newline at the end for
8887 * echo commands
8888 */
8889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008890ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891{
8892 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 int ret = OK;
8895 char_u *p;
8896 garray_T ga;
8897 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008898 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
8900 ga_init2(&ga, 1, 80);
8901
8902 if (eap->skip)
8903 ++emsg_skip;
8904 while (*arg != NUL && *arg != '|' && *arg != '\n')
8905 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008906 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8907 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
8910 if (!eap->skip)
8911 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008912 char_u buf[NUMBUFLEN];
8913
8914 if (eap->cmdidx == CMD_execute)
8915 p = tv_get_string_buf(&rettv, buf);
8916 else
8917 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 len = (int)STRLEN(p);
8919 if (ga_grow(&ga, len + 2) == FAIL)
8920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 ret = FAIL;
8923 break;
8924 }
8925 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 ga.ga_len += len;
8929 }
8930
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 arg = skipwhite(arg);
8933 }
8934
8935 if (ret != FAIL && ga.ga_data != NULL)
8936 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008937 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8938 {
8939 /* Mark the already saved text as finishing the line, so that what
8940 * follows is displayed on a new line when scrolling back at the
8941 * more prompt. */
8942 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008943 }
8944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008946 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008947 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008948 out_flush();
8949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 else if (eap->cmdidx == CMD_echoerr)
8951 {
8952 /* We don't want to abort following commands, restore did_emsg. */
8953 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008954 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 if (!force_abort)
8956 did_emsg = save_did_emsg;
8957 }
8958 else if (eap->cmdidx == CMD_execute)
8959 do_cmdline((char_u *)ga.ga_data,
8960 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8961 }
8962
8963 ga_clear(&ga);
8964
8965 if (eap->skip)
8966 --emsg_skip;
8967
8968 eap->nextcmd = check_nextcmd(arg);
8969}
8970
8971/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 * Find window specified by "vp" in tabpage "tp".
8973 */
8974 win_T *
8975find_win_by_nr(
8976 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008977 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008978{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008980 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 if (nr < 0)
8983 return NULL;
8984 if (nr == 0)
8985 return curwin;
8986
Bram Moolenaar29323592016-07-24 22:04:11 +02008987 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8988 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008989 if (nr >= LOWEST_WIN_ID)
8990 {
8991 if (wp->w_id == nr)
8992 return wp;
8993 }
8994 else if (--nr <= 0)
8995 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008996 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008998 {
8999#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009000 // check tab-local popup windows
9001 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009002 if (wp->w_id == nr)
9003 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009004 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009005 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9006 if (wp->w_id == nr)
9007 return wp;
9008#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009010 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012}
9013
9014/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009015 * Find a window: When using a Window ID in any tab page, when using a number
9016 * in the current tab page.
9017 */
9018 win_T *
9019find_win_by_nr_or_id(typval_T *vp)
9020{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009021 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009022
9023 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009024 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009025 return find_win_by_nr(vp, NULL);
9026}
9027
9028/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009030 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 */
9032 win_T *
9033find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009034 typval_T *wvp, // VAR_UNKNOWN for current window
9035 typval_T *tvp, // VAR_UNKNOWN for current tab page
9036 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037{
9038 win_T *wp = NULL;
9039 tabpage_T *tp = NULL;
9040 long n;
9041
9042 if (wvp->v_type != VAR_UNKNOWN)
9043 {
9044 if (tvp->v_type != VAR_UNKNOWN)
9045 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009046 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009047 if (n >= 0)
9048 tp = find_tabpage(n);
9049 }
9050 else
9051 tp = curtab;
9052
9053 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009054 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009056 if (wp == NULL && wvp->v_type == VAR_NUMBER
9057 && wvp->vval.v_number != -1)
9058 // A window with the specified number is not found
9059 tp = NULL;
9060 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009061 }
9062 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009063 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009065 tp = curtab;
9066 }
9067
9068 if (ptp != NULL)
9069 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070
9071 return wp;
9072}
9073
9074/*
9075 * getwinvar() and gettabwinvar()
9076 */
9077 void
9078getwinvar(
9079 typval_T *argvars,
9080 typval_T *rettv,
9081 int off) /* 1 for gettabwinvar() */
9082{
9083 win_T *win;
9084 char_u *varname;
9085 dictitem_T *v;
9086 tabpage_T *tp = NULL;
9087 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 win_T *oldcurwin;
9089 tabpage_T *oldtabpage;
9090 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009093 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009094 else
9095 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009097 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 ++emsg_off;
9099
9100 rettv->v_type = VAR_STRING;
9101 rettv->vval.v_string = NULL;
9102
9103 if (win != NULL && varname != NULL)
9104 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 /* Set curwin to be our win, temporarily. Also set the tabpage,
9106 * otherwise the window is not valid. Only do this when needed,
9107 * autocommands get blocked. */
9108 need_switch_win = !(tp == curtab && win == curwin);
9109 if (!need_switch_win
9110 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009112 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009114 if (varname[1] == NUL)
9115 {
9116 /* get all window-local options in a dict */
9117 dict_T *opts = get_winbuf_options(FALSE);
9118
9119 if (opts != NULL)
9120 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009121 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009122 done = TRUE;
9123 }
9124 }
9125 else if (get_option_tv(&varname, rettv, 1) == OK)
9126 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009127 done = TRUE;
9128 }
9129 else
9130 {
9131 /* Look up the variable. */
9132 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9133 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9134 varname, FALSE);
9135 if (v != NULL)
9136 {
9137 copy_tv(&v->di_tv, rettv);
9138 done = TRUE;
9139 }
9140 }
9141 }
9142
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 if (need_switch_win)
9144 /* restore previous notion of curwin */
9145 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 }
9147
9148 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9149 /* use the default return value */
9150 copy_tv(&argvars[off + 2], rettv);
9151
9152 --emsg_off;
9153}
9154
9155/*
9156 * "setwinvar()" and "settabwinvar()" functions
9157 */
9158 void
9159setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9160{
9161 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 win_T *save_curwin;
9163 tabpage_T *save_curtab;
9164 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 char_u *varname, *winvarname;
9166 typval_T *varp;
9167 char_u nbuf[NUMBUFLEN];
9168 tabpage_T *tp = NULL;
9169
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009170 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 return;
9172
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009173 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009174 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009175 else
9176 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009178 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009179 varp = &argvars[off + 2];
9180
9181 if (win != NULL && varname != NULL && varp != NULL)
9182 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183 need_switch_win = !(tp == curtab && win == curwin);
9184 if (!need_switch_win
9185 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009186 {
9187 if (*varname == '&')
9188 {
9189 long numval;
9190 char_u *strval;
9191 int error = FALSE;
9192
9193 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009194 numval = (long)tv_get_number_chk(varp, &error);
9195 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 if (!error && strval != NULL)
9197 set_option_value(varname, numval, strval, OPT_LOCAL);
9198 }
9199 else
9200 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009201 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 if (winvarname != NULL)
9203 {
9204 STRCPY(winvarname, "w:");
9205 STRCPY(winvarname + 2, varname);
9206 set_var(winvarname, varp, TRUE);
9207 vim_free(winvarname);
9208 }
9209 }
9210 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009211 if (need_switch_win)
9212 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213 }
9214}
9215
9216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9218 * "arg" points to the "&" or '+' when called, to "option" when returning.
9219 * Returns NULL when no option name found. Otherwise pointer to the char
9220 * after the option name.
9221 */
9222 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009223find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 char_u *p = *arg;
9226
9227 ++p;
9228 if (*p == 'g' && p[1] == ':')
9229 {
9230 *opt_flags = OPT_GLOBAL;
9231 p += 2;
9232 }
9233 else if (*p == 'l' && p[1] == ':')
9234 {
9235 *opt_flags = OPT_LOCAL;
9236 p += 2;
9237 }
9238 else
9239 *opt_flags = 0;
9240
9241 if (!ASCII_ISALPHA(*p))
9242 return NULL;
9243 *arg = p;
9244
9245 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9246 p += 4; /* termcap option */
9247 else
9248 while (ASCII_ISALPHA(*p))
9249 ++p;
9250 return p;
9251}
9252
9253/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009254 * Return the autoload script name for a function or variable name.
9255 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009256 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009258 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009259autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009260{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009261 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009262 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009263
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009264 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009265 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009266 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009267 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009268 STRCPY(scriptname, "autoload/");
9269 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009270 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9271 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009272 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009273 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009274 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009275}
9276
9277/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009278 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009279 * Return TRUE if a package was loaded.
9280 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009282script_autoload(
9283 char_u *name,
9284 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009285{
9286 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009287 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009288 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009289 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009290
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009291 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009292 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009293 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009294 return FALSE;
9295
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009296 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009297
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009298 /* Find the name in the list of previously loaded package names. Skip
9299 * "autoload/", it's always the same. */
9300 for (i = 0; i < ga_loaded.ga_len; ++i)
9301 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9302 break;
9303 if (!reload && i < ga_loaded.ga_len)
9304 ret = FALSE; /* was loaded already */
9305 else
9306 {
9307 /* Remember the name if it wasn't loaded already. */
9308 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9309 {
9310 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9311 tofree = NULL;
9312 }
9313
9314 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009315 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009316 ret = TRUE;
9317 }
9318
9319 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009320 return ret;
9321}
9322
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9324typedef enum
9325{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009326 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9327 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9328 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329} var_flavour_T;
9330
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009332var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 char_u *p = varname;
9335
9336 if (ASCII_ISUPPER(*p))
9337 {
9338 while (*(++p))
9339 if (ASCII_ISLOWER(*p))
9340 return VAR_FLAVOUR_SESSION;
9341 return VAR_FLAVOUR_VIMINFO;
9342 }
9343 else
9344 return VAR_FLAVOUR_DEFAULT;
9345}
9346#endif
9347
9348#if defined(FEAT_VIMINFO) || defined(PROTO)
9349/*
9350 * Restore global vars that start with a capital from the viminfo file
9351 */
9352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009353read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354{
9355 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009356 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009357 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009358 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
9360 if (!writing && (find_viminfo_parameter('!') != NULL))
9361 {
9362 tab = vim_strchr(virp->vir_line + 1, '\t');
9363 if (tab != NULL)
9364 {
9365 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009366 switch (*tab)
9367 {
9368 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009369#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009370 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009371#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009372 case 'D': type = VAR_DICT; break;
9373 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009374 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009375 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
9378 tab = vim_strchr(tab, '\t');
9379 if (tab != NULL)
9380 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009381 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009382 if (type == VAR_STRING || type == VAR_DICT
9383 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009384 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009386#ifdef FEAT_FLOAT
9387 else if (type == VAR_FLOAT)
9388 (void)string2float(tab + 1, &tv.vval.v_float);
9389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009391 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009392 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009393 {
9394 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9395
9396 if (etv == NULL)
9397 /* Failed to parse back the dict or list, use it as a
9398 * string. */
9399 tv.v_type = VAR_STRING;
9400 else
9401 {
9402 vim_free(tv.vval.v_string);
9403 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009404 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009405 }
9406 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009407 else if (type == VAR_BLOB)
9408 {
9409 blob_T *blob = string2blob(tv.vval.v_string);
9410
9411 if (blob == NULL)
9412 // Failed to parse back the blob, use it as a string.
9413 tv.v_type = VAR_STRING;
9414 else
9415 {
9416 vim_free(tv.vval.v_string);
9417 tv.v_type = VAR_BLOB;
9418 tv.vval.v_blob = blob;
9419 }
9420 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009421
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009422 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009423 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009424 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009425 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009426
9427 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009428 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009429 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9430 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009431 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 }
9433 }
9434 }
9435
9436 return viminfo_readline(virp);
9437}
9438
9439/*
9440 * Write global vars that start with a capital to the viminfo file
9441 */
9442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009443write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444{
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 hashitem_T *hi;
9446 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009447 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009448 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009449 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009451 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452
9453 if (find_viminfo_parameter('!') == NULL)
9454 return;
9455
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009456 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009457
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009458 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009461 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009463 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009464 this_var = HI2DI(hi);
9465 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009468 {
9469 case VAR_STRING: s = "STR"; break;
9470 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009471 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009472 case VAR_DICT: s = "DIC"; break;
9473 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009474 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009475 case VAR_SPECIAL: s = "XPL"; break;
9476
9477 case VAR_UNKNOWN:
9478 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009479 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009480 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009481 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009482 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009483 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009485 if (this_var->di_tv.v_type == VAR_SPECIAL)
9486 {
9487 sprintf((char *)numbuf, "%ld",
9488 (long)this_var->di_tv.vval.v_number);
9489 p = numbuf;
9490 tofree = NULL;
9491 }
9492 else
9493 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009494 if (p != NULL)
9495 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009496 vim_free(tofree);
9497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 }
9499 }
9500}
9501#endif
9502
9503#if defined(FEAT_SESSION) || defined(PROTO)
9504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
Bram Moolenaar33570922005-01-25 22:26:29 +00009507 hashitem_T *hi;
9508 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009509 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 char_u *p, *t;
9511
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009512 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009515 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009517 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009518 this_var = HI2DI(hi);
9519 if ((this_var->di_tv.v_type == VAR_NUMBER
9520 || this_var->di_tv.v_type == VAR_STRING)
9521 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009522 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009523 /* Escape special characters with a backslash. Turn a LF and
9524 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009525 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009526 (char_u *)"\\\"\n\r");
9527 if (p == NULL) /* out of memory */
9528 break;
9529 for (t = p; *t != NUL; ++t)
9530 if (*t == '\n')
9531 *t = 'n';
9532 else if (*t == '\r')
9533 *t = 'r';
9534 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009535 this_var->di_key,
9536 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9537 : ' ',
9538 p,
9539 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9540 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009541 || put_eol(fd) == FAIL)
9542 {
9543 vim_free(p);
9544 return FAIL;
9545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 vim_free(p);
9547 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009548#ifdef FEAT_FLOAT
9549 else if (this_var->di_tv.v_type == VAR_FLOAT
9550 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9551 {
9552 float_T f = this_var->di_tv.vval.v_float;
9553 int sign = ' ';
9554
9555 if (f < 0)
9556 {
9557 f = -f;
9558 sign = '-';
9559 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009560 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009561 this_var->di_key, sign, f) < 0)
9562 || put_eol(fd) == FAIL)
9563 return FAIL;
9564 }
9565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 }
9567 }
9568 return OK;
9569}
9570#endif
9571
Bram Moolenaar661b1822005-07-28 22:36:45 +00009572/*
9573 * Display script name where an item was last set.
9574 * Should only be invoked when 'verbose' is non-zero.
9575 */
9576 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009577last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009578{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009579 char_u *p;
9580
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009581 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009582 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009583 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009584 if (p != NULL)
9585 {
9586 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009587 msg_puts(_("\n\tLast set from "));
9588 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009589 if (script_ctx.sc_lnum > 0)
9590 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009591 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009592 msg_outnum((long)script_ctx.sc_lnum);
9593 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009594 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009595 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009596 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009597 }
9598}
9599
Bram Moolenaar61be3762019-03-19 23:04:17 +01009600/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009601 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9602 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009603 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009605reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009606{
9607 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9608 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009609 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9610 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009611 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009612 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009613}
9614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009615/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 * Add an assert error to v:errors.
9617 */
9618 void
9619assert_error(garray_T *gap)
9620{
9621 struct vimvar *vp = &vimvars[VV_ERRORS];
9622
9623 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9624 /* Make sure v:errors is a list. */
9625 set_vim_var_list(VV_ERRORS, list_alloc());
9626 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9627}
Bram Moolenaar31988702018-02-13 12:57:42 +01009628/*
9629 * Compare "typ1" and "typ2". Put the result in "typ1".
9630 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009631 int
9632typval_compare(
9633 typval_T *typ1, /* first operand */
9634 typval_T *typ2, /* second operand */
9635 exptype_T type, /* operator */
9636 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009637 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009638{
9639 int i;
9640 varnumber_T n1, n2;
9641 char_u *s1, *s2;
9642 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9643
Bram Moolenaar31988702018-02-13 12:57:42 +01009644 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009645 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009646 /* For "is" a different type always means FALSE, for "notis"
9647 * it means TRUE. */
9648 n1 = (type == TYPE_NEQUAL);
9649 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009650 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9651 {
9652 if (type_is)
9653 {
9654 n1 = (typ1->v_type == typ2->v_type
9655 && typ1->vval.v_blob == typ2->vval.v_blob);
9656 if (type == TYPE_NEQUAL)
9657 n1 = !n1;
9658 }
9659 else if (typ1->v_type != typ2->v_type
9660 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9661 {
9662 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009663 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009664 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009665 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009666 clear_tv(typ1);
9667 return FAIL;
9668 }
9669 else
9670 {
9671 // Compare two Blobs for being equal or unequal.
9672 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9673 if (type == TYPE_NEQUAL)
9674 n1 = !n1;
9675 }
9676 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009677 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9678 {
9679 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009680 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009681 n1 = (typ1->v_type == typ2->v_type
9682 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009683 if (type == TYPE_NEQUAL)
9684 n1 = !n1;
9685 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009686 else if (typ1->v_type != typ2->v_type
9687 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009688 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009689 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009690 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009691 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009692 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009693 clear_tv(typ1);
9694 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009695 }
9696 else
9697 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009698 /* Compare two Lists for being equal or unequal. */
9699 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9700 ic, FALSE);
9701 if (type == TYPE_NEQUAL)
9702 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009703 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009704 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009705
9706 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9707 {
9708 if (type_is)
9709 {
9710 n1 = (typ1->v_type == typ2->v_type
9711 && typ1->vval.v_dict == typ2->vval.v_dict);
9712 if (type == TYPE_NEQUAL)
9713 n1 = !n1;
9714 }
9715 else if (typ1->v_type != typ2->v_type
9716 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9717 {
9718 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009719 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009720 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009721 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009722 clear_tv(typ1);
9723 return FAIL;
9724 }
9725 else
9726 {
9727 /* Compare two Dictionaries for being equal or unequal. */
9728 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9729 ic, FALSE);
9730 if (type == TYPE_NEQUAL)
9731 n1 = !n1;
9732 }
9733 }
9734
9735 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9736 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9737 {
9738 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009740 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009741 clear_tv(typ1);
9742 return FAIL;
9743 }
9744 if ((typ1->v_type == VAR_PARTIAL
9745 && typ1->vval.v_partial == NULL)
9746 || (typ2->v_type == VAR_PARTIAL
9747 && typ2->vval.v_partial == NULL))
9748 /* when a partial is NULL assume not equal */
9749 n1 = FALSE;
9750 else if (type_is)
9751 {
9752 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9753 /* strings are considered the same if their value is
9754 * the same */
9755 n1 = tv_equal(typ1, typ2, ic, FALSE);
9756 else if (typ1->v_type == VAR_PARTIAL
9757 && typ2->v_type == VAR_PARTIAL)
9758 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9759 else
9760 n1 = FALSE;
9761 }
9762 else
9763 n1 = tv_equal(typ1, typ2, ic, FALSE);
9764 if (type == TYPE_NEQUAL)
9765 n1 = !n1;
9766 }
9767
9768#ifdef FEAT_FLOAT
9769 /*
9770 * If one of the two variables is a float, compare as a float.
9771 * When using "=~" or "!~", always compare as string.
9772 */
9773 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9774 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9775 {
9776 float_T f1, f2;
9777
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009778 f1 = tv_get_float(typ1);
9779 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009780 n1 = FALSE;
9781 switch (type)
9782 {
9783 case TYPE_EQUAL: n1 = (f1 == f2); break;
9784 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9785 case TYPE_GREATER: n1 = (f1 > f2); break;
9786 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9787 case TYPE_SMALLER: n1 = (f1 < f2); break;
9788 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9789 case TYPE_UNKNOWN:
9790 case TYPE_MATCH:
9791 case TYPE_NOMATCH: break; /* avoid gcc warning */
9792 }
9793 }
9794#endif
9795
9796 /*
9797 * If one of the two variables is a number, compare as a number.
9798 * When using "=~" or "!~", always compare as string.
9799 */
9800 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9801 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009803 n1 = tv_get_number(typ1);
9804 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009805 switch (type)
9806 {
9807 case TYPE_EQUAL: n1 = (n1 == n2); break;
9808 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9809 case TYPE_GREATER: n1 = (n1 > n2); break;
9810 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9811 case TYPE_SMALLER: n1 = (n1 < n2); break;
9812 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9813 case TYPE_UNKNOWN:
9814 case TYPE_MATCH:
9815 case TYPE_NOMATCH: break; /* avoid gcc warning */
9816 }
9817 }
9818 else
9819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009820 s1 = tv_get_string_buf(typ1, buf1);
9821 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009822 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9823 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9824 else
9825 i = 0;
9826 n1 = FALSE;
9827 switch (type)
9828 {
9829 case TYPE_EQUAL: n1 = (i == 0); break;
9830 case TYPE_NEQUAL: n1 = (i != 0); break;
9831 case TYPE_GREATER: n1 = (i > 0); break;
9832 case TYPE_GEQUAL: n1 = (i >= 0); break;
9833 case TYPE_SMALLER: n1 = (i < 0); break;
9834 case TYPE_SEQUAL: n1 = (i <= 0); break;
9835
9836 case TYPE_MATCH:
9837 case TYPE_NOMATCH:
9838 n1 = pattern_match(s2, s1, ic);
9839 if (type == TYPE_NOMATCH)
9840 n1 = !n1;
9841 break;
9842
9843 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9844 }
9845 }
9846 clear_tv(typ1);
9847 typ1->v_type = VAR_NUMBER;
9848 typ1->vval.v_number = n1;
9849
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009850 return OK;
9851}
9852
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009853 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009854typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009855{
9856 char_u *tofree;
9857 char_u numbuf[NUMBUFLEN];
9858 char_u *ret = NULL;
9859
9860 if (arg == NULL)
9861 return vim_strsave((char_u *)"(does not exist)");
9862 ret = tv2string(arg, &tofree, numbuf, 0);
9863 /* Make a copy if we have a value but it's not in allocated memory. */
9864 if (ret != NULL && tofree == NULL)
9865 ret = vim_strsave(ret);
9866 return ret;
9867}
9868
9869 int
9870var_exists(char_u *var)
9871{
9872 char_u *name;
9873 char_u *tofree;
9874 typval_T tv;
9875 int len = 0;
9876 int n = FALSE;
9877
9878 /* get_name_len() takes care of expanding curly braces */
9879 name = var;
9880 len = get_name_len(&var, &tofree, TRUE, FALSE);
9881 if (len > 0)
9882 {
9883 if (tofree != NULL)
9884 name = tofree;
9885 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9886 if (n)
9887 {
9888 /* handle d.key, l[idx], f(expr) */
9889 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9890 if (n)
9891 clear_tv(&tv);
9892 }
9893 }
9894 if (*var != NUL)
9895 n = FALSE;
9896
9897 vim_free(tofree);
9898 return n;
9899}
9900
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901#endif /* FEAT_EVAL */
9902
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009904#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905
Bram Moolenaar4f974752019-02-17 17:44:42 +01009906#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907/*
9908 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
9911/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009912 * Get the short path (8.3) for the filename in "fnamep".
9913 * Only works for a valid file name.
9914 * When the path gets longer "fnamep" is changed and the allocated buffer
9915 * is put in "bufp".
9916 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9917 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 */
9919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009920get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009922 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 char_u *newbuf;
9924
9925 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009926 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 if (l > len - 1)
9928 {
9929 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009930 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 newbuf = vim_strnsave(*fnamep, l);
9932 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009933 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
9935 vim_free(*bufp);
9936 *fnamep = *bufp = newbuf;
9937
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009938 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009939 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 }
9941
9942 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009943 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944}
9945
9946/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009947 * Get the short path (8.3) for the filename in "fname". The converted
9948 * path is returned in "bufp".
9949 *
9950 * Some of the directories specified in "fname" may not exist. This function
9951 * will shorten the existing directories at the beginning of the path and then
9952 * append the remaining non-existing path.
9953 *
9954 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009955 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009956 * bufp - Pointer to an allocated buffer for the filename.
9957 * fnamelen - Length of the filename pointed to by fname
9958 *
9959 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 */
9961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009962shortpath_for_invalid_fname(
9963 char_u **fname,
9964 char_u **bufp,
9965 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009967 char_u *short_fname, *save_fname, *pbuf_unused;
9968 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009970 int old_len, len;
9971 int new_len, sfx_len;
9972 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973
9974 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009975 old_len = *fnamelen;
9976 save_fname = vim_strnsave(*fname, old_len);
9977 pbuf_unused = NULL;
9978 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009980 endp = save_fname + old_len - 1; /* Find the end of the copy */
9981 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009983 /*
9984 * Try shortening the supplied path till it succeeds by removing one
9985 * directory at a time from the tail of the path.
9986 */
9987 len = 0;
9988 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009990 /* go back one path-separator */
9991 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9992 --endp;
9993 if (endp <= save_fname)
9994 break; /* processed the complete path */
9995
9996 /*
9997 * Replace the path separator with a NUL and try to shorten the
9998 * resulting path.
9999 */
10000 ch = *endp;
10001 *endp = 0;
10002 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010003 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010004 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10005 {
10006 retval = FAIL;
10007 goto theend;
10008 }
10009 *endp = ch; /* preserve the string */
10010
10011 if (len > 0)
10012 break; /* successfully shortened the path */
10013
10014 /* failed to shorten the path. Skip the path separator */
10015 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 }
10017
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010018 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010020 /*
10021 * Succeeded in shortening the path. Now concatenate the shortened
10022 * path with the remaining path at the tail.
10023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010025 /* Compute the length of the new path. */
10026 sfx_len = (int)(save_endp - endp) + 1;
10027 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010029 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010031 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010033 /* There is not enough space in the currently allocated string,
10034 * copy it to a buffer big enough. */
10035 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010037 {
10038 retval = FAIL;
10039 goto theend;
10040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 }
10042 else
10043 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010044 /* Transfer short_fname to the main buffer (it's big enough),
10045 * unless get_short_pathname() did its work in-place. */
10046 *fname = *bufp = save_fname;
10047 if (short_fname != save_fname)
10048 vim_strncpy(save_fname, short_fname, len);
10049 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010051
10052 /* concat the not-shortened part of the path */
10053 vim_strncpy(*fname + len, endp, sfx_len);
10054 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010056
10057theend:
10058 vim_free(pbuf_unused);
10059 vim_free(save_fname);
10060
10061 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062}
10063
10064/*
10065 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010066 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 */
10068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010069shortpath_for_partial(
10070 char_u **fnamep,
10071 char_u **bufp,
10072 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074 int sepcount, len, tflen;
10075 char_u *p;
10076 char_u *pbuf, *tfname;
10077 int hasTilde;
10078
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010079 /* Count up the path separators from the RHS.. so we know which part
10080 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010082 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 if (vim_ispathsep(*p))
10084 ++sepcount;
10085
10086 /* Need full path first (use expand_env() to remove a "~/") */
10087 hasTilde = (**fnamep == '~');
10088 if (hasTilde)
10089 pbuf = tfname = expand_env_save(*fnamep);
10090 else
10091 pbuf = tfname = FullName_save(*fnamep, FALSE);
10092
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010093 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010095 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10096 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
10098 if (len == 0)
10099 {
10100 /* Don't have a valid filename, so shorten the rest of the
10101 * path if we can. This CAN give us invalid 8.3 filenames, but
10102 * there's not a lot of point in guessing what it might be.
10103 */
10104 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010105 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10106 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 }
10108
10109 /* Count the paths backward to find the beginning of the desired string. */
10110 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010111 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010112 if (has_mbyte)
10113 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 if (vim_ispathsep(*p))
10115 {
10116 if (sepcount == 0 || (hasTilde && sepcount == 1))
10117 break;
10118 else
10119 sepcount --;
10120 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 if (hasTilde)
10123 {
10124 --p;
10125 if (p >= tfname)
10126 *p = '~';
10127 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010128 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 }
10130 else
10131 ++p;
10132
10133 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10134 vim_free(*bufp);
10135 *fnamelen = (int)STRLEN(p);
10136 *bufp = pbuf;
10137 *fnamep = p;
10138
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010139 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010141#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142
10143/*
10144 * Adjust a filename, according to a string of modifiers.
10145 * *fnamep must be NUL terminated when called. When returning, the length is
10146 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010147 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 * When there is an error, *fnamep is set to NULL.
10149 */
10150 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010151modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010152 char_u *src, // string with modifiers
10153 int tilde_file, // "~" is a file name, not $HOME
10154 int *usedlen, // characters after src that are used
10155 char_u **fnamep, // file name so far
10156 char_u **bufp, // buffer for allocated file name or NULL
10157 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
10159 int valid = 0;
10160 char_u *tail;
10161 char_u *s, *p, *pbuf;
10162 char_u dirname[MAXPATHL];
10163 int c;
10164 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010165#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010166 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 int has_shortname = 0;
10168#endif
10169
10170repeat:
10171 /* ":p" - full path/file_name */
10172 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10173 {
10174 has_fullname = 1;
10175
10176 valid |= VALID_PATH;
10177 *usedlen += 2;
10178
10179 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10180 if ((*fnamep)[0] == '~'
10181#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10182 && ((*fnamep)[1] == '/'
10183# ifdef BACKSLASH_IN_FILENAME
10184 || (*fnamep)[1] == '\\'
10185# endif
10186 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010188 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 )
10190 {
10191 *fnamep = expand_env_save(*fnamep);
10192 vim_free(*bufp); /* free any allocated file name */
10193 *bufp = *fnamep;
10194 if (*fnamep == NULL)
10195 return -1;
10196 }
10197
10198 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010199 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 {
10201 if (vim_ispathsep(*p)
10202 && p[1] == '.'
10203 && (p[2] == NUL
10204 || vim_ispathsep(p[2])
10205 || (p[2] == '.'
10206 && (p[3] == NUL || vim_ispathsep(p[3])))))
10207 break;
10208 }
10209
10210 /* FullName_save() is slow, don't use it when not needed. */
10211 if (*p != NUL || !vim_isAbsName(*fnamep))
10212 {
10213 *fnamep = FullName_save(*fnamep, *p != NUL);
10214 vim_free(*bufp); /* free any allocated file name */
10215 *bufp = *fnamep;
10216 if (*fnamep == NULL)
10217 return -1;
10218 }
10219
Bram Moolenaar4f974752019-02-17 17:44:42 +010010220#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010221# if _WIN32_WINNT >= 0x0500
10222 if (vim_strchr(*fnamep, '~') != NULL)
10223 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010224 // Expand 8.3 filename to full path. Needed to make sure the same
10225 // file does not have two different names.
10226 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10227 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10228 WCHAR buf[_MAX_PATH];
10229
10230 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010231 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010232 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010233 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010234 char_u *p = utf16_to_enc(buf, NULL);
10235
10236 if (p != NULL)
10237 {
10238 vim_free(*bufp); // free any allocated file name
10239 *bufp = *fnamep = p;
10240 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010241 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010242 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010243 }
10244 }
10245# endif
10246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 /* Append a path separator to a directory. */
10248 if (mch_isdir(*fnamep))
10249 {
10250 /* Make room for one or two extra characters. */
10251 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10252 vim_free(*bufp); /* free any allocated file name */
10253 *bufp = *fnamep;
10254 if (*fnamep == NULL)
10255 return -1;
10256 add_pathsep(*fnamep);
10257 }
10258 }
10259
10260 /* ":." - path relative to the current directory */
10261 /* ":~" - path relative to the home directory */
10262 /* ":8" - shortname path - postponed till after */
10263 while (src[*usedlen] == ':'
10264 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10265 {
10266 *usedlen += 2;
10267 if (c == '8')
10268 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010269#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 has_shortname = 1; /* Postpone this. */
10271#endif
10272 continue;
10273 }
10274 pbuf = NULL;
10275 /* Need full path first (use expand_env() to remove a "~/") */
10276 if (!has_fullname)
10277 {
10278 if (c == '.' && **fnamep == '~')
10279 p = pbuf = expand_env_save(*fnamep);
10280 else
10281 p = pbuf = FullName_save(*fnamep, FALSE);
10282 }
10283 else
10284 p = *fnamep;
10285
10286 has_fullname = 0;
10287
10288 if (p != NULL)
10289 {
10290 if (c == '.')
10291 {
10292 mch_dirname(dirname, MAXPATHL);
10293 s = shorten_fname(p, dirname);
10294 if (s != NULL)
10295 {
10296 *fnamep = s;
10297 if (pbuf != NULL)
10298 {
10299 vim_free(*bufp); /* free any allocated file name */
10300 *bufp = pbuf;
10301 pbuf = NULL;
10302 }
10303 }
10304 }
10305 else
10306 {
10307 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10308 /* Only replace it when it starts with '~' */
10309 if (*dirname == '~')
10310 {
10311 s = vim_strsave(dirname);
10312 if (s != NULL)
10313 {
10314 *fnamep = s;
10315 vim_free(*bufp);
10316 *bufp = s;
10317 }
10318 }
10319 }
10320 vim_free(pbuf);
10321 }
10322 }
10323
10324 tail = gettail(*fnamep);
10325 *fnamelen = (int)STRLEN(*fnamep);
10326
10327 /* ":h" - head, remove "/file_name", can be repeated */
10328 /* Don't remove the first "/" or "c:\" */
10329 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10330 {
10331 valid |= VALID_HEAD;
10332 *usedlen += 2;
10333 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010334 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010335 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 *fnamelen = (int)(tail - *fnamep);
10337#ifdef VMS
10338 if (*fnamelen > 0)
10339 *fnamelen += 1; /* the path separator is part of the path */
10340#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010341 if (*fnamelen == 0)
10342 {
10343 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10344 p = vim_strsave((char_u *)".");
10345 if (p == NULL)
10346 return -1;
10347 vim_free(*bufp);
10348 *bufp = *fnamep = tail = p;
10349 *fnamelen = 1;
10350 }
10351 else
10352 {
10353 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010354 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 }
10357
10358 /* ":8" - shortname */
10359 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10360 {
10361 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010362#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 has_shortname = 1;
10364#endif
10365 }
10366
Bram Moolenaar4f974752019-02-17 17:44:42 +010010367#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010368 /*
10369 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 */
10371 if (has_shortname)
10372 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010373 /* Copy the string if it is shortened by :h and when it wasn't copied
10374 * yet, because we are going to change it in place. Avoids changing
10375 * the buffer name for "%:8". */
10376 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 {
10378 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010379 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 return -1;
10381 vim_free(*bufp);
10382 *bufp = *fnamep = p;
10383 }
10384
10385 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010386 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 if (!has_fullname && !vim_isAbsName(*fnamep))
10388 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010389 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 return -1;
10391 }
10392 else
10393 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010394 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395
Bram Moolenaardc935552011-08-17 15:23:23 +020010396 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010398 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 return -1;
10400
10401 if (l == 0)
10402 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010403 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010405 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 return -1;
10407 }
10408 *fnamelen = l;
10409 }
10410 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010411#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
10413 /* ":t" - tail, just the basename */
10414 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10415 {
10416 *usedlen += 2;
10417 *fnamelen -= (int)(tail - *fnamep);
10418 *fnamep = tail;
10419 }
10420
10421 /* ":e" - extension, can be repeated */
10422 /* ":r" - root, without extension, can be repeated */
10423 while (src[*usedlen] == ':'
10424 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10425 {
10426 /* find a '.' in the tail:
10427 * - for second :e: before the current fname
10428 * - otherwise: The last '.'
10429 */
10430 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10431 s = *fnamep - 2;
10432 else
10433 s = *fnamep + *fnamelen - 1;
10434 for ( ; s > tail; --s)
10435 if (s[0] == '.')
10436 break;
10437 if (src[*usedlen + 1] == 'e') /* :e */
10438 {
10439 if (s > tail)
10440 {
10441 *fnamelen += (int)(*fnamep - (s + 1));
10442 *fnamep = s + 1;
10443#ifdef VMS
10444 /* cut version from the extension */
10445 s = *fnamep + *fnamelen - 1;
10446 for ( ; s > *fnamep; --s)
10447 if (s[0] == ';')
10448 break;
10449 if (s > *fnamep)
10450 *fnamelen = s - *fnamep;
10451#endif
10452 }
10453 else if (*fnamep <= tail)
10454 *fnamelen = 0;
10455 }
10456 else /* :r */
10457 {
10458 if (s > tail) /* remove one extension */
10459 *fnamelen = (int)(s - *fnamep);
10460 }
10461 *usedlen += 2;
10462 }
10463
10464 /* ":s?pat?foo?" - substitute */
10465 /* ":gs?pat?foo?" - global substitute */
10466 if (src[*usedlen] == ':'
10467 && (src[*usedlen + 1] == 's'
10468 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10469 {
10470 char_u *str;
10471 char_u *pat;
10472 char_u *sub;
10473 int sep;
10474 char_u *flags;
10475 int didit = FALSE;
10476
10477 flags = (char_u *)"";
10478 s = src + *usedlen + 2;
10479 if (src[*usedlen + 1] == 'g')
10480 {
10481 flags = (char_u *)"g";
10482 ++s;
10483 }
10484
10485 sep = *s++;
10486 if (sep)
10487 {
10488 /* find end of pattern */
10489 p = vim_strchr(s, sep);
10490 if (p != NULL)
10491 {
10492 pat = vim_strnsave(s, (int)(p - s));
10493 if (pat != NULL)
10494 {
10495 s = p + 1;
10496 /* find end of substitution */
10497 p = vim_strchr(s, sep);
10498 if (p != NULL)
10499 {
10500 sub = vim_strnsave(s, (int)(p - s));
10501 str = vim_strnsave(*fnamep, *fnamelen);
10502 if (sub != NULL && str != NULL)
10503 {
10504 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010505 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 if (s != NULL)
10507 {
10508 *fnamep = s;
10509 *fnamelen = (int)STRLEN(s);
10510 vim_free(*bufp);
10511 *bufp = s;
10512 didit = TRUE;
10513 }
10514 }
10515 vim_free(sub);
10516 vim_free(str);
10517 }
10518 vim_free(pat);
10519 }
10520 }
10521 /* after using ":s", repeat all the modifiers */
10522 if (didit)
10523 goto repeat;
10524 }
10525 }
10526
Bram Moolenaar26df0922014-02-23 23:39:13 +010010527 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10528 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010529 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010530 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010531 if (c != NUL)
10532 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010533 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010534 if (c != NUL)
10535 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010536 if (p == NULL)
10537 return -1;
10538 vim_free(*bufp);
10539 *bufp = *fnamep = p;
10540 *fnamelen = (int)STRLEN(p);
10541 *usedlen += 2;
10542 }
10543
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 return valid;
10545}
10546
10547/*
10548 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010549 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 * "flags" can be "g" to do a global substitute.
10551 * Returns an allocated string, NULL for error.
10552 */
10553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010554do_string_sub(
10555 char_u *str,
10556 char_u *pat,
10557 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010558 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010559 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560{
10561 int sublen;
10562 regmatch_T regmatch;
10563 int i;
10564 int do_all;
10565 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010566 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 garray_T ga;
10568 char_u *ret;
10569 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010570 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571
10572 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10573 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010574 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575
10576 ga_init2(&ga, 1, 200);
10577
10578 do_all = (flags[0] == 'g');
10579
10580 regmatch.rm_ic = p_ic;
10581 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10582 if (regmatch.regprog != NULL)
10583 {
10584 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010585 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10587 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010588 /* Skip empty match except for first match. */
10589 if (regmatch.startp[0] == regmatch.endp[0])
10590 {
10591 if (zero_width == regmatch.startp[0])
10592 {
10593 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010594 i = MB_PTR2LEN(tail);
10595 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10596 (size_t)i);
10597 ga.ga_len += i;
10598 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010599 continue;
10600 }
10601 zero_width = regmatch.startp[0];
10602 }
10603
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 /*
10605 * Get some space for a temporary buffer to do the substitution
10606 * into. It will contain:
10607 * - The text up to where the match is.
10608 * - The substituted text.
10609 * - The text after the match.
10610 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010611 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010612 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10614 {
10615 ga_clear(&ga);
10616 break;
10617 }
10618
10619 /* copy the text up to where the match is */
10620 i = (int)(regmatch.startp[0] - tail);
10621 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10622 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010623 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 + ga.ga_len + i, TRUE, TRUE, FALSE);
10625 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010626 tail = regmatch.endp[0];
10627 if (*tail == NUL)
10628 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 if (!do_all)
10630 break;
10631 }
10632
10633 if (ga.ga_data != NULL)
10634 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10635
Bram Moolenaar473de612013-06-08 18:19:48 +020010636 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 }
10638
10639 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10640 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010641 if (p_cpo == empty_option)
10642 p_cpo = save_cpo;
10643 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010644 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010645 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646
10647 return ret;
10648}
10649
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010650 static int
10651filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10652{
10653 typval_T rettv;
10654 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010655 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656
10657 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10658 argv[0] = vimvars[VV_KEY].vv_tv;
10659 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010660 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10661 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010662 if (map)
10663 {
10664 /* map(): replace the list item value */
10665 clear_tv(tv);
10666 rettv.v_lock = 0;
10667 *tv = rettv;
10668 }
10669 else
10670 {
10671 int error = FALSE;
10672
10673 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010674 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675 clear_tv(&rettv);
10676 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010677 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678 if (error)
10679 goto theend;
10680 }
10681 retval = OK;
10682theend:
10683 clear_tv(&vimvars[VV_VAL].vv_tv);
10684 return retval;
10685}
10686
10687
10688/*
10689 * Implementation of map() and filter().
10690 */
10691 void
10692filter_map(typval_T *argvars, typval_T *rettv, int map)
10693{
10694 typval_T *expr;
10695 listitem_T *li, *nli;
10696 list_T *l = NULL;
10697 dictitem_T *di;
10698 hashtab_T *ht;
10699 hashitem_T *hi;
10700 dict_T *d = NULL;
10701 typval_T save_val;
10702 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010703 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010704 int rem;
10705 int todo;
10706 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10707 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10708 : N_("filter() argument"));
10709 int save_did_emsg;
10710 int idx = 0;
10711
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010712 if (argvars[0].v_type == VAR_BLOB)
10713 {
10714 if ((b = argvars[0].vval.v_blob) == NULL)
10715 return;
10716 }
10717 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010718 {
10719 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010720 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010721 return;
10722 }
10723 else if (argvars[0].v_type == VAR_DICT)
10724 {
10725 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010726 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010727 return;
10728 }
10729 else
10730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010731 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732 return;
10733 }
10734
10735 expr = &argvars[1];
10736 /* On type errors, the preceding call has already displayed an error
10737 * message. Avoid a misleading error message for an empty string that
10738 * was not passed as argument. */
10739 if (expr->v_type != VAR_UNKNOWN)
10740 {
10741 prepare_vimvar(VV_VAL, &save_val);
10742
10743 /* We reset "did_emsg" to be able to detect whether an error
10744 * occurred during evaluation of the expression. */
10745 save_did_emsg = did_emsg;
10746 did_emsg = FALSE;
10747
10748 prepare_vimvar(VV_KEY, &save_key);
10749 if (argvars[0].v_type == VAR_DICT)
10750 {
10751 vimvars[VV_KEY].vv_type = VAR_STRING;
10752
10753 ht = &d->dv_hashtab;
10754 hash_lock(ht);
10755 todo = (int)ht->ht_used;
10756 for (hi = ht->ht_array; todo > 0; ++hi)
10757 {
10758 if (!HASHITEM_EMPTY(hi))
10759 {
10760 int r;
10761
10762 --todo;
10763 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010764 if (map && (var_check_lock(di->di_tv.v_lock,
10765 arg_errmsg, TRUE)
10766 || var_check_ro(di->di_flags,
10767 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 break;
10769 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10770 r = filter_map_one(&di->di_tv, expr, map, &rem);
10771 clear_tv(&vimvars[VV_KEY].vv_tv);
10772 if (r == FAIL || did_emsg)
10773 break;
10774 if (!map && rem)
10775 {
10776 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10777 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10778 break;
10779 dictitem_remove(d, di);
10780 }
10781 }
10782 }
10783 hash_unlock(ht);
10784 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010785 else if (argvars[0].v_type == VAR_BLOB)
10786 {
10787 int i;
10788 typval_T tv;
10789
10790 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10791 for (i = 0; i < b->bv_ga.ga_len; i++)
10792 {
10793 tv.v_type = VAR_NUMBER;
10794 tv.vval.v_number = blob_get(b, i);
10795 vimvars[VV_KEY].vv_nr = idx;
10796 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10797 break;
10798 if (tv.v_type != VAR_NUMBER)
10799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010800 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010801 return;
10802 }
10803 tv.v_type = VAR_NUMBER;
10804 blob_set(b, i, tv.vval.v_number);
10805 if (!map && rem)
10806 {
10807 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10808
10809 mch_memmove(p + idx, p + i + 1,
10810 (size_t)b->bv_ga.ga_len - i - 1);
10811 --b->bv_ga.ga_len;
10812 --i;
10813 }
10814 }
10815 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816 else
10817 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010818 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010819 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10820
10821 for (li = l->lv_first; li != NULL; li = nli)
10822 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010823 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010824 break;
10825 nli = li->li_next;
10826 vimvars[VV_KEY].vv_nr = idx;
10827 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10828 || did_emsg)
10829 break;
10830 if (!map && rem)
10831 listitem_remove(l, li);
10832 ++idx;
10833 }
10834 }
10835
10836 restore_vimvar(VV_KEY, &save_key);
10837 restore_vimvar(VV_VAL, &save_val);
10838
10839 did_emsg |= save_did_emsg;
10840 }
10841
10842 copy_tv(&argvars[0], rettv);
10843}
10844
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */