blob: 38c192ed1b328436313a77db474d981161c55a2f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaarab1fa392016-03-15 19:33:34 +0100113static char *e_dict_both = N_("E924: can't have both a \"self\" dict and a partial: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100115#define NAMESPACE_CHAR (char_u *)"abglstvw"
116
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200117static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000118#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
123 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000124static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125
126/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000129 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 */
131static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132#define COPYID_INC 2
133#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134
Bram Moolenaar8502c702014-06-17 12:51:16 +0200135/* Abort conversion to string after a recursion error. */
136static int did_echo_string_emsg = FALSE;
137
Bram Moolenaard9fba312005-06-26 22:34:35 +0000138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100157#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
158
159/* Values for get_lval() flags argument: */
160#define GLV_QUIET TFN_QUIET /* no error messages */
161#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000162
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163/*
164 * Structure to hold info for a user function.
165 */
166typedef struct ufunc ufunc_T;
167
168struct ufunc
169{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000170 int uf_varargs; /* variable nr of arguments */
171 int uf_flags;
172 int uf_calls; /* nr of active calls */
173 garray_T uf_args; /* arguments */
174 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175#ifdef FEAT_PROFILE
176 int uf_profiling; /* TRUE when func is being profiled */
177 /* profiling the function as a whole */
178 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T uf_tm_total; /* time spent in function + children */
180 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tm_children; /* time spent in children this call */
182 /* profiling the function per line */
183 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000184 proftime_T *uf_tml_total; /* time spent in a line + children */
185 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000186 proftime_T uf_tml_start; /* start time for current line */
187 proftime_T uf_tml_children; /* time spent in children for this line */
188 proftime_T uf_tml_wait; /* start wait time for current line */
189 int uf_tml_idx; /* index of line being timed; -1 if none */
190 int uf_tml_execed; /* line being timed was executed */
191#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 int uf_refcount; /* for numbered function: reference count */
195 char_u uf_name[1]; /* name of function (actually longer); can
196 start with <SNR>123_ (<SNR> is K_SPECIAL
197 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198};
199
200/* function flags */
201#define FC_ABORT 1 /* abort function on error */
202#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000203#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000206 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000208static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000210/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000211static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
212
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000213/* list heads for garbage collection */
214static dict_T *first_dict = NULL; /* list of all dicts */
215static list_T *first_list = NULL; /* list of all lists */
216
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000217/* From user function to hashitem and back. */
218static ufunc_T dumuf;
219#define UF2HIKEY(fp) ((fp)->uf_name)
220#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
221#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
222
223#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
224#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar33570922005-01-25 22:26:29 +0000226#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
227#define VAR_SHORT_LEN 20 /* short variable name length */
228#define FIXVAR_CNT 12 /* number of fixed variables */
229
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000231typedef struct funccall_S funccall_T;
232
233struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 ufunc_T *func; /* function being called */
236 int linenr; /* next line to be executed */
237 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000238 struct /* fixed variables for arguments */
239 {
240 dictitem_T var; /* variable (without room for name) */
241 char_u room[VAR_SHORT_LEN]; /* room for the name */
242 } fixvar[FIXVAR_CNT];
243 dict_T l_vars; /* l: local function variables */
244 dictitem_T l_vars_var; /* variable for l: scope */
245 dict_T l_avars; /* a: argument variables */
246 dictitem_T l_avars_var; /* variable for a: scope */
247 list_T l_varlist; /* list for a:000 */
248 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
249 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 linenr_T breakpoint; /* next line with breakpoint or zero */
251 int dbg_tick; /* debug_tick when breakpoint was set */
252 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000253#ifdef FEAT_PROFILE
254 proftime_T prof_child; /* time spent in a child */
255#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000256 funccall_T *caller; /* calling function or NULL */
257};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260 * Info used by a ":for" loop.
261 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000262typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263{
264 int fi_semicolon; /* TRUE if ending in '; var]' */
265 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 listwatch_T fi_lw; /* keep an eye on the item used. */
267 list_T *fi_list; /* list being used */
268} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000271 * Struct used by trans_function_name()
272 */
273typedef struct
274{
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000276 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dictitem_T *fd_di; /* Dictionary item used */
278} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000279
Bram Moolenaara7043832005-01-21 11:56:39 +0000280
281/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000282 * Array to hold the value of v: variables.
283 * The value is in a dictitem, so that it can also be used in the v: scope.
284 * The reason to use this table anyway is for very quick access to the
285 * variables with the VV_ defines.
286 */
287#include "version.h"
288
289/* values for vv_flags: */
290#define VV_COMPAT 1 /* compatible, also used without "v:" */
291#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000292#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000294#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
296static struct vimvar
297{
298 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000299 dictitem_T vv_di; /* value and name for key */
300 char vv_filler[16]; /* space for LONGEST name below!!! */
301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000375};
376
377/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000378#define vv_type vv_di.di_tv.v_type
379#define vv_nr vv_di.di_tv.vval.v_number
380#define vv_float vv_di.di_tv.vval.v_float
381#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000382#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200383#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000384#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000385
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200386static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000387#define vimvarht vimvardict.dv_hashtab
388
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100389static void prepare_vimvar(int idx, typval_T *save_tv);
390static void restore_vimvar(int idx, typval_T *save_tv);
391static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
392static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
393static char_u *skip_var_one(char_u *arg);
394static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
395static void list_glob_vars(int *first);
396static void list_buf_vars(int *first);
397static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000398#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100399static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_vim_vars(int *first);
402static void list_script_vars(int *first);
403static void list_func_vars(int *first);
404static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
405static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
406static int check_changedtick(char_u *arg);
407static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
408static void clear_lval(lval_T *lp);
409static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
410static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
411static void list_fix_watch(list_T *l, listitem_T *item);
412static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
413static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
414static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
415static void item_lock(typval_T *tv, int deep, int lock);
416static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100418static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
419static int eval1(char_u **arg, typval_T *rettv, int evaluate);
420static int eval2(char_u **arg, typval_T *rettv, int evaluate);
421static int eval3(char_u **arg, typval_T *rettv, int evaluate);
422static int eval4(char_u **arg, typval_T *rettv, int evaluate);
423static int eval5(char_u **arg, typval_T *rettv, int evaluate);
424static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
425static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000426
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100427static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
428static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
432static long list_len(list_T *l);
433static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
434static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
435static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
436static long list_find_nr(list_T *l, long idx, int *errorp);
437static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100438static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
439static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
440static list_T *list_copy(list_T *orig, int deep, int copyID);
441static char_u *list2string(typval_T *tv, int copyID);
442static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
443static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
444static int free_unref_items(int copyID);
445static dictitem_T *dictitem_copy(dictitem_T *org);
446static void dictitem_remove(dict_T *dict, dictitem_T *item);
447static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
448static long dict_len(dict_T *d);
449static char_u *dict2string(typval_T *tv, int copyID);
450static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
451static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *string_quote(char_u *str, int function);
454static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
455static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100456static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
457static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100458static void emsg_funcname(char *ermsg, char_u *name);
459static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_abs(typval_T *argvars, typval_T *rettv);
463static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_add(typval_T *argvars, typval_T *rettv);
466static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
467static void f_and(typval_T *argvars, typval_T *rettv);
468static void f_append(typval_T *argvars, typval_T *rettv);
469static void f_argc(typval_T *argvars, typval_T *rettv);
470static void f_argidx(typval_T *argvars, typval_T *rettv);
471static void f_arglistid(typval_T *argvars, typval_T *rettv);
472static void f_argv(typval_T *argvars, typval_T *rettv);
473static void f_assert_equal(typval_T *argvars, typval_T *rettv);
474static void f_assert_exception(typval_T *argvars, typval_T *rettv);
475static void f_assert_fails(typval_T *argvars, typval_T *rettv);
476static void f_assert_false(typval_T *argvars, typval_T *rettv);
477static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_asin(typval_T *argvars, typval_T *rettv);
480static void f_atan(typval_T *argvars, typval_T *rettv);
481static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_browse(typval_T *argvars, typval_T *rettv);
484static void f_browsedir(typval_T *argvars, typval_T *rettv);
485static void f_bufexists(typval_T *argvars, typval_T *rettv);
486static void f_buflisted(typval_T *argvars, typval_T *rettv);
487static void f_bufloaded(typval_T *argvars, typval_T *rettv);
488static void f_bufname(typval_T *argvars, typval_T *rettv);
489static void f_bufnr(typval_T *argvars, typval_T *rettv);
490static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
491static void f_byte2line(typval_T *argvars, typval_T *rettv);
492static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
493static void f_byteidx(typval_T *argvars, typval_T *rettv);
494static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
495static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100500static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
502static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100503static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100505static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100506static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
507static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100508static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100512static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100513static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100514#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_changenr(typval_T *argvars, typval_T *rettv);
516static void f_char2nr(typval_T *argvars, typval_T *rettv);
517static void f_cindent(typval_T *argvars, typval_T *rettv);
518static void f_clearmatches(typval_T *argvars, typval_T *rettv);
519static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_complete(typval_T *argvars, typval_T *rettv);
522static void f_complete_add(typval_T *argvars, typval_T *rettv);
523static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_confirm(typval_T *argvars, typval_T *rettv);
526static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_cos(typval_T *argvars, typval_T *rettv);
529static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_count(typval_T *argvars, typval_T *rettv);
532static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
533static void f_cursor(typval_T *argsvars, typval_T *rettv);
534static void f_deepcopy(typval_T *argvars, typval_T *rettv);
535static void f_delete(typval_T *argvars, typval_T *rettv);
536static void f_did_filetype(typval_T *argvars, typval_T *rettv);
537static void f_diff_filler(typval_T *argvars, typval_T *rettv);
538static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100539static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_empty(typval_T *argvars, typval_T *rettv);
541static void f_escape(typval_T *argvars, typval_T *rettv);
542static void f_eval(typval_T *argvars, typval_T *rettv);
543static void f_eventhandler(typval_T *argvars, typval_T *rettv);
544static void f_executable(typval_T *argvars, typval_T *rettv);
545static void f_exepath(typval_T *argvars, typval_T *rettv);
546static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_expand(typval_T *argvars, typval_T *rettv);
551static void f_extend(typval_T *argvars, typval_T *rettv);
552static void f_feedkeys(typval_T *argvars, typval_T *rettv);
553static void f_filereadable(typval_T *argvars, typval_T *rettv);
554static void f_filewritable(typval_T *argvars, typval_T *rettv);
555static void f_filter(typval_T *argvars, typval_T *rettv);
556static void f_finddir(typval_T *argvars, typval_T *rettv);
557static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_float2nr(typval_T *argvars, typval_T *rettv);
560static void f_floor(typval_T *argvars, typval_T *rettv);
561static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_fnameescape(typval_T *argvars, typval_T *rettv);
564static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
565static void f_foldclosed(typval_T *argvars, typval_T *rettv);
566static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
567static void f_foldlevel(typval_T *argvars, typval_T *rettv);
568static void f_foldtext(typval_T *argvars, typval_T *rettv);
569static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
570static void f_foreground(typval_T *argvars, typval_T *rettv);
571static void f_function(typval_T *argvars, typval_T *rettv);
572static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
573static void f_get(typval_T *argvars, typval_T *rettv);
574static void f_getbufline(typval_T *argvars, typval_T *rettv);
575static void f_getbufvar(typval_T *argvars, typval_T *rettv);
576static void f_getchar(typval_T *argvars, typval_T *rettv);
577static void f_getcharmod(typval_T *argvars, typval_T *rettv);
578static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
579static void f_getcmdline(typval_T *argvars, typval_T *rettv);
580static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
581static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
582static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
583static void f_getcwd(typval_T *argvars, typval_T *rettv);
584static void f_getfontname(typval_T *argvars, typval_T *rettv);
585static void f_getfperm(typval_T *argvars, typval_T *rettv);
586static void f_getfsize(typval_T *argvars, typval_T *rettv);
587static void f_getftime(typval_T *argvars, typval_T *rettv);
588static void f_getftype(typval_T *argvars, typval_T *rettv);
589static void f_getline(typval_T *argvars, typval_T *rettv);
590static void f_getmatches(typval_T *argvars, typval_T *rettv);
591static void f_getpid(typval_T *argvars, typval_T *rettv);
592static void f_getcurpos(typval_T *argvars, typval_T *rettv);
593static void f_getpos(typval_T *argvars, typval_T *rettv);
594static void f_getqflist(typval_T *argvars, typval_T *rettv);
595static void f_getreg(typval_T *argvars, typval_T *rettv);
596static void f_getregtype(typval_T *argvars, typval_T *rettv);
597static void f_gettabvar(typval_T *argvars, typval_T *rettv);
598static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
599static void f_getwinposx(typval_T *argvars, typval_T *rettv);
600static void f_getwinposy(typval_T *argvars, typval_T *rettv);
601static void f_getwinvar(typval_T *argvars, typval_T *rettv);
602static void f_glob(typval_T *argvars, typval_T *rettv);
603static void f_globpath(typval_T *argvars, typval_T *rettv);
604static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
605static void f_has(typval_T *argvars, typval_T *rettv);
606static void f_has_key(typval_T *argvars, typval_T *rettv);
607static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
608static void f_hasmapto(typval_T *argvars, typval_T *rettv);
609static void f_histadd(typval_T *argvars, typval_T *rettv);
610static void f_histdel(typval_T *argvars, typval_T *rettv);
611static void f_histget(typval_T *argvars, typval_T *rettv);
612static void f_histnr(typval_T *argvars, typval_T *rettv);
613static void f_hlID(typval_T *argvars, typval_T *rettv);
614static void f_hlexists(typval_T *argvars, typval_T *rettv);
615static void f_hostname(typval_T *argvars, typval_T *rettv);
616static void f_iconv(typval_T *argvars, typval_T *rettv);
617static void f_indent(typval_T *argvars, typval_T *rettv);
618static void f_index(typval_T *argvars, typval_T *rettv);
619static void f_input(typval_T *argvars, typval_T *rettv);
620static void f_inputdialog(typval_T *argvars, typval_T *rettv);
621static void f_inputlist(typval_T *argvars, typval_T *rettv);
622static void f_inputrestore(typval_T *argvars, typval_T *rettv);
623static void f_inputsave(typval_T *argvars, typval_T *rettv);
624static void f_inputsecret(typval_T *argvars, typval_T *rettv);
625static void f_insert(typval_T *argvars, typval_T *rettv);
626static void f_invert(typval_T *argvars, typval_T *rettv);
627static void f_isdirectory(typval_T *argvars, typval_T *rettv);
628static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100629#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
630static void f_isnan(typval_T *argvars, typval_T *rettv);
631#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100632static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100633#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100634static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100635static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100636static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100637static void f_job_start(typval_T *argvars, typval_T *rettv);
638static void f_job_stop(typval_T *argvars, typval_T *rettv);
639static void f_job_status(typval_T *argvars, typval_T *rettv);
640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100642static void f_js_decode(typval_T *argvars, typval_T *rettv);
643static void f_js_encode(typval_T *argvars, typval_T *rettv);
644static void f_json_decode(typval_T *argvars, typval_T *rettv);
645static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_keys(typval_T *argvars, typval_T *rettv);
647static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
648static void f_len(typval_T *argvars, typval_T *rettv);
649static void f_libcall(typval_T *argvars, typval_T *rettv);
650static void f_libcallnr(typval_T *argvars, typval_T *rettv);
651static void f_line(typval_T *argvars, typval_T *rettv);
652static void f_line2byte(typval_T *argvars, typval_T *rettv);
653static void f_lispindent(typval_T *argvars, typval_T *rettv);
654static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_log(typval_T *argvars, typval_T *rettv);
657static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_map(typval_T *argvars, typval_T *rettv);
663static void f_maparg(typval_T *argvars, typval_T *rettv);
664static void f_mapcheck(typval_T *argvars, typval_T *rettv);
665static void f_match(typval_T *argvars, typval_T *rettv);
666static void f_matchadd(typval_T *argvars, typval_T *rettv);
667static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
668static void f_matcharg(typval_T *argvars, typval_T *rettv);
669static void f_matchdelete(typval_T *argvars, typval_T *rettv);
670static void f_matchend(typval_T *argvars, typval_T *rettv);
671static void f_matchlist(typval_T *argvars, typval_T *rettv);
672static void f_matchstr(typval_T *argvars, typval_T *rettv);
673static void f_max(typval_T *argvars, typval_T *rettv);
674static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
683static void f_nr2char(typval_T *argvars, typval_T *rettv);
684static void f_or(typval_T *argvars, typval_T *rettv);
685static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
693static void f_printf(typval_T *argvars, typval_T *rettv);
694static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#endif
698#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_range(typval_T *argvars, typval_T *rettv);
702static void f_readfile(typval_T *argvars, typval_T *rettv);
703static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100704#ifdef FEAT_FLOAT
705static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_reltimestr(typval_T *argvars, typval_T *rettv);
708static void f_remote_expr(typval_T *argvars, typval_T *rettv);
709static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
710static void f_remote_peek(typval_T *argvars, typval_T *rettv);
711static void f_remote_read(typval_T *argvars, typval_T *rettv);
712static void f_remote_send(typval_T *argvars, typval_T *rettv);
713static void f_remove(typval_T *argvars, typval_T *rettv);
714static void f_rename(typval_T *argvars, typval_T *rettv);
715static void f_repeat(typval_T *argvars, typval_T *rettv);
716static void f_resolve(typval_T *argvars, typval_T *rettv);
717static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_screenattr(typval_T *argvars, typval_T *rettv);
722static void f_screenchar(typval_T *argvars, typval_T *rettv);
723static void f_screencol(typval_T *argvars, typval_T *rettv);
724static void f_screenrow(typval_T *argvars, typval_T *rettv);
725static void f_search(typval_T *argvars, typval_T *rettv);
726static void f_searchdecl(typval_T *argvars, typval_T *rettv);
727static void f_searchpair(typval_T *argvars, typval_T *rettv);
728static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
729static void f_searchpos(typval_T *argvars, typval_T *rettv);
730static void f_server2client(typval_T *argvars, typval_T *rettv);
731static void f_serverlist(typval_T *argvars, typval_T *rettv);
732static void f_setbufvar(typval_T *argvars, typval_T *rettv);
733static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
734static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100735static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_setline(typval_T *argvars, typval_T *rettv);
737static void f_setloclist(typval_T *argvars, typval_T *rettv);
738static void f_setmatches(typval_T *argvars, typval_T *rettv);
739static void f_setpos(typval_T *argvars, typval_T *rettv);
740static void f_setqflist(typval_T *argvars, typval_T *rettv);
741static void f_setreg(typval_T *argvars, typval_T *rettv);
742static void f_settabvar(typval_T *argvars, typval_T *rettv);
743static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
744static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100745#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_shellescape(typval_T *argvars, typval_T *rettv);
749static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
750static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sin(typval_T *argvars, typval_T *rettv);
753static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sort(typval_T *argvars, typval_T *rettv);
756static void f_soundfold(typval_T *argvars, typval_T *rettv);
757static void f_spellbadword(typval_T *argvars, typval_T *rettv);
758static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
759static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sqrt(typval_T *argvars, typval_T *rettv);
762static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_str2nr(typval_T *argvars, typval_T *rettv);
765static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000766#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_stridx(typval_T *argvars, typval_T *rettv);
770static void f_string(typval_T *argvars, typval_T *rettv);
771static void f_strlen(typval_T *argvars, typval_T *rettv);
772static void f_strpart(typval_T *argvars, typval_T *rettv);
773static void f_strridx(typval_T *argvars, typval_T *rettv);
774static void f_strtrans(typval_T *argvars, typval_T *rettv);
775static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
776static void f_strwidth(typval_T *argvars, typval_T *rettv);
777static void f_submatch(typval_T *argvars, typval_T *rettv);
778static void f_substitute(typval_T *argvars, typval_T *rettv);
779static void f_synID(typval_T *argvars, typval_T *rettv);
780static void f_synIDattr(typval_T *argvars, typval_T *rettv);
781static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
782static void f_synstack(typval_T *argvars, typval_T *rettv);
783static void f_synconcealed(typval_T *argvars, typval_T *rettv);
784static void f_system(typval_T *argvars, typval_T *rettv);
785static void f_systemlist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
787static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
788static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
789static void f_taglist(typval_T *argvars, typval_T *rettv);
790static void f_tagfiles(typval_T *argvars, typval_T *rettv);
791static void f_tempname(typval_T *argvars, typval_T *rettv);
792static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200793#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_tan(typval_T *argvars, typval_T *rettv);
795static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100797static void f_tolower(typval_T *argvars, typval_T *rettv);
798static void f_toupper(typval_T *argvars, typval_T *rettv);
799static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000800#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100801static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000802#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_type(typval_T *argvars, typval_T *rettv);
804static void f_undofile(typval_T *argvars, typval_T *rettv);
805static void f_undotree(typval_T *argvars, typval_T *rettv);
806static void f_uniq(typval_T *argvars, typval_T *rettv);
807static void f_values(typval_T *argvars, typval_T *rettv);
808static void f_virtcol(typval_T *argvars, typval_T *rettv);
809static void f_visualmode(typval_T *argvars, typval_T *rettv);
810static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100811static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100812static void f_win_getid(typval_T *argvars, typval_T *rettv);
813static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
814static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
815static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100816static void f_winbufnr(typval_T *argvars, typval_T *rettv);
817static void f_wincol(typval_T *argvars, typval_T *rettv);
818static void f_winheight(typval_T *argvars, typval_T *rettv);
819static void f_winline(typval_T *argvars, typval_T *rettv);
820static void f_winnr(typval_T *argvars, typval_T *rettv);
821static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
822static void f_winrestview(typval_T *argvars, typval_T *rettv);
823static void f_winsaveview(typval_T *argvars, typval_T *rettv);
824static void f_winwidth(typval_T *argvars, typval_T *rettv);
825static void f_writefile(typval_T *argvars, typval_T *rettv);
826static void f_wordcount(typval_T *argvars, typval_T *rettv);
827static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000828
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100829static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
830static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
831static int get_env_len(char_u **arg);
832static int get_id_len(char_u **arg);
833static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
834static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000835#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
836#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
837 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100838static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
839static int eval_isnamec(int c);
840static int eval_isnamec1(int c);
841static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
842static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static typval_T *alloc_string_tv(char_u *string);
844static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100845#ifdef FEAT_FLOAT
846static float_T get_tv_float(typval_T *varp);
847#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static linenr_T get_tv_lnum(typval_T *argvars);
849static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100850static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
851static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
852static hashtab_T *find_var_ht(char_u *name, char_u **varname);
853static funccall_T *get_funccal(void);
854static void vars_clear_ext(hashtab_T *ht, int free_val);
855static void delete_var(hashtab_T *ht, hashitem_T *hi);
856static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
857static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
858static void set_var(char_u *name, typval_T *varp, int copy);
859static int var_check_ro(int flags, char_u *name, int use_gettext);
860static int var_check_fixed(int flags, char_u *name, int use_gettext);
861static int var_check_func_name(char_u *name, int new_var);
862static int valid_varname(char_u *varname);
863static int tv_check_lock(int lock, char_u *name, int use_gettext);
864static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
865static char_u *find_option_end(char_u **arg, int *opt_flags);
866static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
867static int eval_fname_script(char_u *p);
868static int eval_fname_sid(char_u *p);
869static void list_func_head(ufunc_T *fp, int indent);
870static ufunc_T *find_func(char_u *name);
871static int function_exists(char_u *name);
872static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static void func_do_profile(ufunc_T *fp);
875static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
876static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000877static int
878# ifdef __BORLANDC__
879 _RTLENTRYF
880# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000882static int
883# ifdef __BORLANDC__
884 _RTLENTRYF
885# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int script_autoload(char_u *name, int reload);
889static char_u *autoload_name(char_u *name);
890static void cat_func_name(char_u *buf, ufunc_T *fp);
891static void func_free(ufunc_T *fp);
892static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
893static int can_free_funccal(funccall_T *fc, int copyID) ;
894static void free_funccal(funccall_T *fc, int free_val);
895static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
896static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
897static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
898static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
900static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
901static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int write_list(FILE *fd, list_T *list, int binary);
903static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000904
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200905
906#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static int compare_func_name(const void *s1, const void *s2);
908static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909#endif
910
Bram Moolenaar33570922005-01-25 22:26:29 +0000911/*
912 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000913 */
914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000916{
Bram Moolenaar33570922005-01-25 22:26:29 +0000917 int i;
918 struct vimvar *p;
919
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200920 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
921 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200922 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000923 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000924 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
926 for (i = 0; i < VV_LEN; ++i)
927 {
928 p = &vimvars[i];
929 STRCPY(p->vv_di.di_key, p->vv_name);
930 if (p->vv_flags & VV_RO)
931 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
932 else if (p->vv_flags & VV_RO_SBX)
933 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
934 else
935 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000936
937 /* add to v: scope dict, unless the value is not always available */
938 if (p->vv_type != VAR_UNKNOWN)
939 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000941 /* add to compat scope dict */
942 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100944 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
945
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000946 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100947 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200948 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100949 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100950
951 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
952 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
953 set_vim_var_nr(VV_NONE, VVAL_NONE);
954 set_vim_var_nr(VV_NULL, VVAL_NULL);
955
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200956 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200957
958#ifdef EBCDIC
959 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100960 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200961 */
962 sortFunctions();
963#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000964}
965
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966#if defined(EXITFREE) || defined(PROTO)
967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100968eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969{
970 int i;
971 struct vimvar *p;
972
973 for (i = 0; i < VV_LEN; ++i)
974 {
975 p = &vimvars[i];
976 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000977 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000978 vim_free(p->vv_str);
979 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000980 }
981 else if (p->vv_di.di_tv.v_type == VAR_LIST)
982 {
983 list_unref(p->vv_list);
984 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 }
987 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000988 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 hash_clear(&compat_hashtab);
990
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100992# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200993 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100994# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995
996 /* global variables */
997 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000998
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000999 /* autoloaded script names */
1000 ga_clear_strings(&ga_loaded);
1001
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 /* Script-local variables. First clear all the variables and in a second
1003 * loop free the scriptvar_T, because a variable in one script might hold
1004 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001007 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 ga_clear(&ga_scripts);
1010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001011 /* unreferenced lists and dicts */
1012 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001013
1014 /* functions */
1015 free_all_functions();
1016 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001017}
1018#endif
1019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Return the name of the executed function.
1022 */
1023 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * Return the address holding the next breakpoint line for a funccall cookie.
1031 */
1032 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001033func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
1039 * Return the address holding the debug tick for a funccall cookie.
1040 */
1041 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001042func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045}
1046
1047/*
1048 * Return the nesting level for a funccall cookie.
1049 */
1050 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar33570922005-01-25 22:26:29 +00001053 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054}
1055
1056/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001057funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001059/* pointer to list of previously used funccal, still around because some
1060 * item in it is still being used. */
1061funccall_T *previous_funccal = NULL;
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063/*
1064 * Return TRUE when a function was ended by a ":return" command.
1065 */
1066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001067current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 return current_funccal->returned;
1070}
1071
1072
1073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * Set an internal variable to a string value. Creates the variable if it does
1075 * not already exist.
1076 */
1077 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001081 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 val = vim_strsave(value);
1084 if (val != NULL)
1085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001089 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 }
1093}
1094
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097static char_u *redir_endp = NULL;
1098static char_u *redir_varname = NULL;
1099
1100/*
1101 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001102 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 * Returns OK if successfully completed the setup. FAIL otherwise.
1104 */
1105 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001106var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107{
1108 int save_emsg;
1109 int err;
1110 typval_T tv;
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 {
1115 EMSG(_(e_invarg));
1116 return FAIL;
1117 }
1118
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 redir_varname = vim_strsave(name);
1121 if (redir_varname == NULL)
1122 return FAIL;
1123
1124 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1125 if (redir_lval == NULL)
1126 {
1127 var_redir_stop();
1128 return FAIL;
1129 }
1130
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 /* The output is stored in growarray "redir_ga" until redirection ends. */
1132 ga_init2(&redir_ga, (int)sizeof(char), 500);
1133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001135 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001136 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1138 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001139 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp != NULL && *redir_endp != NUL)
1141 /* Trailing characters are present after the variable name */
1142 EMSG(_(e_trailing));
1143 else
1144 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 var_redir_stop();
1147 return FAIL;
1148 }
1149
1150 /* check if we can write to the variable: set it to or append an empty
1151 * string */
1152 save_emsg = did_emsg;
1153 did_emsg = FALSE;
1154 tv.v_type = VAR_STRING;
1155 tv.vval.v_string = (char_u *)"";
1156 if (append)
1157 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1158 else
1159 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001160 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001162 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (err)
1164 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 var_redir_stop();
1167 return FAIL;
1168 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169
1170 return OK;
1171}
1172
1173/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001174 * Append "value[value_len]" to the variable set by var_redir_start().
1175 * The actual appending is postponed until redirection ends, because the value
1176 * appended may in fact be the string we write to, changing it may cause freed
1177 * memory to be used:
1178 * :redir => foo
1179 * :let foo
1180 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 */
1182 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001183var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186
1187 if (redir_lval == NULL)
1188 return;
1189
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 {
1197 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 }
1200 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202}
1203
1204/*
1205 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001209var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 typval_T tv;
1212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 if (redir_lval != NULL)
1214 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001215 /* If there was no error: assign the text to the variable. */
1216 if (redir_endp != NULL)
1217 {
1218 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1219 tv.v_type = VAR_STRING;
1220 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001221 /* Call get_lval() again, if it's inside a Dict or List it may
1222 * have changed. */
1223 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001224 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001225 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1226 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1227 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001229
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 /* free the collected output */
1231 vim_free(redir_ga.ga_data);
1232 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 vim_free(redir_lval);
1235 redir_lval = NULL;
1236 }
1237 vim_free(redir_varname);
1238 redir_varname = NULL;
1239}
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241# if defined(FEAT_MBYTE) || defined(PROTO)
1242 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001243eval_charconvert(
1244 char_u *enc_from,
1245 char_u *enc_to,
1246 char_u *fname_from,
1247 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1252 set_vim_var_string(VV_CC_TO, enc_to, -1);
1253 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1254 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1255 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1256 err = TRUE;
1257 set_vim_var_string(VV_CC_FROM, NULL, -1);
1258 set_vim_var_string(VV_CC_TO, NULL, -1);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1261
1262 if (err)
1263 return FAIL;
1264 return OK;
1265}
1266# endif
1267
1268# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 int err = FALSE;
1273
1274 set_vim_var_string(VV_FNAME_IN, fname, -1);
1275 set_vim_var_string(VV_CMDARG, args, -1);
1276 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1277 err = TRUE;
1278 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1279 set_vim_var_string(VV_CMDARG, NULL, -1);
1280
1281 if (err)
1282 {
1283 mch_remove(fname);
1284 return FAIL;
1285 }
1286 return OK;
1287}
1288# endif
1289
1290# if defined(FEAT_DIFF) || defined(PROTO)
1291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292eval_diff(
1293 char_u *origfile,
1294 char_u *newfile,
1295 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1300 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1301 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1302 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1303 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1304 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1305 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1306}
1307
1308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309eval_patch(
1310 char_u *origfile,
1311 char_u *difffile,
1312 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 int err;
1315
1316 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1317 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1318 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1319 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1320 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1321 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1322 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1323}
1324# endif
1325
1326/*
1327 * Top level evaluation function, returning a boolean.
1328 * Sets "error" to TRUE if there was an error.
1329 * Return TRUE or FALSE.
1330 */
1331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_to_bool(
1333 char_u *arg,
1334 int *error,
1335 char_u **nextcmd,
1336 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 int retval = FALSE;
1340
1341 if (skip)
1342 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else
1346 {
1347 *error = FALSE;
1348 if (!skip)
1349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 }
1354 if (skip)
1355 --emsg_skip;
1356
1357 return retval;
1358}
1359
1360/*
1361 * Top level evaluation function, returning a string. If "skip" is TRUE,
1362 * only parsing to "nextcmd" is done, without reporting errors. Return
1363 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1364 */
1365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001366eval_to_string_skip(
1367 char_u *arg,
1368 char_u **nextcmd,
1369 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u *retval;
1373
1374 if (skip)
1375 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 retval = vim_strsave(get_tv_string(&tv));
1381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 if (skip)
1384 --emsg_skip;
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390 * Skip over an expression at "*pp".
1391 * Return FAIL for an error, OK otherwise.
1392 */
1393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001394skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395{
Bram Moolenaar33570922005-01-25 22:26:29 +00001396 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397
1398 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400}
1401
1402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001404 * When "convert" is TRUE convert a List into a sequence of lines and convert
1405 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Return pointer to allocated memory, or NULL for failure.
1407 */
1408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409eval_to_string(
1410 char_u *arg,
1411 char_u **nextcmd,
1412 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001417#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 retval = NULL;
1423 else
1424 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001425 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001426 {
1427 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001428 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001429 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001430 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001431 if (tv.vval.v_list->lv_len > 0)
1432 ga_append(&ga, NL);
1433 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001434 ga_append(&ga, NUL);
1435 retval = (char_u *)ga.ga_data;
1436 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001437#ifdef FEAT_FLOAT
1438 else if (convert && tv.v_type == VAR_FLOAT)
1439 {
1440 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1441 retval = vim_strsave(numbuf);
1442 }
1443#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001444 else
1445 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001446 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 return retval;
1450}
1451
1452/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001453 * Call eval_to_string() without using current local variables and using
1454 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string_safe(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 char_u *retval;
1463 void *save_funccalp;
1464
1465 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 if (use_sandbox)
1467 ++sandbox;
1468 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001470 if (use_sandbox)
1471 --sandbox;
1472 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 restore_funccal(save_funccalp);
1474 return retval;
1475}
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477/*
1478 * Top level evaluation function, returning a number.
1479 * Evaluates "expr" silently.
1480 * Returns -1 for an error.
1481 */
1482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
Bram Moolenaar33570922005-01-25 22:26:29 +00001485 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001487 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 ++emsg_off;
1490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 retval = -1;
1493 else
1494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 --emsg_off;
1499
1500 return retval;
1501}
1502
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503/*
1504 * Prepare v: variable "idx" to be used.
1505 * Save the current typeval in "save_tv".
1506 * When not used yet add the variable to the v: hashtable.
1507 */
1508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001510{
1511 *save_tv = vimvars[idx].vv_tv;
1512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1513 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1514}
1515
1516/*
1517 * Restore v: variable "idx" to typeval "save_tv".
1518 * When no longer defined, remove the variable from the v: hashtable.
1519 */
1520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522{
1523 hashitem_T *hi;
1524
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525 vimvars[idx].vv_tv = *save_tv;
1526 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1527 {
1528 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1529 if (HASHITEM_EMPTY(hi))
1530 EMSG2(_(e_intern2), "restore_vimvar()");
1531 else
1532 hash_remove(&vimvarht, hi);
1533 }
1534}
1535
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001536#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537/*
1538 * Evaluate an expression to a list with suggestions.
1539 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001540 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541 */
1542 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544{
1545 typval_T save_val;
1546 typval_T rettv;
1547 list_T *list = NULL;
1548 char_u *p = skipwhite(expr);
1549
1550 /* Set "v:val" to the bad word. */
1551 prepare_vimvar(VV_VAL, &save_val);
1552 vimvars[VV_VAL].vv_type = VAR_STRING;
1553 vimvars[VV_VAL].vv_str = badword;
1554 if (p_verbose == 0)
1555 ++emsg_off;
1556
1557 if (eval1(&p, &rettv, TRUE) == OK)
1558 {
1559 if (rettv.v_type != VAR_LIST)
1560 clear_tv(&rettv);
1561 else
1562 list = rettv.vval.v_list;
1563 }
1564
1565 if (p_verbose == 0)
1566 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 restore_vimvar(VV_VAL, &save_val);
1568
1569 return list;
1570}
1571
1572/*
1573 * "list" is supposed to contain two items: a word and a number. Return the
1574 * word in "pp" and the number as the return value.
1575 * Return -1 if anything isn't right.
1576 * Used to get the good word and score from the eval_spell_expr() result.
1577 */
1578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 listitem_T *li;
1582
1583 li = list->lv_first;
1584 if (li == NULL)
1585 return -1;
1586 *pp = get_tv_string(&li->li_tv);
1587
1588 li = li->li_next;
1589 if (li == NULL)
1590 return -1;
1591 return get_tv_number(&li->li_tv);
1592}
1593#endif
1594
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001596 * Top level evaluation function.
1597 * Returns an allocated typval_T with the result.
1598 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599 */
1600 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602{
1603 typval_T *tv;
1604
1605 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 {
1608 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 }
1611
1612 return tv;
1613}
1614
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 * Uses argv[argc] for the function arguments. Only Number and String
1619 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001622 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623call_vim_function(
1624 char_u *func,
1625 int argc,
1626 char_u **argv,
1627 int safe, /* use the sandbox */
1628 int str_arg_only, /* all arguments are strings */
1629 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 long n;
1633 int len;
1634 int i;
1635 int doesrange;
1636 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001639 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 for (i = 0; i < argc; i++)
1644 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 /* Pass a NULL or empty argument as an empty string */
1646 if (argv[i] == NULL || *argv[i] == NUL)
1647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001648 argvars[i].v_type = VAR_STRING;
1649 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001650 continue;
1651 }
1652
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001653 if (str_arg_only)
1654 len = 0;
1655 else
1656 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001657 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (len != 0 && len == (int)STRLEN(argv[i]))
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_NUMBER;
1661 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001665 argvars[i].v_type = VAR_STRING;
1666 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 }
1669
1670 if (safe)
1671 {
1672 save_funccalp = save_funccal();
1673 ++sandbox;
1674 }
1675
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1677 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001679 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (safe)
1681 {
1682 --sandbox;
1683 restore_funccal(save_funccalp);
1684 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 vim_free(argvars);
1686
1687 if (ret == FAIL)
1688 clear_tv(rettv);
1689
1690 return ret;
1691}
1692
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001693/*
1694 * Call vimL function "func" and return the result as a number.
1695 * Returns -1 when calling the function fails.
1696 * Uses argv[argc] for the function arguments.
1697 */
1698 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699call_func_retnr(
1700 char_u *func,
1701 int argc,
1702 char_u **argv,
1703 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001704{
1705 typval_T rettv;
1706 long retval;
1707
1708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1710 return -1;
1711
1712 retval = get_tv_number_chk(&rettv, NULL);
1713 clear_tv(&rettv);
1714 return retval;
1715}
1716
1717#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1718 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1719
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001722 * Call vimL function "func" and return the result as a string.
1723 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 * Uses argv[argc] for the function arguments.
1725 */
1726 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727call_func_retstr(
1728 char_u *func,
1729 int argc,
1730 char_u **argv,
1731 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732{
1733 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001734 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001736 /* All arguments are passed as strings, no conversion to number. */
1737 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 return NULL;
1739
1740 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 return retval;
1743}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001744# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001746/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001747 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001749 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752call_func_retlist(
1753 char_u *func,
1754 int argc,
1755 char_u **argv,
1756 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757{
1758 typval_T rettv;
1759
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762 return NULL;
1763
1764 if (rettv.v_type != VAR_LIST)
1765 {
1766 clear_tv(&rettv);
1767 return NULL;
1768 }
1769
1770 return rettv.vval.v_list;
1771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
1773
1774/*
1775 * Save the current function call pointer, and set it to NULL.
1776 * Used when executing autocommands and for ":source".
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 current_funccal = NULL;
1784 return (void *)fc;
1785}
1786
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790 funccall_T *fc = (funccall_T *)vfc;
1791
1792 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
1794
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795#if defined(FEAT_PROFILE) || defined(PROTO)
1796/*
1797 * Prepare profiling for entering a child or something else that is not
1798 * counted for the script/function itself.
1799 * Should always be called in pair with prof_child_exit().
1800 */
1801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802prof_child_enter(
1803 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001804{
1805 funccall_T *fc = current_funccal;
1806
1807 if (fc != NULL && fc->func->uf_profiling)
1808 profile_start(&fc->prof_child);
1809 script_prof_save(tm);
1810}
1811
1812/*
1813 * Take care of time spent in a child.
1814 * Should always be called after prof_child_enter().
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817prof_child_exit(
1818 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819{
1820 funccall_T *fc = current_funccal;
1821
1822 if (fc != NULL && fc->func->uf_profiling)
1823 {
1824 profile_end(&fc->prof_child);
1825 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1826 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1827 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1828 }
1829 script_prof_restore(tm);
1830}
1831#endif
1832
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835/*
1836 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1837 * it in "*cp". Doesn't give error messages.
1838 */
1839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 int retval;
1844 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001845 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1846 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001849 if (use_sandbox)
1850 ++sandbox;
1851 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 if (tv.v_type == VAR_NUMBER)
1859 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001860 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 retval = 0;
1862 else
1863 {
1864 /* If the result is a string, check if there is a non-digit before
1865 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (!VIM_ISDIGIT(*s) && *s != '-')
1868 *cp = *s++;
1869 retval = atol((char *)s);
1870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001874 if (use_sandbox)
1875 --sandbox;
1876 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return retval;
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * ":let" list all variable values
1884 * ":let var1 var2" list variable values
1885 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 * ":let var += expr" assignment command.
1887 * ":let var -= expr" assignment command.
1888 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 int var_count = 0;
1899 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 argend = skip_var_list(arg, &var_count, &semicolon);
1905 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001907 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1908 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001909 expr = skipwhite(argend);
1910 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1911 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001913 /*
1914 * ":let" without "=": list variables
1915 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 if (*arg == '[')
1917 EMSG(_(e_invarg));
1918 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001920 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 list_glob_vars(&first);
1925 list_buf_vars(&first);
1926 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001927#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001929#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_script_vars(&first);
1931 list_func_vars(&first);
1932 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 eap->nextcmd = check_nextcmd(arg);
1935 }
1936 else
1937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 op[0] = '=';
1939 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001942 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1943 op[0] = *expr; /* +=, -= or .= */
1944 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 else
1947 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 {
1954 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 --emsg_skip;
1957 }
1958 else if (i != FAIL)
1959 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964 }
1965}
1966
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967/*
1968 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1969 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 * When "nextchars" is not NULL it points to a string with characters that
1971 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1972 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 * Returns OK or FAIL;
1974 */
1975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976ex_let_vars(
1977 char_u *arg_start,
1978 typval_T *tv,
1979 int copy, /* copy values from "tv", don't move */
1980 int semicolon, /* from skip_var_list() */
1981 int var_count, /* from skip_var_list() */
1982 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983{
1984 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 listitem_T *item;
1988 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989
1990 if (*arg != '[')
1991 {
1992 /*
1993 * ":let var = expr" or ":for var in list"
1994 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 return OK;
1998 }
1999
2000 /*
2001 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2002 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002003 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 {
2005 EMSG(_(e_listreq));
2006 return FAIL;
2007 }
2008
2009 i = list_len(l);
2010 if (semicolon == 0 && var_count < i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015 if (var_count - semicolon > i)
2016 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002017 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 return FAIL;
2019 }
2020
2021 item = l->lv_first;
2022 while (*arg != ']')
2023 {
2024 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 item = item->li_next;
2027 if (arg == NULL)
2028 return FAIL;
2029
2030 arg = skipwhite(arg);
2031 if (*arg == ';')
2032 {
2033 /* Put the rest of the list (may be empty) in the var after ';'.
2034 * Create a new list for this. */
2035 l = list_alloc();
2036 if (l == NULL)
2037 return FAIL;
2038 while (item != NULL)
2039 {
2040 list_append_tv(l, &item->li_tv);
2041 item = item->li_next;
2042 }
2043
2044 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002045 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 ltv.vval.v_list = l;
2047 l->lv_refcount = 1;
2048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2050 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 clear_tv(&ltv);
2052 if (arg == NULL)
2053 return FAIL;
2054 break;
2055 }
2056 else if (*arg != ',' && *arg != ']')
2057 {
2058 EMSG2(_(e_intern2), "ex_let_vars()");
2059 return FAIL;
2060 }
2061 }
2062
2063 return OK;
2064}
2065
2066/*
2067 * Skip over assignable variable "var" or list of variables "[var, var]".
2068 * Used for ":let varvar = expr" and ":for varvar in expr".
2069 * For "[var, var]" increment "*var_count" for each variable.
2070 * for "[var, var; var]" set "semicolon".
2071 * Return NULL for an error.
2072 */
2073 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002074skip_var_list(
2075 char_u *arg,
2076 int *var_count,
2077 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078{
2079 char_u *p, *s;
2080
2081 if (*arg == '[')
2082 {
2083 /* "[var, var]": find the matching ']'. */
2084 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002085 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 {
2087 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2088 s = skip_var_one(p);
2089 if (s == p)
2090 {
2091 EMSG2(_(e_invarg2), p);
2092 return NULL;
2093 }
2094 ++*var_count;
2095
2096 p = skipwhite(s);
2097 if (*p == ']')
2098 break;
2099 else if (*p == ';')
2100 {
2101 if (*semicolon == 1)
2102 {
2103 EMSG(_("Double ; in list of variables"));
2104 return NULL;
2105 }
2106 *semicolon = 1;
2107 }
2108 else if (*p != ',')
2109 {
2110 EMSG2(_(e_invarg2), p);
2111 return NULL;
2112 }
2113 }
2114 return p + 1;
2115 }
2116 else
2117 return skip_var_one(arg);
2118}
2119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002121 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002127 if (*arg == '@' && arg[1] != NUL)
2128 return arg + 2;
2129 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2130 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131}
2132
Bram Moolenaara7043832005-01-21 11:56:39 +00002133/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 * List variables for hashtab "ht" with prefix "prefix".
2135 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002138list_hashtable_vars(
2139 hashtab_T *ht,
2140 char_u *prefix,
2141 int empty,
2142 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar33570922005-01-25 22:26:29 +00002144 hashitem_T *hi;
2145 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 int todo;
2147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2150 {
2151 if (!HASHITEM_EMPTY(hi))
2152 {
2153 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002154 di = HI2DI(hi);
2155 if (empty || di->di_tv.v_type != VAR_STRING
2156 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158 }
2159 }
2160}
2161
2162/*
2163 * List global variables.
2164 */
2165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
2171/*
2172 * List buffer variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002176{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177 char_u numbuf[NUMBUFLEN];
2178
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181
2182 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2184 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
2187/*
2188 * List window variables.
2189 */
2190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002192{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002193 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197#ifdef FEAT_WINDOWS
2198/*
2199 * List tab page variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002204 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206}
2207#endif
2208
Bram Moolenaara7043832005-01-21 11:56:39 +00002209/*
2210 * List Vim variables.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216}
2217
2218/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219 * List script-local variables, if there is a script.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223{
2224 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2226 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227}
2228
2229/*
2230 * List function variables, if there is a function.
2231 */
2232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234{
2235 if (current_funccal != NULL)
2236 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238}
2239
2240/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 * List variables in "arg".
2242 */
2243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245{
2246 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 char_u *name_start;
2250 char_u *arg_subsc;
2251 char_u *tofree;
2252 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
2254 while (!ends_excmd(*arg) && !got_int)
2255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2260 {
2261 emsg_severe = TRUE;
2262 EMSG(_(e_trailing));
2263 break;
2264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* get_name_len() takes care of expanding curly braces */
2269 name_start = name = arg;
2270 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2271 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 /* This is mainly to keep test 49 working: when expanding
2274 * curly braces fails overrule the exception error message. */
2275 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 emsg_severe = TRUE;
2278 EMSG2(_(e_invarg2), arg);
2279 break;
2280 }
2281 error = TRUE;
2282 }
2283 else
2284 {
2285 if (tofree != NULL)
2286 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002287 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 else
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 /* handle d.key, l[idx], f(expr) */
2292 arg_subsc = arg;
2293 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002301 case 'g': list_glob_vars(first); break;
2302 case 'b': list_buf_vars(first); break;
2303 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002306#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'v': list_vim_vars(first); break;
2308 case 's': list_script_vars(first); break;
2309 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 default:
2311 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
2314 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 {
2316 char_u numbuf[NUMBUFLEN];
2317 char_u *tf;
2318 int c;
2319 char_u *s;
2320
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002321 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 c = *arg;
2323 *arg = NUL;
2324 list_one_var_a((char_u *)"",
2325 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002326 tv.v_type,
2327 s == NULL ? (char_u *)"" : s,
2328 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 *arg = c;
2330 vim_free(tf);
2331 }
2332 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342
2343 return arg;
2344}
2345
2346/*
2347 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2348 * Returns a pointer to the char just after the var name.
2349 * Returns NULL if there is an error.
2350 */
2351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002352ex_let_one(
2353 char_u *arg, /* points to variable name */
2354 typval_T *tv, /* value to assign to variable */
2355 int copy, /* copy value from "tv" */
2356 char_u *endchars, /* valid chars after variable name or NULL */
2357 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358{
2359 int c1;
2360 char_u *name;
2361 char_u *p;
2362 char_u *arg_end = NULL;
2363 int len;
2364 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366
2367 /*
2368 * ":let $VAR = expr": Set environment variable.
2369 */
2370 if (*arg == '$')
2371 {
2372 /* Find the end of the name. */
2373 ++arg;
2374 name = arg;
2375 len = get_env_len(&arg);
2376 if (len == 0)
2377 EMSG2(_(e_invarg2), name - 1);
2378 else
2379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 if (op != NULL && (*op == '+' || *op == '-'))
2381 EMSG2(_(e_letwrong), op);
2382 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002385 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 {
2387 c1 = name[len];
2388 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 p = get_tv_string_chk(tv);
2390 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 int mustfree = FALSE;
2393 char_u *s = vim_getenv(name, &mustfree);
2394
2395 if (s != NULL)
2396 {
2397 p = tofree = concat_str(s, p);
2398 if (mustfree)
2399 vim_free(s);
2400 }
2401 }
2402 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 if (STRICMP(name, "HOME") == 0)
2406 init_homedir();
2407 else if (didset_vim && STRICMP(name, "VIM") == 0)
2408 didset_vim = FALSE;
2409 else if (didset_vimruntime
2410 && STRICMP(name, "VIMRUNTIME") == 0)
2411 didset_vimruntime = FALSE;
2412 arg_end = arg;
2413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 }
2417 }
2418 }
2419
2420 /*
2421 * ":let &option = expr": Set option value.
2422 * ":let &l:option = expr": Set local option value.
2423 * ":let &g:option = expr": Set global option value.
2424 */
2425 else if (*arg == '&')
2426 {
2427 /* Find the end of the name. */
2428 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 if (p == NULL || (endchars != NULL
2430 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 EMSG(_(e_letunexp));
2432 else
2433 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 long n;
2435 int opt_type;
2436 long numval;
2437 char_u *stringval = NULL;
2438 char_u *s;
2439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 c1 = *p;
2441 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442
2443 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 s = get_tv_string_chk(tv); /* != NULL if number or string */
2445 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
2447 opt_type = get_option_value(arg, &numval,
2448 &stringval, opt_flags);
2449 if ((opt_type == 1 && *op == '.')
2450 || (opt_type == 0 && *op != '.'))
2451 EMSG2(_(e_letwrong), op);
2452 else
2453 {
2454 if (opt_type == 1) /* number */
2455 {
2456 if (*op == '+')
2457 n = numval + n;
2458 else
2459 n = numval - n;
2460 }
2461 else if (opt_type == 0 && stringval != NULL) /* string */
2462 {
2463 s = concat_str(stringval, s);
2464 vim_free(stringval);
2465 stringval = s;
2466 }
2467 }
2468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 if (s != NULL)
2470 {
2471 set_option_value(arg, n, s, opt_flags);
2472 arg_end = p;
2473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
2477 }
2478
2479 /*
2480 * ":let @r = expr": Set register contents.
2481 */
2482 else if (*arg == '@')
2483 {
2484 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (op != NULL && (*op == '+' || *op == '-'))
2486 EMSG2(_(e_letwrong), op);
2487 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002488 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002492 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 char_u *s;
2494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 p = get_tv_string_chk(tv);
2496 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002498 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 if (s != NULL)
2500 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 vim_free(s);
2503 }
2504 }
2505 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002508 arg_end = arg + 1;
2509 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002510 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002518 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002522 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2526 EMSG(_(e_letunexp));
2527 else
2528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 arg_end = p;
2531 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 }
2535
2536 else
2537 EMSG2(_(e_invarg2), arg);
2538
2539 return arg_end;
2540}
2541
2542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2544 */
2545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547{
2548 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2549 {
2550 EMSG2(_(e_readonlyvar), arg);
2551 return TRUE;
2552 }
2553 return FALSE;
2554}
2555
2556/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Get an lval: variable, Dict item or List item that can be assigned a value
2558 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2559 * "name.key", "name.key[expr]" etc.
2560 * Indexing only works if "name" is an existing List or Dictionary.
2561 * "name" points to the start of the name.
2562 * If "rettv" is not NULL it points to the value to be assigned.
2563 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2564 * wrong; must end in space or cmd separator.
2565 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002566 * flags:
2567 * GLV_QUIET: do not give error messages
2568 * GLV_NO_AUTOLOAD: do not use script autoloading
2569 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002571 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 */
2574 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002575get_lval(
2576 char_u *name,
2577 typval_T *rettv,
2578 lval_T *lp,
2579 int unlet,
2580 int skip,
2581 int flags, /* GLV_ values */
2582 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 char_u *expr_start, *expr_end;
2586 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 dictitem_T *v;
2588 typval_T var1;
2589 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002595 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599
2600 if (skip)
2601 {
2602 /* When skipping just find the end of the name. */
2603 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002604 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 }
2606
2607 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002608 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (expr_start != NULL)
2610 {
2611 /* Don't expand the name when we already know there is an error. */
2612 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2613 && *p != '[' && *p != '.')
2614 {
2615 EMSG(_(e_trailing));
2616 return NULL;
2617 }
2618
2619 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2620 if (lp->ll_exp_name == NULL)
2621 {
2622 /* Report an invalid expression in braces, unless the
2623 * expression evaluation has been cancelled due to an
2624 * aborting error, an interrupt, or an exception. */
2625 if (!aborting() && !quiet)
2626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002627 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 EMSG2(_(e_invarg2), name);
2629 return NULL;
2630 }
2631 }
2632 lp->ll_name = lp->ll_exp_name;
2633 }
2634 else
2635 lp->ll_name = name;
2636
2637 /* Without [idx] or .key we are done. */
2638 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2639 return p;
2640
2641 cc = *p;
2642 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (v == NULL && !quiet)
2645 EMSG2(_(e_undefvar), lp->ll_name);
2646 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 if (v == NULL)
2648 return NULL;
2649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /*
2651 * Loop until no more [idx] or .key is following.
2652 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2657 && !(lp->ll_tv->v_type == VAR_DICT
2658 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
2661 EMSG(_("E689: Can only index a List or Dictionary"));
2662 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E708: [:] must come last"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 len = -1;
2672 if (*p == '.')
2673 {
2674 key = p + 1;
2675 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2676 ;
2677 if (len == 0)
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!quiet)
2680 EMSG(_(e_emptykey));
2681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = key + len;
2684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 else
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p == ':')
2690 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 empty1 = FALSE;
2694 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 if (get_tv_string_chk(&var1) == NULL)
2697 {
2698 /* not a number or string */
2699 clear_tv(&var1);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
2704 /* Optionally get the second index [ :expr]. */
2705 if (*p == ':')
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 if (!empty1)
2712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (rettv != NULL && (rettv->v_type != VAR_LIST
2716 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (!empty1)
2721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 p = skipwhite(p + 1);
2725 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 else
2728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (!empty1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (get_tv_string_chk(&var2) == NULL)
2737 {
2738 /* not a number or string */
2739 if (!empty1)
2740 clear_tv(&var1);
2741 clear_tv(&var2);
2742 return NULL;
2743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!quiet)
2753 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (!empty1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760
2761 /* Skip to past ']'. */
2762 ++p;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
2767 if (len == -1)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002770 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (*key == NUL)
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (!quiet)
2774 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 }
2778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 lp->ll_list = NULL;
2780 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 /* When assigning to a scope dictionary check that a function and
2784 * variable name is valid (only variable name unless it is l: or
2785 * g: dictionary). Disallow overwriting a builtin function. */
2786 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 int prevval;
2789 int wrong;
2790
2791 if (len != -1)
2792 {
2793 prevval = key[len];
2794 key[len] = NUL;
2795 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002796 else
2797 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2799 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 || !valid_varname(key);
2802 if (len != -1)
2803 key[len] = prevval;
2804 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 return NULL;
2806 }
2807
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* Can't add "v:" variable. */
2811 if (lp->ll_dict == &vimvardict)
2812 {
2813 EMSG2(_(e_illvar), name);
2814 return NULL;
2815 }
2816
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002817 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 p = NULL;
2834 break;
2835 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002837 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (len == -1)
2841 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844 else
2845 {
2846 /*
2847 * Get the number and item for the only or first index of the List.
2848 */
2849 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 else
2852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002853 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 clear_tv(&var1);
2855 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_list = lp->ll_tv->vval.v_list;
2858 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2859 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002861 if (lp->ll_n1 < 0)
2862 {
2863 lp->ll_n1 = 0;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 }
2866 }
2867 if (lp->ll_li == NULL)
2868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 }
2875
2876 /*
2877 * May need to find the item or absolute index for the second
2878 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 * When no index given: "lp->ll_empty2" is TRUE.
2880 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002884 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 {
2891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 }
2897
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2899 if (lp->ll_n1 < 0)
2900 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2901 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002902 {
2903 if (!quiet)
2904 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 return p;
2914}
2915
2916/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 */
2919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 vim_free(lp->ll_exp_name);
2923 vim_free(lp->ll_newkey);
2924}
2925
2926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 */
2931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932set_var_lval(
2933 lval_T *lp,
2934 char_u *endp,
2935 typval_T *rettv,
2936 int copy,
2937 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938{
2939 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 listitem_T *ri;
2941 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942
2943 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 cc = *endp;
2948 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 if (op != NULL && *op != '=')
2950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002955 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002956 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002958 if ((di == NULL
2959 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2960 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2961 FALSE)))
2962 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 set_var(lp->ll_name, &tv, FALSE);
2964 clear_tv(&tv);
2965 }
2966 }
2967 else
2968 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 else if (tv_check_lock(lp->ll_newkey == NULL
2973 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002974 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 else if (lp->ll_range)
2977 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 listitem_T *ll_li = lp->ll_li;
2979 int ll_n1 = lp->ll_n1;
2980
2981 /*
2982 * Check whether any of the list items is locked
2983 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002984 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002986 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002987 return;
2988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2990 break;
2991 ll_li = ll_li->li_next;
2992 ++ll_n1;
2993 }
2994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /*
2996 * Assign the List values to the list items.
2997 */
2998 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 if (op != NULL && *op != '=')
3001 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3002 else
3003 {
3004 clear_tv(&lp->ll_li->li_tv);
3005 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 ri = ri->li_next;
3008 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3009 break;
3010 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003013 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 ri = NULL;
3016 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 lp->ll_li = lp->ll_li->li_next;
3020 ++lp->ll_n1;
3021 }
3022 if (ri != NULL)
3023 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 else if (lp->ll_empty2
3025 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 : lp->ll_n1 != lp->ll_n2)
3027 EMSG(_("E711: List value has not enough items"));
3028 }
3029 else
3030 {
3031 /*
3032 * Assign to a List or Dictionary item.
3033 */
3034 if (lp->ll_newkey != NULL)
3035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (op != NULL && *op != '=')
3037 {
3038 EMSG2(_(e_letwrong), op);
3039 return;
3040 }
3041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 if (di == NULL)
3045 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3047 {
3048 vim_free(di);
3049 return;
3050 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else if (op != NULL && *op != '=')
3054 {
3055 tv_op(lp->ll_tv, rettv, op);
3056 return;
3057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 /*
3062 * Assign the value to the variable or list item.
3063 */
3064 if (copy)
3065 copy_tv(rettv, lp->ll_tv);
3066 else
3067 {
3068 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003069 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003070 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 }
3072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073}
3074
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003075/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3077 * Returns OK or FAIL.
3078 */
3079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081{
3082 long n;
3083 char_u numbuf[NUMBUFLEN];
3084 char_u *s;
3085
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003086 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3087 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3088 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 {
3090 switch (tv1->v_type)
3091 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003092 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 case VAR_DICT:
3094 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003095 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003096 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003097 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003098 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 break;
3100
3101 case VAR_LIST:
3102 if (*op != '+' || tv2->v_type != VAR_LIST)
3103 break;
3104 /* List += List */
3105 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3106 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3107 return OK;
3108
3109 case VAR_NUMBER:
3110 case VAR_STRING:
3111 if (tv2->v_type == VAR_LIST)
3112 break;
3113 if (*op == '+' || *op == '-')
3114 {
3115 /* nr += nr or nr -= nr*/
3116 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
3118 if (tv2->v_type == VAR_FLOAT)
3119 {
3120 float_T f = n;
3121
3122 if (*op == '+')
3123 f += tv2->vval.v_float;
3124 else
3125 f -= tv2->vval.v_float;
3126 clear_tv(tv1);
3127 tv1->v_type = VAR_FLOAT;
3128 tv1->vval.v_float = f;
3129 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131#endif
3132 {
3133 if (*op == '+')
3134 n += get_tv_number(tv2);
3135 else
3136 n -= get_tv_number(tv2);
3137 clear_tv(tv1);
3138 tv1->v_type = VAR_NUMBER;
3139 tv1->vval.v_number = n;
3140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 }
3142 else
3143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144 if (tv2->v_type == VAR_FLOAT)
3145 break;
3146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 /* str .= str */
3148 s = get_tv_string(tv1);
3149 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_STRING;
3152 tv1->vval.v_string = s;
3153 }
3154 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003157#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158 {
3159 float_T f;
3160
3161 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3162 && tv2->v_type != VAR_NUMBER
3163 && tv2->v_type != VAR_STRING))
3164 break;
3165 if (tv2->v_type == VAR_FLOAT)
3166 f = tv2->vval.v_float;
3167 else
3168 f = get_tv_number(tv2);
3169 if (*op == '+')
3170 tv1->vval.v_float += f;
3171 else
3172 tv1->vval.v_float -= f;
3173 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003175 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003176 }
3177 }
3178
3179 EMSG2(_(e_letwrong), op);
3180 return FAIL;
3181}
3182
3183/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 * Add a watcher to a list.
3185 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003187list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
3189 lw->lw_next = l->lv_watch;
3190 l->lv_watch = lw;
3191}
3192
3193/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003194 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 * No warning when it isn't found...
3196 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
3202 lwp = &l->lv_watch;
3203 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3204 {
3205 if (lw == lwrem)
3206 {
3207 *lwp = lw->lw_next;
3208 break;
3209 }
3210 lwp = &lw->lw_next;
3211 }
3212}
3213
3214/*
3215 * Just before removing an item from a list: advance watchers to the next
3216 * item.
3217 */
3218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222
3223 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3224 if (lw->lw_item == item)
3225 lw->lw_item = item->li_next;
3226}
3227
3228/*
3229 * Evaluate the expression used in a ":for var in expr" command.
3230 * "arg" points to "var".
3231 * Set "*errp" to TRUE for an error, FALSE otherwise;
3232 * Return a pointer that holds the info. Null when there is an error.
3233 */
3234 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235eval_for_line(
3236 char_u *arg,
3237 int *errp,
3238 char_u **nextcmdp,
3239 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 typval_T tv;
3244 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 *errp = TRUE; /* default: there is an error */
3247
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 if (fi == NULL)
3250 return NULL;
3251
3252 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3253 if (expr == NULL)
3254 return fi;
3255
3256 expr = skipwhite(expr);
3257 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3258 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003259 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 return fi;
3261 }
3262
3263 if (skip)
3264 ++emsg_skip;
3265 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3266 {
3267 *errp = FALSE;
3268 if (!skip)
3269 {
3270 l = tv.vval.v_list;
3271 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 clear_tv(&tv);
3275 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 else
3277 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003278 /* No need to increment the refcount, it's already set for the
3279 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 fi->fi_list = l;
3281 list_add_watch(l, &fi->fi_lw);
3282 fi->fi_lw.lw_item = l->lv_first;
3283 }
3284 }
3285 }
3286 if (skip)
3287 --emsg_skip;
3288
3289 return fi;
3290}
3291
3292/*
3293 * Use the first item in a ":for" list. Advance to the next.
3294 * Assign the values to the variable (list). "arg" points to the first one.
3295 * Return TRUE when a valid item was found, FALSE when at end of list or
3296 * something wrong.
3297 */
3298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003301 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304
3305 item = fi->fi_lw.lw_item;
3306 if (item == NULL)
3307 result = FALSE;
3308 else
3309 {
3310 fi->fi_lw.lw_item = item->li_next;
3311 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3312 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3313 }
3314 return result;
3315}
3316
3317/*
3318 * Free the structure used to store info used by ":for".
3319 */
3320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322{
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003325 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003328 list_unref(fi->fi_list);
3329 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 vim_free(fi);
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3334
3335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336set_context_for_expression(
3337 expand_T *xp,
3338 char_u *arg,
3339 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 int got_eq = FALSE;
3342 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 if (cmdidx == CMD_let)
3346 {
3347 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 if (vim_iswhite(*p))
3356 break;
3357 }
3358 return;
3359 }
3360 }
3361 else
3362 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3363 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 while ((xp->xp_pattern = vim_strpbrk(arg,
3365 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3366 {
3367 c = *xp->xp_pattern;
3368 if (c == '&')
3369 {
3370 c = xp->xp_pattern[1];
3371 if (c == '&')
3372 {
3373 ++xp->xp_pattern;
3374 xp->xp_context = cmdidx != CMD_let || got_eq
3375 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3376 }
3377 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003380 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3381 xp->xp_pattern += 2;
3382
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else if (c == '$')
3386 {
3387 /* environment variable */
3388 xp->xp_context = EXPAND_ENV_VARS;
3389 }
3390 else if (c == '=')
3391 {
3392 got_eq = TRUE;
3393 xp->xp_context = EXPAND_EXPRESSION;
3394 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 && xp->xp_context == EXPAND_FUNCTIONS
3397 && vim_strchr(xp->xp_pattern, '(') == NULL)
3398 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003399 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401 }
3402 else if (cmdidx != CMD_let || got_eq)
3403 {
3404 if (c == '"') /* string */
3405 {
3406 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3407 if (c == '\\' && xp->xp_pattern[1] != NUL)
3408 ++xp->xp_pattern;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '\'') /* literal string */
3412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003413 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3415 /* skip */ ;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '|')
3419 {
3420 if (xp->xp_pattern[1] == '|')
3421 {
3422 ++xp->xp_pattern;
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
3426 xp->xp_context = EXPAND_COMMANDS;
3427 }
3428 else
3429 xp->xp_context = EXPAND_EXPRESSION;
3430 }
3431 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003432 /* Doesn't look like something valid, expand as an expression
3433 * anyway. */
3434 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 arg = xp->xp_pattern;
3436 if (*arg != NUL)
3437 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3438 /* skip */ ;
3439 }
3440 xp->xp_pattern = arg;
3441}
3442
3443#endif /* FEAT_CMDL_COMPL */
3444
3445/*
3446 * ":1,25call func(arg1, arg2)" function call.
3447 */
3448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *arg = eap->arg;
3452 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003454 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 linenr_T lnum;
3458 int doesrange;
3459 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 funcdict_T fudi;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003461 partial_T *partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003463 if (eap->skip)
3464 {
3465 /* trans_function_name() doesn't work well when skipping, use eval0()
3466 * instead to skip to any following command, e.g. for:
3467 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003468 ++emsg_skip;
3469 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3470 clear_tv(&rettv);
3471 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003472 return;
3473 }
3474
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003475 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003476 if (fudi.fd_newkey != NULL)
3477 {
3478 /* Still need to give an error message for missing key. */
3479 EMSG2(_(e_dictkey), fudi.fd_newkey);
3480 vim_free(fudi.fd_newkey);
3481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003482 if (tofree == NULL)
3483 return;
3484
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003485 /* Increase refcount on dictionary, it could get deleted when evaluating
3486 * the arguments. */
3487 if (fudi.fd_dict != NULL)
3488 ++fudi.fd_dict->dv_refcount;
3489
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003490 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003491 len = (int)STRLEN(tofree);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003492 name = deref_func_name(tofree, &len, &partial, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar532c7802005-01-27 14:44:31 +00003494 /* Skip white space to allow ":call func ()". Not good, but required for
3495 * backward compatibility. */
3496 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 if (*startarg != '(')
3500 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003501 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 goto end;
3503 }
3504
3505 /*
3506 * When skipping, evaluate the function once, to find the end of the
3507 * arguments.
3508 * When the function takes a range, this is discovered after the first
3509 * call, and the loop is broken.
3510 */
3511 if (eap->skip)
3512 {
3513 ++emsg_skip;
3514 lnum = eap->line2; /* do it once, also with an invalid range */
3515 }
3516 else
3517 lnum = eap->line1;
3518 for ( ; lnum <= eap->line2; ++lnum)
3519 {
3520 if (!eap->skip && eap->addr_count > 0)
3521 {
3522 curwin->w_cursor.lnum = lnum;
3523 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003524#ifdef FEAT_VIRTUALEDIT
3525 curwin->w_cursor.coladd = 0;
3526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003529 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003530 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003531 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 failed = TRUE;
3534 break;
3535 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003536
3537 /* Handle a function returning a Funcref, Dictionary or List. */
3538 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3539 {
3540 failed = TRUE;
3541 break;
3542 }
3543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (doesrange || eap->skip)
3546 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003551 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (aborting())
3553 break;
3554 }
3555 if (eap->skip)
3556 --emsg_skip;
3557
3558 if (!failed)
3559 {
3560 /* Check for trailing illegal characters and a following command. */
3561 if (!ends_excmd(*arg))
3562 {
3563 emsg_severe = TRUE;
3564 EMSG(_(e_trailing));
3565 }
3566 else
3567 eap->nextcmd = check_nextcmd(arg);
3568 }
3569
3570end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003571 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003572 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573}
3574
3575/*
3576 * ":unlet[!] var1 ... " command.
3577 */
3578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581 ex_unletlock(eap, eap->arg, 0);
3582}
3583
3584/*
3585 * ":lockvar" and ":unlockvar" commands
3586 */
3587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003591 int deep = 2;
3592
3593 if (eap->forceit)
3594 deep = -1;
3595 else if (vim_isdigit(*arg))
3596 {
3597 deep = getdigits(&arg);
3598 arg = skipwhite(arg);
3599 }
3600
3601 ex_unletlock(eap, arg, deep);
3602}
3603
3604/*
3605 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3606 */
3607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003608ex_unletlock(
3609 exarg_T *eap,
3610 char_u *argstart,
3611 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612{
3613 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 do
3619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003621 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003622 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (lv.ll_name == NULL)
3624 error = TRUE; /* error but continue parsing */
3625 if (name_end == NULL || (!vim_iswhite(*name_end)
3626 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 if (name_end != NULL)
3629 {
3630 emsg_severe = TRUE;
3631 EMSG(_(e_trailing));
3632 }
3633 if (!(eap->skip || error))
3634 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 break;
3636 }
3637
3638 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639 {
3640 if (eap->cmdidx == CMD_unlet)
3641 {
3642 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3643 error = TRUE;
3644 }
3645 else
3646 {
3647 if (do_lock_var(&lv, name_end, deep,
3648 eap->cmdidx == CMD_lockvar) == FAIL)
3649 error = TRUE;
3650 }
3651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 if (!eap->skip)
3654 clear_lval(&lv);
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 arg = skipwhite(name_end);
3657 } while (!ends_excmd(*arg));
3658
3659 eap->nextcmd = check_nextcmd(arg);
3660}
3661
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003663do_unlet_var(
3664 lval_T *lp,
3665 char_u *name_end,
3666 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 int ret = OK;
3669 int cc;
3670
3671 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 cc = *name_end;
3674 *name_end = NUL;
3675
3676 /* Normal name or expanded name. */
3677 if (check_changedtick(lp->ll_name))
3678 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003682 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003683 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003685 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003686 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003687 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 else if (lp->ll_range)
3689 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003690 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003692 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003693
3694 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3695 {
3696 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003697 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003698 return FAIL;
3699 ll_li = li;
3700 ++ll_n1;
3701 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702
3703 /* Delete a range of List items. */
3704 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3705 {
3706 li = lp->ll_li->li_next;
3707 listitem_remove(lp->ll_list, lp->ll_li);
3708 lp->ll_li = li;
3709 ++lp->ll_n1;
3710 }
3711 }
3712 else
3713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 /* unlet a List item. */
3716 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003719 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 }
3721
3722 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003723}
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725/*
3726 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 */
3729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003730do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
Bram Moolenaar33570922005-01-25 22:26:29 +00003732 hashtab_T *ht;
3733 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003734 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003735 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003736 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 ht = find_var_ht(name, &varname);
3739 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003741 if (ht == &globvarht)
3742 d = &globvardict;
3743 else if (current_funccal != NULL
3744 && ht == &current_funccal->l_vars.dv_hashtab)
3745 d = &current_funccal->l_vars;
3746 else if (ht == &compat_hashtab)
3747 d = &vimvardict;
3748 else
3749 {
3750 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3751 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3752 }
3753 if (d == NULL)
3754 {
3755 EMSG2(_(e_intern2), "do_unlet()");
3756 return FAIL;
3757 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 hi = hash_find(ht, varname);
3759 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003760 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003761 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003762 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003763 || var_check_ro(di->di_flags, name, FALSE)
3764 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003765 return FAIL;
3766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003767 delete_var(ht, hi);
3768 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771 if (forceit)
3772 return OK;
3773 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 return FAIL;
3775}
3776
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003777/*
3778 * Lock or unlock variable indicated by "lp".
3779 * "deep" is the levels to go (-1 for unlimited);
3780 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3781 */
3782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003783do_lock_var(
3784 lval_T *lp,
3785 char_u *name_end,
3786 int deep,
3787 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003788{
3789 int ret = OK;
3790 int cc;
3791 dictitem_T *di;
3792
3793 if (deep == 0) /* nothing to do */
3794 return OK;
3795
3796 if (lp->ll_tv == NULL)
3797 {
3798 cc = *name_end;
3799 *name_end = NUL;
3800
3801 /* Normal name or expanded name. */
3802 if (check_changedtick(lp->ll_name))
3803 ret = FAIL;
3804 else
3805 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003806 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003807 if (di == NULL)
3808 ret = FAIL;
3809 else
3810 {
3811 if (lock)
3812 di->di_flags |= DI_FLAGS_LOCK;
3813 else
3814 di->di_flags &= ~DI_FLAGS_LOCK;
3815 item_lock(&di->di_tv, deep, lock);
3816 }
3817 }
3818 *name_end = cc;
3819 }
3820 else if (lp->ll_range)
3821 {
3822 listitem_T *li = lp->ll_li;
3823
3824 /* (un)lock a range of List items. */
3825 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3826 {
3827 item_lock(&li->li_tv, deep, lock);
3828 li = li->li_next;
3829 ++lp->ll_n1;
3830 }
3831 }
3832 else if (lp->ll_list != NULL)
3833 /* (un)lock a List item. */
3834 item_lock(&lp->ll_li->li_tv, deep, lock);
3835 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003836 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837 item_lock(&lp->ll_di->di_tv, deep, lock);
3838
3839 return ret;
3840}
3841
3842/*
3843 * Lock or unlock an item. "deep" is nr of levels to go.
3844 */
3845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003846item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003847{
3848 static int recurse = 0;
3849 list_T *l;
3850 listitem_T *li;
3851 dict_T *d;
3852 hashitem_T *hi;
3853 int todo;
3854
3855 if (recurse >= DICT_MAXNEST)
3856 {
3857 EMSG(_("E743: variable nested too deep for (un)lock"));
3858 return;
3859 }
3860 if (deep == 0)
3861 return;
3862 ++recurse;
3863
3864 /* lock/unlock the item itself */
3865 if (lock)
3866 tv->v_lock |= VAR_LOCKED;
3867 else
3868 tv->v_lock &= ~VAR_LOCKED;
3869
3870 switch (tv->v_type)
3871 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003872 case VAR_UNKNOWN:
3873 case VAR_NUMBER:
3874 case VAR_STRING:
3875 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003876 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003877 case VAR_FLOAT:
3878 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003879 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003880 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003881 break;
3882
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003883 case VAR_LIST:
3884 if ((l = tv->vval.v_list) != NULL)
3885 {
3886 if (lock)
3887 l->lv_lock |= VAR_LOCKED;
3888 else
3889 l->lv_lock &= ~VAR_LOCKED;
3890 if (deep < 0 || deep > 1)
3891 /* recursive: lock/unlock the items the List contains */
3892 for (li = l->lv_first; li != NULL; li = li->li_next)
3893 item_lock(&li->li_tv, deep - 1, lock);
3894 }
3895 break;
3896 case VAR_DICT:
3897 if ((d = tv->vval.v_dict) != NULL)
3898 {
3899 if (lock)
3900 d->dv_lock |= VAR_LOCKED;
3901 else
3902 d->dv_lock &= ~VAR_LOCKED;
3903 if (deep < 0 || deep > 1)
3904 {
3905 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003906 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003907 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3908 {
3909 if (!HASHITEM_EMPTY(hi))
3910 {
3911 --todo;
3912 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3913 }
3914 }
3915 }
3916 }
3917 }
3918 --recurse;
3919}
3920
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003922 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3923 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924 */
3925 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003926tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003927{
3928 return (tv->v_lock & VAR_LOCKED)
3929 || (tv->v_type == VAR_LIST
3930 && tv->vval.v_list != NULL
3931 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3932 || (tv->v_type == VAR_DICT
3933 && tv->vval.v_dict != NULL
3934 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3935}
3936
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3938/*
3939 * Delete all "menutrans_" variables.
3940 */
3941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003942del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003945 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003948 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 {
3951 if (!HASHITEM_EMPTY(hi))
3952 {
3953 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3955 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003956 }
3957 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959}
3960#endif
3961
3962#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3963
3964/*
3965 * Local string buffer for the next two functions to store a variable name
3966 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3967 * get_user_var_name().
3968 */
3969
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003970static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
3972static char_u *varnamebuf = NULL;
3973static int varnamebuflen = 0;
3974
3975/*
3976 * Function to concatenate a prefix and a variable name.
3977 */
3978 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003979cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 int len;
3982
3983 len = (int)STRLEN(name) + 3;
3984 if (len > varnamebuflen)
3985 {
3986 vim_free(varnamebuf);
3987 len += 10; /* some additional space */
3988 varnamebuf = alloc(len);
3989 if (varnamebuf == NULL)
3990 {
3991 varnamebuflen = 0;
3992 return NULL;
3993 }
3994 varnamebuflen = len;
3995 }
3996 *varnamebuf = prefix;
3997 varnamebuf[1] = ':';
3998 STRCPY(varnamebuf + 2, name);
3999 return varnamebuf;
4000}
4001
4002/*
4003 * Function given to ExpandGeneric() to obtain the list of user defined
4004 * (global/buffer/window/built-in) variable names.
4005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004007get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004009 static long_u gdone;
4010 static long_u bdone;
4011 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012#ifdef FEAT_WINDOWS
4013 static long_u tdone;
4014#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004015 static int vidx;
4016 static hashitem_T *hi;
4017 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004022#ifdef FEAT_WINDOWS
4023 tdone = 0;
4024#endif
4025 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004026
4027 /* Global variables */
4028 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004030 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004031 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004032 else
4033 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004034 while (HASHITEM_EMPTY(hi))
4035 ++hi;
4036 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4037 return cat_prefix_varname('g', hi->hi_key);
4038 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
4041 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004042 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004045 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004047 else
4048 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 while (HASHITEM_EMPTY(hi))
4050 ++hi;
4051 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004055 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 return (char_u *)"b:changedtick";
4057 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004058
4059 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004060 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004063 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004065 else
4066 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 while (HASHITEM_EMPTY(hi))
4068 ++hi;
4069 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004071
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072#ifdef FEAT_WINDOWS
4073 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004074 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004075 if (tdone < ht->ht_used)
4076 {
4077 if (tdone++ == 0)
4078 hi = ht->ht_array;
4079 else
4080 ++hi;
4081 while (HASHITEM_EMPTY(hi))
4082 ++hi;
4083 return cat_prefix_varname('t', hi->hi_key);
4084 }
4085#endif
4086
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 /* v: variables */
4088 if (vidx < VV_LEN)
4089 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091 vim_free(varnamebuf);
4092 varnamebuf = NULL;
4093 varnamebuflen = 0;
4094 return NULL;
4095}
4096
4097#endif /* FEAT_CMDL_COMPL */
4098
4099/*
4100 * types for expressions.
4101 */
4102typedef enum
4103{
4104 TYPE_UNKNOWN = 0
4105 , TYPE_EQUAL /* == */
4106 , TYPE_NEQUAL /* != */
4107 , TYPE_GREATER /* > */
4108 , TYPE_GEQUAL /* >= */
4109 , TYPE_SMALLER /* < */
4110 , TYPE_SEQUAL /* <= */
4111 , TYPE_MATCH /* =~ */
4112 , TYPE_NOMATCH /* !~ */
4113} exptype_T;
4114
4115/*
4116 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4119 */
4120
4121/*
4122 * Handle zero level expression.
4123 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004125 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * Return OK or FAIL.
4127 */
4128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004129eval0(
4130 char_u *arg,
4131 typval_T *rettv,
4132 char_u **nextcmd,
4133 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 int ret;
4136 char_u *p;
4137
4138 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 if (ret == FAIL || !ends_excmd(*p))
4141 {
4142 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 /*
4145 * Report the invalid expression unless the expression evaluation has
4146 * been cancelled due to an aborting error, an interrupt, or an
4147 * exception.
4148 */
4149 if (!aborting())
4150 EMSG2(_(e_invexpr2), arg);
4151 ret = FAIL;
4152 }
4153 if (nextcmd != NULL)
4154 *nextcmd = check_nextcmd(p);
4155
4156 return ret;
4157}
4158
4159/*
4160 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004161 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 *
4163 * "arg" must point to the first non-white of the expression.
4164 * "arg" is advanced to the next non-white after the recognized expression.
4165 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004166 * Note: "rettv.v_lock" is not set.
4167 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 * Return OK or FAIL.
4169 */
4170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004171eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004174 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
4176 /*
4177 * Get the first variable.
4178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return FAIL;
4181
4182 if ((*arg)[0] == '?')
4183 {
4184 result = FALSE;
4185 if (evaluate)
4186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 int error = FALSE;
4188
4189 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (error)
4193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202
4203 /*
4204 * Check for the ":".
4205 */
4206 if ((*arg)[0] != ':')
4207 {
4208 EMSG(_("E109: Missing ':' after '?'"));
4209 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 return FAIL;
4212 }
4213
4214 /*
4215 * Get the third variable.
4216 */
4217 *arg = skipwhite(*arg + 1);
4218 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4219 {
4220 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return FAIL;
4223 }
4224 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227
4228 return OK;
4229}
4230
4231/*
4232 * Handle first level expression:
4233 * expr2 || expr2 || expr2 logical OR
4234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
4238 * Return OK or FAIL.
4239 */
4240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004241eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242{
Bram Moolenaar33570922005-01-25 22:26:29 +00004243 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 long result;
4245 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /*
4249 * Get the first variable.
4250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253
4254 /*
4255 * Repeat until there is no following "||".
4256 */
4257 first = TRUE;
4258 result = FALSE;
4259 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4260 {
4261 if (evaluate && first)
4262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (error)
4267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 first = FALSE;
4269 }
4270
4271 /*
4272 * Get the second variable.
4273 */
4274 *arg = skipwhite(*arg + 2);
4275 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4276 return FAIL;
4277
4278 /*
4279 * Compute the result.
4280 */
4281 if (evaluate && !result)
4282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (error)
4287 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 if (evaluate)
4290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 rettv->v_type = VAR_NUMBER;
4292 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 }
4295
4296 return OK;
4297}
4298
4299/*
4300 * Handle second level expression:
4301 * expr3 && expr3 && expr3 logical AND
4302 *
4303 * "arg" must point to the first non-white of the expression.
4304 * "arg" is advanced to the next non-white after the recognized expression.
4305 *
4306 * Return OK or FAIL.
4307 */
4308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004309eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
Bram Moolenaar33570922005-01-25 22:26:29 +00004311 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 long result;
4313 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 /*
4317 * Get the first variable.
4318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 return FAIL;
4321
4322 /*
4323 * Repeat until there is no following "&&".
4324 */
4325 first = TRUE;
4326 result = TRUE;
4327 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4328 {
4329 if (evaluate && first)
4330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (error)
4335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 first = FALSE;
4337 }
4338
4339 /*
4340 * Get the second variable.
4341 */
4342 *arg = skipwhite(*arg + 2);
4343 if (eval4(arg, &var2, evaluate && result) == FAIL)
4344 return FAIL;
4345
4346 /*
4347 * Compute the result.
4348 */
4349 if (evaluate && result)
4350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (error)
4355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 if (evaluate)
4358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 rettv->v_type = VAR_NUMBER;
4360 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 }
4363
4364 return OK;
4365}
4366
4367/*
4368 * Handle third level expression:
4369 * var1 == var2
4370 * var1 =~ var2
4371 * var1 != var2
4372 * var1 !~ var2
4373 * var1 > var2
4374 * var1 >= var2
4375 * var1 < var2
4376 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004377 * var1 is var2
4378 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 *
4380 * "arg" must point to the first non-white of the expression.
4381 * "arg" is advanced to the next non-white after the recognized expression.
4382 *
4383 * Return OK or FAIL.
4384 */
4385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004386eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
Bram Moolenaar33570922005-01-25 22:26:29 +00004388 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 char_u *p;
4390 int i;
4391 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 int len = 2;
4394 long n1, n2;
4395 char_u *s1, *s2;
4396 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4397 regmatch_T regmatch;
4398 int ic;
4399 char_u *save_cpo;
4400
4401 /*
4402 * Get the first variable.
4403 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FAIL;
4406
4407 p = *arg;
4408 switch (p[0])
4409 {
4410 case '=': if (p[1] == '=')
4411 type = TYPE_EQUAL;
4412 else if (p[1] == '~')
4413 type = TYPE_MATCH;
4414 break;
4415 case '!': if (p[1] == '=')
4416 type = TYPE_NEQUAL;
4417 else if (p[1] == '~')
4418 type = TYPE_NOMATCH;
4419 break;
4420 case '>': if (p[1] != '=')
4421 {
4422 type = TYPE_GREATER;
4423 len = 1;
4424 }
4425 else
4426 type = TYPE_GEQUAL;
4427 break;
4428 case '<': if (p[1] != '=')
4429 {
4430 type = TYPE_SMALLER;
4431 len = 1;
4432 }
4433 else
4434 type = TYPE_SEQUAL;
4435 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 case 'i': if (p[1] == 's')
4437 {
4438 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4439 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004440 i = p[len];
4441 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 {
4443 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4444 type_is = TRUE;
4445 }
4446 }
4447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449
4450 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004451 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 */
4453 if (type != TYPE_UNKNOWN)
4454 {
4455 /* extra question mark appended: ignore case */
4456 if (p[len] == '?')
4457 {
4458 ic = TRUE;
4459 ++len;
4460 }
4461 /* extra '#' appended: match case */
4462 else if (p[len] == '#')
4463 {
4464 ic = FALSE;
4465 ++len;
4466 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004467 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 else
4469 ic = p_ic;
4470
4471 /*
4472 * Get the second variable.
4473 */
4474 *arg = skipwhite(p + len);
4475 if (eval5(arg, &var2, evaluate) == FAIL)
4476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return FAIL;
4479 }
4480
4481 if (evaluate)
4482 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 if (type_is && rettv->v_type != var2.v_type)
4484 {
4485 /* For "is" a different type always means FALSE, for "notis"
4486 * it means TRUE. */
4487 n1 = (type == TYPE_NEQUAL);
4488 }
4489 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4490 {
4491 if (type_is)
4492 {
4493 n1 = (rettv->v_type == var2.v_type
4494 && rettv->vval.v_list == var2.vval.v_list);
4495 if (type == TYPE_NEQUAL)
4496 n1 = !n1;
4497 }
4498 else if (rettv->v_type != var2.v_type
4499 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4500 {
4501 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004502 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004504 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004505 clear_tv(rettv);
4506 clear_tv(&var2);
4507 return FAIL;
4508 }
4509 else
4510 {
4511 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004512 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4513 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 if (type == TYPE_NEQUAL)
4515 n1 = !n1;
4516 }
4517 }
4518
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004519 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4520 {
4521 if (type_is)
4522 {
4523 n1 = (rettv->v_type == var2.v_type
4524 && rettv->vval.v_dict == var2.vval.v_dict);
4525 if (type == TYPE_NEQUAL)
4526 n1 = !n1;
4527 }
4528 else if (rettv->v_type != var2.v_type
4529 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4530 {
4531 if (rettv->v_type != var2.v_type)
4532 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4533 else
4534 EMSG(_("E736: Invalid operation for Dictionary"));
4535 clear_tv(rettv);
4536 clear_tv(&var2);
4537 return FAIL;
4538 }
4539 else
4540 {
4541 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004542 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4543 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004544 if (type == TYPE_NEQUAL)
4545 n1 = !n1;
4546 }
4547 }
4548
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004549 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4550 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004551 {
4552 if (rettv->v_type != var2.v_type
4553 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4554 {
4555 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004556 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004558 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 clear_tv(rettv);
4560 clear_tv(&var2);
4561 return FAIL;
4562 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004563 else if (rettv->v_type == VAR_PARTIAL)
4564 {
4565 /* Partials are only equal when identical. */
4566 n1 = rettv->vval.v_partial != NULL
4567 && rettv->vval.v_partial == var2.vval.v_partial;
4568 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 else
4570 {
4571 /* Compare two Funcrefs for being equal or unequal. */
4572 if (rettv->vval.v_string == NULL
4573 || var2.vval.v_string == NULL)
4574 n1 = FALSE;
4575 else
4576 n1 = STRCMP(rettv->vval.v_string,
4577 var2.vval.v_string) == 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004579 if (type == TYPE_NEQUAL)
4580 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004581 }
4582
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004583#ifdef FEAT_FLOAT
4584 /*
4585 * If one of the two variables is a float, compare as a float.
4586 * When using "=~" or "!~", always compare as string.
4587 */
4588 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4589 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4590 {
4591 float_T f1, f2;
4592
4593 if (rettv->v_type == VAR_FLOAT)
4594 f1 = rettv->vval.v_float;
4595 else
4596 f1 = get_tv_number(rettv);
4597 if (var2.v_type == VAR_FLOAT)
4598 f2 = var2.vval.v_float;
4599 else
4600 f2 = get_tv_number(&var2);
4601 n1 = FALSE;
4602 switch (type)
4603 {
4604 case TYPE_EQUAL: n1 = (f1 == f2); break;
4605 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4606 case TYPE_GREATER: n1 = (f1 > f2); break;
4607 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4608 case TYPE_SMALLER: n1 = (f1 < f2); break;
4609 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4610 case TYPE_UNKNOWN:
4611 case TYPE_MATCH:
4612 case TYPE_NOMATCH: break; /* avoid gcc warning */
4613 }
4614 }
4615#endif
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 /*
4618 * If one of the two variables is a number, compare as a number.
4619 * When using "=~" or "!~", always compare as string.
4620 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004621 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 n1 = get_tv_number(rettv);
4625 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 switch (type)
4627 {
4628 case TYPE_EQUAL: n1 = (n1 == n2); break;
4629 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4630 case TYPE_GREATER: n1 = (n1 > n2); break;
4631 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4632 case TYPE_SMALLER: n1 = (n1 < n2); break;
4633 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4634 case TYPE_UNKNOWN:
4635 case TYPE_MATCH:
4636 case TYPE_NOMATCH: break; /* avoid gcc warning */
4637 }
4638 }
4639 else
4640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 s1 = get_tv_string_buf(rettv, buf1);
4642 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4644 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4645 else
4646 i = 0;
4647 n1 = FALSE;
4648 switch (type)
4649 {
4650 case TYPE_EQUAL: n1 = (i == 0); break;
4651 case TYPE_NEQUAL: n1 = (i != 0); break;
4652 case TYPE_GREATER: n1 = (i > 0); break;
4653 case TYPE_GEQUAL: n1 = (i >= 0); break;
4654 case TYPE_SMALLER: n1 = (i < 0); break;
4655 case TYPE_SEQUAL: n1 = (i <= 0); break;
4656
4657 case TYPE_MATCH:
4658 case TYPE_NOMATCH:
4659 /* avoid 'l' flag in 'cpoptions' */
4660 save_cpo = p_cpo;
4661 p_cpo = (char_u *)"";
4662 regmatch.regprog = vim_regcomp(s2,
4663 RE_MAGIC + RE_STRING);
4664 regmatch.rm_ic = ic;
4665 if (regmatch.regprog != NULL)
4666 {
4667 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004668 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (type == TYPE_NOMATCH)
4670 n1 = !n1;
4671 }
4672 p_cpo = save_cpo;
4673 break;
4674
4675 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4676 }
4677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 clear_tv(rettv);
4679 clear_tv(&var2);
4680 rettv->v_type = VAR_NUMBER;
4681 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683 }
4684
4685 return OK;
4686}
4687
4688/*
4689 * Handle fourth level expression:
4690 * + number addition
4691 * - number subtraction
4692 * . string concatenation
4693 *
4694 * "arg" must point to the first non-white of the expression.
4695 * "arg" is advanced to the next non-white after the recognized expression.
4696 *
4697 * Return OK or FAIL.
4698 */
4699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004700eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
Bram Moolenaar33570922005-01-25 22:26:29 +00004702 typval_T var2;
4703 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 int op;
4705 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004706#ifdef FEAT_FLOAT
4707 float_T f1 = 0, f2 = 0;
4708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 char_u *s1, *s2;
4710 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4711 char_u *p;
4712
4713 /*
4714 * Get the first variable.
4715 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004716 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 return FAIL;
4718
4719 /*
4720 * Repeat computing, until no '+', '-' or '.' is following.
4721 */
4722 for (;;)
4723 {
4724 op = **arg;
4725 if (op != '+' && op != '-' && op != '.')
4726 break;
4727
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 if ((op != '+' || rettv->v_type != VAR_LIST)
4729#ifdef FEAT_FLOAT
4730 && (op == '.' || rettv->v_type != VAR_FLOAT)
4731#endif
4732 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004733 {
4734 /* For "list + ...", an illegal use of the first operand as
4735 * a number cannot be determined before evaluating the 2nd
4736 * operand: if this is also a list, all is ok.
4737 * For "something . ...", "something - ..." or "non-list + ...",
4738 * we know that the first operand needs to be a string or number
4739 * without evaluating the 2nd operand. So check before to avoid
4740 * side effects after an error. */
4741 if (evaluate && get_tv_string_chk(rettv) == NULL)
4742 {
4743 clear_tv(rettv);
4744 return FAIL;
4745 }
4746 }
4747
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 /*
4749 * Get the second variable.
4750 */
4751 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004752 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 return FAIL;
4756 }
4757
4758 if (evaluate)
4759 {
4760 /*
4761 * Compute the result.
4762 */
4763 if (op == '.')
4764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4766 s2 = get_tv_string_buf_chk(&var2, buf2);
4767 if (s2 == NULL) /* type error ? */
4768 {
4769 clear_tv(rettv);
4770 clear_tv(&var2);
4771 return FAIL;
4772 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004773 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(rettv);
4775 rettv->v_type = VAR_STRING;
4776 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004778 else if (op == '+' && rettv->v_type == VAR_LIST
4779 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004780 {
4781 /* concatenate Lists */
4782 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4783 &var3) == FAIL)
4784 {
4785 clear_tv(rettv);
4786 clear_tv(&var2);
4787 return FAIL;
4788 }
4789 clear_tv(rettv);
4790 *rettv = var3;
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 else
4793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 int error = FALSE;
4795
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796#ifdef FEAT_FLOAT
4797 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004799 f1 = rettv->vval.v_float;
4800 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004801 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802 else
4803#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 n1 = get_tv_number_chk(rettv, &error);
4806 if (error)
4807 {
4808 /* This can only happen for "list + non-list". For
4809 * "non-list + ..." or "something - ...", we returned
4810 * before evaluating the 2nd operand. */
4811 clear_tv(rettv);
4812 return FAIL;
4813 }
4814#ifdef FEAT_FLOAT
4815 if (var2.v_type == VAR_FLOAT)
4816 f1 = n1;
4817#endif
4818 }
4819#ifdef FEAT_FLOAT
4820 if (var2.v_type == VAR_FLOAT)
4821 {
4822 f2 = var2.vval.v_float;
4823 n2 = 0;
4824 }
4825 else
4826#endif
4827 {
4828 n2 = get_tv_number_chk(&var2, &error);
4829 if (error)
4830 {
4831 clear_tv(rettv);
4832 clear_tv(&var2);
4833 return FAIL;
4834 }
4835#ifdef FEAT_FLOAT
4836 if (rettv->v_type == VAR_FLOAT)
4837 f2 = n2;
4838#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841
4842#ifdef FEAT_FLOAT
4843 /* If there is a float on either side the result is a float. */
4844 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4845 {
4846 if (op == '+')
4847 f1 = f1 + f2;
4848 else
4849 f1 = f1 - f2;
4850 rettv->v_type = VAR_FLOAT;
4851 rettv->vval.v_float = f1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004854#endif
4855 {
4856 if (op == '+')
4857 n1 = n1 + n2;
4858 else
4859 n1 = n1 - n2;
4860 rettv->v_type = VAR_NUMBER;
4861 rettv->vval.v_number = n1;
4862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 }
4867 return OK;
4868}
4869
4870/*
4871 * Handle fifth level expression:
4872 * * number multiplication
4873 * / number division
4874 * % number modulo
4875 *
4876 * "arg" must point to the first non-white of the expression.
4877 * "arg" is advanced to the next non-white after the recognized expression.
4878 *
4879 * Return OK or FAIL.
4880 */
4881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004882eval6(
4883 char_u **arg,
4884 typval_T *rettv,
4885 int evaluate,
4886 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
Bram Moolenaar33570922005-01-25 22:26:29 +00004888 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 int op;
4890 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891#ifdef FEAT_FLOAT
4892 int use_float = FALSE;
4893 float_T f1 = 0, f2;
4894#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 /*
4898 * Get the first variable.
4899 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004900 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 return FAIL;
4902
4903 /*
4904 * Repeat computing, until no '*', '/' or '%' is following.
4905 */
4906 for (;;)
4907 {
4908 op = **arg;
4909 if (op != '*' && op != '/' && op != '%')
4910 break;
4911
4912 if (evaluate)
4913 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914#ifdef FEAT_FLOAT
4915 if (rettv->v_type == VAR_FLOAT)
4916 {
4917 f1 = rettv->vval.v_float;
4918 use_float = TRUE;
4919 n1 = 0;
4920 }
4921 else
4922#endif
4923 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004925 if (error)
4926 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
4929 n1 = 0;
4930
4931 /*
4932 * Get the second variable.
4933 */
4934 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004935 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 return FAIL;
4937
4938 if (evaluate)
4939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940#ifdef FEAT_FLOAT
4941 if (var2.v_type == VAR_FLOAT)
4942 {
4943 if (!use_float)
4944 {
4945 f1 = n1;
4946 use_float = TRUE;
4947 }
4948 f2 = var2.vval.v_float;
4949 n2 = 0;
4950 }
4951 else
4952#endif
4953 {
4954 n2 = get_tv_number_chk(&var2, &error);
4955 clear_tv(&var2);
4956 if (error)
4957 return FAIL;
4958#ifdef FEAT_FLOAT
4959 if (use_float)
4960 f2 = n2;
4961#endif
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964 /*
4965 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004968#ifdef FEAT_FLOAT
4969 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 if (op == '*')
4972 f1 = f1 * f2;
4973 else if (op == '/')
4974 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975# ifdef VMS
4976 /* VMS crashes on divide by zero, work around it */
4977 if (f2 == 0.0)
4978 {
4979 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004980 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004982 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004983 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004984 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004985 }
4986 else
4987 f1 = f1 / f2;
4988# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 /* We rely on the floating point library to handle divide
4990 * by zero to result in "inf" and not a crash. */
4991 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004992# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004996 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 return FAIL;
4998 }
4999 rettv->v_type = VAR_FLOAT;
5000 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 if (op == '*')
5006 n1 = n1 * n2;
5007 else if (op == '/')
5008 {
5009 if (n2 == 0) /* give an error message? */
5010 {
5011 if (n1 == 0)
5012 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5013 else if (n1 < 0)
5014 n1 = -0x7fffffffL;
5015 else
5016 n1 = 0x7fffffffL;
5017 }
5018 else
5019 n1 = n1 / n2;
5020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 {
5023 if (n2 == 0) /* give an error message? */
5024 n1 = 0;
5025 else
5026 n1 = n1 % n2;
5027 }
5028 rettv->v_type = VAR_NUMBER;
5029 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032 }
5033
5034 return OK;
5035}
5036
5037/*
5038 * Handle sixth level expression:
5039 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005040 * "string" string constant
5041 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 * &option-name option value
5043 * @r register contents
5044 * identifier variable value
5045 * function() function call
5046 * $VAR environment variable
5047 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005048 * [expr, expr] List
5049 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 *
5051 * Also handle:
5052 * ! in front logical NOT
5053 * - in front unary minus
5054 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 * trailing [] subscript in String or List
5056 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 *
5058 * "arg" must point to the first non-white of the expression.
5059 * "arg" is advanced to the next non-white after the recognized expression.
5060 *
5061 * Return OK or FAIL.
5062 */
5063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005064eval7(
5065 char_u **arg,
5066 typval_T *rettv,
5067 int evaluate,
5068 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 long n;
5071 int len;
5072 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 char_u *start_leader, *end_leader;
5074 int ret = OK;
5075 char_u *alias;
5076
5077 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 /*
5084 * Skip '!' and '-' characters. They are handled later.
5085 */
5086 start_leader = *arg;
5087 while (**arg == '!' || **arg == '-' || **arg == '+')
5088 *arg = skipwhite(*arg + 1);
5089 end_leader = *arg;
5090
5091 switch (**arg)
5092 {
5093 /*
5094 * Number constant.
5095 */
5096 case '0':
5097 case '1':
5098 case '2':
5099 case '3':
5100 case '4':
5101 case '5':
5102 case '6':
5103 case '7':
5104 case '8':
5105 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106 {
5107#ifdef FEAT_FLOAT
5108 char_u *p = skipdigits(*arg + 1);
5109 int get_float = FALSE;
5110
5111 /* We accept a float when the format matches
5112 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005113 * strict to avoid backwards compatibility problems.
5114 * Don't look for a float after the "." operator, so that
5115 * ":let vers = 1.2.3" doesn't fail. */
5116 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 get_float = TRUE;
5119 p = skipdigits(p + 2);
5120 if (*p == 'e' || *p == 'E')
5121 {
5122 ++p;
5123 if (*p == '-' || *p == '+')
5124 ++p;
5125 if (!vim_isdigit(*p))
5126 get_float = FALSE;
5127 else
5128 p = skipdigits(p + 1);
5129 }
5130 if (ASCII_ISALPHA(*p) || *p == '.')
5131 get_float = FALSE;
5132 }
5133 if (get_float)
5134 {
5135 float_T f;
5136
5137 *arg += string2float(*arg, &f);
5138 if (evaluate)
5139 {
5140 rettv->v_type = VAR_FLOAT;
5141 rettv->vval.v_float = f;
5142 }
5143 }
5144 else
5145#endif
5146 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005147 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005148 *arg += len;
5149 if (evaluate)
5150 {
5151 rettv->v_type = VAR_NUMBER;
5152 rettv->vval.v_number = n;
5153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 /*
5159 * String constant: "string".
5160 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 break;
5169
5170 /*
5171 * List: [expr, expr]
5172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175
5176 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 * Dictionary: {key: val, key: val}
5178 */
5179 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5180 break;
5181
5182 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005183 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005185 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /*
5189 * Environment variable: $VAR.
5190 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193
5194 /*
5195 * Register contents: @r.
5196 */
5197 case '@': ++*arg;
5198 if (evaluate)
5199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005201 rettv->vval.v_string = get_reg_contents(**arg,
5202 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204 if (**arg != NUL)
5205 ++*arg;
5206 break;
5207
5208 /*
5209 * nested expression: (expression).
5210 */
5211 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (**arg == ')')
5214 ++*arg;
5215 else if (ret == OK)
5216 {
5217 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 ret = FAIL;
5220 }
5221 break;
5222
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 break;
5225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226
5227 if (ret == NOTDONE)
5228 {
5229 /*
5230 * Must be a variable or function name.
5231 * Can also be a curly-braces kind of name: {expr}.
5232 */
5233 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 if (alias != NULL)
5236 s = alias;
5237
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005238 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 ret = FAIL;
5240 else
5241 {
5242 if (**arg == '(') /* recursive! */
5243 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005244 partial_T *partial;
5245
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 /* If "s" is the name of a variable of type VAR_FUNC
5247 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005248 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249
5250 /* Invoke the function. */
5251 ret = get_func_tv(s, len, rettv, arg,
5252 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005253 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005254
5255 /* If evaluate is FALSE rettv->v_type was not set in
5256 * get_func_tv, but it's needed in handle_subscript() to parse
5257 * what follows. So set it here. */
5258 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5259 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005260 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005261 rettv->v_type = VAR_FUNC;
5262 }
5263
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 /* Stop the expression evaluation when immediately
5265 * aborting on error, or when an interrupt occurred or
5266 * an exception was thrown but not caught. */
5267 if (aborting())
5268 {
5269 if (ret == OK)
5270 clear_tv(rettv);
5271 ret = FAIL;
5272 }
5273 }
5274 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005275 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005276 else
5277 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005279 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 }
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 *arg = skipwhite(*arg);
5283
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005284 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5285 * expr(expr). */
5286 if (ret == OK)
5287 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5291 */
5292 if (ret == OK && evaluate && end_leader > start_leader)
5293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005295 int val = 0;
5296#ifdef FEAT_FLOAT
5297 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 if (rettv->v_type == VAR_FLOAT)
5300 f = rettv->vval.v_float;
5301 else
5302#endif
5303 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 clear_tv(rettv);
5307 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 else
5310 {
5311 while (end_leader > start_leader)
5312 {
5313 --end_leader;
5314 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315 {
5316#ifdef FEAT_FLOAT
5317 if (rettv->v_type == VAR_FLOAT)
5318 f = !f;
5319 else
5320#endif
5321 val = !val;
5322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005323 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005324 {
5325#ifdef FEAT_FLOAT
5326 if (rettv->v_type == VAR_FLOAT)
5327 f = -f;
5328 else
5329#endif
5330 val = -val;
5331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005333#ifdef FEAT_FLOAT
5334 if (rettv->v_type == VAR_FLOAT)
5335 {
5336 clear_tv(rettv);
5337 rettv->vval.v_float = f;
5338 }
5339 else
5340#endif
5341 {
5342 clear_tv(rettv);
5343 rettv->v_type = VAR_NUMBER;
5344 rettv->vval.v_number = val;
5345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348
5349 return ret;
5350}
5351
5352/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005353 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5354 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5356 */
5357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005358eval_index(
5359 char_u **arg,
5360 typval_T *rettv,
5361 int evaluate,
5362 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363{
5364 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005365 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 long len = -1;
5368 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005375 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005376 if (verbose)
5377 EMSG(_("E695: Cannot index a Funcref"));
5378 return FAIL;
5379 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005380#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005381 if (verbose)
5382 EMSG(_(e_float_as_string));
5383 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005384#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005385 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005386 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005387 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005388 if (verbose)
5389 EMSG(_("E909: Cannot index a special variable"));
5390 return FAIL;
5391 case VAR_UNKNOWN:
5392 if (evaluate)
5393 return FAIL;
5394 /* FALLTHROUGH */
5395
5396 case VAR_STRING:
5397 case VAR_NUMBER:
5398 case VAR_LIST:
5399 case VAR_DICT:
5400 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005401 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005403 init_tv(&var1);
5404 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * dict.name
5409 */
5410 key = *arg + 1;
5411 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5412 ;
5413 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 }
5417 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419 /*
5420 * something[idx]
5421 *
5422 * Get the (first) variable from inside the [].
5423 */
5424 *arg = skipwhite(*arg + 1);
5425 if (**arg == ':')
5426 empty1 = TRUE;
5427 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5428 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5430 {
5431 /* not a number or string */
5432 clear_tv(&var1);
5433 return FAIL;
5434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435
5436 /*
5437 * Get the second variable from inside the [:].
5438 */
5439 if (**arg == ':')
5440 {
5441 range = TRUE;
5442 *arg = skipwhite(*arg + 1);
5443 if (**arg == ']')
5444 empty2 = TRUE;
5445 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005447 if (!empty1)
5448 clear_tv(&var1);
5449 return FAIL;
5450 }
5451 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5452 {
5453 /* not a number or string */
5454 if (!empty1)
5455 clear_tv(&var1);
5456 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 return FAIL;
5458 }
5459 }
5460
5461 /* Check for the ']'. */
5462 if (**arg != ']')
5463 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005464 if (verbose)
5465 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 clear_tv(&var1);
5467 if (range)
5468 clear_tv(&var2);
5469 return FAIL;
5470 }
5471 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473
5474 if (evaluate)
5475 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476 n1 = 0;
5477 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 n1 = get_tv_number(&var1);
5480 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 }
5482 if (range)
5483 {
5484 if (empty2)
5485 n2 = -1;
5486 else
5487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488 n2 = get_tv_number(&var2);
5489 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 }
5491 }
5492
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005495 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005496 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005497 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005498 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005499 case VAR_SPECIAL:
5500 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005501 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005502 break; /* not evaluating, skipping over subscript */
5503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 case VAR_NUMBER:
5505 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 len = (long)STRLEN(s);
5508 if (range)
5509 {
5510 /* The resulting variable is a substring. If the indexes
5511 * are out of range the result is empty. */
5512 if (n1 < 0)
5513 {
5514 n1 = len + n1;
5515 if (n1 < 0)
5516 n1 = 0;
5517 }
5518 if (n2 < 0)
5519 n2 = len + n2;
5520 else if (n2 >= len)
5521 n2 = len;
5522 if (n1 >= len || n2 < 0 || n1 > n2)
5523 s = NULL;
5524 else
5525 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5526 }
5527 else
5528 {
5529 /* The resulting variable is a string of a single
5530 * character. If the index is too big or negative the
5531 * result is empty. */
5532 if (n1 >= len || n1 < 0)
5533 s = NULL;
5534 else
5535 s = vim_strnsave(s + n1, 1);
5536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 clear_tv(rettv);
5538 rettv->v_type = VAR_STRING;
5539 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 break;
5541
5542 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 if (n1 < 0)
5545 n1 = len + n1;
5546 if (!empty1 && (n1 < 0 || n1 >= len))
5547 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005548 /* For a range we allow invalid values and return an empty
5549 * list. A list index out of range is an error. */
5550 if (!range)
5551 {
5552 if (verbose)
5553 EMSGN(_(e_listidx), n1);
5554 return FAIL;
5555 }
5556 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 }
5558 if (range)
5559 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005560 list_T *l;
5561 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562
5563 if (n2 < 0)
5564 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005565 else if (n2 >= len)
5566 n2 = len - 1;
5567 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 l = list_alloc();
5570 if (l == NULL)
5571 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 n1 <= n2; ++n1)
5574 {
5575 if (list_append_tv(l, &item->li_tv) == FAIL)
5576 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005577 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 return FAIL;
5579 }
5580 item = item->li_next;
5581 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 clear_tv(rettv);
5583 rettv->v_type = VAR_LIST;
5584 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005585 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 }
5587 else
5588 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005589 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 clear_tv(rettv);
5591 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005592 }
5593 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594
5595 case VAR_DICT:
5596 if (range)
5597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005598 if (verbose)
5599 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 if (len == -1)
5601 clear_tv(&var1);
5602 return FAIL;
5603 }
5604 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005605 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606
5607 if (len == -1)
5608 {
5609 key = get_tv_string(&var1);
5610 if (*key == NUL)
5611 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005612 if (verbose)
5613 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 clear_tv(&var1);
5615 return FAIL;
5616 }
5617 }
5618
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005619 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005621 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005622 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 if (len == -1)
5624 clear_tv(&var1);
5625 if (item == NULL)
5626 return FAIL;
5627
5628 copy_tv(&item->di_tv, &var1);
5629 clear_tv(rettv);
5630 *rettv = var1;
5631 }
5632 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 }
5634 }
5635
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 return OK;
5637}
5638
5639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 * Get an option value.
5641 * "arg" points to the '&' or '+' before the option name.
5642 * "arg" is advanced to character after the option name.
5643 * Return OK or FAIL.
5644 */
5645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005646get_option_tv(
5647 char_u **arg,
5648 typval_T *rettv, /* when NULL, only check if option exists */
5649 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651 char_u *option_end;
5652 long numval;
5653 char_u *stringval;
5654 int opt_type;
5655 int c;
5656 int working = (**arg == '+'); /* has("+option") */
5657 int ret = OK;
5658 int opt_flags;
5659
5660 /*
5661 * Isolate the option name and find its value.
5662 */
5663 option_end = find_option_end(arg, &opt_flags);
5664 if (option_end == NULL)
5665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 EMSG2(_("E112: Option name missing: %s"), *arg);
5668 return FAIL;
5669 }
5670
5671 if (!evaluate)
5672 {
5673 *arg = option_end;
5674 return OK;
5675 }
5676
5677 c = *option_end;
5678 *option_end = NUL;
5679 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 if (opt_type == -3) /* invalid name */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 EMSG2(_("E113: Unknown option: %s"), *arg);
5686 ret = FAIL;
5687 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 if (opt_type == -2) /* hidden string option */
5691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 rettv->v_type = VAR_STRING;
5693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else if (opt_type == -1) /* hidden number option */
5696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 rettv->v_type = VAR_NUMBER;
5698 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 else if (opt_type == 1) /* number option */
5701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 rettv->v_type = VAR_NUMBER;
5703 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
5705 else /* string option */
5706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005707 rettv->v_type = VAR_STRING;
5708 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
5710 }
5711 else if (working && (opt_type == -2 || opt_type == -1))
5712 ret = FAIL;
5713
5714 *option_end = c; /* put back for error messages */
5715 *arg = option_end;
5716
5717 return ret;
5718}
5719
5720/*
5721 * Allocate a variable for a string constant.
5722 * Return OK or FAIL.
5723 */
5724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005725get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 char_u *p;
5728 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 int extra = 0;
5730
5731 /*
5732 * Find the end of the string, skipping backslashed characters.
5733 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
5736 if (*p == '\\' && p[1] != NUL)
5737 {
5738 ++p;
5739 /* A "\<x>" form occupies at least 4 characters, and produces up
5740 * to 6 characters: reserve space for 2 extra */
5741 if (*p == '<')
5742 extra += 2;
5743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745
5746 if (*p != '"')
5747 {
5748 EMSG2(_("E114: Missing quote: %s"), *arg);
5749 return FAIL;
5750 }
5751
5752 /* If only parsing, set *arg and return here */
5753 if (!evaluate)
5754 {
5755 *arg = p + 1;
5756 return OK;
5757 }
5758
5759 /*
5760 * Copy the string into allocated memory, handling backslashed
5761 * characters.
5762 */
5763 name = alloc((unsigned)(p - *arg + extra));
5764 if (name == NULL)
5765 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 rettv->v_type = VAR_STRING;
5767 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 if (*p == '\\')
5772 {
5773 switch (*++p)
5774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 case 'b': *name++ = BS; ++p; break;
5776 case 'e': *name++ = ESC; ++p; break;
5777 case 'f': *name++ = FF; ++p; break;
5778 case 'n': *name++ = NL; ++p; break;
5779 case 'r': *name++ = CAR; ++p; break;
5780 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782 case 'X': /* hex: "\x1", "\x12" */
5783 case 'x':
5784 case 'u': /* Unicode: "\u0023" */
5785 case 'U':
5786 if (vim_isxdigit(p[1]))
5787 {
5788 int n, nr;
5789 int c = toupper(*p);
5790
5791 if (c == 'X')
5792 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005793 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005795 else
5796 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 nr = 0;
5798 while (--n >= 0 && vim_isxdigit(p[1]))
5799 {
5800 ++p;
5801 nr = (nr << 4) + hex2nr(*p);
5802 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804#ifdef FEAT_MBYTE
5805 /* For "\u" store the number according to
5806 * 'encoding'. */
5807 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 else
5810#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814
5815 /* octal: "\1", "\12", "\123" */
5816 case '0':
5817 case '1':
5818 case '2':
5819 case '3':
5820 case '4':
5821 case '5':
5822 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 case '7': *name = *p++ - '0';
5824 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 *name = (*name << 3) + *p++ - '0';
5827 if (*p >= '0' && *p <= '7')
5828 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 break;
5832
5833 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (extra != 0)
5836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 break;
5839 }
5840 /* FALLTHROUGH */
5841
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 break;
5844 }
5845 }
5846 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 *arg = p + 1;
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 return OK;
5854}
5855
5856/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 * Return OK or FAIL.
5859 */
5860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005861get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 int reduce = 0;
5866
5867 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 */
5870 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5871 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 break;
5876 ++reduce;
5877 ++p;
5878 }
5879 }
5880
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 return FAIL;
5885 }
5886
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 if (!evaluate)
5889 {
5890 *arg = p + 1;
5891 return OK;
5892 }
5893
5894 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 */
5897 str = alloc((unsigned)((p - *arg) - reduce));
5898 if (str == NULL)
5899 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 rettv->v_type = VAR_STRING;
5901 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 break;
5909 ++p;
5910 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 *arg = p + 1;
5915
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 return OK;
5917}
5918
5919/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 * Allocate a variable for a List and fill it from "*arg".
5921 * Return OK or FAIL.
5922 */
5923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005924get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l = NULL;
5927 typval_T tv;
5928 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929
5930 if (evaluate)
5931 {
5932 l = list_alloc();
5933 if (l == NULL)
5934 return FAIL;
5935 }
5936
5937 *arg = skipwhite(*arg + 1);
5938 while (**arg != ']' && **arg != NUL)
5939 {
5940 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5941 goto failret;
5942 if (evaluate)
5943 {
5944 item = listitem_alloc();
5945 if (item != NULL)
5946 {
5947 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005948 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 list_append(l, item);
5950 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005951 else
5952 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 }
5954
5955 if (**arg == ']')
5956 break;
5957 if (**arg != ',')
5958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 goto failret;
5961 }
5962 *arg = skipwhite(*arg + 1);
5963 }
5964
5965 if (**arg != ']')
5966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005967 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968failret:
5969 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005970 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 return FAIL;
5972 }
5973
5974 *arg = skipwhite(*arg + 1);
5975 if (evaluate)
5976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005977 rettv->v_type = VAR_LIST;
5978 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 ++l->lv_refcount;
5980 }
5981
5982 return OK;
5983}
5984
5985/*
5986 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005987 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005989 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005990list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005992 list_T *l;
5993
5994 l = (list_T *)alloc_clear(sizeof(list_T));
5995 if (l != NULL)
5996 {
5997 /* Prepend the list to the list of lists for garbage collection. */
5998 if (first_list != NULL)
5999 first_list->lv_used_prev = l;
6000 l->lv_used_prev = NULL;
6001 l->lv_used_next = first_list;
6002 first_list = l;
6003 }
6004 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006008 * Allocate an empty list for a return value.
6009 * Returns OK or FAIL.
6010 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006013{
6014 list_T *l = list_alloc();
6015
6016 if (l == NULL)
6017 return FAIL;
6018
6019 rettv->vval.v_list = l;
6020 rettv->v_type = VAR_LIST;
6021 ++l->lv_refcount;
6022 return OK;
6023}
6024
6025/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 * Unreference a list: decrement the reference count and free it when it
6027 * becomes zero.
6028 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006029 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006032 if (l != NULL && --l->lv_refcount <= 0)
6033 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034}
6035
6036/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006037 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 * Ignores the reference count.
6039 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006041list_free(
6042 list_T *l,
6043 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 /* Remove the list from the list of lists for garbage collection. */
6048 if (l->lv_used_prev == NULL)
6049 first_list = l->lv_used_next;
6050 else
6051 l->lv_used_prev->lv_used_next = l->lv_used_next;
6052 if (l->lv_used_next != NULL)
6053 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6054
Bram Moolenaard9fba312005-06-26 22:34:35 +00006055 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006057 /* Remove the item before deleting it. */
6058 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006059 if (recurse || (item->li_tv.v_type != VAR_LIST
6060 && item->li_tv.v_type != VAR_DICT))
6061 clear_tv(&item->li_tv);
6062 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 }
6064 vim_free(l);
6065}
6066
6067/*
6068 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006069 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006071 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006072listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073{
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075}
6076
6077/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006078 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006080 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006081listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006083 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 vim_free(item);
6085}
6086
6087/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006088 * Remove a list item from a List and free it. Also clears the value.
6089 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006090 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006092{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006093 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006094 listitem_free(item);
6095}
6096
6097/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 * Get the number of items in a list.
6099 */
6100 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006101list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103 if (l == NULL)
6104 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006105 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106}
6107
6108/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 * Return TRUE when two lists have exactly the same values.
6110 */
6111 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006112list_equal(
6113 list_T *l1,
6114 list_T *l2,
6115 int ic, /* ignore case for strings */
6116 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117{
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006120 if (l1 == NULL || l2 == NULL)
6121 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006122 if (l1 == l2)
6123 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006124 if (list_len(l1) != list_len(l2))
6125 return FALSE;
6126
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127 for (item1 = l1->lv_first, item2 = l2->lv_first;
6128 item1 != NULL && item2 != NULL;
6129 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006130 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 return FALSE;
6132 return item1 == NULL && item2 == NULL;
6133}
6134
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006135/*
6136 * Return the dictitem that an entry in a hashtable points to.
6137 */
6138 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006139dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006140{
6141 return HI2DI(hi);
6142}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006143
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006144/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145 * Return TRUE when two dictionaries have exactly the same key/values.
6146 */
6147 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006148dict_equal(
6149 dict_T *d1,
6150 dict_T *d2,
6151 int ic, /* ignore case for strings */
6152 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153{
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 hashitem_T *hi;
6155 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006156 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006158 if (d1 == NULL || d2 == NULL)
6159 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006160 if (d1 == d2)
6161 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162 if (dict_len(d1) != dict_len(d2))
6163 return FALSE;
6164
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006165 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006167 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 if (!HASHITEM_EMPTY(hi))
6169 {
6170 item2 = dict_find(d2, hi->hi_key, -1);
6171 if (item2 == NULL)
6172 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006173 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006174 return FALSE;
6175 --todo;
6176 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006177 }
6178 return TRUE;
6179}
6180
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181static int tv_equal_recurse_limit;
6182
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006183/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006185 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006186 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006187 */
6188 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006189tv_equal(
6190 typval_T *tv1,
6191 typval_T *tv2,
6192 int ic, /* ignore case */
6193 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194{
6195 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006196 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006198 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006203 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006204 * recursiveness to a limit. We guess they are equal then.
6205 * A fixed limit has the problem of still taking an awful long time.
6206 * Reduce the limit every time running into it. That should work fine for
6207 * deeply linked structures that are not recursively linked and catch
6208 * recursiveness quickly. */
6209 if (!recursive)
6210 tv_equal_recurse_limit = 1000;
6211 if (recursive_cnt >= tv_equal_recurse_limit)
6212 {
6213 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006214 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006215 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006218 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006219 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006220 ++recursive_cnt;
6221 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6222 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006223 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006224
6225 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006226 ++recursive_cnt;
6227 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6228 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006229 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006230
6231 case VAR_FUNC:
6232 return (tv1->vval.v_string != NULL
6233 && tv2->vval.v_string != NULL
6234 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6235
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006236 case VAR_PARTIAL:
6237 return tv1->vval.v_partial != NULL
6238 && tv1->vval.v_partial == tv2->vval.v_partial;
6239
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006240 case VAR_NUMBER:
6241 return tv1->vval.v_number == tv2->vval.v_number;
6242
6243 case VAR_STRING:
6244 s1 = get_tv_string_buf(tv1, buf1);
6245 s2 = get_tv_string_buf(tv2, buf2);
6246 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006247
6248 case VAR_SPECIAL:
6249 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006250
6251 case VAR_FLOAT:
6252#ifdef FEAT_FLOAT
6253 return tv1->vval.v_float == tv2->vval.v_float;
6254#endif
6255 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006256#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006257 return tv1->vval.v_job == tv2->vval.v_job;
6258#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006259 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006260#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006261 return tv1->vval.v_channel == tv2->vval.v_channel;
6262#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006263 case VAR_UNKNOWN:
6264 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006266
Bram Moolenaara03f2332016-02-06 18:09:59 +01006267 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6268 * does not equal anything, not even itself. */
6269 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006270}
6271
6272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273 * Locate item with index "n" in list "l" and return it.
6274 * A negative index is counted from the end; -1 is the last item.
6275 * Returns NULL when "n" is out of range.
6276 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006277 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006278list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279{
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 long idx;
6282
6283 if (l == NULL)
6284 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006285
6286 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006288 n = l->lv_len + n;
6289
6290 /* Check for index out of range. */
6291 if (n < 0 || n >= l->lv_len)
6292 return NULL;
6293
6294 /* When there is a cached index may start search from there. */
6295 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297 if (n < l->lv_idx / 2)
6298 {
6299 /* closest to the start of the list */
6300 item = l->lv_first;
6301 idx = 0;
6302 }
6303 else if (n > (l->lv_idx + l->lv_len) / 2)
6304 {
6305 /* closest to the end of the list */
6306 item = l->lv_last;
6307 idx = l->lv_len - 1;
6308 }
6309 else
6310 {
6311 /* closest to the cached index */
6312 item = l->lv_idx_item;
6313 idx = l->lv_idx;
6314 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 }
6316 else
6317 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006318 if (n < l->lv_len / 2)
6319 {
6320 /* closest to the start of the list */
6321 item = l->lv_first;
6322 idx = 0;
6323 }
6324 else
6325 {
6326 /* closest to the end of the list */
6327 item = l->lv_last;
6328 idx = l->lv_len - 1;
6329 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006331
6332 while (n > idx)
6333 {
6334 /* search forward */
6335 item = item->li_next;
6336 ++idx;
6337 }
6338 while (n < idx)
6339 {
6340 /* search backward */
6341 item = item->li_prev;
6342 --idx;
6343 }
6344
6345 /* cache the used index */
6346 l->lv_idx = idx;
6347 l->lv_idx_item = item;
6348
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349 return item;
6350}
6351
6352/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006353 * Get list item "l[idx]" as a number.
6354 */
6355 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356list_find_nr(
6357 list_T *l,
6358 long idx,
6359 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006360{
6361 listitem_T *li;
6362
6363 li = list_find(l, idx);
6364 if (li == NULL)
6365 {
6366 if (errorp != NULL)
6367 *errorp = TRUE;
6368 return -1L;
6369 }
6370 return get_tv_number_chk(&li->li_tv, errorp);
6371}
6372
6373/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006374 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6375 */
6376 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006377list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006378{
6379 listitem_T *li;
6380
6381 li = list_find(l, idx - 1);
6382 if (li == NULL)
6383 {
6384 EMSGN(_(e_listidx), idx);
6385 return NULL;
6386 }
6387 return get_tv_string(&li->li_tv);
6388}
6389
6390/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006391 * Locate "item" list "l" and return its index.
6392 * Returns -1 when "item" is not in the list.
6393 */
6394 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006395list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006396{
6397 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006399
6400 if (l == NULL)
6401 return -1;
6402 idx = 0;
6403 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6404 ++idx;
6405 if (li == NULL)
6406 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006407 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006408}
6409
6410/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 * Append item "item" to the end of list "l".
6412 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415{
6416 if (l->lv_last == NULL)
6417 {
6418 /* empty list */
6419 l->lv_first = item;
6420 l->lv_last = item;
6421 item->li_prev = NULL;
6422 }
6423 else
6424 {
6425 l->lv_last->li_next = item;
6426 item->li_prev = l->lv_last;
6427 l->lv_last = item;
6428 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006429 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 item->li_next = NULL;
6431}
6432
6433/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006438list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006440 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441
Bram Moolenaar05159a02005-02-26 23:04:13 +00006442 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006444 copy_tv(tv, &li->li_tv);
6445 list_append(l, li);
6446 return OK;
6447}
6448
6449/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006450 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006451 * Return FAIL when out of memory.
6452 */
6453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006454list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006455{
6456 listitem_T *li = listitem_alloc();
6457
6458 if (li == NULL)
6459 return FAIL;
6460 li->li_tv.v_type = VAR_DICT;
6461 li->li_tv.v_lock = 0;
6462 li->li_tv.vval.v_dict = dict;
6463 list_append(list, li);
6464 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 return OK;
6466}
6467
6468/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006469 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006470 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006471 * Returns FAIL when out of memory.
6472 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006474list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475{
6476 listitem_T *li = listitem_alloc();
6477
6478 if (li == NULL)
6479 return FAIL;
6480 list_append(l, li);
6481 li->li_tv.v_type = VAR_STRING;
6482 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006483 if (str == NULL)
6484 li->li_tv.vval.v_string = NULL;
6485 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006486 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006487 return FAIL;
6488 return OK;
6489}
6490
6491/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006492 * Append "n" to list "l".
6493 * Returns FAIL when out of memory.
6494 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006497{
6498 listitem_T *li;
6499
6500 li = listitem_alloc();
6501 if (li == NULL)
6502 return FAIL;
6503 li->li_tv.v_type = VAR_NUMBER;
6504 li->li_tv.v_lock = 0;
6505 li->li_tv.vval.v_number = n;
6506 list_append(l, li);
6507 return OK;
6508}
6509
6510/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 * If "item" is NULL append at the end.
6513 * Return FAIL when out of memory.
6514 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006516list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519
6520 if (ni == NULL)
6521 return FAIL;
6522 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006523 list_insert(l, ni, item);
6524 return OK;
6525}
6526
6527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006528list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006529{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 if (item == NULL)
6531 /* Append new item at end of list. */
6532 list_append(l, ni);
6533 else
6534 {
6535 /* Insert new item before existing item. */
6536 ni->li_prev = item->li_prev;
6537 ni->li_next = item;
6538 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006539 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006541 ++l->lv_idx;
6542 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006544 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006546 l->lv_idx_item = NULL;
6547 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006549 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551}
6552
6553/*
6554 * Extend "l1" with "l2".
6555 * If "bef" is NULL append at the end, otherwise insert before this item.
6556 * Returns FAIL when out of memory.
6557 */
6558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006562 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006564 /* We also quit the loop when we have inserted the original item count of
6565 * the list, avoid a hang when we extend a list with itself. */
6566 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6568 return FAIL;
6569 return OK;
6570}
6571
6572/*
6573 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6574 * Return FAIL when out of memory.
6575 */
6576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578{
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006581 if (l1 == NULL || l2 == NULL)
6582 return FAIL;
6583
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586 if (l == NULL)
6587 return FAIL;
6588 tv->v_type = VAR_LIST;
6589 tv->vval.v_list = l;
6590
6591 /* append all items from the second list */
6592 return list_extend(l, l2, NULL);
6593}
6594
6595/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006596 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006598 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 * Returns NULL when out of memory.
6600 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006602list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603{
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 list_T *copy;
6605 listitem_T *item;
6606 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607
6608 if (orig == NULL)
6609 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610
6611 copy = list_alloc();
6612 if (copy != NULL)
6613 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 if (copyID != 0)
6615 {
6616 /* Do this before adding the items, because one of the items may
6617 * refer back to this list. */
6618 orig->lv_copyID = copyID;
6619 orig->lv_copylist = copy;
6620 }
6621 for (item = orig->lv_first; item != NULL && !got_int;
6622 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 {
6624 ni = listitem_alloc();
6625 if (ni == NULL)
6626 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628 {
6629 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6630 {
6631 vim_free(ni);
6632 break;
6633 }
6634 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006636 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 list_append(copy, ni);
6638 }
6639 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 if (item != NULL)
6641 {
6642 list_unref(copy);
6643 copy = NULL;
6644 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 }
6646
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 return copy;
6648}
6649
6650/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006652 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006653 * This used to be called list_remove, but that conflicts with a Sun header
6654 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006657vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006658{
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661 /* notify watchers */
6662 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006664 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665 list_fix_watch(l, ip);
6666 if (ip == item2)
6667 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669
6670 if (item2->li_next == NULL)
6671 l->lv_last = item->li_prev;
6672 else
6673 item2->li_next->li_prev = item->li_prev;
6674 if (item->li_prev == NULL)
6675 l->lv_first = item2->li_next;
6676 else
6677 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006678 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679}
6680
6681/*
6682 * Return an allocated string with the string representation of a list.
6683 * May return NULL.
6684 */
6685 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006686list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687{
6688 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006689
6690 if (tv->vval.v_list == NULL)
6691 return NULL;
6692 ga_init2(&ga, (int)sizeof(char), 80);
6693 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006694 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006695 {
6696 vim_free(ga.ga_data);
6697 return NULL;
6698 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699 ga_append(&ga, ']');
6700 ga_append(&ga, NUL);
6701 return (char_u *)ga.ga_data;
6702}
6703
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704typedef struct join_S {
6705 char_u *s;
6706 char_u *tofree;
6707} join_T;
6708
6709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710list_join_inner(
6711 garray_T *gap, /* to store the result in */
6712 list_T *l,
6713 char_u *sep,
6714 int echo_style,
6715 int copyID,
6716 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717{
6718 int i;
6719 join_T *p;
6720 int len;
6721 int sumlen = 0;
6722 int first = TRUE;
6723 char_u *tofree;
6724 char_u numbuf[NUMBUFLEN];
6725 listitem_T *item;
6726 char_u *s;
6727
6728 /* Stringify each item in the list. */
6729 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6730 {
6731 if (echo_style)
6732 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6733 else
6734 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6735 if (s == NULL)
6736 return FAIL;
6737
6738 len = (int)STRLEN(s);
6739 sumlen += len;
6740
Bram Moolenaarcde88542015-08-11 19:14:00 +02006741 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006742 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6743 if (tofree != NULL || s != numbuf)
6744 {
6745 p->s = s;
6746 p->tofree = tofree;
6747 }
6748 else
6749 {
6750 p->s = vim_strnsave(s, len);
6751 p->tofree = p->s;
6752 }
6753
6754 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006755 if (did_echo_string_emsg) /* recursion error, bail out */
6756 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006757 }
6758
6759 /* Allocate result buffer with its total size, avoid re-allocation and
6760 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6761 if (join_gap->ga_len >= 2)
6762 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6763 if (ga_grow(gap, sumlen + 2) == FAIL)
6764 return FAIL;
6765
6766 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6767 {
6768 if (first)
6769 first = FALSE;
6770 else
6771 ga_concat(gap, sep);
6772 p = ((join_T *)join_gap->ga_data) + i;
6773
6774 if (p->s != NULL)
6775 ga_concat(gap, p->s);
6776 line_breakcheck();
6777 }
6778
6779 return OK;
6780}
6781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006783 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006784 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006785 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006786 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006788list_join(
6789 garray_T *gap,
6790 list_T *l,
6791 char_u *sep,
6792 int echo_style,
6793 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006795 garray_T join_ga;
6796 int retval;
6797 join_T *p;
6798 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799
Bram Moolenaard39a7512015-04-16 22:51:22 +02006800 if (l->lv_len < 1)
6801 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006802 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6803 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6804
6805 /* Dispose each item in join_ga. */
6806 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006807 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006808 p = (join_T *)join_ga.ga_data;
6809 for (i = 0; i < join_ga.ga_len; ++i)
6810 {
6811 vim_free(p->tofree);
6812 ++p;
6813 }
6814 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006815 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006816
6817 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006818}
6819
6820/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006821 * Return the next (unique) copy ID.
6822 * Used for serializing nested structures.
6823 */
6824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006825get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006826{
6827 current_copyID += COPYID_INC;
6828 return current_copyID;
6829}
6830
6831/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 * Garbage collection for lists and dictionaries.
6833 *
6834 * We use reference counts to be able to free most items right away when they
6835 * are no longer used. But for composite items it's possible that it becomes
6836 * unused while the reference count is > 0: When there is a recursive
6837 * reference. Example:
6838 * :let l = [1, 2, 3]
6839 * :let d = {9: l}
6840 * :let l[1] = d
6841 *
6842 * Since this is quite unusual we handle this with garbage collection: every
6843 * once in a while find out which lists and dicts are not referenced from any
6844 * variable.
6845 *
6846 * Here is a good reference text about garbage collection (refers to Python
6847 * but it applies to all reference-counting mechanisms):
6848 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850
6851/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 * Do garbage collection for lists and dicts.
6853 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006854 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006857{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006859 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 buf_T *buf;
6861 win_T *wp;
6862 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006863 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006864 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006866#ifdef FEAT_WINDOWS
6867 tabpage_T *tp;
6868#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006870 /* Only do this once. */
6871 want_garbage_collect = FALSE;
6872 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006873 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006874
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 /* We advance by two because we add one for items referenced through
6876 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006877 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 /*
6880 * 1. Go through all accessible variables and mark all lists and dicts
6881 * with copyID.
6882 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883
6884 /* Don't free variables in the previous_funccal list unless they are only
6885 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006886 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6888 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6890 NULL);
6891 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6892 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893 }
6894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 /* script-local variables */
6896 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* buffer-local variables */
6900 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6902 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
6904 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6907 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006908#ifdef FEAT_AUTOCMD
6909 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6911 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006912#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006914#ifdef FEAT_WINDOWS
6915 /* tabpage-local variables */
6916 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006919#endif
6920
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923
6924 /* function-local variables */
6925 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6926 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6928 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 }
6930
Bram Moolenaard812df62008-11-09 12:46:09 +00006931 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006933
Bram Moolenaar1dced572012-04-05 16:54:08 +02006934#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006936#endif
6937
Bram Moolenaardb913952012-06-29 12:54:53 +02006938#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006940#endif
6941
6942#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006944#endif
6945
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006946#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006947 abort = abort || set_ref_in_channel(copyID);
6948#endif
6949
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 /*
6953 * 2. Free lists and dictionaries that are not referenced.
6954 */
6955 did_free = free_unref_items(copyID);
6956
6957 /*
6958 * 3. Check if any funccal can be freed now.
6959 */
6960 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 if (can_free_funccal(*pfc, copyID))
6963 {
6964 fc = *pfc;
6965 *pfc = fc->caller;
6966 free_funccal(fc, TRUE);
6967 did_free = TRUE;
6968 did_free_funccal = TRUE;
6969 }
6970 else
6971 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006973 if (did_free_funccal)
6974 /* When a funccal was freed some more items might be garbage
6975 * collected, so run again. */
6976 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006978 else if (p_verbose > 0)
6979 {
6980 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6981 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982
6983 return did_free;
6984}
6985
6986/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006987 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988 */
6989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006990free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 dict_T *dd, *dd_next;
6993 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 int did_free = FALSE;
6995
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 */
6999 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007000 {
7001 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007004 /* Free the Dictionary and ordinary items it contains, but don't
7005 * recurse into Lists and Dictionaries, they will be in the list
7006 * of dicts or list of lists. */
7007 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007010 dd = dd_next;
7011 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012
7013 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 * Go through the list of lists and free items without the copyID.
7015 * But don't free a list that has a watcher (used in a for loop), these
7016 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 */
7018 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007019 {
7020 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007021 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7022 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007024 /* Free the List and ordinary items it contains, but don't recurse
7025 * into Lists and Dictionaries, they will be in the list of dicts
7026 * or list of lists. */
7027 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007030 ll = ll_next;
7031 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 return did_free;
7034}
7035
7036/*
7037 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 * "list_stack" is used to add lists to be marked. Can be NULL.
7039 *
7040 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007043set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044{
7045 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 hashtab_T *cur_ht;
7049 ht_stack_T *ht_stack = NULL;
7050 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 cur_ht = ht;
7053 for (;;)
7054 {
7055 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 /* Mark each item in the hashtab. If the item contains a hashtab
7058 * it is added to ht_stack, if it contains a list it is added to
7059 * list_stack. */
7060 todo = (int)cur_ht->ht_used;
7061 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7062 if (!HASHITEM_EMPTY(hi))
7063 {
7064 --todo;
7065 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7066 &ht_stack, list_stack);
7067 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069
7070 if (ht_stack == NULL)
7071 break;
7072
7073 /* take an item from the stack */
7074 cur_ht = ht_stack->ht;
7075 tempitem = ht_stack;
7076 ht_stack = ht_stack->prev;
7077 free(tempitem);
7078 }
7079
7080 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081}
7082
7083/*
7084 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7086 *
7087 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007090set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007091{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 listitem_T *li;
7093 int abort = FALSE;
7094 list_T *cur_l;
7095 list_stack_T *list_stack = NULL;
7096 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 cur_l = l;
7099 for (;;)
7100 {
7101 if (!abort)
7102 /* Mark each item in the list. If the item contains a hashtab
7103 * it is added to ht_stack, if it contains a list it is added to
7104 * list_stack. */
7105 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7106 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7107 ht_stack, &list_stack);
7108 if (list_stack == NULL)
7109 break;
7110
7111 /* take an item from the stack */
7112 cur_l = list_stack->list;
7113 tempitem = list_stack;
7114 list_stack = list_stack->prev;
7115 free(tempitem);
7116 }
7117
7118 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119}
7120
7121/*
7122 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007123 * "list_stack" is used to add lists to be marked. Can be NULL.
7124 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7125 *
7126 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007129set_ref_in_item(
7130 typval_T *tv,
7131 int copyID,
7132 ht_stack_T **ht_stack,
7133 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007134{
7135 dict_T *dd;
7136 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007138
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007140 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007141 dd = tv->vval.v_dict;
7142 if (dd != NULL && dd->dv_copyID != copyID)
7143 {
7144 /* Didn't see this dict yet. */
7145 dd->dv_copyID = copyID;
7146 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007148 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7149 }
7150 else
7151 {
7152 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7153 if (newitem == NULL)
7154 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 else
7156 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007157 newitem->ht = &dd->dv_hashtab;
7158 newitem->prev = *ht_stack;
7159 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007161 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007162 }
7163 }
7164 else if (tv->v_type == VAR_LIST)
7165 {
7166 ll = tv->vval.v_list;
7167 if (ll != NULL && ll->lv_copyID != copyID)
7168 {
7169 /* Didn't see this list yet. */
7170 ll->lv_copyID = copyID;
7171 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007173 abort = set_ref_in_list(ll, copyID, ht_stack);
7174 }
7175 else
7176 {
7177 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007179 if (newitem == NULL)
7180 abort = TRUE;
7181 else
7182 {
7183 newitem->list = ll;
7184 newitem->prev = *list_stack;
7185 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007187 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007188 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007189 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007191}
7192
7193/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194 * Allocate an empty header for a dictionary.
7195 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007196 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007197dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198{
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007203 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007204 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007205 if (first_dict != NULL)
7206 first_dict->dv_used_prev = d;
7207 d->dv_used_next = first_dict;
7208 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007209 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007212 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007213 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007214 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007215 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218}
7219
7220/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007221 * Allocate an empty dict for a return value.
7222 * Returns OK or FAIL.
7223 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007225rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007226{
7227 dict_T *d = dict_alloc();
7228
7229 if (d == NULL)
7230 return FAIL;
7231
7232 rettv->vval.v_dict = d;
7233 rettv->v_type = VAR_DICT;
7234 ++d->dv_refcount;
7235 return OK;
7236}
7237
7238
7239/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 * Unreference a Dictionary: decrement the reference count and free it when it
7241 * becomes zero.
7242 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007246 if (d != NULL && --d->dv_refcount <= 0)
7247 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248}
7249
7250/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007251 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 * Ignores the reference count.
7253 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007255dict_free(
7256 dict_T *d,
7257 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007260 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007261 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007263 /* Remove the dict from the list of dicts for garbage collection. */
7264 if (d->dv_used_prev == NULL)
7265 first_dict = d->dv_used_next;
7266 else
7267 d->dv_used_prev->dv_used_next = d->dv_used_next;
7268 if (d->dv_used_next != NULL)
7269 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7270
7271 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007272 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007273 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 if (!HASHITEM_EMPTY(hi))
7277 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007278 /* Remove the item before deleting it, just in case there is
7279 * something recursive causing trouble. */
7280 di = HI2DI(hi);
7281 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007282 if (recurse || (di->di_tv.v_type != VAR_LIST
7283 && di->di_tv.v_type != VAR_DICT))
7284 clear_tv(&di->di_tv);
7285 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 --todo;
7287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 vim_free(d);
7291}
7292
7293/*
7294 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295 * The "key" is copied to the new item.
7296 * Note that the value of the item "di_tv" still needs to be initialized!
7297 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007299 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301{
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007304 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007308 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007309 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311}
7312
7313/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314 * Make a copy of a Dictionary item.
7315 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007317dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318{
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7322 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 if (di != NULL)
7324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007326 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 copy_tv(&org->di_tv, &di->di_tv);
7328 }
7329 return di;
7330}
7331
7332/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 * Remove item "item" from Dictionary "dict" and free it.
7334 */
7335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007336dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337{
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 if (HASHITEM_EMPTY(hi))
7342 EMSG2(_(e_intern2), "dictitem_remove()");
7343 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 dictitem_free(item);
7346}
7347
7348/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349 * Free a dict item. Also clears the value.
7350 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007352dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007355 if (item->di_flags & DI_FLAGS_ALLOC)
7356 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357}
7358
7359/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7361 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 * Returns NULL when out of memory.
7364 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007366dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367{
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 dict_T *copy;
7369 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372
7373 if (orig == NULL)
7374 return NULL;
7375
7376 copy = dict_alloc();
7377 if (copy != NULL)
7378 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007379 if (copyID != 0)
7380 {
7381 orig->dv_copyID = copyID;
7382 orig->dv_copydict = copy;
7383 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007384 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 --todo;
7390
7391 di = dictitem_alloc(hi->hi_key);
7392 if (di == NULL)
7393 break;
7394 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 {
7396 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7397 copyID) == FAIL)
7398 {
7399 vim_free(di);
7400 break;
7401 }
7402 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 else
7404 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7405 if (dict_add(copy, di) == FAIL)
7406 {
7407 dictitem_free(di);
7408 break;
7409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007412
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007414 if (todo > 0)
7415 {
7416 dict_unref(copy);
7417 copy = NULL;
7418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 }
7420
7421 return copy;
7422}
7423
7424/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007426 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007429dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430{
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432}
7433
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007435 * Add a number or string entry to dictionary "d".
7436 * When "str" is NULL use number "nr", otherwise use "str".
7437 * Returns FAIL when out of memory and when key already exists.
7438 */
7439 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007440dict_add_nr_str(
7441 dict_T *d,
7442 char *key,
7443 long nr,
7444 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007445{
7446 dictitem_T *item;
7447
7448 item = dictitem_alloc((char_u *)key);
7449 if (item == NULL)
7450 return FAIL;
7451 item->di_tv.v_lock = 0;
7452 if (str == NULL)
7453 {
7454 item->di_tv.v_type = VAR_NUMBER;
7455 item->di_tv.vval.v_number = nr;
7456 }
7457 else
7458 {
7459 item->di_tv.v_type = VAR_STRING;
7460 item->di_tv.vval.v_string = vim_strsave(str);
7461 }
7462 if (dict_add(d, item) == FAIL)
7463 {
7464 dictitem_free(item);
7465 return FAIL;
7466 }
7467 return OK;
7468}
7469
7470/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007471 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007472 * Returns FAIL when out of memory and when key already exists.
7473 */
7474 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007476{
7477 dictitem_T *item;
7478
7479 item = dictitem_alloc((char_u *)key);
7480 if (item == NULL)
7481 return FAIL;
7482 item->di_tv.v_lock = 0;
7483 item->di_tv.v_type = VAR_LIST;
7484 item->di_tv.vval.v_list = list;
7485 if (dict_add(d, item) == FAIL)
7486 {
7487 dictitem_free(item);
7488 return FAIL;
7489 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007490 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007491 return OK;
7492}
7493
7494/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 * Get the number of items in a Dictionary.
7496 */
7497 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007498dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007500 if (d == NULL)
7501 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007502 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503}
7504
7505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 * Find item "key[len]" in Dictionary "d".
7507 * If "len" is negative use strlen(key).
7508 * Returns NULL when not found.
7509 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007510 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007511dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513#define AKEYLEN 200
7514 char_u buf[AKEYLEN];
7515 char_u *akey;
7516 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 if (len < 0)
7520 akey = key;
7521 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007522 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 tofree = akey = vim_strnsave(key, len);
7524 if (akey == NULL)
7525 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007526 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 else
7528 {
7529 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007530 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 akey = buf;
7532 }
7533
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 vim_free(tofree);
7536 if (HASHITEM_EMPTY(hi))
7537 return NULL;
7538 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539}
7540
7541/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007542 * Get a string item from a dictionary.
7543 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007544 * Returns NULL if the entry doesn't exist or out of memory.
7545 */
7546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007548{
7549 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007550 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007551
7552 di = dict_find(d, key, -1);
7553 if (di == NULL)
7554 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007555 s = get_tv_string(&di->di_tv);
7556 if (save && s != NULL)
7557 s = vim_strsave(s);
7558 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007559}
7560
7561/*
7562 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007563 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007564 */
7565 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007566get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007567{
7568 dictitem_T *di;
7569
7570 di = dict_find(d, key, -1);
7571 if (di == NULL)
7572 return 0;
7573 return get_tv_number(&di->di_tv);
7574}
7575
7576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 * Return an allocated string with the string representation of a Dictionary.
7578 * May return NULL.
7579 */
7580 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582{
7583 garray_T ga;
7584 int first = TRUE;
7585 char_u *tofree;
7586 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593 return NULL;
7594 ga_init2(&ga, (int)sizeof(char), 80);
7595 ga_append(&ga, '{');
7596
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007597 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007598 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007600 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602 --todo;
7603
7604 if (first)
7605 first = FALSE;
7606 else
7607 ga_concat(&ga, (char_u *)", ");
7608
7609 tofree = string_quote(hi->hi_key, FALSE);
7610 if (tofree != NULL)
7611 {
7612 ga_concat(&ga, tofree);
7613 vim_free(tofree);
7614 }
7615 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007616 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 if (s != NULL)
7618 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007620 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007621 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007622 line_breakcheck();
7623
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007626 if (todo > 0)
7627 {
7628 vim_free(ga.ga_data);
7629 return NULL;
7630 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631
7632 ga_append(&ga, '}');
7633 ga_append(&ga, NUL);
7634 return (char_u *)ga.ga_data;
7635}
7636
7637/*
7638 * Allocate a variable for a Dictionary and fill it from "*arg".
7639 * Return OK or FAIL. Returns NOTDONE for {expr}.
7640 */
7641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643{
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 dict_T *d = NULL;
7645 typval_T tvkey;
7646 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007647 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651
7652 /*
7653 * First check if it's not a curly-braces thing: {expr}.
7654 * Must do this without evaluating, otherwise a function may be called
7655 * twice. Unfortunately this means we need to call eval1() twice for the
7656 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007659 if (*start != '}')
7660 {
7661 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7662 return FAIL;
7663 if (*start == '}')
7664 return NOTDONE;
7665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666
7667 if (evaluate)
7668 {
7669 d = dict_alloc();
7670 if (d == NULL)
7671 return FAIL;
7672 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 tvkey.v_type = VAR_UNKNOWN;
7674 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675
7676 *arg = skipwhite(*arg + 1);
7677 while (**arg != '}' && **arg != NUL)
7678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 goto failret;
7681 if (**arg != ':')
7682 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007683 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 goto failret;
7686 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007687 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007689 key = get_tv_string_buf_chk(&tvkey, buf);
7690 if (key == NULL || *key == NUL)
7691 {
7692 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7693 if (key != NULL)
7694 EMSG(_(e_emptykey));
7695 clear_tv(&tvkey);
7696 goto failret;
7697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699
7700 *arg = skipwhite(*arg + 1);
7701 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7702 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007703 if (evaluate)
7704 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 goto failret;
7706 }
7707 if (evaluate)
7708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007709 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 if (item != NULL)
7711 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007712 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 clear_tv(&tv);
7715 goto failret;
7716 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 item = dictitem_alloc(key);
7718 clear_tv(&tvkey);
7719 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007722 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007723 if (dict_add(d, item) == FAIL)
7724 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 }
7726 }
7727
7728 if (**arg == '}')
7729 break;
7730 if (**arg != ',')
7731 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007732 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 goto failret;
7734 }
7735 *arg = skipwhite(*arg + 1);
7736 }
7737
7738 if (**arg != '}')
7739 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007740 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741failret:
7742 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007743 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 return FAIL;
7745 }
7746
7747 *arg = skipwhite(*arg + 1);
7748 if (evaluate)
7749 {
7750 rettv->v_type = VAR_DICT;
7751 rettv->vval.v_dict = d;
7752 ++d->dv_refcount;
7753 }
7754
7755 return OK;
7756}
7757
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007758#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759#endif
7760
Bram Moolenaar17a13432016-01-24 14:22:10 +01007761 static char *
7762get_var_special_name(int nr)
7763{
7764 switch (nr)
7765 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007766 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007767 case VVAL_TRUE: return "v:true";
7768 case VVAL_NONE: return "v:none";
7769 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007770 }
7771 EMSG2(_(e_intern2), "get_var_special_name()");
7772 return "42";
7773}
7774
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 * Return a string with the string representation of a variable.
7777 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007778 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007779 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007780 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007781 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 */
7783 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007784echo_string(
7785 typval_T *tv,
7786 char_u **tofree,
7787 char_u *numbuf,
7788 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 static int recurse = 0;
7791 char_u *r = NULL;
7792
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007794 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007795 if (!did_echo_string_emsg)
7796 {
7797 /* Only give this message once for a recursive call to avoid
7798 * flooding the user with errors. And stop iterating over lists
7799 * and dicts. */
7800 did_echo_string_emsg = TRUE;
7801 EMSG(_("E724: variable nested too deep for displaying"));
7802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007804 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007805 }
7806 ++recurse;
7807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007808 switch (tv->v_type)
7809 {
7810 case VAR_FUNC:
7811 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007812 r = tv->vval.v_string;
7813 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007814
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007815 case VAR_PARTIAL:
7816 *tofree = NULL;
7817 /* TODO: arguments */
7818 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7819 break;
7820
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007822 if (tv->vval.v_list == NULL)
7823 {
7824 *tofree = NULL;
7825 r = NULL;
7826 }
7827 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7828 {
7829 *tofree = NULL;
7830 r = (char_u *)"[...]";
7831 }
7832 else
7833 {
7834 tv->vval.v_list->lv_copyID = copyID;
7835 *tofree = list2string(tv, copyID);
7836 r = *tofree;
7837 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007838 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007839
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841 if (tv->vval.v_dict == NULL)
7842 {
7843 *tofree = NULL;
7844 r = NULL;
7845 }
7846 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7847 {
7848 *tofree = NULL;
7849 r = (char_u *)"{...}";
7850 }
7851 else
7852 {
7853 tv->vval.v_dict->dv_copyID = copyID;
7854 *tofree = dict2string(tv, copyID);
7855 r = *tofree;
7856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007858
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007859 case VAR_STRING:
7860 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007861 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007862 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007863 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 *tofree = NULL;
7865 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007866 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007867
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007869#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870 *tofree = NULL;
7871 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7872 r = numbuf;
7873 break;
7874#endif
7875
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007876 case VAR_SPECIAL:
7877 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007878 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007879 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007881
Bram Moolenaar8502c702014-06-17 12:51:16 +02007882 if (--recurse == 0)
7883 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885}
7886
7887/*
7888 * Return a string with the string representation of a variable.
7889 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7890 * "numbuf" is used for a number.
7891 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007892 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 */
7894 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895tv2string(
7896 typval_T *tv,
7897 char_u **tofree,
7898 char_u *numbuf,
7899 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900{
7901 switch (tv->v_type)
7902 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007903 case VAR_FUNC:
7904 *tofree = string_quote(tv->vval.v_string, TRUE);
7905 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007906 case VAR_PARTIAL:
7907 *tofree = string_quote(tv->vval.v_partial == NULL ? NULL
7908 : tv->vval.v_partial->pt_name, TRUE);
7909 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 case VAR_STRING:
7911 *tofree = string_quote(tv->vval.v_string, FALSE);
7912 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007914#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007915 *tofree = NULL;
7916 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7917 return numbuf;
7918#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007919 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007920 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007922 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007923 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007924 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007925 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007926 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007928 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929}
7930
7931/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007932 * Return string "str" in ' quotes, doubling ' characters.
7933 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 */
7936 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007937string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007938{
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940 char_u *p, *r, *s;
7941
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 len = (function ? 13 : 3);
7943 if (str != NULL)
7944 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007945 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 for (p = str; *p != NUL; mb_ptr_adv(p))
7947 if (*p == '\'')
7948 ++len;
7949 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 s = r = alloc(len);
7951 if (r != NULL)
7952 {
7953 if (function)
7954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007956 r += 10;
7957 }
7958 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 if (str != NULL)
7961 for (p = str; *p != NUL; )
7962 {
7963 if (*p == '\'')
7964 *r++ = '\'';
7965 MB_COPY_CHAR(p, r);
7966 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 if (function)
7969 *r++ = ')';
7970 *r++ = NUL;
7971 }
7972 return s;
7973}
7974
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007975#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976/*
7977 * Convert the string "text" to a floating point number.
7978 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7979 * this always uses a decimal point.
7980 * Returns the length of the text that was consumed.
7981 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007983string2float(
7984 char_u *text,
7985 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986{
7987 char *s = (char *)text;
7988 float_T f;
7989
7990 f = strtod(s, &s);
7991 *value = f;
7992 return (int)((char_u *)s - text);
7993}
7994#endif
7995
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 * Get the value of an environment variable.
7998 * "arg" is pointing to the '$'. It is advanced to after the name.
7999 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008000 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 */
8002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 char_u *string = NULL;
8006 int len;
8007 int cc;
8008 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008009 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
8011 ++*arg;
8012 name = *arg;
8013 len = get_env_len(arg);
8014 if (evaluate)
8015 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008016 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008017 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008018
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008019 cc = name[len];
8020 name[len] = NUL;
8021 /* first try vim_getenv(), fast for normal environment vars */
8022 string = vim_getenv(name, &mustfree);
8023 if (string != NULL && *string != NUL)
8024 {
8025 if (!mustfree)
8026 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008028 else
8029 {
8030 if (mustfree)
8031 vim_free(string);
8032
8033 /* next try expanding things like $VIM and ${HOME} */
8034 string = expand_env_save(name - 1);
8035 if (string != NULL && *string == '$')
8036 {
8037 vim_free(string);
8038 string = NULL;
8039 }
8040 }
8041 name[len] = cc;
8042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043 rettv->v_type = VAR_STRING;
8044 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 }
8046
8047 return OK;
8048}
8049
8050/*
8051 * Array with names and number of arguments of all internal functions
8052 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8053 */
8054static struct fst
8055{
8056 char *f_name; /* function name */
8057 char f_min_argc; /* minimal number of arguments */
8058 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008059 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008060 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061} functions[] =
8062{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063#ifdef FEAT_FLOAT
8064 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008065 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008067 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008068 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008069 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"append", 2, 2, f_append},
8071 {"argc", 0, 0, f_argc},
8072 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008073 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008074 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008075#ifdef FEAT_FLOAT
8076 {"asin", 1, 1, f_asin}, /* WJMc */
8077#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008078 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008079 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008080 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008081 {"assert_false", 1, 2, f_assert_false},
8082 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#ifdef FEAT_FLOAT
8084 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008085 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008088 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"bufexists", 1, 1, f_bufexists},
8090 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8091 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8092 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8093 {"buflisted", 1, 1, f_buflisted},
8094 {"bufloaded", 1, 1, f_bufloaded},
8095 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008096 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"bufwinnr", 1, 1, f_bufwinnr},
8098 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008099 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008100 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008101 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#ifdef FEAT_FLOAT
8103 {"ceil", 1, 1, f_ceil},
8104#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008106 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008107 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8108 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008109 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008110 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008111 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008112 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008113 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008114 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008115 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008116 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8117 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008118 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008119 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008120#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008121 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008122 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008124 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008126#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008127 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008128 {"complete_add", 1, 1, f_complete_add},
8129 {"complete_check", 0, 0, f_complete_check},
8130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#ifdef FEAT_FLOAT
8134 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008135 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008137 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008139 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008140 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008141 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008143 {"diff_filler", 1, 1, f_diff_filler},
8144 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008145 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008146 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008148 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"eventhandler", 0, 0, f_eventhandler},
8150 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008151 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008153#ifdef FEAT_FLOAT
8154 {"exp", 1, 1, f_exp},
8155#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008156 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008157 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008158 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8160 {"filereadable", 1, 1, f_filereadable},
8161 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008162 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008163 {"finddir", 1, 3, f_finddir},
8164 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"float2nr", 1, 1, f_float2nr},
8167 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008168 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008170 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"fnamemodify", 2, 2, f_fnamemodify},
8172 {"foldclosed", 1, 1, f_foldclosed},
8173 {"foldclosedend", 1, 1, f_foldclosedend},
8174 {"foldlevel", 1, 1, f_foldlevel},
8175 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008176 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008178 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008179 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008180 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008181 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008182 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"getchar", 0, 1, f_getchar},
8184 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008185 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getcmdline", 0, 0, f_getcmdline},
8187 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008188 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008189 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008190 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008191 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008192 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008193 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"getfsize", 1, 1, f_getfsize},
8195 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008196 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008197 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008198 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008199 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008200 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008201 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008202 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008203 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008205 {"gettabvar", 2, 3, f_gettabvar},
8206 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"getwinposx", 0, 0, f_getwinposx},
8208 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008209 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008210 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008211 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008212 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008214 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008215 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008216 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8218 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8219 {"histadd", 2, 2, f_histadd},
8220 {"histdel", 1, 2, f_histdel},
8221 {"histget", 1, 2, f_histget},
8222 {"histnr", 1, 1, f_histnr},
8223 {"hlID", 1, 1, f_hlID},
8224 {"hlexists", 1, 1, f_hlexists},
8225 {"hostname", 0, 0, f_hostname},
8226 {"iconv", 3, 3, f_iconv},
8227 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008228 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008229 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008231 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"inputrestore", 0, 0, f_inputrestore},
8233 {"inputsave", 0, 0, f_inputsave},
8234 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008236 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008238 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008239#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8240 {"isnan", 1, 1, f_isnan},
8241#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008242 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008243#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008244 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008245 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008246 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008247 {"job_start", 1, 2, f_job_start},
8248 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008249 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008250#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008251 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008252 {"js_decode", 1, 1, f_js_decode},
8253 {"js_encode", 1, 1, f_js_encode},
8254 {"json_decode", 1, 1, f_json_decode},
8255 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008256 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008258 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"libcall", 3, 3, f_libcall},
8260 {"libcallnr", 3, 3, f_libcallnr},
8261 {"line", 1, 1, f_line},
8262 {"line2byte", 1, 1, f_line2byte},
8263 {"lispindent", 1, 1, f_lispindent},
8264 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008266 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008267 {"log10", 1, 1, f_log10},
8268#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008269#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008270 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008271#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008272 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008273 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008274 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008275 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008276 {"matchadd", 2, 5, f_matchadd},
8277 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008278 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008279 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008280 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008281 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008282 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008283 {"max", 1, 1, f_max},
8284 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008285#ifdef vim_mkdir
8286 {"mkdir", 1, 3, f_mkdir},
8287#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008288 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008289#ifdef FEAT_MZSCHEME
8290 {"mzeval", 1, 1, f_mzeval},
8291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008293 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008294 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008295 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008296#ifdef FEAT_PERL
8297 {"perleval", 1, 1, f_perleval},
8298#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008299#ifdef FEAT_FLOAT
8300 {"pow", 2, 2, f_pow},
8301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008303 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008304 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008305#ifdef FEAT_PYTHON3
8306 {"py3eval", 1, 1, f_py3eval},
8307#endif
8308#ifdef FEAT_PYTHON
8309 {"pyeval", 1, 1, f_pyeval},
8310#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008311 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008312 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008313 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008314#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008315 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008316#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008317 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"remote_expr", 2, 3, f_remote_expr},
8319 {"remote_foreground", 1, 1, f_remote_foreground},
8320 {"remote_peek", 1, 2, f_remote_peek},
8321 {"remote_read", 1, 1, f_remote_read},
8322 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008323 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008325 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008327 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008328#ifdef FEAT_FLOAT
8329 {"round", 1, 1, f_round},
8330#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008331 {"screenattr", 2, 2, f_screenattr},
8332 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008333 {"screencol", 0, 0, f_screencol},
8334 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008335 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008336 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008337 {"searchpair", 3, 7, f_searchpair},
8338 {"searchpairpos", 3, 7, f_searchpairpos},
8339 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"server2client", 2, 2, f_server2client},
8341 {"serverlist", 0, 0, f_serverlist},
8342 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008343 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008345 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008347 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008348 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008349 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008350 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008352 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008353 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008355#ifdef FEAT_CRYPT
8356 {"sha256", 1, 1, f_sha256},
8357#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008358 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008359 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361#ifdef FEAT_FLOAT
8362 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008363 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008364#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008365 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008366 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008367 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008368 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008369 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
8371 {"sqrt", 1, 1, f_sqrt},
8372 {"str2float", 1, 1, f_str2float},
8373#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008374 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008375 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008376 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377#ifdef HAVE_STRFTIME
8378 {"strftime", 1, 2, f_strftime},
8379#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008381 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"strlen", 1, 1, f_strlen},
8383 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008384 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008386 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008387 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"substitute", 4, 4, f_substitute},
8389 {"synID", 3, 3, f_synID},
8390 {"synIDattr", 2, 3, f_synIDattr},
8391 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008392 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008393 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008394 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008395 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008396 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008397 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008398 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008399 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008400 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008401#ifdef FEAT_FLOAT
8402 {"tan", 1, 1, f_tan},
8403 {"tanh", 1, 1, f_tanh},
8404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008406 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"tolower", 1, 1, f_tolower},
8408 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008409 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008410#ifdef FEAT_FLOAT
8411 {"trunc", 1, 1, f_trunc},
8412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008414 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008415 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008416 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008417 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"virtcol", 1, 1, f_virtcol},
8419 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008420 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008421 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008422 {"win_getid", 0, 2, f_win_getid},
8423 {"win_gotoid", 1, 1, f_win_gotoid},
8424 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8425 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"winbufnr", 1, 1, f_winbufnr},
8427 {"wincol", 0, 0, f_wincol},
8428 {"winheight", 1, 1, f_winheight},
8429 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008430 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008432 {"winrestview", 1, 1, f_winrestview},
8433 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008435 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008436 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008437 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438};
8439
8440#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8441
8442/*
8443 * Function given to ExpandGeneric() to obtain the list of internal
8444 * or user defined function names.
8445 */
8446 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008447get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448{
8449 static int intidx = -1;
8450 char_u *name;
8451
8452 if (idx == 0)
8453 intidx = -1;
8454 if (intidx < 0)
8455 {
8456 name = get_user_func_name(xp, idx);
8457 if (name != NULL)
8458 return name;
8459 }
8460 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8461 {
8462 STRCPY(IObuff, functions[intidx].f_name);
8463 STRCAT(IObuff, "(");
8464 if (functions[intidx].f_max_argc == 0)
8465 STRCAT(IObuff, ")");
8466 return IObuff;
8467 }
8468
8469 return NULL;
8470}
8471
8472/*
8473 * Function given to ExpandGeneric() to obtain the list of internal or
8474 * user defined variable or function names.
8475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008477get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 static int intidx = -1;
8480 char_u *name;
8481
8482 if (idx == 0)
8483 intidx = -1;
8484 if (intidx < 0)
8485 {
8486 name = get_function_name(xp, idx);
8487 if (name != NULL)
8488 return name;
8489 }
8490 return get_user_var_name(xp, ++intidx);
8491}
8492
8493#endif /* FEAT_CMDL_COMPL */
8494
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008495#if defined(EBCDIC) || defined(PROTO)
8496/*
8497 * Compare struct fst by function name.
8498 */
8499 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008500compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008501{
8502 struct fst *p1 = (struct fst *)s1;
8503 struct fst *p2 = (struct fst *)s2;
8504
8505 return STRCMP(p1->f_name, p2->f_name);
8506}
8507
8508/*
8509 * Sort the function table by function name.
8510 * The sorting of the table above is ASCII dependant.
8511 * On machines using EBCDIC we have to sort it.
8512 */
8513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008514sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008515{
8516 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8517
8518 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8519}
8520#endif
8521
8522
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523/*
8524 * Find internal function in table above.
8525 * Return index, or -1 if not found
8526 */
8527 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008528find_internal_func(
8529 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 int first = 0;
8532 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8533 int cmp;
8534 int x;
8535
8536 /*
8537 * Find the function name in the table. Binary search.
8538 */
8539 while (first <= last)
8540 {
8541 x = first + ((unsigned)(last - first) >> 1);
8542 cmp = STRCMP(name, functions[x].f_name);
8543 if (cmp < 0)
8544 last = x - 1;
8545 else if (cmp > 0)
8546 first = x + 1;
8547 else
8548 return x;
8549 }
8550 return -1;
8551}
8552
8553/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008554 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8555 * name it contains, otherwise return "name".
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008556 * If "name" is of type VAR_PARTIAL also return "partial"
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 */
8558 static char_u *
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008559deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560{
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008562 int cc;
8563
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008564 *partial = NULL;
8565
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008566 cc = name[*lenp];
8567 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008568 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008569 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008570 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008573 {
8574 *lenp = 0;
8575 return (char_u *)""; /* just in case */
8576 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008577 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008579 }
8580
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008581 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8582 {
8583 *partial = v->di_tv.vval.v_partial;
8584 if (*partial == NULL)
8585 {
8586 *lenp = 0;
8587 return (char_u *)""; /* just in case */
8588 }
8589 *lenp = (int)STRLEN((*partial)->pt_name);
8590 return (*partial)->pt_name;
8591 }
8592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 return name;
8594}
8595
8596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 * Allocate a variable for the result of a function.
8598 * Return OK or FAIL.
8599 */
8600 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008601get_func_tv(
8602 char_u *name, /* name of the function */
8603 int len, /* length of "name" */
8604 typval_T *rettv,
8605 char_u **arg, /* argument, pointing to the '(' */
8606 linenr_T firstline, /* first line of range */
8607 linenr_T lastline, /* last line of range */
8608 int *doesrange, /* return: function handled range */
8609 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008610 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008611 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
8613 char_u *argp;
8614 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008615 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 int argcount = 0; /* number of arguments found */
8617
8618 /*
8619 * Get the arguments.
8620 */
8621 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008622 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {
8624 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8625 if (*argp == ')' || *argp == ',' || *argp == NUL)
8626 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8628 {
8629 ret = FAIL;
8630 break;
8631 }
8632 ++argcount;
8633 if (*argp != ',')
8634 break;
8635 }
8636 if (*argp == ')')
8637 ++argp;
8638 else
8639 ret = FAIL;
8640
8641 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008643 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 {
8646 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008647 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008649 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
8652 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
8655 *arg = skipwhite(argp);
8656 return ret;
8657}
8658
8659
8660/*
8661 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008662 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008663 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008666call_func(
8667 char_u *funcname, /* name of the function */
8668 int len, /* length of "name" */
8669 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008670 int argcount_in, /* number of "argvars" */
8671 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008672 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008673 linenr_T firstline, /* first line of range */
8674 linenr_T lastline, /* last line of range */
8675 int *doesrange, /* return: function handled range */
8676 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008677 partial_T *partial, /* optional, can be NULL */
8678 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681#define ERROR_UNKNOWN 0
8682#define ERROR_TOOMANY 1
8683#define ERROR_TOOFEW 2
8684#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008685#define ERROR_DICT 4
8686#define ERROR_NONE 5
8687#define ERROR_OTHER 6
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008688#define ERROR_BOTH 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 int error = ERROR_NONE;
8690 int i;
8691 int llen;
8692 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693#define FLEN_FIXED 40
8694 char_u fname_buf[FLEN_FIXED + 1];
8695 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008696 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008697 int argcount = argcount_in;
8698 typval_T *argvars = argvars_in;
8699 dict_T *selfdict = selfdict_in;
8700 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8701 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008702
8703 /* Make a copy of the name, if it comes from a funcref variable it could
8704 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008705 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008706 if (name == NULL)
8707 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
8709 /*
8710 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8711 * Change <SNR>123_name() to K_SNR 123_name().
8712 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 llen = eval_fname_script(name);
8715 if (llen > 0)
8716 {
8717 fname_buf[0] = K_SPECIAL;
8718 fname_buf[1] = KS_EXTRA;
8719 fname_buf[2] = (int)KE_SNR;
8720 i = 3;
8721 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8722 {
8723 if (current_SID <= 0)
8724 error = ERROR_SCRIPT;
8725 else
8726 {
8727 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8728 i = (int)STRLEN(fname_buf);
8729 }
8730 }
8731 if (i + STRLEN(name + llen) < FLEN_FIXED)
8732 {
8733 STRCPY(fname_buf + i, name + llen);
8734 fname = fname_buf;
8735 }
8736 else
8737 {
8738 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8739 if (fname == NULL)
8740 error = ERROR_OTHER;
8741 else
8742 {
8743 mch_memmove(fname, fname_buf, (size_t)i);
8744 STRCPY(fname + i, name + llen);
8745 }
8746 }
8747 }
8748 else
8749 fname = name;
8750
8751 *doesrange = FALSE;
8752
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008753 if (partial != NULL)
8754 {
8755 if (partial->pt_dict != NULL)
8756 {
8757 if (selfdict_in != NULL)
8758 error = ERROR_BOTH;
8759 selfdict = partial->pt_dict;
8760 }
8761 if (error == ERROR_NONE && partial->pt_argc > 0)
8762 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008763 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8764 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8765 for (i = 0; i < argcount_in; ++i)
8766 argv[i + argv_clear] = argvars_in[i];
8767 argvars = argv;
8768 argcount = partial->pt_argc + argcount_in;
8769 }
8770 }
8771
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 /* execute the function if no errors detected and executing */
8774 if (evaluate && error == ERROR_NONE)
8775 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008776 char_u *rfname = fname;
8777
8778 /* Ignore "g:" before a function name. */
8779 if (fname[0] == 'g' && fname[1] == ':')
8780 rfname = fname + 2;
8781
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008782 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8783 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 error = ERROR_UNKNOWN;
8785
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008786 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
8788 /*
8789 * User defined function.
8790 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008791 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008794 /* Trigger FuncUndefined event, may load the function. */
8795 if (fp == NULL
8796 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008797 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008798 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008800 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008801 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
8803#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008804 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008805 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008806 {
8807 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008808 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008809 }
8810
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 if (fp != NULL)
8812 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008813 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008815 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008817 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008819 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 else
8822 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008823 int did_save_redo = FALSE;
8824
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 /*
8826 * Call the user function.
8827 * Save and restore search patterns, script variables and
8828 * redo buffer.
8829 */
8830 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008831#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008832 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008833#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008834 {
8835 saveRedobuff();
8836 did_save_redo = TRUE;
8837 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008838 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008840 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008841 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8842 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8843 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008844 /* Function was unreferenced while being used, free it
8845 * now. */
8846 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008847 if (did_save_redo)
8848 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 restore_search_patterns();
8850 error = ERROR_NONE;
8851 }
8852 }
8853 }
8854 else
8855 {
8856 /*
8857 * Find the function name in the table, call its implementation.
8858 */
8859 i = find_internal_func(fname);
8860 if (i >= 0)
8861 {
8862 if (argcount < functions[i].f_min_argc)
8863 error = ERROR_TOOFEW;
8864 else if (argcount > functions[i].f_max_argc)
8865 error = ERROR_TOOMANY;
8866 else
8867 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008868 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 error = ERROR_NONE;
8871 }
8872 }
8873 }
8874 /*
8875 * The function call (or "FuncUndefined" autocommand sequence) might
8876 * have been aborted by an error, an interrupt, or an explicitly thrown
8877 * exception that has not been caught so far. This situation can be
8878 * tested for by calling aborting(). For an error in an internal
8879 * function or for the "E132" error in call_user_func(), however, the
8880 * throw point at which the "force_abort" flag (temporarily reset by
8881 * emsg()) is normally updated has not been reached yet. We need to
8882 * update that flag first to make aborting() reliable.
8883 */
8884 update_force_abort();
8885 }
8886 if (error == ERROR_NONE)
8887 ret = OK;
8888
8889 /*
8890 * Report an error unless the argument evaluation or function call has been
8891 * cancelled due to an aborting error, an interrupt, or an exception.
8892 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008893 if (!aborting())
8894 {
8895 switch (error)
8896 {
8897 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008898 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008899 break;
8900 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008901 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008902 break;
8903 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008904 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008905 name);
8906 break;
8907 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008908 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008909 name);
8910 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008911 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008912 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008913 name);
8914 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008915 case ERROR_BOTH:
Bram Moolenaarab1fa392016-03-15 19:33:34 +01008916 emsg_funcname(e_dict_both, name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008917 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008918 }
8919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008921 while (argv_clear > 0)
8922 clear_tv(&argv[--argv_clear]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 if (fname != name && fname != fname_buf)
8924 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008925 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
8927 return ret;
8928}
8929
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008930/*
8931 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008932 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008933 */
8934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008935emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008936{
8937 char_u *p;
8938
8939 if (*name == K_SPECIAL)
8940 p = concat_str((char_u *)"<SNR>", name + 3);
8941 else
8942 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008943 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008944 if (p != name)
8945 vim_free(p);
8946}
8947
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008948/*
8949 * Return TRUE for a non-zero Number and a non-empty String.
8950 */
8951 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008952non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008953{
8954 return ((argvars[0].v_type == VAR_NUMBER
8955 && argvars[0].vval.v_number != 0)
8956 || (argvars[0].v_type == VAR_STRING
8957 && argvars[0].vval.v_string != NULL
8958 && *argvars[0].vval.v_string != NUL));
8959}
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961/*********************************************
8962 * Implementation of the built-in functions
8963 */
8964
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008965#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008966static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008967
8968/*
8969 * Get the float value of "argvars[0]" into "f".
8970 * Returns FAIL when the argument is not a Number or Float.
8971 */
8972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008973get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008974{
8975 if (argvars[0].v_type == VAR_FLOAT)
8976 {
8977 *f = argvars[0].vval.v_float;
8978 return OK;
8979 }
8980 if (argvars[0].v_type == VAR_NUMBER)
8981 {
8982 *f = (float_T)argvars[0].vval.v_number;
8983 return OK;
8984 }
8985 EMSG(_("E808: Number or Float required"));
8986 return FAIL;
8987}
8988
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008989/*
8990 * "abs(expr)" function
8991 */
8992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008993f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008994{
8995 if (argvars[0].v_type == VAR_FLOAT)
8996 {
8997 rettv->v_type = VAR_FLOAT;
8998 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8999 }
9000 else
9001 {
9002 varnumber_T n;
9003 int error = FALSE;
9004
9005 n = get_tv_number_chk(&argvars[0], &error);
9006 if (error)
9007 rettv->vval.v_number = -1;
9008 else if (n > 0)
9009 rettv->vval.v_number = n;
9010 else
9011 rettv->vval.v_number = -n;
9012 }
9013}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009014
9015/*
9016 * "acos()" function
9017 */
9018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009019f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009020{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009021 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009022
9023 rettv->v_type = VAR_FLOAT;
9024 if (get_float_arg(argvars, &f) == OK)
9025 rettv->vval.v_float = acos(f);
9026 else
9027 rettv->vval.v_float = 0.0;
9028}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009029#endif
9030
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009032 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 */
9034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009035f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009040 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009042 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009043 && !tv_check_lock(l->lv_lock,
9044 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009045 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009047 }
9048 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049 EMSG(_(e_listreq));
9050}
9051
9052/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009053 * "alloc_fail(id, countdown, repeat)" function
9054 */
9055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009056f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009057{
9058 if (argvars[0].v_type != VAR_NUMBER
9059 || argvars[0].vval.v_number <= 0
9060 || argvars[1].v_type != VAR_NUMBER
9061 || argvars[1].vval.v_number < 0
9062 || argvars[2].v_type != VAR_NUMBER)
9063 EMSG(_(e_invarg));
9064 else
9065 {
9066 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009067 if (alloc_fail_id >= aid_last)
9068 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009069 alloc_fail_countdown = argvars[1].vval.v_number;
9070 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009071 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009072 }
9073}
9074
9075/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009076 * "and(expr, expr)" function
9077 */
9078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009079f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009080{
9081 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9082 & get_tv_number_chk(&argvars[1], NULL);
9083}
9084
9085/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009086 * "append(lnum, string/list)" function
9087 */
9088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009089f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009090{
9091 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009092 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009093 list_T *l = NULL;
9094 listitem_T *li = NULL;
9095 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009096 long added = 0;
9097
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009098 /* When coming here from Insert mode, sync undo, so that this can be
9099 * undone separately from what was previously inserted. */
9100 if (u_sync_once == 2)
9101 {
9102 u_sync_once = 1; /* notify that u_sync() was called */
9103 u_sync(TRUE);
9104 }
9105
Bram Moolenaar0d660222005-01-07 21:51:51 +00009106 lnum = get_tv_lnum(argvars);
9107 if (lnum >= 0
9108 && lnum <= curbuf->b_ml.ml_line_count
9109 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009110 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009111 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009112 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009113 l = argvars[1].vval.v_list;
9114 if (l == NULL)
9115 return;
9116 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009117 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009118 for (;;)
9119 {
9120 if (l == NULL)
9121 tv = &argvars[1]; /* append a string */
9122 else if (li == NULL)
9123 break; /* end of list */
9124 else
9125 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009126 line = get_tv_string_chk(tv);
9127 if (line == NULL) /* type error */
9128 {
9129 rettv->vval.v_number = 1; /* Failed */
9130 break;
9131 }
9132 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133 ++added;
9134 if (l == NULL)
9135 break;
9136 li = li->li_next;
9137 }
9138
9139 appended_lines_mark(lnum, added);
9140 if (curwin->w_cursor.lnum > lnum)
9141 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143 else
9144 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145}
9146
9147/*
9148 * "argc()" function
9149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009151f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
9156/*
9157 * "argidx()" function
9158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009160f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163}
9164
9165/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009166 * "arglistid()" function
9167 */
9168 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009169f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009170{
9171 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009172
9173 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009174 wp = find_tabwin(&argvars[0], &argvars[1]);
9175 if (wp != NULL)
9176 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009177}
9178
9179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 * "argv(nr)" function
9181 */
9182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009183f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 int idx;
9186
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009187 if (argvars[0].v_type != VAR_UNKNOWN)
9188 {
9189 idx = get_tv_number_chk(&argvars[0], NULL);
9190 if (idx >= 0 && idx < ARGCOUNT)
9191 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9192 else
9193 rettv->vval.v_string = NULL;
9194 rettv->v_type = VAR_STRING;
9195 }
9196 else if (rettv_list_alloc(rettv) == OK)
9197 for (idx = 0; idx < ARGCOUNT; ++idx)
9198 list_append_string(rettv->vval.v_list,
9199 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200}
9201
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009202static void prepare_assert_error(garray_T*gap);
9203static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9204static void assert_error(garray_T *gap);
9205static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009206
9207/*
9208 * Prepare "gap" for an assert error and add the sourcing position.
9209 */
9210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009211prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009212{
9213 char buf[NUMBUFLEN];
9214
9215 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009216 if (sourcing_name != NULL)
9217 {
9218 ga_concat(gap, sourcing_name);
9219 if (sourcing_lnum > 0)
9220 ga_concat(gap, (char_u *)" ");
9221 }
9222 if (sourcing_lnum > 0)
9223 {
9224 sprintf(buf, "line %ld", (long)sourcing_lnum);
9225 ga_concat(gap, (char_u *)buf);
9226 }
9227 if (sourcing_name != NULL || sourcing_lnum > 0)
9228 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009229}
9230
9231/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009232 * Append "str" to "gap", escaping unprintable characters.
9233 * Changes NL to \n, CR to \r, etc.
9234 */
9235 static void
9236ga_concat_esc(garray_T *gap, char_u *str)
9237{
9238 char_u *p;
9239 char_u buf[NUMBUFLEN];
9240
Bram Moolenaarf1551962016-03-15 12:55:58 +01009241 if (str == NULL)
9242 {
9243 ga_concat(gap, (char_u *)"NULL");
9244 return;
9245 }
9246
Bram Moolenaar23689172016-02-15 22:37:37 +01009247 for (p = str; *p != NUL; ++p)
9248 switch (*p)
9249 {
9250 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9251 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9252 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9253 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9254 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9255 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9256 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9257 default:
9258 if (*p < ' ')
9259 {
9260 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9261 ga_concat(gap, buf);
9262 }
9263 else
9264 ga_append(gap, *p);
9265 break;
9266 }
9267}
9268
9269/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009270 * Fill "gap" with information about an assert error.
9271 */
9272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009273fill_assert_error(
9274 garray_T *gap,
9275 typval_T *opt_msg_tv,
9276 char_u *exp_str,
9277 typval_T *exp_tv,
9278 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009279{
9280 char_u numbuf[NUMBUFLEN];
9281 char_u *tofree;
9282
9283 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9284 {
9285 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9286 vim_free(tofree);
9287 }
9288 else
9289 {
9290 ga_concat(gap, (char_u *)"Expected ");
9291 if (exp_str == NULL)
9292 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009293 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009294 vim_free(tofree);
9295 }
9296 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009297 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009298 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009299 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300 vim_free(tofree);
9301 }
9302}
Bram Moolenaar43345542015-11-29 17:35:35 +01009303
9304/*
9305 * Add an assert error to v:errors.
9306 */
9307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009309{
9310 struct vimvar *vp = &vimvars[VV_ERRORS];
9311
9312 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9313 /* Make sure v:errors is a list. */
9314 set_vim_var_list(VV_ERRORS, list_alloc());
9315 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9316}
9317
Bram Moolenaar43345542015-11-29 17:35:35 +01009318/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009319 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009320 */
9321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009322f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009323{
9324 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009325
9326 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9327 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009328 prepare_assert_error(&ga);
9329 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9330 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009331 ga_clear(&ga);
9332 }
9333}
9334
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009335/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009336 * "assert_exception(string[, msg])" function
9337 */
9338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009339f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009340{
9341 garray_T ga;
9342 char *error;
9343
9344 error = (char *)get_tv_string_chk(&argvars[0]);
9345 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9346 {
9347 prepare_assert_error(&ga);
9348 ga_concat(&ga, (char_u *)"v:exception is not set");
9349 assert_error(&ga);
9350 ga_clear(&ga);
9351 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009352 else if (error != NULL
9353 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009354 {
9355 prepare_assert_error(&ga);
9356 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9357 &vimvars[VV_EXCEPTION].vv_tv);
9358 assert_error(&ga);
9359 ga_clear(&ga);
9360 }
9361}
9362
9363/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009364 * "assert_fails(cmd [, error])" function
9365 */
9366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009367f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009368{
9369 char_u *cmd = get_tv_string_chk(&argvars[0]);
9370 garray_T ga;
9371
9372 called_emsg = FALSE;
9373 suppress_errthrow = TRUE;
9374 emsg_silent = TRUE;
9375 do_cmdline_cmd(cmd);
9376 if (!called_emsg)
9377 {
9378 prepare_assert_error(&ga);
9379 ga_concat(&ga, (char_u *)"command did not fail: ");
9380 ga_concat(&ga, cmd);
9381 assert_error(&ga);
9382 ga_clear(&ga);
9383 }
9384 else if (argvars[1].v_type != VAR_UNKNOWN)
9385 {
9386 char_u buf[NUMBUFLEN];
9387 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9388
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009389 if (error == NULL
9390 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009391 {
9392 prepare_assert_error(&ga);
9393 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9394 &vimvars[VV_ERRMSG].vv_tv);
9395 assert_error(&ga);
9396 ga_clear(&ga);
9397 }
9398 }
9399
9400 called_emsg = FALSE;
9401 suppress_errthrow = FALSE;
9402 emsg_silent = FALSE;
9403 emsg_on_display = FALSE;
9404 set_vim_var_string(VV_ERRMSG, NULL, 0);
9405}
9406
9407/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009408 * Common for assert_true() and assert_false().
9409 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009411assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009412{
9413 int error = FALSE;
9414 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009415
Bram Moolenaar37127922016-02-06 20:29:28 +01009416 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009417 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009418 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009419 if (argvars[0].v_type != VAR_NUMBER
9420 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9421 || error)
9422 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009423 prepare_assert_error(&ga);
9424 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009425 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009426 NULL, &argvars[0]);
9427 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009428 ga_clear(&ga);
9429 }
9430}
9431
9432/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009433 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009434 */
9435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009436f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009437{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009438 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009439}
9440
9441/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009442 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009443 */
9444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009445f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009446{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009447 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009448}
9449
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009450#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009451/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009452 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009453 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009455f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009456{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009457 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009458
9459 rettv->v_type = VAR_FLOAT;
9460 if (get_float_arg(argvars, &f) == OK)
9461 rettv->vval.v_float = asin(f);
9462 else
9463 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009464}
9465
9466/*
9467 * "atan()" function
9468 */
9469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009470f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009471{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009472 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009473
9474 rettv->v_type = VAR_FLOAT;
9475 if (get_float_arg(argvars, &f) == OK)
9476 rettv->vval.v_float = atan(f);
9477 else
9478 rettv->vval.v_float = 0.0;
9479}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009480
9481/*
9482 * "atan2()" function
9483 */
9484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009485f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009486{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009487 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009488
9489 rettv->v_type = VAR_FLOAT;
9490 if (get_float_arg(argvars, &fx) == OK
9491 && get_float_arg(&argvars[1], &fy) == OK)
9492 rettv->vval.v_float = atan2(fx, fy);
9493 else
9494 rettv->vval.v_float = 0.0;
9495}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496#endif
9497
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498/*
9499 * "browse(save, title, initdir, default)" function
9500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009502f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503{
9504#ifdef FEAT_BROWSE
9505 int save;
9506 char_u *title;
9507 char_u *initdir;
9508 char_u *defname;
9509 char_u buf[NUMBUFLEN];
9510 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 save = get_tv_number_chk(&argvars[0], &error);
9514 title = get_tv_string_chk(&argvars[1]);
9515 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9516 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 if (error || title == NULL || initdir == NULL || defname == NULL)
9519 rettv->vval.v_string = NULL;
9520 else
9521 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009522 do_browse(save ? BROWSE_SAVE : 0,
9523 title, defname, NULL, initdir, NULL, curbuf);
9524#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009526#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009528}
9529
9530/*
9531 * "browsedir(title, initdir)" function
9532 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009535{
9536#ifdef FEAT_BROWSE
9537 char_u *title;
9538 char_u *initdir;
9539 char_u buf[NUMBUFLEN];
9540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009541 title = get_tv_string_chk(&argvars[0]);
9542 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 if (title == NULL || initdir == NULL)
9545 rettv->vval.v_string = NULL;
9546 else
9547 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009548 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553}
9554
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009555static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557/*
9558 * Find a buffer by number or exact name.
9559 */
9560 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009561find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009565 if (avar->v_type == VAR_NUMBER)
9566 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009567 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009570 if (buf == NULL)
9571 {
9572 /* No full path name match, try a match with a URL or a "nofile"
9573 * buffer, these don't use the full path. */
9574 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9575 if (buf->b_fname != NULL
9576 && (path_with_url(buf->b_fname)
9577#ifdef FEAT_QUICKFIX
9578 || bt_nofile(buf)
9579#endif
9580 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009581 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009582 break;
9583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 }
9585 return buf;
9586}
9587
9588/*
9589 * "bufexists(expr)" function
9590 */
9591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595}
9596
9597/*
9598 * "buflisted(expr)" function
9599 */
9600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009601f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602{
9603 buf_T *buf;
9604
9605 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607}
9608
9609/*
9610 * "bufloaded(expr)" function
9611 */
9612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009613f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 buf_T *buf;
9616
9617 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619}
9620
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009621 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009622buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 int save_magic;
9625 char_u *save_cpo;
9626 buf_T *buf;
9627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9629 save_magic = p_magic;
9630 p_magic = TRUE;
9631 save_cpo = p_cpo;
9632 p_cpo = (char_u *)"";
9633
9634 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009635 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636
9637 p_magic = save_magic;
9638 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009639 return buf;
9640}
9641
9642/*
9643 * Get buffer by number or pattern.
9644 */
9645 static buf_T *
9646get_buf_tv(typval_T *tv, int curtab_only)
9647{
9648 char_u *name = tv->vval.v_string;
9649 buf_T *buf;
9650
9651 if (tv->v_type == VAR_NUMBER)
9652 return buflist_findnr((int)tv->vval.v_number);
9653 if (tv->v_type != VAR_STRING)
9654 return NULL;
9655 if (name == NULL || *name == NUL)
9656 return curbuf;
9657 if (name[0] == '$' && name[1] == NUL)
9658 return lastbuf;
9659
9660 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661
9662 /* If not found, try expanding the name, like done for bufexists(). */
9663 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665
9666 return buf;
9667}
9668
9669/*
9670 * "bufname(expr)" function
9671 */
9672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009673f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675 buf_T *buf;
9676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009677 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009679 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 --emsg_off;
9686}
9687
9688/*
9689 * "bufnr(expr)" function
9690 */
9691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009692f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009695 int error = FALSE;
9696 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009698 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009700 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009701 --emsg_off;
9702
9703 /* If the buffer isn't found and the second argument is not zero create a
9704 * new buffer. */
9705 if (buf == NULL
9706 && argvars[1].v_type != VAR_UNKNOWN
9707 && get_tv_number_chk(&argvars[1], &error) != 0
9708 && !error
9709 && (name = get_tv_string_chk(&argvars[0])) != NULL
9710 && !error)
9711 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717}
9718
9719/*
9720 * "bufwinnr(nr)" function
9721 */
9722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009723f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725#ifdef FEAT_WINDOWS
9726 win_T *wp;
9727 int winnr = 0;
9728#endif
9729 buf_T *buf;
9730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009733 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734#ifdef FEAT_WINDOWS
9735 for (wp = firstwin; wp; wp = wp->w_next)
9736 {
9737 ++winnr;
9738 if (wp->w_buffer == buf)
9739 break;
9740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744#endif
9745 --emsg_off;
9746}
9747
9748/*
9749 * "byte2line(byte)" function
9750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009752f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#else
9757 long boff = 0;
9758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 (linenr_T)0, &boff);
9765#endif
9766}
9767
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009770{
9771#ifdef FEAT_MBYTE
9772 char_u *t;
9773#endif
9774 char_u *str;
9775 long idx;
9776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 str = get_tv_string_chk(&argvars[0]);
9778 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009780 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009781 return;
9782
9783#ifdef FEAT_MBYTE
9784 t = str;
9785 for ( ; idx > 0; idx--)
9786 {
9787 if (*t == NUL) /* EOL reached */
9788 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009789 if (enc_utf8 && comp)
9790 t += utf_ptr2len(t);
9791 else
9792 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009793 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009794 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009795#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009796 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009798#endif
9799}
9800
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009801/*
9802 * "byteidx()" function
9803 */
9804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009805f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009806{
9807 byteidx(argvars, rettv, FALSE);
9808}
9809
9810/*
9811 * "byteidxcomp()" function
9812 */
9813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009814f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009815{
9816 byteidx(argvars, rettv, TRUE);
9817}
9818
Bram Moolenaardb913952012-06-29 12:54:53 +02009819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009820func_call(
9821 char_u *name,
9822 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009823 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009824 dict_T *selfdict,
9825 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009826{
9827 listitem_T *item;
9828 typval_T argv[MAX_FUNC_ARGS + 1];
9829 int argc = 0;
9830 int dummy;
9831 int r = 0;
9832
9833 for (item = args->vval.v_list->lv_first; item != NULL;
9834 item = item->li_next)
9835 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009836 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009837 {
9838 EMSG(_("E699: Too many arguments"));
9839 break;
9840 }
9841 /* Make a copy of each argument. This is needed to be able to set
9842 * v_lock to VAR_FIXED in the copy without changing the original list.
9843 */
9844 copy_tv(&item->li_tv, &argv[argc++]);
9845 }
9846
9847 if (item == NULL)
9848 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9849 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009850 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009851
9852 /* Free the arguments. */
9853 while (argc > 0)
9854 clear_tv(&argv[--argc]);
9855
9856 return r;
9857}
9858
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009859/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009860 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009861 */
9862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009863f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009864{
9865 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009866 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009867 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009868
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009869 if (argvars[1].v_type != VAR_LIST)
9870 {
9871 EMSG(_(e_listreq));
9872 return;
9873 }
9874 if (argvars[1].vval.v_list == NULL)
9875 return;
9876
9877 if (argvars[0].v_type == VAR_FUNC)
9878 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009879 else if (argvars[0].v_type == VAR_PARTIAL)
9880 {
9881 partial = argvars[0].vval.v_partial;
9882 func = partial->pt_name;
9883 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009884 else
9885 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 if (*func == NUL)
9887 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009888
Bram Moolenaare9a41262005-01-15 22:18:47 +00009889 if (argvars[2].v_type != VAR_UNKNOWN)
9890 {
9891 if (argvars[2].v_type != VAR_DICT)
9892 {
9893 EMSG(_(e_dictreq));
9894 return;
9895 }
9896 selfdict = argvars[2].vval.v_dict;
9897 }
9898
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009899 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900}
9901
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009902#ifdef FEAT_FLOAT
9903/*
9904 * "ceil({float})" function
9905 */
9906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009907f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009908{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009909 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009910
9911 rettv->v_type = VAR_FLOAT;
9912 if (get_float_arg(argvars, &f) == OK)
9913 rettv->vval.v_float = ceil(f);
9914 else
9915 rettv->vval.v_float = 0.0;
9916}
9917#endif
9918
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009919#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009920/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009921 * "ch_close()" function
9922 */
9923 static void
9924f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9925{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009926 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009927
9928 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009929 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009930 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009931 channel_clear(channel);
9932 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009933}
9934
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009935/*
9936 * "ch_getbufnr()" function
9937 */
9938 static void
9939f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9940{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009941 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009942
9943 rettv->vval.v_number = -1;
9944 if (channel != NULL)
9945 {
9946 char_u *what = get_tv_string(&argvars[1]);
9947 int part;
9948
9949 if (STRCMP(what, "err") == 0)
9950 part = PART_ERR;
9951 else if (STRCMP(what, "out") == 0)
9952 part = PART_OUT;
9953 else if (STRCMP(what, "in") == 0)
9954 part = PART_IN;
9955 else
9956 part = PART_SOCK;
9957 if (channel->ch_part[part].ch_buffer != NULL)
9958 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9959 }
9960}
9961
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009962/*
9963 * "ch_getjob()" function
9964 */
9965 static void
9966f_ch_getjob(typval_T *argvars, typval_T *rettv)
9967{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009968 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009969
9970 if (channel != NULL)
9971 {
9972 rettv->v_type = VAR_JOB;
9973 rettv->vval.v_job = channel->ch_job;
9974 if (channel->ch_job != NULL)
9975 ++channel->ch_job->jv_refcount;
9976 }
9977}
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009978
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009979/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009980 * "ch_log()" function
9981 */
9982 static void
9983f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
9984{
9985 char_u *msg = get_tv_string(&argvars[0]);
9986 channel_T *channel = NULL;
9987
9988 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009989 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009990
9991 ch_log(channel, (char *)msg);
9992}
9993
9994/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009995 * "ch_logfile()" function
9996 */
9997 static void
9998f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9999{
10000 char_u *fname;
10001 char_u *opt = (char_u *)"";
10002 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010003
10004 fname = get_tv_string(&argvars[0]);
10005 if (argvars[1].v_type == VAR_STRING)
10006 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010007 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010008}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010009
10010/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010011 * "ch_open()" function
10012 */
10013 static void
10014f_ch_open(typval_T *argvars, typval_T *rettv)
10015{
Bram Moolenaar77073442016-02-13 23:23:53 +010010016 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010017 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010018}
10019
10020/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010021 * "ch_read()" function
10022 */
10023 static void
10024f_ch_read(typval_T *argvars, typval_T *rettv)
10025{
10026 common_channel_read(argvars, rettv, FALSE);
10027}
10028
10029/*
10030 * "ch_readraw()" function
10031 */
10032 static void
10033f_ch_readraw(typval_T *argvars, typval_T *rettv)
10034{
10035 common_channel_read(argvars, rettv, TRUE);
10036}
10037
10038/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010039 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010040 */
10041 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010042f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10043{
10044 ch_expr_common(argvars, rettv, TRUE);
10045}
10046
10047/*
10048 * "ch_sendexpr()" function
10049 */
10050 static void
10051f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10052{
10053 ch_expr_common(argvars, rettv, FALSE);
10054}
10055
10056/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010057 * "ch_evalraw()" function
10058 */
10059 static void
10060f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10061{
10062 ch_raw_common(argvars, rettv, TRUE);
10063}
10064
10065/*
10066 * "ch_sendraw()" function
10067 */
10068 static void
10069f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10070{
10071 ch_raw_common(argvars, rettv, FALSE);
10072}
10073
10074/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010075 * "ch_setoptions()" function
10076 */
10077 static void
10078f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10079{
10080 channel_T *channel;
10081 jobopt_T opt;
10082
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010083 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010084 if (channel == NULL)
10085 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010086 clear_job_options(&opt);
10087 if (get_job_options(&argvars[1], &opt,
10088 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010089 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010090 channel_set_options(channel, &opt);
10091}
10092
10093/*
10094 * "ch_status()" function
10095 */
10096 static void
10097f_ch_status(typval_T *argvars, typval_T *rettv)
10098{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010099 channel_T *channel;
10100
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010101 /* return an empty string by default */
10102 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010103 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010104
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010105 channel = get_channel_arg(&argvars[0], FALSE);
10106 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010107}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010108#endif
10109
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010110/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010111 * "changenr()" function
10112 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010114f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010115{
10116 rettv->vval.v_number = curbuf->b_u_seq_cur;
10117}
10118
10119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 * "char2nr(string)" function
10121 */
10122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010123f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124{
10125#ifdef FEAT_MBYTE
10126 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010127 {
10128 int utf8 = 0;
10129
10130 if (argvars[1].v_type != VAR_UNKNOWN)
10131 utf8 = get_tv_number_chk(&argvars[1], NULL);
10132
10133 if (utf8)
10134 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10135 else
10136 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 else
10139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141}
10142
10143/*
10144 * "cindent(lnum)" function
10145 */
10146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010147f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
10149#ifdef FEAT_CINDENT
10150 pos_T pos;
10151 linenr_T lnum;
10152
10153 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10156 {
10157 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 curwin->w_cursor = pos;
10160 }
10161 else
10162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164}
10165
10166/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010167 * "clearmatches()" function
10168 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010170f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010171{
10172#ifdef FEAT_SEARCH_EXTRA
10173 clear_matches(curwin);
10174#endif
10175}
10176
10177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 * "col(string)" function
10179 */
10180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010181f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182{
10183 colnr_T col = 0;
10184 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010185 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010187 fp = var2fpos(&argvars[0], FALSE, &fnum);
10188 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 {
10190 if (fp->col == MAXCOL)
10191 {
10192 /* '> can be MAXCOL, get the length of the line then */
10193 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010194 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 else
10196 col = MAXCOL;
10197 }
10198 else
10199 {
10200 col = fp->col + 1;
10201#ifdef FEAT_VIRTUALEDIT
10202 /* col(".") when the cursor is on the NUL at the end of the line
10203 * because of "coladd" can be seen as an extra column. */
10204 if (virtual_active() && fp == &curwin->w_cursor)
10205 {
10206 char_u *p = ml_get_cursor();
10207
10208 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10209 curwin->w_virtcol - curwin->w_cursor.coladd))
10210 {
10211# ifdef FEAT_MBYTE
10212 int l;
10213
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010214 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 col += l;
10216# else
10217 if (*p != NUL && p[1] == NUL)
10218 ++col;
10219# endif
10220 }
10221 }
10222#endif
10223 }
10224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010225 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226}
10227
Bram Moolenaar572cb562005-08-05 21:35:02 +000010228#if defined(FEAT_INS_EXPAND)
10229/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010230 * "complete()" function
10231 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010233f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010234{
10235 int startcol;
10236
10237 if ((State & INSERT) == 0)
10238 {
10239 EMSG(_("E785: complete() can only be used in Insert mode"));
10240 return;
10241 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010242
10243 /* Check for undo allowed here, because if something was already inserted
10244 * the line was already saved for undo and this check isn't done. */
10245 if (!undo_allowed())
10246 return;
10247
Bram Moolenaarade00832006-03-10 21:46:58 +000010248 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10249 {
10250 EMSG(_(e_invarg));
10251 return;
10252 }
10253
10254 startcol = get_tv_number_chk(&argvars[0], NULL);
10255 if (startcol <= 0)
10256 return;
10257
10258 set_completion(startcol - 1, argvars[1].vval.v_list);
10259}
10260
10261/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010262 * "complete_add()" function
10263 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010265f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010266{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010267 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010268}
10269
10270/*
10271 * "complete_check()" function
10272 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010274f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010275{
10276 int saved = RedrawingDisabled;
10277
10278 RedrawingDisabled = 0;
10279 ins_compl_check_keys(0);
10280 rettv->vval.v_number = compl_interrupted;
10281 RedrawingDisabled = saved;
10282}
10283#endif
10284
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285/*
10286 * "confirm(message, buttons[, default [, type]])" function
10287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010289f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290{
10291#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10292 char_u *message;
10293 char_u *buttons = NULL;
10294 char_u buf[NUMBUFLEN];
10295 char_u buf2[NUMBUFLEN];
10296 int def = 1;
10297 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 char_u *typestr;
10299 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 message = get_tv_string_chk(&argvars[0]);
10302 if (message == NULL)
10303 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010304 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10307 if (buttons == NULL)
10308 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010309 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010312 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10315 if (typestr == NULL)
10316 error = TRUE;
10317 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 switch (TOUPPER_ASC(*typestr))
10320 {
10321 case 'E': type = VIM_ERROR; break;
10322 case 'Q': type = VIM_QUESTION; break;
10323 case 'I': type = VIM_INFO; break;
10324 case 'W': type = VIM_WARNING; break;
10325 case 'G': type = VIM_GENERIC; break;
10326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 }
10328 }
10329 }
10330 }
10331
10332 if (buttons == NULL || *buttons == NUL)
10333 buttons = (char_u *)_("&Ok");
10334
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010335 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010337 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338#endif
10339}
10340
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010341/*
10342 * "copy()" function
10343 */
10344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010345f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010346{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010347 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010348}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010350#ifdef FEAT_FLOAT
10351/*
10352 * "cos()" function
10353 */
10354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010355f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010356{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010357 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010358
10359 rettv->v_type = VAR_FLOAT;
10360 if (get_float_arg(argvars, &f) == OK)
10361 rettv->vval.v_float = cos(f);
10362 else
10363 rettv->vval.v_float = 0.0;
10364}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010365
10366/*
10367 * "cosh()" function
10368 */
10369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010370f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010371{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010372 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010373
10374 rettv->v_type = VAR_FLOAT;
10375 if (get_float_arg(argvars, &f) == OK)
10376 rettv->vval.v_float = cosh(f);
10377 else
10378 rettv->vval.v_float = 0.0;
10379}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010380#endif
10381
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010383 * "count()" function
10384 */
10385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010387{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010388 long n = 0;
10389 int ic = FALSE;
10390
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010392 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010393 listitem_T *li;
10394 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010395 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010396
Bram Moolenaare9a41262005-01-15 22:18:47 +000010397 if ((l = argvars[0].vval.v_list) != NULL)
10398 {
10399 li = l->lv_first;
10400 if (argvars[2].v_type != VAR_UNKNOWN)
10401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 int error = FALSE;
10403
10404 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 if (argvars[3].v_type != VAR_UNKNOWN)
10406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 idx = get_tv_number_chk(&argvars[3], &error);
10408 if (!error)
10409 {
10410 li = list_find(l, idx);
10411 if (li == NULL)
10412 EMSGN(_(e_listidx), idx);
10413 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010414 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010415 if (error)
10416 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 }
10418
10419 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010420 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010421 ++n;
10422 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 else if (argvars[0].v_type == VAR_DICT)
10425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 int todo;
10427 dict_T *d;
10428 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010429
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010430 if ((d = argvars[0].vval.v_dict) != NULL)
10431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 int error = FALSE;
10433
Bram Moolenaare9a41262005-01-15 22:18:47 +000010434 if (argvars[2].v_type != VAR_UNKNOWN)
10435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010437 if (argvars[3].v_type != VAR_UNKNOWN)
10438 EMSG(_(e_invarg));
10439 }
10440
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010441 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010442 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010443 {
10444 if (!HASHITEM_EMPTY(hi))
10445 {
10446 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010447 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010448 ++n;
10449 }
10450 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010451 }
10452 }
10453 else
10454 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010455 rettv->vval.v_number = n;
10456}
10457
10458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10460 *
10461 * Checks the existence of a cscope connection.
10462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010464f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466#ifdef FEAT_CSCOPE
10467 int num = 0;
10468 char_u *dbpath = NULL;
10469 char_u *prepend = NULL;
10470 char_u buf[NUMBUFLEN];
10471
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010472 if (argvars[0].v_type != VAR_UNKNOWN
10473 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475 num = (int)get_tv_number(&argvars[0]);
10476 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 }
10480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482#endif
10483}
10484
10485/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010486 * "cursor(lnum, col)" function, or
10487 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010489 * Moves the cursor to the specified line and column.
10490 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010493f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010496#ifdef FEAT_VIRTUALEDIT
10497 long coladd = 0;
10498#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010499 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010501 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010502 if (argvars[1].v_type == VAR_UNKNOWN)
10503 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010504 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010505 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010506
Bram Moolenaar493c1782014-05-28 14:34:46 +020010507 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010508 {
10509 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010510 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010511 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010512 line = pos.lnum;
10513 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010514#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010515 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010516#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010517 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010518 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010519 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010520 set_curswant = FALSE;
10521 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010522 }
10523 else
10524 {
10525 line = get_tv_lnum(argvars);
10526 col = get_tv_number_chk(&argvars[1], NULL);
10527#ifdef FEAT_VIRTUALEDIT
10528 if (argvars[2].v_type != VAR_UNKNOWN)
10529 coladd = get_tv_number_chk(&argvars[2], NULL);
10530#endif
10531 }
10532 if (line < 0 || col < 0
10533#ifdef FEAT_VIRTUALEDIT
10534 || coladd < 0
10535#endif
10536 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 if (line > 0)
10539 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 if (col > 0)
10541 curwin->w_cursor.col = col - 1;
10542#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010543 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544#endif
10545
10546 /* Make sure the cursor is in a valid position. */
10547 check_cursor();
10548#ifdef FEAT_MBYTE
10549 /* Correct cursor for multi-byte character. */
10550 if (has_mbyte)
10551 mb_adjust_cursor();
10552#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010553
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010554 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010555 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556}
10557
10558/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010559 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 */
10561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010562f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010564 int noref = 0;
10565
10566 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010568 if (noref < 0 || noref > 1)
10569 EMSG(_(e_invarg));
10570 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010571 {
10572 current_copyID += COPYID_INC;
10573 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575}
10576
10577/*
10578 * "delete()" function
10579 */
10580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010581f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010583 char_u nbuf[NUMBUFLEN];
10584 char_u *name;
10585 char_u *flags;
10586
10587 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010589 return;
10590
10591 name = get_tv_string(&argvars[0]);
10592 if (name == NULL || *name == NUL)
10593 {
10594 EMSG(_(e_invarg));
10595 return;
10596 }
10597
10598 if (argvars[1].v_type != VAR_UNKNOWN)
10599 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010601 flags = (char_u *)"";
10602
10603 if (*flags == NUL)
10604 /* delete a file */
10605 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10606 else if (STRCMP(flags, "d") == 0)
10607 /* delete an empty directory */
10608 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10609 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010610 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010611 rettv->vval.v_number = delete_recursive(name);
10612 else
10613 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614}
10615
10616/*
10617 * "did_filetype()" function
10618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010620f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621{
10622#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010623 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624#endif
10625}
10626
10627/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010628 * "diff_filler()" function
10629 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010631f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010632{
10633#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010635#endif
10636}
10637
10638/*
10639 * "diff_hlID()" function
10640 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010642f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010643{
10644#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010646 static linenr_T prev_lnum = 0;
10647 static int changedtick = 0;
10648 static int fnum = 0;
10649 static int change_start = 0;
10650 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010651 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010652 int filler_lines;
10653 int col;
10654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 if (lnum < 0) /* ignore type error in {lnum} arg */
10656 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010657 if (lnum != prev_lnum
10658 || changedtick != curbuf->b_changedtick
10659 || fnum != curbuf->b_fnum)
10660 {
10661 /* New line, buffer, change: need to get the values. */
10662 filler_lines = diff_check(curwin, lnum);
10663 if (filler_lines < 0)
10664 {
10665 if (filler_lines == -1)
10666 {
10667 change_start = MAXCOL;
10668 change_end = -1;
10669 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10670 hlID = HLF_ADD; /* added line */
10671 else
10672 hlID = HLF_CHD; /* changed line */
10673 }
10674 else
10675 hlID = HLF_ADD; /* added line */
10676 }
10677 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010678 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010679 prev_lnum = lnum;
10680 changedtick = curbuf->b_changedtick;
10681 fnum = curbuf->b_fnum;
10682 }
10683
10684 if (hlID == HLF_CHD || hlID == HLF_TXD)
10685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010687 if (col >= change_start && col <= change_end)
10688 hlID = HLF_TXD; /* changed text */
10689 else
10690 hlID = HLF_CHD; /* changed line */
10691 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010692 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010693#endif
10694}
10695
10696/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010697 * "disable_char_avail_for_testing({expr})" function
10698 */
10699 static void
10700f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10701{
10702 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10703}
10704
10705/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010706 * "empty({expr})" function
10707 */
10708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010709f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010710{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010711 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010712
10713 switch (argvars[0].v_type)
10714 {
10715 case VAR_STRING:
10716 case VAR_FUNC:
10717 n = argvars[0].vval.v_string == NULL
10718 || *argvars[0].vval.v_string == NUL;
10719 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010720 case VAR_PARTIAL:
10721 n = FALSE;
10722 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010723 case VAR_NUMBER:
10724 n = argvars[0].vval.v_number == 0;
10725 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010726 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010727#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010728 n = argvars[0].vval.v_float == 0.0;
10729 break;
10730#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010731 case VAR_LIST:
10732 n = argvars[0].vval.v_list == NULL
10733 || argvars[0].vval.v_list->lv_first == NULL;
10734 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010735 case VAR_DICT:
10736 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010738 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010739 case VAR_SPECIAL:
10740 n = argvars[0].vval.v_number != VVAL_TRUE;
10741 break;
10742
Bram Moolenaar835dc632016-02-07 14:27:38 +010010743 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010744#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010745 n = argvars[0].vval.v_job == NULL
10746 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10747 break;
10748#endif
10749 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010750#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010751 n = argvars[0].vval.v_channel == NULL
10752 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010753 break;
10754#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010755 case VAR_UNKNOWN:
10756 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10757 n = TRUE;
10758 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010759 }
10760
10761 rettv->vval.v_number = n;
10762}
10763
10764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 * "escape({string}, {chars})" function
10766 */
10767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010768f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769{
10770 char_u buf[NUMBUFLEN];
10771
Bram Moolenaar758711c2005-02-02 23:11:38 +000010772 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10773 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775}
10776
10777/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010778 * "eval()" function
10779 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010781f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010782{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010783 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 s = get_tv_string_chk(&argvars[0]);
10786 if (s != NULL)
10787 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010788
Bram Moolenaar615b9972015-01-14 17:15:05 +010010789 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10791 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010792 if (p != NULL && !aborting())
10793 EMSG2(_(e_invexpr2), p);
10794 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010796 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010797 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010798 else if (*s != NUL)
10799 EMSG(_(e_trailing));
10800}
10801
10802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 * "eventhandler()" function
10804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010806f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010808 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809}
10810
10811/*
10812 * "executable()" function
10813 */
10814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010815f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010817 char_u *name = get_tv_string(&argvars[0]);
10818
10819 /* Check in $PATH and also check directly if there is a directory name. */
10820 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10821 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010822}
10823
10824/*
10825 * "exepath()" function
10826 */
10827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010828f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010829{
10830 char_u *p = NULL;
10831
Bram Moolenaarb5971142015-03-21 17:32:19 +010010832 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010833 rettv->v_type = VAR_STRING;
10834 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835}
10836
10837/*
10838 * "exists()" function
10839 */
10840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010841f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843 char_u *p;
10844 char_u *name;
10845 int n = FALSE;
10846 int len = 0;
10847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 if (*p == '$') /* environment variable */
10850 {
10851 /* first try "normal" environment variables (fast) */
10852 if (mch_getenv(p + 1) != NULL)
10853 n = TRUE;
10854 else
10855 {
10856 /* try expanding things like $VIM and ${HOME} */
10857 p = expand_env_save(p);
10858 if (p != NULL && *p != '$')
10859 n = TRUE;
10860 vim_free(p);
10861 }
10862 }
10863 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010866 if (*skipwhite(p) != NUL)
10867 n = FALSE; /* trailing garbage */
10868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 else if (*p == '*') /* internal or user defined function */
10870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010871 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 }
10873 else if (*p == ':')
10874 {
10875 n = cmd_exists(p + 1);
10876 }
10877 else if (*p == '#')
10878 {
10879#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010880 if (p[1] == '#')
10881 n = autocmd_supported(p + 2);
10882 else
10883 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884#endif
10885 }
10886 else /* internal variable */
10887 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010888 char_u *tofree;
10889 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010891 /* get_name_len() takes care of expanding curly braces */
10892 name = p;
10893 len = get_name_len(&p, &tofree, TRUE, FALSE);
10894 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010896 if (tofree != NULL)
10897 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010898 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010899 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010901 /* handle d.key, l[idx], f(expr) */
10902 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10903 if (n)
10904 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010907 if (*p != NUL)
10908 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010910 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
10912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914}
10915
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010916#ifdef FEAT_FLOAT
10917/*
10918 * "exp()" function
10919 */
10920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010921f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010922{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010923 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010924
10925 rettv->v_type = VAR_FLOAT;
10926 if (get_float_arg(argvars, &f) == OK)
10927 rettv->vval.v_float = exp(f);
10928 else
10929 rettv->vval.v_float = 0.0;
10930}
10931#endif
10932
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933/*
10934 * "expand()" function
10935 */
10936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010937f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938{
10939 char_u *s;
10940 int len;
10941 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010942 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010945 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010948 if (argvars[1].v_type != VAR_UNKNOWN
10949 && argvars[2].v_type != VAR_UNKNOWN
10950 && get_tv_number_chk(&argvars[2], &error)
10951 && !error)
10952 {
10953 rettv->v_type = VAR_LIST;
10954 rettv->vval.v_list = NULL;
10955 }
10956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 if (*s == '%' || *s == '#' || *s == '<')
10959 {
10960 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010961 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010963 if (rettv->v_type == VAR_LIST)
10964 {
10965 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10966 list_append_string(rettv->vval.v_list, result, -1);
10967 else
10968 vim_free(result);
10969 }
10970 else
10971 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 }
10973 else
10974 {
10975 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010976 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 if (argvars[1].v_type != VAR_UNKNOWN
10978 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010979 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 if (!error)
10981 {
10982 ExpandInit(&xpc);
10983 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010984 if (p_wic)
10985 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010986 if (rettv->v_type == VAR_STRING)
10987 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10988 options, WILD_ALL);
10989 else if (rettv_list_alloc(rettv) != FAIL)
10990 {
10991 int i;
10992
10993 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10994 for (i = 0; i < xpc.xp_numfiles; i++)
10995 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10996 ExpandCleanup(&xpc);
10997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 }
10999 else
11000 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 }
11002}
11003
11004/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011005 * Go over all entries in "d2" and add them to "d1".
11006 * When "action" is "error" then a duplicate key is an error.
11007 * When "action" is "force" then a duplicate key is overwritten.
11008 * Otherwise duplicate keys are ignored ("action" is "keep").
11009 */
11010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011011dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011012{
11013 dictitem_T *di1;
11014 hashitem_T *hi2;
11015 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011016 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011017
11018 todo = (int)d2->dv_hashtab.ht_used;
11019 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11020 {
11021 if (!HASHITEM_EMPTY(hi2))
11022 {
11023 --todo;
11024 di1 = dict_find(d1, hi2->hi_key, -1);
11025 if (d1->dv_scope != 0)
11026 {
11027 /* Disallow replacing a builtin function in l: and g:.
11028 * Check the key to be valid when adding to any
11029 * scope. */
11030 if (d1->dv_scope == VAR_DEF_SCOPE
11031 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11032 && var_check_func_name(hi2->hi_key,
11033 di1 == NULL))
11034 break;
11035 if (!valid_varname(hi2->hi_key))
11036 break;
11037 }
11038 if (di1 == NULL)
11039 {
11040 di1 = dictitem_copy(HI2DI(hi2));
11041 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11042 dictitem_free(di1);
11043 }
11044 else if (*action == 'e')
11045 {
11046 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11047 break;
11048 }
11049 else if (*action == 'f' && HI2DI(hi2) != di1)
11050 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011051 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11052 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011053 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011054 clear_tv(&di1->di_tv);
11055 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11056 }
11057 }
11058 }
11059}
11060
11061/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011062 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011063 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011064 */
11065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011066f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011067{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011068 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011069
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011071 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011072 list_T *l1, *l2;
11073 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011074 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011076
Bram Moolenaare9a41262005-01-15 22:18:47 +000011077 l1 = argvars[0].vval.v_list;
11078 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011079 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011080 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011081 {
11082 if (argvars[2].v_type != VAR_UNKNOWN)
11083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 before = get_tv_number_chk(&argvars[2], &error);
11085 if (error)
11086 return; /* type error; errmsg already given */
11087
Bram Moolenaar758711c2005-02-02 23:11:38 +000011088 if (before == l1->lv_len)
11089 item = NULL;
11090 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011092 item = list_find(l1, before);
11093 if (item == NULL)
11094 {
11095 EMSGN(_(e_listidx), before);
11096 return;
11097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098 }
11099 }
11100 else
11101 item = NULL;
11102 list_extend(l1, l2, item);
11103
Bram Moolenaare9a41262005-01-15 22:18:47 +000011104 copy_tv(&argvars[0], rettv);
11105 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11108 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011109 dict_T *d1, *d2;
11110 char_u *action;
11111 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112
11113 d1 = argvars[0].vval.v_dict;
11114 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011115 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011116 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 {
11118 /* Check the third argument. */
11119 if (argvars[2].v_type != VAR_UNKNOWN)
11120 {
11121 static char *(av[]) = {"keep", "force", "error"};
11122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 action = get_tv_string_chk(&argvars[2]);
11124 if (action == NULL)
11125 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 for (i = 0; i < 3; ++i)
11127 if (STRCMP(action, av[i]) == 0)
11128 break;
11129 if (i == 3)
11130 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011131 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 return;
11133 }
11134 }
11135 else
11136 action = (char_u *)"force";
11137
Bram Moolenaara9922d62013-05-30 13:01:18 +020011138 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139
Bram Moolenaare9a41262005-01-15 22:18:47 +000011140 copy_tv(&argvars[0], rettv);
11141 }
11142 }
11143 else
11144 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011145}
11146
11147/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011148 * "feedkeys()" function
11149 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011151f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011152{
11153 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011154 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011155 char_u *keys, *flags;
11156 char_u nbuf[NUMBUFLEN];
11157 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011158 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011159 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011160
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011161 /* This is not allowed in the sandbox. If the commands would still be
11162 * executed in the sandbox it would be OK, but it probably happens later,
11163 * when "sandbox" is no longer set. */
11164 if (check_secure())
11165 return;
11166
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011167 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011168
11169 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011170 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011171 flags = get_tv_string_buf(&argvars[1], nbuf);
11172 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011173 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011174 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011175 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011176 case 'n': remap = FALSE; break;
11177 case 'm': remap = TRUE; break;
11178 case 't': typed = TRUE; break;
11179 case 'i': insert = TRUE; break;
11180 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011181 }
11182 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011183 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011184
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011185 if (*keys != NUL || execute)
11186 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011187 /* Need to escape K_SPECIAL and CSI before putting the string in the
11188 * typeahead buffer. */
11189 keys_esc = vim_strsave_escape_csi(keys);
11190 if (keys_esc != NULL)
11191 {
11192 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011193 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011194 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011195 if (vgetc_busy)
11196 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011197 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011198 {
11199 int save_msg_scroll = msg_scroll;
11200
11201 /* Avoid a 1 second delay when the keys start Insert mode. */
11202 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011203 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011204 msg_scroll |= save_msg_scroll;
11205 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011206 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011207 }
11208}
11209
11210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 * "filereadable()" function
11212 */
11213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011214f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011216 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 char_u *p;
11218 int n;
11219
Bram Moolenaarc236c162008-07-13 17:41:49 +000011220#ifndef O_NONBLOCK
11221# define O_NONBLOCK 0
11222#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011224 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11225 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 {
11227 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011228 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 }
11230 else
11231 n = FALSE;
11232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234}
11235
11236/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011237 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 * rights to write into.
11239 */
11240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011241f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011243 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244}
11245
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011246 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011247findfilendir(
11248 typval_T *argvars UNUSED,
11249 typval_T *rettv,
11250 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011251{
11252#ifdef FEAT_SEARCHPATH
11253 char_u *fname;
11254 char_u *fresult = NULL;
11255 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11256 char_u *p;
11257 char_u pathbuf[NUMBUFLEN];
11258 int count = 1;
11259 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011260 int error = FALSE;
11261#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011262
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011263 rettv->vval.v_string = NULL;
11264 rettv->v_type = VAR_STRING;
11265
11266#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011268
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011269 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11272 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011273 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 else
11275 {
11276 if (*p != NUL)
11277 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011280 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011282 }
11283
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011284 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11285 error = TRUE;
11286
11287 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011289 do
11290 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011291 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011292 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011293 fresult = find_file_in_path_option(first ? fname : NULL,
11294 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011295 0, first, path,
11296 find_what,
11297 curbuf->b_ffname,
11298 find_what == FINDFILE_DIR
11299 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011301
11302 if (fresult != NULL && rettv->v_type == VAR_LIST)
11303 list_append_string(rettv->vval.v_list, fresult, -1);
11304
11305 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011307
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011308 if (rettv->v_type == VAR_STRING)
11309 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011310#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011311}
11312
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011313static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11314static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011315
11316/*
11317 * Implementation of map() and filter().
11318 */
11319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011320filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011321{
11322 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011323 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 listitem_T *li, *nli;
11325 list_T *l = NULL;
11326 dictitem_T *di;
11327 hashtab_T *ht;
11328 hashitem_T *hi;
11329 dict_T *d = NULL;
11330 typval_T save_val;
11331 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011332 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011333 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011334 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011335 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011336 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011337 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011338 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011339
Bram Moolenaare9a41262005-01-15 22:18:47 +000011340 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011341 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011342 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011343 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011344 return;
11345 }
11346 else if (argvars[0].v_type == VAR_DICT)
11347 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011348 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011349 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011350 return;
11351 }
11352 else
11353 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011354 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011355 return;
11356 }
11357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358 expr = get_tv_string_buf_chk(&argvars[1], buf);
11359 /* On type errors, the preceding call has already displayed an error
11360 * message. Avoid a misleading error message for an empty string that
11361 * was not passed as argument. */
11362 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 prepare_vimvar(VV_VAL, &save_val);
11365 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011366
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011367 /* We reset "did_emsg" to be able to detect whether an error
11368 * occurred during evaluation of the expression. */
11369 save_did_emsg = did_emsg;
11370 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011371
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011372 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 vimvars[VV_KEY].vv_type = VAR_STRING;
11376
11377 ht = &d->dv_hashtab;
11378 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011379 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 if (!HASHITEM_EMPTY(hi))
11383 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011384 int r;
11385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 --todo;
11387 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011388 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011389 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11390 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 break;
11392 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011393 r = filter_map_one(&di->di_tv, expr, map, &rem);
11394 clear_tv(&vimvars[VV_KEY].vv_tv);
11395 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396 break;
11397 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011398 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011399 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11400 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011401 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011403 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 }
11405 }
11406 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 }
11408 else
11409 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011410 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 for (li = l->lv_first; li != NULL; li = nli)
11413 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011414 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011415 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011417 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011418 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011419 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011420 break;
11421 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011423 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011424 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011426
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011427 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011429
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011430 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011432
11433 copy_tv(&argvars[0], rettv);
11434}
11435
11436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011437filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011438{
Bram Moolenaar33570922005-01-25 22:26:29 +000011439 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011440 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011441 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442
Bram Moolenaar33570922005-01-25 22:26:29 +000011443 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011444 s = expr;
11445 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011446 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011447 if (*s != NUL) /* check for trailing chars after expr */
11448 {
11449 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011450 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011451 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011452 }
11453 if (map)
11454 {
11455 /* map(): replace the list item value */
11456 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011457 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011458 *tv = rettv;
11459 }
11460 else
11461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462 int error = FALSE;
11463
Bram Moolenaare9a41262005-01-15 22:18:47 +000011464 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 /* On type error, nothing has been removed; return FAIL to stop the
11468 * loop. The error message was given by get_tv_number_chk(). */
11469 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011470 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011472 retval = OK;
11473theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011475 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011476}
11477
11478/*
11479 * "filter()" function
11480 */
11481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011482f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011483{
11484 filter_map(argvars, rettv, FALSE);
11485}
11486
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011488 * "finddir({fname}[, {path}[, {count}]])" function
11489 */
11490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011491f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011492{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011493 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494}
11495
11496/*
11497 * "findfile({fname}[, {path}[, {count}]])" function
11498 */
11499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011500f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011502 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503}
11504
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011505#ifdef FEAT_FLOAT
11506/*
11507 * "float2nr({float})" function
11508 */
11509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011510f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011511{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011512 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011513
11514 if (get_float_arg(argvars, &f) == OK)
11515 {
11516 if (f < -0x7fffffff)
11517 rettv->vval.v_number = -0x7fffffff;
11518 else if (f > 0x7fffffff)
11519 rettv->vval.v_number = 0x7fffffff;
11520 else
11521 rettv->vval.v_number = (varnumber_T)f;
11522 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011523}
11524
11525/*
11526 * "floor({float})" function
11527 */
11528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011529f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011530{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011531 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011532
11533 rettv->v_type = VAR_FLOAT;
11534 if (get_float_arg(argvars, &f) == OK)
11535 rettv->vval.v_float = floor(f);
11536 else
11537 rettv->vval.v_float = 0.0;
11538}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011539
11540/*
11541 * "fmod()" function
11542 */
11543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011544f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011545{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011546 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011547
11548 rettv->v_type = VAR_FLOAT;
11549 if (get_float_arg(argvars, &fx) == OK
11550 && get_float_arg(&argvars[1], &fy) == OK)
11551 rettv->vval.v_float = fmod(fx, fy);
11552 else
11553 rettv->vval.v_float = 0.0;
11554}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011555#endif
11556
Bram Moolenaar0d660222005-01-07 21:51:51 +000011557/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011558 * "fnameescape({string})" function
11559 */
11560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011561f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011562{
11563 rettv->vval.v_string = vim_strsave_fnameescape(
11564 get_tv_string(&argvars[0]), FALSE);
11565 rettv->v_type = VAR_STRING;
11566}
11567
11568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 * "fnamemodify({fname}, {mods})" function
11570 */
11571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011572f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574 char_u *fname;
11575 char_u *mods;
11576 int usedlen = 0;
11577 int len;
11578 char_u *fbuf = NULL;
11579 char_u buf[NUMBUFLEN];
11580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011581 fname = get_tv_string_chk(&argvars[0]);
11582 mods = get_tv_string_buf_chk(&argvars[1], buf);
11583 if (fname == NULL || mods == NULL)
11584 fname = NULL;
11585 else
11586 {
11587 len = (int)STRLEN(fname);
11588 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 vim_free(fbuf);
11597}
11598
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011599static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600
11601/*
11602 * "foldclosed()" function
11603 */
11604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011605foldclosed_both(
11606 typval_T *argvars UNUSED,
11607 typval_T *rettv,
11608 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609{
11610#ifdef FEAT_FOLDING
11611 linenr_T lnum;
11612 linenr_T first, last;
11613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11616 {
11617 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11618 {
11619 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 return;
11624 }
11625 }
11626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628}
11629
11630/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011631 * "foldclosed()" function
11632 */
11633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011634f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011635{
11636 foldclosed_both(argvars, rettv, FALSE);
11637}
11638
11639/*
11640 * "foldclosedend()" function
11641 */
11642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011643f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644{
11645 foldclosed_both(argvars, rettv, TRUE);
11646}
11647
11648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 * "foldlevel()" function
11650 */
11651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011652f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653{
11654#ifdef FEAT_FOLDING
11655 linenr_T lnum;
11656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661}
11662
11663/*
11664 * "foldtext()" function
11665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011667f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668{
11669#ifdef FEAT_FOLDING
11670 linenr_T lnum;
11671 char_u *s;
11672 char_u *r;
11673 int len;
11674 char *txt;
11675#endif
11676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677 rettv->v_type = VAR_STRING;
11678 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011680 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11681 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11682 <= curbuf->b_ml.ml_line_count
11683 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 {
11685 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011686 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11687 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 {
11689 if (!linewhite(lnum))
11690 break;
11691 ++lnum;
11692 }
11693
11694 /* Find interesting text in this line. */
11695 s = skipwhite(ml_get(lnum));
11696 /* skip C comment-start */
11697 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011700 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011701 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011702 {
11703 s = skipwhite(ml_get(lnum + 1));
11704 if (*s == '*')
11705 s = skipwhite(s + 1);
11706 }
11707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 txt = _("+-%s%3ld lines: ");
11709 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011710 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 + 20 /* for %3ld */
11712 + STRLEN(s))); /* concatenated */
11713 if (r != NULL)
11714 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011715 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11716 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11717 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 len = (int)STRLEN(r);
11719 STRCAT(r, s);
11720 /* remove 'foldmarker' and 'commentstring' */
11721 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 }
11724 }
11725#endif
11726}
11727
11728/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011729 * "foldtextresult(lnum)" function
11730 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011732f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011733{
11734#ifdef FEAT_FOLDING
11735 linenr_T lnum;
11736 char_u *text;
11737 char_u buf[51];
11738 foldinfo_T foldinfo;
11739 int fold_count;
11740#endif
11741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011742 rettv->v_type = VAR_STRING;
11743 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011744#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011745 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011746 /* treat illegal types and illegal string values for {lnum} the same */
11747 if (lnum < 0)
11748 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011749 fold_count = foldedCount(curwin, lnum, &foldinfo);
11750 if (fold_count > 0)
11751 {
11752 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11753 &foldinfo, buf);
11754 if (text == buf)
11755 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011757 }
11758#endif
11759}
11760
11761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 * "foreground()" function
11763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011765f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767#ifdef FEAT_GUI
11768 if (gui.in_use)
11769 gui_mch_set_foreground();
11770#else
11771# ifdef WIN32
11772 win32_set_foreground();
11773# endif
11774#endif
11775}
11776
11777/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011778 * "function()" function
11779 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011781f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011782{
11783 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011784 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011785 int use_string = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011786
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011787 if (argvars[0].v_type == VAR_FUNC)
11788 {
11789 /* function(MyFunc, [arg], dict) */
11790 s = argvars[0].vval.v_string;
11791 }
11792 else if (argvars[0].v_type == VAR_PARTIAL
11793 && argvars[0].vval.v_partial != NULL)
11794 /* function(dict.MyFunc, [arg]) */
11795 s = argvars[0].vval.v_partial->pt_name;
11796 else
11797 {
11798 /* function('MyFunc', [arg], dict) */
11799 s = get_tv_string(&argvars[0]);
11800 use_string = TRUE;
11801 }
11802
11803 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011804 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011805 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011806 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11807 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011808 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011809 else
11810 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011811 int dict_idx = 0;
11812 int arg_idx = 0;
11813 list_T *list = NULL;
11814
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011815 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011816 {
11817 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011818 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011819
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011820 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11821 * also be called from another script. Using trans_function_name()
11822 * would also work, but some plugins depend on the name being
11823 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011824 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011825 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11826 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011827 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011828 STRCPY(name, sid_buf);
11829 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011830 }
11831 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011832 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011833 name = vim_strsave(s);
11834
11835 if (argvars[1].v_type != VAR_UNKNOWN)
11836 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011837 if (argvars[2].v_type != VAR_UNKNOWN)
11838 {
11839 /* function(name, [args], dict) */
11840 arg_idx = 1;
11841 dict_idx = 2;
11842 }
11843 else if (argvars[1].v_type == VAR_DICT)
11844 /* function(name, dict) */
11845 dict_idx = 1;
11846 else
11847 /* function(name, [args]) */
11848 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011849 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011850 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011851 if (argvars[dict_idx].v_type != VAR_DICT)
11852 {
11853 EMSG(_("E922: expected a dict"));
11854 vim_free(name);
11855 return;
11856 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011857 if (argvars[0].v_type == VAR_PARTIAL)
11858 {
11859 EMSG2(_(e_dict_both), name);
11860 vim_free(name);
11861 return;
11862 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011863 if (argvars[dict_idx].vval.v_dict == NULL)
11864 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011865 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011866 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011867 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011868 if (argvars[arg_idx].v_type != VAR_LIST)
11869 {
11870 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11871 vim_free(name);
11872 return;
11873 }
11874 list = argvars[arg_idx].vval.v_list;
11875 if (list == NULL || list->lv_len == 0)
11876 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011877 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011878 }
11879 if (dict_idx > 0 || arg_idx > 0)
11880 {
11881 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011882
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011883 if (pt != NULL)
11884 {
11885 if (arg_idx > 0)
11886 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011887 listitem_T *li;
11888 int i = 0;
11889
11890 pt->pt_argv = (typval_T *)alloc(
11891 sizeof(typval_T) * list->lv_len);
11892 if (pt->pt_argv == NULL)
11893 {
11894 vim_free(pt);
11895 vim_free(name);
11896 return;
11897 }
11898 else
11899 {
11900 pt->pt_argc = list->lv_len;
11901 for (li = list->lv_first; li != NULL; li = li->li_next)
11902 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
11903 }
11904 }
11905
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011906 if (argvars[0].v_type == VAR_PARTIAL)
11907 {
11908 pt->pt_dict = argvars[0].vval.v_partial->pt_dict;
11909 ++pt->pt_dict->dv_refcount;
11910 }
11911 else if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011912 {
11913 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11914 ++pt->pt_dict->dv_refcount;
11915 }
11916
11917 pt->pt_refcount = 1;
11918 pt->pt_name = name;
11919 func_ref(pt->pt_name);
11920 }
11921 rettv->v_type = VAR_PARTIAL;
11922 rettv->vval.v_partial = pt;
11923 }
11924 else
11925 {
11926 rettv->v_type = VAR_FUNC;
11927 rettv->vval.v_string = name;
11928 func_ref(name);
11929 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011930 }
11931}
11932
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011933 static void
11934partial_free(partial_T *pt)
11935{
11936 int i;
11937
11938 for (i = 0; i < pt->pt_argc; ++i)
11939 clear_tv(&pt->pt_argv[i]);
11940 vim_free(pt->pt_argv);
11941 func_unref(pt->pt_name);
11942 vim_free(pt->pt_name);
11943 vim_free(pt);
11944}
11945
11946/*
11947 * Unreference a closure: decrement the reference count and free it when it
11948 * becomes zero.
11949 */
11950 void
11951partial_unref(partial_T *pt)
11952{
11953 if (pt != NULL && --pt->pt_refcount <= 0)
11954 partial_free(pt);
11955}
11956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011958 * "garbagecollect()" function
11959 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011961f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011962{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011963 /* This is postponed until we are back at the toplevel, because we may be
11964 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11965 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011966
11967 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11968 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011969}
11970
11971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011972 * "get()" function
11973 */
11974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011975f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011976{
Bram Moolenaar33570922005-01-25 22:26:29 +000011977 listitem_T *li;
11978 list_T *l;
11979 dictitem_T *di;
11980 dict_T *d;
11981 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011982
Bram Moolenaare9a41262005-01-15 22:18:47 +000011983 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011985 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 int error = FALSE;
11988
11989 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11990 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011991 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011993 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011994 else if (argvars[0].v_type == VAR_DICT)
11995 {
11996 if ((d = argvars[0].vval.v_dict) != NULL)
11997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011998 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011999 if (di != NULL)
12000 tv = &di->di_tv;
12001 }
12002 }
12003 else
12004 EMSG2(_(e_listdictarg), "get()");
12005
12006 if (tv == NULL)
12007 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012009 copy_tv(&argvars[2], rettv);
12010 }
12011 else
12012 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012013}
12014
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012015static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012016
12017/*
12018 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012019 * Return a range (from start to end) of lines in rettv from the specified
12020 * buffer.
12021 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012022 */
12023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012024get_buffer_lines(
12025 buf_T *buf,
12026 linenr_T start,
12027 linenr_T end,
12028 int retlist,
12029 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012030{
12031 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012032
Bram Moolenaar959a1432013-12-14 12:17:38 +010012033 rettv->v_type = VAR_STRING;
12034 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012035 if (retlist && rettv_list_alloc(rettv) == FAIL)
12036 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012037
12038 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12039 return;
12040
12041 if (!retlist)
12042 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012043 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12044 p = ml_get_buf(buf, start, FALSE);
12045 else
12046 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012047 rettv->vval.v_string = vim_strsave(p);
12048 }
12049 else
12050 {
12051 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012052 return;
12053
12054 if (start < 1)
12055 start = 1;
12056 if (end > buf->b_ml.ml_line_count)
12057 end = buf->b_ml.ml_line_count;
12058 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012059 if (list_append_string(rettv->vval.v_list,
12060 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012061 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012062 }
12063}
12064
12065/*
12066 * "getbufline()" function
12067 */
12068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012069f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012070{
12071 linenr_T lnum;
12072 linenr_T end;
12073 buf_T *buf;
12074
12075 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12076 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012077 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012078 --emsg_off;
12079
Bram Moolenaar661b1822005-07-28 22:36:45 +000012080 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012081 if (argvars[2].v_type == VAR_UNKNOWN)
12082 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012083 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012084 end = get_tv_lnum_buf(&argvars[2], buf);
12085
Bram Moolenaar342337a2005-07-21 21:11:17 +000012086 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087}
12088
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089/*
12090 * "getbufvar()" function
12091 */
12092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012093f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012094{
12095 buf_T *buf;
12096 buf_T *save_curbuf;
12097 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012098 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012099 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012101 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12102 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012103 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012104 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012106 rettv->v_type = VAR_STRING;
12107 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108
12109 if (buf != NULL && varname != NULL)
12110 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012111 /* set curbuf to be our buf, temporarily */
12112 save_curbuf = curbuf;
12113 curbuf = buf;
12114
Bram Moolenaar0d660222005-01-07 21:51:51 +000012115 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012116 {
12117 if (get_option_tv(&varname, rettv, TRUE) == OK)
12118 done = TRUE;
12119 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012120 else if (STRCMP(varname, "changedtick") == 0)
12121 {
12122 rettv->v_type = VAR_NUMBER;
12123 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012124 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126 else
12127 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012128 /* Look up the variable. */
12129 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12130 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12131 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012133 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012135 done = TRUE;
12136 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012137 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012138
12139 /* restore previous notion of curbuf */
12140 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141 }
12142
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012143 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12144 /* use the default value */
12145 copy_tv(&argvars[2], rettv);
12146
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147 --emsg_off;
12148}
12149
12150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 * "getchar()" function
12152 */
12153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012154f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155{
12156 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012157 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012159 /* Position the cursor. Needed after a message that ends in a space. */
12160 windgoto(msg_row, msg_col);
12161
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 ++no_mapping;
12163 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012164 for (;;)
12165 {
12166 if (argvars[0].v_type == VAR_UNKNOWN)
12167 /* getchar(): blocking wait. */
12168 n = safe_vgetc();
12169 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12170 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012171 n = vpeekc_any();
12172 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012173 /* illegal argument or getchar(0) and no char avail: return zero */
12174 n = 0;
12175 else
12176 /* getchar(0) and char avail: return char */
12177 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012178
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012179 if (n == K_IGNORE)
12180 continue;
12181 break;
12182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 --no_mapping;
12184 --allow_keys;
12185
Bram Moolenaar219b8702006-11-01 14:32:36 +000012186 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12187 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12188 vimvars[VV_MOUSE_COL].vv_nr = 0;
12189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 if (IS_SPECIAL(n) || mod_mask != 0)
12192 {
12193 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12194 int i = 0;
12195
12196 /* Turn a special key into three bytes, plus modifier. */
12197 if (mod_mask != 0)
12198 {
12199 temp[i++] = K_SPECIAL;
12200 temp[i++] = KS_MODIFIER;
12201 temp[i++] = mod_mask;
12202 }
12203 if (IS_SPECIAL(n))
12204 {
12205 temp[i++] = K_SPECIAL;
12206 temp[i++] = K_SECOND(n);
12207 temp[i++] = K_THIRD(n);
12208 }
12209#ifdef FEAT_MBYTE
12210 else if (has_mbyte)
12211 i += (*mb_char2bytes)(n, temp + i);
12212#endif
12213 else
12214 temp[i++] = n;
12215 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216 rettv->v_type = VAR_STRING;
12217 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012218
12219#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012220 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012221 {
12222 int row = mouse_row;
12223 int col = mouse_col;
12224 win_T *win;
12225 linenr_T lnum;
12226# ifdef FEAT_WINDOWS
12227 win_T *wp;
12228# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012229 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012230
12231 if (row >= 0 && col >= 0)
12232 {
12233 /* Find the window at the mouse coordinates and compute the
12234 * text position. */
12235 win = mouse_find_win(&row, &col);
12236 (void)mouse_comp_pos(win, &row, &col, &lnum);
12237# ifdef FEAT_WINDOWS
12238 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012239 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012240# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012241 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012242 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12243 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12244 }
12245 }
12246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 }
12248}
12249
12250/*
12251 * "getcharmod()" function
12252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012254f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257}
12258
12259/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012260 * "getcharsearch()" function
12261 */
12262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012263f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012264{
12265 if (rettv_dict_alloc(rettv) != FAIL)
12266 {
12267 dict_T *dict = rettv->vval.v_dict;
12268
12269 dict_add_nr_str(dict, "char", 0L, last_csearch());
12270 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12271 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12272 }
12273}
12274
12275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 * "getcmdline()" function
12277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012279f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281 rettv->v_type = VAR_STRING;
12282 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283}
12284
12285/*
12286 * "getcmdpos()" function
12287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012289f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292}
12293
12294/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012295 * "getcmdtype()" function
12296 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012298f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012299{
12300 rettv->v_type = VAR_STRING;
12301 rettv->vval.v_string = alloc(2);
12302 if (rettv->vval.v_string != NULL)
12303 {
12304 rettv->vval.v_string[0] = get_cmdline_type();
12305 rettv->vval.v_string[1] = NUL;
12306 }
12307}
12308
12309/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012310 * "getcmdwintype()" function
12311 */
12312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012313f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012314{
12315 rettv->v_type = VAR_STRING;
12316 rettv->vval.v_string = NULL;
12317#ifdef FEAT_CMDWIN
12318 rettv->vval.v_string = alloc(2);
12319 if (rettv->vval.v_string != NULL)
12320 {
12321 rettv->vval.v_string[0] = cmdwin_type;
12322 rettv->vval.v_string[1] = NUL;
12323 }
12324#endif
12325}
12326
12327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 * "getcwd()" function
12329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012331f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012333 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012334 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012337 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012338
12339 wp = find_tabwin(&argvars[0], &argvars[1]);
12340 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012342 if (wp->w_localdir != NULL)
12343 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12344 else if(globaldir != NULL)
12345 rettv->vval.v_string = vim_strsave(globaldir);
12346 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012347 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012348 cwd = alloc(MAXPATHL);
12349 if (cwd != NULL)
12350 {
12351 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12352 rettv->vval.v_string = vim_strsave(cwd);
12353 vim_free(cwd);
12354 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012355 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012356#ifdef BACKSLASH_IN_FILENAME
12357 if (rettv->vval.v_string != NULL)
12358 slash_adjust(rettv->vval.v_string);
12359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 }
12361}
12362
12363/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012364 * "getfontname()" function
12365 */
12366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012367f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012368{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 rettv->v_type = VAR_STRING;
12370 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012371#ifdef FEAT_GUI
12372 if (gui.in_use)
12373 {
12374 GuiFont font;
12375 char_u *name = NULL;
12376
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012377 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012378 {
12379 /* Get the "Normal" font. Either the name saved by
12380 * hl_set_font_name() or from the font ID. */
12381 font = gui.norm_font;
12382 name = hl_get_font_name();
12383 }
12384 else
12385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012386 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012387 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12388 return;
12389 font = gui_mch_get_font(name, FALSE);
12390 if (font == NOFONT)
12391 return; /* Invalid font name, return empty string. */
12392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012394 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012395 gui_mch_free_font(font);
12396 }
12397#endif
12398}
12399
12400/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012401 * "getfperm({fname})" function
12402 */
12403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012404f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012405{
12406 char_u *fname;
12407 struct stat st;
12408 char_u *perm = NULL;
12409 char_u flags[] = "rwx";
12410 int i;
12411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012412 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012414 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012415 if (mch_stat((char *)fname, &st) >= 0)
12416 {
12417 perm = vim_strsave((char_u *)"---------");
12418 if (perm != NULL)
12419 {
12420 for (i = 0; i < 9; i++)
12421 {
12422 if (st.st_mode & (1 << (8 - i)))
12423 perm[i] = flags[i % 3];
12424 }
12425 }
12426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012427 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012428}
12429
12430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 * "getfsize({fname})" function
12432 */
12433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012434f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435{
12436 char_u *fname;
12437 struct stat st;
12438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442
12443 if (mch_stat((char *)fname, &st) >= 0)
12444 {
12445 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012446 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012450
12451 /* non-perfect check for overflow */
12452 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12453 rettv->vval.v_number = -2;
12454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 }
12456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458}
12459
12460/*
12461 * "getftime({fname})" function
12462 */
12463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012464f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465{
12466 char_u *fname;
12467 struct stat st;
12468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470
12471 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475}
12476
12477/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012478 * "getftype({fname})" function
12479 */
12480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012481f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012482{
12483 char_u *fname;
12484 struct stat st;
12485 char_u *type = NULL;
12486 char *t;
12487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012491 if (mch_lstat((char *)fname, &st) >= 0)
12492 {
12493#ifdef S_ISREG
12494 if (S_ISREG(st.st_mode))
12495 t = "file";
12496 else if (S_ISDIR(st.st_mode))
12497 t = "dir";
12498# ifdef S_ISLNK
12499 else if (S_ISLNK(st.st_mode))
12500 t = "link";
12501# endif
12502# ifdef S_ISBLK
12503 else if (S_ISBLK(st.st_mode))
12504 t = "bdev";
12505# endif
12506# ifdef S_ISCHR
12507 else if (S_ISCHR(st.st_mode))
12508 t = "cdev";
12509# endif
12510# ifdef S_ISFIFO
12511 else if (S_ISFIFO(st.st_mode))
12512 t = "fifo";
12513# endif
12514# ifdef S_ISSOCK
12515 else if (S_ISSOCK(st.st_mode))
12516 t = "fifo";
12517# endif
12518 else
12519 t = "other";
12520#else
12521# ifdef S_IFMT
12522 switch (st.st_mode & S_IFMT)
12523 {
12524 case S_IFREG: t = "file"; break;
12525 case S_IFDIR: t = "dir"; break;
12526# ifdef S_IFLNK
12527 case S_IFLNK: t = "link"; break;
12528# endif
12529# ifdef S_IFBLK
12530 case S_IFBLK: t = "bdev"; break;
12531# endif
12532# ifdef S_IFCHR
12533 case S_IFCHR: t = "cdev"; break;
12534# endif
12535# ifdef S_IFIFO
12536 case S_IFIFO: t = "fifo"; break;
12537# endif
12538# ifdef S_IFSOCK
12539 case S_IFSOCK: t = "socket"; break;
12540# endif
12541 default: t = "other";
12542 }
12543# else
12544 if (mch_isdir(fname))
12545 t = "dir";
12546 else
12547 t = "file";
12548# endif
12549#endif
12550 type = vim_strsave((char_u *)t);
12551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012553}
12554
12555/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012556 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012557 */
12558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012559f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012560{
12561 linenr_T lnum;
12562 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012563 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012564
12565 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012566 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012567 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012568 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012569 retlist = FALSE;
12570 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012572 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012574 retlist = TRUE;
12575 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012576
Bram Moolenaar342337a2005-07-21 21:11:17 +000012577 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578}
12579
12580/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012581 * "getmatches()" function
12582 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012584f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012585{
12586#ifdef FEAT_SEARCH_EXTRA
12587 dict_T *dict;
12588 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012589 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012590
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012591 if (rettv_list_alloc(rettv) == OK)
12592 {
12593 while (cur != NULL)
12594 {
12595 dict = dict_alloc();
12596 if (dict == NULL)
12597 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012598 if (cur->match.regprog == NULL)
12599 {
12600 /* match added with matchaddpos() */
12601 for (i = 0; i < MAXPOSMATCH; ++i)
12602 {
12603 llpos_T *llpos;
12604 char buf[6];
12605 list_T *l;
12606
12607 llpos = &cur->pos.pos[i];
12608 if (llpos->lnum == 0)
12609 break;
12610 l = list_alloc();
12611 if (l == NULL)
12612 break;
12613 list_append_number(l, (varnumber_T)llpos->lnum);
12614 if (llpos->col > 0)
12615 {
12616 list_append_number(l, (varnumber_T)llpos->col);
12617 list_append_number(l, (varnumber_T)llpos->len);
12618 }
12619 sprintf(buf, "pos%d", i + 1);
12620 dict_add_list(dict, buf, l);
12621 }
12622 }
12623 else
12624 {
12625 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12626 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012627 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012628 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12629 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012630# ifdef FEAT_CONCEAL
12631 if (cur->conceal_char)
12632 {
12633 char_u buf[MB_MAXBYTES + 1];
12634
12635 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12636 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12637 }
12638# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012639 list_append_dict(rettv->vval.v_list, dict);
12640 cur = cur->next;
12641 }
12642 }
12643#endif
12644}
12645
12646/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012647 * "getpid()" function
12648 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012650f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012651{
12652 rettv->vval.v_number = mch_get_pid();
12653}
12654
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012655static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012656
12657/*
12658 * "getcurpos()" function
12659 */
12660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012661f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012662{
12663 getpos_both(argvars, rettv, TRUE);
12664}
12665
Bram Moolenaar18081e32008-02-20 19:11:07 +000012666/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012667 * "getpos(string)" function
12668 */
12669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012670f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012671{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012672 getpos_both(argvars, rettv, FALSE);
12673}
12674
12675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012676getpos_both(
12677 typval_T *argvars,
12678 typval_T *rettv,
12679 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012680{
Bram Moolenaara5525202006-03-02 22:52:09 +000012681 pos_T *fp;
12682 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012683 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012684
12685 if (rettv_list_alloc(rettv) == OK)
12686 {
12687 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012688 if (getcurpos)
12689 fp = &curwin->w_cursor;
12690 else
12691 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012692 if (fnum != -1)
12693 list_append_number(l, (varnumber_T)fnum);
12694 else
12695 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012696 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12697 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012698 list_append_number(l, (fp != NULL)
12699 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012700 : (varnumber_T)0);
12701 list_append_number(l,
12702#ifdef FEAT_VIRTUALEDIT
12703 (fp != NULL) ? (varnumber_T)fp->coladd :
12704#endif
12705 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012706 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012707 {
12708 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012709 list_append_number(l, curwin->w_curswant == MAXCOL ?
12710 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012711 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012712 }
12713 else
12714 rettv->vval.v_number = FALSE;
12715}
12716
12717/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012718 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012719 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012721f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012722{
12723#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012724 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012725#endif
12726
Bram Moolenaar2641f772005-03-25 21:58:17 +000012727#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012728 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012729 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012730 wp = NULL;
12731 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12732 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012733 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012734 if (wp == NULL)
12735 return;
12736 }
12737
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012738 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012739 }
12740#endif
12741}
12742
12743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 * "getreg()" function
12745 */
12746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012747f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748{
12749 char_u *strregname;
12750 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012751 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012752 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012753 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012755 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012757 strregname = get_tv_string_chk(&argvars[0]);
12758 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012759 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012762 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12763 return_list = get_tv_number_chk(&argvars[2], &error);
12764 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012767 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012768
12769 if (error)
12770 return;
12771
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 regname = (strregname == NULL ? '"' : *strregname);
12773 if (regname == 0)
12774 regname = '"';
12775
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012776 if (return_list)
12777 {
12778 rettv->v_type = VAR_LIST;
12779 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12780 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012781 if (rettv->vval.v_list != NULL)
12782 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012783 }
12784 else
12785 {
12786 rettv->v_type = VAR_STRING;
12787 rettv->vval.v_string = get_reg_contents(regname,
12788 arg2 ? GREG_EXPR_SRC : 0);
12789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790}
12791
12792/*
12793 * "getregtype()" function
12794 */
12795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012796f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797{
12798 char_u *strregname;
12799 int regname;
12800 char_u buf[NUMBUFLEN + 2];
12801 long reglen = 0;
12802
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012803 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012804 {
12805 strregname = get_tv_string_chk(&argvars[0]);
12806 if (strregname == NULL) /* type error; errmsg already given */
12807 {
12808 rettv->v_type = VAR_STRING;
12809 rettv->vval.v_string = NULL;
12810 return;
12811 }
12812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 else
12814 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012815 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816
12817 regname = (strregname == NULL ? '"' : *strregname);
12818 if (regname == 0)
12819 regname = '"';
12820
12821 buf[0] = NUL;
12822 buf[1] = NUL;
12823 switch (get_reg_type(regname, &reglen))
12824 {
12825 case MLINE: buf[0] = 'V'; break;
12826 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827 case MBLOCK:
12828 buf[0] = Ctrl_V;
12829 sprintf((char *)buf + 1, "%ld", reglen + 1);
12830 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 rettv->v_type = VAR_STRING;
12833 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834}
12835
12836/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012837 * "gettabvar()" function
12838 */
12839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012840f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012841{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012842 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012843 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012844 dictitem_T *v;
12845 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012846 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012847
12848 rettv->v_type = VAR_STRING;
12849 rettv->vval.v_string = NULL;
12850
12851 varname = get_tv_string_chk(&argvars[1]);
12852 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12853 if (tp != NULL && varname != NULL)
12854 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012855 /* Set tp to be our tabpage, temporarily. Also set the window to the
12856 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012857 if (switch_win(&oldcurwin, &oldtabpage,
12858 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012859 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012860 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012861 /* look up the variable */
12862 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12863 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12864 if (v != NULL)
12865 {
12866 copy_tv(&v->di_tv, rettv);
12867 done = TRUE;
12868 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012869 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012870
12871 /* restore previous notion of curwin */
12872 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012873 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012874
12875 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012876 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012877}
12878
12879/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012880 * "gettabwinvar()" function
12881 */
12882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012883f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012884{
12885 getwinvar(argvars, rettv, 1);
12886}
12887
12888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 * "getwinposx()" function
12890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012892f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895#ifdef FEAT_GUI
12896 if (gui.in_use)
12897 {
12898 int x, y;
12899
12900 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 }
12903#endif
12904}
12905
12906/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012907 * "win_findbuf()" function
12908 */
12909 static void
12910f_win_findbuf(typval_T *argvars, typval_T *rettv)
12911{
12912 if (rettv_list_alloc(rettv) != FAIL)
12913 win_findbuf(argvars, rettv->vval.v_list);
12914}
12915
12916/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012917 * "win_getid()" function
12918 */
12919 static void
12920f_win_getid(typval_T *argvars, typval_T *rettv)
12921{
12922 rettv->vval.v_number = win_getid(argvars);
12923}
12924
12925/*
12926 * "win_gotoid()" function
12927 */
12928 static void
12929f_win_gotoid(typval_T *argvars, typval_T *rettv)
12930{
12931 rettv->vval.v_number = win_gotoid(argvars);
12932}
12933
12934/*
12935 * "win_id2tabwin()" function
12936 */
12937 static void
12938f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12939{
12940 if (rettv_list_alloc(rettv) != FAIL)
12941 win_id2tabwin(argvars, rettv->vval.v_list);
12942}
12943
12944/*
12945 * "win_id2win()" function
12946 */
12947 static void
12948f_win_id2win(typval_T *argvars, typval_T *rettv)
12949{
12950 rettv->vval.v_number = win_id2win(argvars);
12951}
12952
12953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 * "getwinposy()" function
12955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012957f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960#ifdef FEAT_GUI
12961 if (gui.in_use)
12962 {
12963 int x, y;
12964
12965 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012966 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 }
12968#endif
12969}
12970
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012971/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012972 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012973 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012974 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012975find_win_by_nr(
12976 typval_T *vp,
12977 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012978{
12979#ifdef FEAT_WINDOWS
12980 win_T *wp;
12981#endif
12982 int nr;
12983
12984 nr = get_tv_number_chk(vp, NULL);
12985
12986#ifdef FEAT_WINDOWS
12987 if (nr < 0)
12988 return NULL;
12989 if (nr == 0)
12990 return curwin;
12991
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012992 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12993 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012994 if (--nr <= 0)
12995 break;
12996 return wp;
12997#else
12998 if (nr == 0 || nr == 1)
12999 return curwin;
13000 return NULL;
13001#endif
13002}
13003
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013005 * Find window specified by "wvp" in tabpage "tvp".
13006 */
13007 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013008find_tabwin(
13009 typval_T *wvp, /* VAR_UNKNOWN for current window */
13010 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013011{
13012 win_T *wp = NULL;
13013 tabpage_T *tp = NULL;
13014 long n;
13015
13016 if (wvp->v_type != VAR_UNKNOWN)
13017 {
13018 if (tvp->v_type != VAR_UNKNOWN)
13019 {
13020 n = get_tv_number(tvp);
13021 if (n >= 0)
13022 tp = find_tabpage(n);
13023 }
13024 else
13025 tp = curtab;
13026
13027 if (tp != NULL)
13028 wp = find_win_by_nr(wvp, tp);
13029 }
13030 else
13031 wp = curwin;
13032
13033 return wp;
13034}
13035
13036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 * "getwinvar()" function
13038 */
13039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013040f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013042 getwinvar(argvars, rettv, 0);
13043}
13044
13045/*
13046 * getwinvar() and gettabwinvar()
13047 */
13048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013049getwinvar(
13050 typval_T *argvars,
13051 typval_T *rettv,
13052 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013053{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013054 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013056 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013057 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013058 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013059#ifdef FEAT_WINDOWS
13060 win_T *oldcurwin;
13061 tabpage_T *oldtabpage;
13062 int need_switch_win;
13063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013065#ifdef FEAT_WINDOWS
13066 if (off == 1)
13067 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13068 else
13069 tp = curtab;
13070#endif
13071 win = find_win_by_nr(&argvars[off], tp);
13072 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013073 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013075 rettv->v_type = VAR_STRING;
13076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077
13078 if (win != NULL && varname != NULL)
13079 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013080#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013081 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013082 * otherwise the window is not valid. Only do this when needed,
13083 * autocommands get blocked. */
13084 need_switch_win = !(tp == curtab && win == curwin);
13085 if (!need_switch_win
13086 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13087#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013088 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013089 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013090 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013091 if (get_option_tv(&varname, rettv, 1) == OK)
13092 done = TRUE;
13093 }
13094 else
13095 {
13096 /* Look up the variable. */
13097 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13098 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13099 varname, FALSE);
13100 if (v != NULL)
13101 {
13102 copy_tv(&v->di_tv, rettv);
13103 done = TRUE;
13104 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013107
Bram Moolenaarba117c22015-09-29 16:53:22 +020013108#ifdef FEAT_WINDOWS
13109 if (need_switch_win)
13110 /* restore previous notion of curwin */
13111 restore_win(oldcurwin, oldtabpage, TRUE);
13112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 }
13114
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013115 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13116 /* use the default return value */
13117 copy_tv(&argvars[off + 2], rettv);
13118
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 --emsg_off;
13120}
13121
13122/*
13123 * "glob()" function
13124 */
13125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013126f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013128 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013130 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013132 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013133 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013135 if (argvars[1].v_type != VAR_UNKNOWN)
13136 {
13137 if (get_tv_number_chk(&argvars[1], &error))
13138 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013139 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013140 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013141 if (get_tv_number_chk(&argvars[2], &error))
13142 {
13143 rettv->v_type = VAR_LIST;
13144 rettv->vval.v_list = NULL;
13145 }
13146 if (argvars[3].v_type != VAR_UNKNOWN
13147 && get_tv_number_chk(&argvars[3], &error))
13148 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013149 }
13150 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013151 if (!error)
13152 {
13153 ExpandInit(&xpc);
13154 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013155 if (p_wic)
13156 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013157 if (rettv->v_type == VAR_STRING)
13158 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013159 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013160 else if (rettv_list_alloc(rettv) != FAIL)
13161 {
13162 int i;
13163
13164 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13165 NULL, options, WILD_ALL_KEEP);
13166 for (i = 0; i < xpc.xp_numfiles; i++)
13167 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13168
13169 ExpandCleanup(&xpc);
13170 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013171 }
13172 else
13173 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174}
13175
13176/*
13177 * "globpath()" function
13178 */
13179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013180f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013182 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013184 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013185 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013186 garray_T ga;
13187 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013189 /* When the optional second argument is non-zero, don't remove matches
13190 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013192 if (argvars[2].v_type != VAR_UNKNOWN)
13193 {
13194 if (get_tv_number_chk(&argvars[2], &error))
13195 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013196 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013197 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013198 if (get_tv_number_chk(&argvars[3], &error))
13199 {
13200 rettv->v_type = VAR_LIST;
13201 rettv->vval.v_list = NULL;
13202 }
13203 if (argvars[4].v_type != VAR_UNKNOWN
13204 && get_tv_number_chk(&argvars[4], &error))
13205 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013206 }
13207 }
13208 if (file != NULL && !error)
13209 {
13210 ga_init2(&ga, (int)sizeof(char_u *), 10);
13211 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13212 if (rettv->v_type == VAR_STRING)
13213 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13214 else if (rettv_list_alloc(rettv) != FAIL)
13215 for (i = 0; i < ga.ga_len; ++i)
13216 list_append_string(rettv->vval.v_list,
13217 ((char_u **)(ga.ga_data))[i], -1);
13218 ga_clear_strings(&ga);
13219 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013221 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222}
13223
13224/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013225 * "glob2regpat()" function
13226 */
13227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013228f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013229{
13230 char_u *pat = get_tv_string_chk(&argvars[0]);
13231
13232 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013233 rettv->vval.v_string = (pat == NULL)
13234 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013235}
13236
13237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 * "has()" function
13239 */
13240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013241f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242{
13243 int i;
13244 char_u *name;
13245 int n = FALSE;
13246 static char *(has_list[]) =
13247 {
13248#ifdef AMIGA
13249 "amiga",
13250# ifdef FEAT_ARP
13251 "arp",
13252# endif
13253#endif
13254#ifdef __BEOS__
13255 "beos",
13256#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013257#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 "mac",
13259#endif
13260#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013261 "macunix", /* built with 'darwin' enabled */
13262#endif
13263#if defined(__APPLE__) && __APPLE__ == 1
13264 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266#ifdef __QNX__
13267 "qnx",
13268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269#ifdef UNIX
13270 "unix",
13271#endif
13272#ifdef VMS
13273 "vms",
13274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275#ifdef WIN32
13276 "win32",
13277#endif
13278#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13279 "win32unix",
13280#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013281#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 "win64",
13283#endif
13284#ifdef EBCDIC
13285 "ebcdic",
13286#endif
13287#ifndef CASE_INSENSITIVE_FILENAME
13288 "fname_case",
13289#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013290#ifdef HAVE_ACL
13291 "acl",
13292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293#ifdef FEAT_ARABIC
13294 "arabic",
13295#endif
13296#ifdef FEAT_AUTOCMD
13297 "autocmd",
13298#endif
13299#ifdef FEAT_BEVAL
13300 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013301# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13302 "balloon_multiline",
13303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304#endif
13305#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13306 "builtin_terms",
13307# ifdef ALL_BUILTIN_TCAPS
13308 "all_builtin_terms",
13309# endif
13310#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013311#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13312 || defined(FEAT_GUI_W32) \
13313 || defined(FEAT_GUI_MOTIF))
13314 "browsefilter",
13315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316#ifdef FEAT_BYTEOFF
13317 "byte_offset",
13318#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013319#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013320 "channel",
13321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322#ifdef FEAT_CINDENT
13323 "cindent",
13324#endif
13325#ifdef FEAT_CLIENTSERVER
13326 "clientserver",
13327#endif
13328#ifdef FEAT_CLIPBOARD
13329 "clipboard",
13330#endif
13331#ifdef FEAT_CMDL_COMPL
13332 "cmdline_compl",
13333#endif
13334#ifdef FEAT_CMDHIST
13335 "cmdline_hist",
13336#endif
13337#ifdef FEAT_COMMENTS
13338 "comments",
13339#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013340#ifdef FEAT_CONCEAL
13341 "conceal",
13342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343#ifdef FEAT_CRYPT
13344 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013345 "crypt-blowfish",
13346 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347#endif
13348#ifdef FEAT_CSCOPE
13349 "cscope",
13350#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013351#ifdef FEAT_CURSORBIND
13352 "cursorbind",
13353#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013354#ifdef CURSOR_SHAPE
13355 "cursorshape",
13356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357#ifdef DEBUG
13358 "debug",
13359#endif
13360#ifdef FEAT_CON_DIALOG
13361 "dialog_con",
13362#endif
13363#ifdef FEAT_GUI_DIALOG
13364 "dialog_gui",
13365#endif
13366#ifdef FEAT_DIFF
13367 "diff",
13368#endif
13369#ifdef FEAT_DIGRAPHS
13370 "digraphs",
13371#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013372#ifdef FEAT_DIRECTX
13373 "directx",
13374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375#ifdef FEAT_DND
13376 "dnd",
13377#endif
13378#ifdef FEAT_EMACS_TAGS
13379 "emacs_tags",
13380#endif
13381 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013382 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383#ifdef FEAT_SEARCH_EXTRA
13384 "extra_search",
13385#endif
13386#ifdef FEAT_FKMAP
13387 "farsi",
13388#endif
13389#ifdef FEAT_SEARCHPATH
13390 "file_in_path",
13391#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013392#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013393 "filterpipe",
13394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395#ifdef FEAT_FIND_ID
13396 "find_in_path",
13397#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013398#ifdef FEAT_FLOAT
13399 "float",
13400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401#ifdef FEAT_FOLDING
13402 "folding",
13403#endif
13404#ifdef FEAT_FOOTER
13405 "footer",
13406#endif
13407#if !defined(USE_SYSTEM) && defined(UNIX)
13408 "fork",
13409#endif
13410#ifdef FEAT_GETTEXT
13411 "gettext",
13412#endif
13413#ifdef FEAT_GUI
13414 "gui",
13415#endif
13416#ifdef FEAT_GUI_ATHENA
13417# ifdef FEAT_GUI_NEXTAW
13418 "gui_neXtaw",
13419# else
13420 "gui_athena",
13421# endif
13422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef FEAT_GUI_GTK
13424 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013425# ifdef USE_GTK3
13426 "gui_gtk3",
13427# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013431#ifdef FEAT_GUI_GNOME
13432 "gui_gnome",
13433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434#ifdef FEAT_GUI_MAC
13435 "gui_mac",
13436#endif
13437#ifdef FEAT_GUI_MOTIF
13438 "gui_motif",
13439#endif
13440#ifdef FEAT_GUI_PHOTON
13441 "gui_photon",
13442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443#ifdef FEAT_GUI_W32
13444 "gui_win32",
13445#endif
13446#ifdef FEAT_HANGULIN
13447 "hangul_input",
13448#endif
13449#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13450 "iconv",
13451#endif
13452#ifdef FEAT_INS_EXPAND
13453 "insert_expand",
13454#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013455#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013456 "job",
13457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458#ifdef FEAT_JUMPLIST
13459 "jumplist",
13460#endif
13461#ifdef FEAT_KEYMAP
13462 "keymap",
13463#endif
13464#ifdef FEAT_LANGMAP
13465 "langmap",
13466#endif
13467#ifdef FEAT_LIBCALL
13468 "libcall",
13469#endif
13470#ifdef FEAT_LINEBREAK
13471 "linebreak",
13472#endif
13473#ifdef FEAT_LISP
13474 "lispindent",
13475#endif
13476#ifdef FEAT_LISTCMDS
13477 "listcmds",
13478#endif
13479#ifdef FEAT_LOCALMAP
13480 "localmap",
13481#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013482#ifdef FEAT_LUA
13483# ifndef DYNAMIC_LUA
13484 "lua",
13485# endif
13486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487#ifdef FEAT_MENU
13488 "menu",
13489#endif
13490#ifdef FEAT_SESSION
13491 "mksession",
13492#endif
13493#ifdef FEAT_MODIFY_FNAME
13494 "modify_fname",
13495#endif
13496#ifdef FEAT_MOUSE
13497 "mouse",
13498#endif
13499#ifdef FEAT_MOUSESHAPE
13500 "mouseshape",
13501#endif
13502#if defined(UNIX) || defined(VMS)
13503# ifdef FEAT_MOUSE_DEC
13504 "mouse_dec",
13505# endif
13506# ifdef FEAT_MOUSE_GPM
13507 "mouse_gpm",
13508# endif
13509# ifdef FEAT_MOUSE_JSB
13510 "mouse_jsbterm",
13511# endif
13512# ifdef FEAT_MOUSE_NET
13513 "mouse_netterm",
13514# endif
13515# ifdef FEAT_MOUSE_PTERM
13516 "mouse_pterm",
13517# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013518# ifdef FEAT_MOUSE_SGR
13519 "mouse_sgr",
13520# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013521# ifdef FEAT_SYSMOUSE
13522 "mouse_sysmouse",
13523# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013524# ifdef FEAT_MOUSE_URXVT
13525 "mouse_urxvt",
13526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527# ifdef FEAT_MOUSE_XTERM
13528 "mouse_xterm",
13529# endif
13530#endif
13531#ifdef FEAT_MBYTE
13532 "multi_byte",
13533#endif
13534#ifdef FEAT_MBYTE_IME
13535 "multi_byte_ime",
13536#endif
13537#ifdef FEAT_MULTI_LANG
13538 "multi_lang",
13539#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013540#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013541#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013542 "mzscheme",
13543#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545#ifdef FEAT_OLE
13546 "ole",
13547#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013548 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549#ifdef FEAT_PATH_EXTRA
13550 "path_extra",
13551#endif
13552#ifdef FEAT_PERL
13553#ifndef DYNAMIC_PERL
13554 "perl",
13555#endif
13556#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013557#ifdef FEAT_PERSISTENT_UNDO
13558 "persistent_undo",
13559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560#ifdef FEAT_PYTHON
13561#ifndef DYNAMIC_PYTHON
13562 "python",
13563#endif
13564#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013565#ifdef FEAT_PYTHON3
13566#ifndef DYNAMIC_PYTHON3
13567 "python3",
13568#endif
13569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570#ifdef FEAT_POSTSCRIPT
13571 "postscript",
13572#endif
13573#ifdef FEAT_PRINTER
13574 "printer",
13575#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013576#ifdef FEAT_PROFILE
13577 "profile",
13578#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013579#ifdef FEAT_RELTIME
13580 "reltime",
13581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#ifdef FEAT_QUICKFIX
13583 "quickfix",
13584#endif
13585#ifdef FEAT_RIGHTLEFT
13586 "rightleft",
13587#endif
13588#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13589 "ruby",
13590#endif
13591#ifdef FEAT_SCROLLBIND
13592 "scrollbind",
13593#endif
13594#ifdef FEAT_CMDL_INFO
13595 "showcmd",
13596 "cmdline_info",
13597#endif
13598#ifdef FEAT_SIGNS
13599 "signs",
13600#endif
13601#ifdef FEAT_SMARTINDENT
13602 "smartindent",
13603#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013604#ifdef STARTUPTIME
13605 "startuptime",
13606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607#ifdef FEAT_STL_OPT
13608 "statusline",
13609#endif
13610#ifdef FEAT_SUN_WORKSHOP
13611 "sun_workshop",
13612#endif
13613#ifdef FEAT_NETBEANS_INTG
13614 "netbeans_intg",
13615#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013616#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013617 "spell",
13618#endif
13619#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 "syntax",
13621#endif
13622#if defined(USE_SYSTEM) || !defined(UNIX)
13623 "system",
13624#endif
13625#ifdef FEAT_TAG_BINS
13626 "tag_binary",
13627#endif
13628#ifdef FEAT_TAG_OLDSTATIC
13629 "tag_old_static",
13630#endif
13631#ifdef FEAT_TAG_ANYWHITE
13632 "tag_any_white",
13633#endif
13634#ifdef FEAT_TCL
13635# ifndef DYNAMIC_TCL
13636 "tcl",
13637# endif
13638#endif
13639#ifdef TERMINFO
13640 "terminfo",
13641#endif
13642#ifdef FEAT_TERMRESPONSE
13643 "termresponse",
13644#endif
13645#ifdef FEAT_TEXTOBJ
13646 "textobjects",
13647#endif
13648#ifdef HAVE_TGETENT
13649 "tgetent",
13650#endif
13651#ifdef FEAT_TITLE
13652 "title",
13653#endif
13654#ifdef FEAT_TOOLBAR
13655 "toolbar",
13656#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013657#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13658 "unnamedplus",
13659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660#ifdef FEAT_USR_CMDS
13661 "user-commands", /* was accidentally included in 5.4 */
13662 "user_commands",
13663#endif
13664#ifdef FEAT_VIMINFO
13665 "viminfo",
13666#endif
13667#ifdef FEAT_VERTSPLIT
13668 "vertsplit",
13669#endif
13670#ifdef FEAT_VIRTUALEDIT
13671 "virtualedit",
13672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674#ifdef FEAT_VISUALEXTRA
13675 "visualextra",
13676#endif
13677#ifdef FEAT_VREPLACE
13678 "vreplace",
13679#endif
13680#ifdef FEAT_WILDIGN
13681 "wildignore",
13682#endif
13683#ifdef FEAT_WILDMENU
13684 "wildmenu",
13685#endif
13686#ifdef FEAT_WINDOWS
13687 "windows",
13688#endif
13689#ifdef FEAT_WAK
13690 "winaltkeys",
13691#endif
13692#ifdef FEAT_WRITEBACKUP
13693 "writebackup",
13694#endif
13695#ifdef FEAT_XIM
13696 "xim",
13697#endif
13698#ifdef FEAT_XFONTSET
13699 "xfontset",
13700#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013701#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013702 "xpm",
13703 "xpm_w32", /* for backward compatibility */
13704#else
13705# if defined(HAVE_XPM)
13706 "xpm",
13707# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709#ifdef USE_XSMP
13710 "xsmp",
13711#endif
13712#ifdef USE_XSMP_INTERACT
13713 "xsmp_interact",
13714#endif
13715#ifdef FEAT_XCLIPBOARD
13716 "xterm_clipboard",
13717#endif
13718#ifdef FEAT_XTERM_SAVE
13719 "xterm_save",
13720#endif
13721#if defined(UNIX) && defined(FEAT_X11)
13722 "X11",
13723#endif
13724 NULL
13725 };
13726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013727 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 for (i = 0; has_list[i] != NULL; ++i)
13729 if (STRICMP(name, has_list[i]) == 0)
13730 {
13731 n = TRUE;
13732 break;
13733 }
13734
13735 if (n == FALSE)
13736 {
13737 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013738 {
13739 if (name[5] == '-'
13740 && STRLEN(name) > 11
13741 && vim_isdigit(name[6])
13742 && vim_isdigit(name[8])
13743 && vim_isdigit(name[10]))
13744 {
13745 int major = atoi((char *)name + 6);
13746 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013747
13748 /* Expect "patch-9.9.01234". */
13749 n = (major < VIM_VERSION_MAJOR
13750 || (major == VIM_VERSION_MAJOR
13751 && (minor < VIM_VERSION_MINOR
13752 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013753 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013754 }
13755 else
13756 n = has_patch(atoi((char *)name + 5));
13757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 else if (STRICMP(name, "vim_starting") == 0)
13759 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013760#ifdef FEAT_MBYTE
13761 else if (STRICMP(name, "multi_byte_encoding") == 0)
13762 n = has_mbyte;
13763#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013764#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13765 else if (STRICMP(name, "balloon_multiline") == 0)
13766 n = multiline_balloon_available();
13767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768#ifdef DYNAMIC_TCL
13769 else if (STRICMP(name, "tcl") == 0)
13770 n = tcl_enabled(FALSE);
13771#endif
13772#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13773 else if (STRICMP(name, "iconv") == 0)
13774 n = iconv_enabled(FALSE);
13775#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013776#ifdef DYNAMIC_LUA
13777 else if (STRICMP(name, "lua") == 0)
13778 n = lua_enabled(FALSE);
13779#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013780#ifdef DYNAMIC_MZSCHEME
13781 else if (STRICMP(name, "mzscheme") == 0)
13782 n = mzscheme_enabled(FALSE);
13783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784#ifdef DYNAMIC_RUBY
13785 else if (STRICMP(name, "ruby") == 0)
13786 n = ruby_enabled(FALSE);
13787#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013788#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789#ifdef DYNAMIC_PYTHON
13790 else if (STRICMP(name, "python") == 0)
13791 n = python_enabled(FALSE);
13792#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013793#endif
13794#ifdef FEAT_PYTHON3
13795#ifdef DYNAMIC_PYTHON3
13796 else if (STRICMP(name, "python3") == 0)
13797 n = python3_enabled(FALSE);
13798#endif
13799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800#ifdef DYNAMIC_PERL
13801 else if (STRICMP(name, "perl") == 0)
13802 n = perl_enabled(FALSE);
13803#endif
13804#ifdef FEAT_GUI
13805 else if (STRICMP(name, "gui_running") == 0)
13806 n = (gui.in_use || gui.starting);
13807# ifdef FEAT_GUI_W32
13808 else if (STRICMP(name, "gui_win32s") == 0)
13809 n = gui_is_win32s();
13810# endif
13811# ifdef FEAT_BROWSE
13812 else if (STRICMP(name, "browse") == 0)
13813 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13814# endif
13815#endif
13816#ifdef FEAT_SYN_HL
13817 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013818 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#endif
13820#if defined(WIN3264)
13821 else if (STRICMP(name, "win95") == 0)
13822 n = mch_windows95();
13823#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013824#ifdef FEAT_NETBEANS_INTG
13825 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013826 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 }
13829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831}
13832
13833/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013834 * "has_key()" function
13835 */
13836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013837f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013838{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013839 if (argvars[0].v_type != VAR_DICT)
13840 {
13841 EMSG(_(e_dictreq));
13842 return;
13843 }
13844 if (argvars[0].vval.v_dict == NULL)
13845 return;
13846
13847 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013848 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013849}
13850
13851/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013852 * "haslocaldir()" function
13853 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013855f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013856{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013857 win_T *wp = NULL;
13858
13859 wp = find_tabwin(&argvars[0], &argvars[1]);
13860 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013861}
13862
13863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 * "hasmapto()" function
13865 */
13866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013867f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868{
13869 char_u *name;
13870 char_u *mode;
13871 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013872 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013874 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013875 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 mode = (char_u *)"nvo";
13877 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013879 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013880 if (argvars[2].v_type != VAR_UNKNOWN)
13881 abbr = get_tv_number(&argvars[2]);
13882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883
Bram Moolenaar2c932302006-03-18 21:42:09 +000013884 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013885 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888}
13889
13890/*
13891 * "histadd()" function
13892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013894f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895{
13896#ifdef FEAT_CMDHIST
13897 int histype;
13898 char_u *str;
13899 char_u buf[NUMBUFLEN];
13900#endif
13901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013902 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 if (check_restricted() || check_secure())
13904 return;
13905#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013906 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13907 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 if (histype >= 0)
13909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 if (*str != NUL)
13912 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013913 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013915 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 return;
13917 }
13918 }
13919#endif
13920}
13921
13922/*
13923 * "histdel()" function
13924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013926f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927{
13928#ifdef FEAT_CMDHIST
13929 int n;
13930 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013931 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13934 if (str == NULL)
13935 n = 0;
13936 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013938 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013939 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013941 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 else
13944 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013945 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013946 get_tv_string_buf(&argvars[1], buf));
13947 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948#endif
13949}
13950
13951/*
13952 * "histget()" function
13953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013955f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956{
13957#ifdef FEAT_CMDHIST
13958 int type;
13959 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013962 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13963 if (str == NULL)
13964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966 {
13967 type = get_histtype(str);
13968 if (argvars[1].v_type == VAR_UNKNOWN)
13969 idx = get_history_idx(type);
13970 else
13971 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13972 /* -1 on type error */
13973 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979}
13980
13981/*
13982 * "histnr()" function
13983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013985f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986{
13987 int i;
13988
13989#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990 char_u *history = get_tv_string_chk(&argvars[0]);
13991
13992 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 if (i >= HIST_CMD && i < HIST_COUNT)
13994 i = get_history_idx(i);
13995 else
13996#endif
13997 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999}
14000
14001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 * "highlightID(name)" function
14003 */
14004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014005f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014007 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008}
14009
14010/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014011 * "highlight_exists()" function
14012 */
14013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014014f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014015{
14016 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14017}
14018
14019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 * "hostname()" function
14021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014023f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024{
14025 char_u hostname[256];
14026
14027 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 rettv->v_type = VAR_STRING;
14029 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030}
14031
14032/*
14033 * iconv() function
14034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014036f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037{
14038#ifdef FEAT_MBYTE
14039 char_u buf1[NUMBUFLEN];
14040 char_u buf2[NUMBUFLEN];
14041 char_u *from, *to, *str;
14042 vimconv_T vimconv;
14043#endif
14044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014045 rettv->v_type = VAR_STRING;
14046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047
14048#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014049 str = get_tv_string(&argvars[0]);
14050 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14051 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 vimconv.vc_type = CONV_NONE;
14053 convert_setup(&vimconv, from, to);
14054
14055 /* If the encodings are equal, no conversion needed. */
14056 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060
14061 convert_setup(&vimconv, NULL, NULL);
14062 vim_free(from);
14063 vim_free(to);
14064#endif
14065}
14066
14067/*
14068 * "indent()" function
14069 */
14070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014071f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072{
14073 linenr_T lnum;
14074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080}
14081
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014082/*
14083 * "index()" function
14084 */
14085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014086f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014087{
Bram Moolenaar33570922005-01-25 22:26:29 +000014088 list_T *l;
14089 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014090 long idx = 0;
14091 int ic = FALSE;
14092
14093 rettv->vval.v_number = -1;
14094 if (argvars[0].v_type != VAR_LIST)
14095 {
14096 EMSG(_(e_listreq));
14097 return;
14098 }
14099 l = argvars[0].vval.v_list;
14100 if (l != NULL)
14101 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014102 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014103 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 int error = FALSE;
14106
Bram Moolenaar758711c2005-02-02 23:11:38 +000014107 /* Start at specified item. Use the cached index that list_find()
14108 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014110 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014111 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 ic = get_tv_number_chk(&argvars[3], &error);
14113 if (error)
14114 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014115 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014116
Bram Moolenaar758711c2005-02-02 23:11:38 +000014117 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014118 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014119 {
14120 rettv->vval.v_number = idx;
14121 break;
14122 }
14123 }
14124}
14125
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126static int inputsecret_flag = 0;
14127
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014128static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014129
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014131 * This function is used by f_input() and f_inputdialog() functions. The third
14132 * argument to f_input() specifies the type of completion to use at the
14133 * prompt. The third argument to f_inputdialog() specifies the value to return
14134 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 */
14136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014137get_user_input(
14138 typval_T *argvars,
14139 typval_T *rettv,
14140 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 char_u *p = NULL;
14144 int c;
14145 char_u buf[NUMBUFLEN];
14146 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014148 int xp_type = EXPAND_NOTHING;
14149 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014152 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153
14154#ifdef NO_CONSOLE_INPUT
14155 /* While starting up, there is no place to enter text. */
14156 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158#endif
14159
14160 cmd_silent = FALSE; /* Want to see the prompt. */
14161 if (prompt != NULL)
14162 {
14163 /* Only the part of the message after the last NL is considered as
14164 * prompt for the command line */
14165 p = vim_strrchr(prompt, '\n');
14166 if (p == NULL)
14167 p = prompt;
14168 else
14169 {
14170 ++p;
14171 c = *p;
14172 *p = NUL;
14173 msg_start();
14174 msg_clr_eos();
14175 msg_puts_attr(prompt, echo_attr);
14176 msg_didout = FALSE;
14177 msg_starthere();
14178 *p = c;
14179 }
14180 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 if (argvars[1].v_type != VAR_UNKNOWN)
14183 {
14184 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14185 if (defstr != NULL)
14186 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014188 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014189 {
14190 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014191 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014192 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014193
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014194 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014195 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014196
Bram Moolenaar4463f292005-09-25 22:20:24 +000014197 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14198 if (xp_name == NULL)
14199 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014200
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014201 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014202
Bram Moolenaar4463f292005-09-25 22:20:24 +000014203 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14204 &xp_arg) == FAIL)
14205 return;
14206 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014207 }
14208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014210 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014211 int save_ex_normal_busy = ex_normal_busy;
14212 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014214 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14215 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014216 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014217 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014218 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014219 && argvars[1].v_type != VAR_UNKNOWN
14220 && argvars[2].v_type != VAR_UNKNOWN)
14221 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14222 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014223
14224 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 /* since the user typed this, no need to wait for return */
14227 need_wait_return = FALSE;
14228 msg_didout = FALSE;
14229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 cmd_silent = cmd_silent_save;
14231}
14232
14233/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014234 * "input()" function
14235 * Also handles inputsecret() when inputsecret is set.
14236 */
14237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014238f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014239{
14240 get_user_input(argvars, rettv, FALSE);
14241}
14242
14243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 * "inputdialog()" function
14245 */
14246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014247f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248{
14249#if defined(FEAT_GUI_TEXTDIALOG)
14250 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14251 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14252 {
14253 char_u *message;
14254 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 message = get_tv_string_chk(&argvars[0]);
14258 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014259 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014260 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 else
14262 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 if (message != NULL && defstr != NULL
14264 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014265 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 else
14268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 if (message != NULL && defstr != NULL
14270 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014271 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->vval.v_string = vim_strsave(
14273 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 }
14279 else
14280#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014281 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282}
14283
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014284/*
14285 * "inputlist()" function
14286 */
14287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014288f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014289{
14290 listitem_T *li;
14291 int selected;
14292 int mouse_used;
14293
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014294#ifdef NO_CONSOLE_INPUT
14295 /* While starting up, there is no place to enter text. */
14296 if (no_console_input())
14297 return;
14298#endif
14299 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14300 {
14301 EMSG2(_(e_listarg), "inputlist()");
14302 return;
14303 }
14304
14305 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014306 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014307 lines_left = Rows; /* avoid more prompt */
14308 msg_scroll = TRUE;
14309 msg_clr_eos();
14310
14311 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14312 {
14313 msg_puts(get_tv_string(&li->li_tv));
14314 msg_putchar('\n');
14315 }
14316
14317 /* Ask for choice. */
14318 selected = prompt_for_number(&mouse_used);
14319 if (mouse_used)
14320 selected -= lines_left;
14321
14322 rettv->vval.v_number = selected;
14323}
14324
14325
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14327
14328/*
14329 * "inputrestore()" function
14330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014332f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333{
14334 if (ga_userinput.ga_len > 0)
14335 {
14336 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14338 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014339 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 }
14341 else if (p_verbose > 1)
14342 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014343 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014344 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 }
14346}
14347
14348/*
14349 * "inputsave()" function
14350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014352f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014354 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 if (ga_grow(&ga_userinput, 1) == OK)
14356 {
14357 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14358 + ga_userinput.ga_len);
14359 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014360 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 }
14362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364}
14365
14366/*
14367 * "inputsecret()" function
14368 */
14369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014370f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371{
14372 ++cmdline_star;
14373 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 --cmdline_star;
14376 --inputsecret_flag;
14377}
14378
14379/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014380 * "insert()" function
14381 */
14382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014383f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014384{
14385 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014386 listitem_T *item;
14387 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014389
14390 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014391 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014392 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014393 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014394 {
14395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 before = get_tv_number_chk(&argvars[2], &error);
14397 if (error)
14398 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014399
Bram Moolenaar758711c2005-02-02 23:11:38 +000014400 if (before == l->lv_len)
14401 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014402 else
14403 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014404 item = list_find(l, before);
14405 if (item == NULL)
14406 {
14407 EMSGN(_(e_listidx), before);
14408 l = NULL;
14409 }
14410 }
14411 if (l != NULL)
14412 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014413 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014414 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014415 }
14416 }
14417}
14418
14419/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014420 * "invert(expr)" function
14421 */
14422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014423f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014424{
14425 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14426}
14427
14428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 * "isdirectory()" function
14430 */
14431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014432f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435}
14436
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014437/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014438 * "islocked()" function
14439 */
14440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014441f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014442{
14443 lval_T lv;
14444 char_u *end;
14445 dictitem_T *di;
14446
14447 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014448 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14449 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014450 if (end != NULL && lv.ll_name != NULL)
14451 {
14452 if (*end != NUL)
14453 EMSG(_(e_trailing));
14454 else
14455 {
14456 if (lv.ll_tv == NULL)
14457 {
14458 if (check_changedtick(lv.ll_name))
14459 rettv->vval.v_number = 1; /* always locked */
14460 else
14461 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014462 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014463 if (di != NULL)
14464 {
14465 /* Consider a variable locked when:
14466 * 1. the variable itself is locked
14467 * 2. the value of the variable is locked.
14468 * 3. the List or Dict value is locked.
14469 */
14470 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14471 || tv_islocked(&di->di_tv));
14472 }
14473 }
14474 }
14475 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014476 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014477 else if (lv.ll_newkey != NULL)
14478 EMSG2(_(e_dictkey), lv.ll_newkey);
14479 else if (lv.ll_list != NULL)
14480 /* List item. */
14481 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14482 else
14483 /* Dictionary item. */
14484 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14485 }
14486 }
14487
14488 clear_lval(&lv);
14489}
14490
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014491#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14492/*
14493 * "isnan()" function
14494 */
14495 static void
14496f_isnan(typval_T *argvars, typval_T *rettv)
14497{
14498 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14499 && isnan(argvars[0].vval.v_float);
14500}
14501#endif
14502
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014503static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014504
14505/*
14506 * Turn a dict into a list:
14507 * "what" == 0: list of keys
14508 * "what" == 1: list of values
14509 * "what" == 2: list of items
14510 */
14511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014512dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014513{
Bram Moolenaar33570922005-01-25 22:26:29 +000014514 list_T *l2;
14515 dictitem_T *di;
14516 hashitem_T *hi;
14517 listitem_T *li;
14518 listitem_T *li2;
14519 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014520 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014521
Bram Moolenaar8c711452005-01-14 21:53:12 +000014522 if (argvars[0].v_type != VAR_DICT)
14523 {
14524 EMSG(_(e_dictreq));
14525 return;
14526 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014527 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014528 return;
14529
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014530 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014531 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014532
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014533 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014534 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014536 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014538 --todo;
14539 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014540
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014541 li = listitem_alloc();
14542 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014543 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014544 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014545
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014546 if (what == 0)
14547 {
14548 /* keys() */
14549 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014550 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014551 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14552 }
14553 else if (what == 1)
14554 {
14555 /* values() */
14556 copy_tv(&di->di_tv, &li->li_tv);
14557 }
14558 else
14559 {
14560 /* items() */
14561 l2 = list_alloc();
14562 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014563 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014564 li->li_tv.vval.v_list = l2;
14565 if (l2 == NULL)
14566 break;
14567 ++l2->lv_refcount;
14568
14569 li2 = listitem_alloc();
14570 if (li2 == NULL)
14571 break;
14572 list_append(l2, li2);
14573 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014574 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014575 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14576
14577 li2 = listitem_alloc();
14578 if (li2 == NULL)
14579 break;
14580 list_append(l2, li2);
14581 copy_tv(&di->di_tv, &li2->li_tv);
14582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014583 }
14584 }
14585}
14586
14587/*
14588 * "items(dict)" function
14589 */
14590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014591f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014592{
14593 dict_list(argvars, rettv, 2);
14594}
14595
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014596#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014597/*
14598 * Get the job from the argument.
14599 * Returns NULL if the job is invalid.
14600 */
14601 static job_T *
14602get_job_arg(typval_T *tv)
14603{
14604 job_T *job;
14605
14606 if (tv->v_type != VAR_JOB)
14607 {
14608 EMSG2(_(e_invarg2), get_tv_string(tv));
14609 return NULL;
14610 }
14611 job = tv->vval.v_job;
14612
14613 if (job == NULL)
14614 EMSG(_("E916: not a valid job"));
14615 return job;
14616}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014617
Bram Moolenaar835dc632016-02-07 14:27:38 +010014618/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014619 * "job_getchannel()" function
14620 */
14621 static void
14622f_job_getchannel(typval_T *argvars, typval_T *rettv)
14623{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014624 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014625
Bram Moolenaar65edff82016-02-21 16:40:11 +010014626 if (job != NULL)
14627 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014628 rettv->v_type = VAR_CHANNEL;
14629 rettv->vval.v_channel = job->jv_channel;
14630 if (job->jv_channel != NULL)
14631 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014632 }
14633}
14634
14635/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014636 * "job_info()" function
14637 */
14638 static void
14639f_job_info(typval_T *argvars, typval_T *rettv)
14640{
14641 job_T *job = get_job_arg(&argvars[0]);
14642
14643 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14644 job_info(job, rettv->vval.v_dict);
14645}
14646
14647/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014648 * "job_setoptions()" function
14649 */
14650 static void
14651f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14652{
14653 job_T *job = get_job_arg(&argvars[0]);
14654 jobopt_T opt;
14655
14656 if (job == NULL)
14657 return;
14658 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014659 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014660 return;
14661 job_set_options(job, &opt);
14662}
14663
14664/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014665 * "job_start()" function
14666 */
14667 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014668f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014669{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014670 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014671 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014672}
14673
14674/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014675 * "job_status()" function
14676 */
14677 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014678f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014679{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014680 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014681
Bram Moolenaar65edff82016-02-21 16:40:11 +010014682 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014683 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014684 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014685 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014686 }
14687}
14688
14689/*
14690 * "job_stop()" function
14691 */
14692 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014693f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014694{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014695 job_T *job = get_job_arg(&argvars[0]);
14696
14697 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014698 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014699}
14700#endif
14701
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014703 * "join()" function
14704 */
14705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014706f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014707{
14708 garray_T ga;
14709 char_u *sep;
14710
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014711 if (argvars[0].v_type != VAR_LIST)
14712 {
14713 EMSG(_(e_listreq));
14714 return;
14715 }
14716 if (argvars[0].vval.v_list == NULL)
14717 return;
14718 if (argvars[1].v_type == VAR_UNKNOWN)
14719 sep = (char_u *)" ";
14720 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014722
14723 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724
14725 if (sep != NULL)
14726 {
14727 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014728 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014729 ga_append(&ga, NUL);
14730 rettv->vval.v_string = (char_u *)ga.ga_data;
14731 }
14732 else
14733 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014734}
14735
14736/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014737 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014738 */
14739 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014740f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014741{
14742 js_read_T reader;
14743
14744 reader.js_buf = get_tv_string(&argvars[0]);
14745 reader.js_fill = NULL;
14746 reader.js_used = 0;
14747 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14748 EMSG(_(e_invarg));
14749}
14750
14751/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014752 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014753 */
14754 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014755f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014756{
14757 rettv->v_type = VAR_STRING;
14758 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14759}
14760
14761/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014762 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014763 */
14764 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014765f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014766{
14767 js_read_T reader;
14768
14769 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014770 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014771 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014772 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014773 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014774}
14775
14776/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014777 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014778 */
14779 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014780f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014781{
14782 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014783 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014784}
14785
14786/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014787 * "keys()" function
14788 */
14789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014790f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014791{
14792 dict_list(argvars, rettv, 0);
14793}
14794
14795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 * "last_buffer_nr()" function.
14797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014799f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800{
14801 int n = 0;
14802 buf_T *buf;
14803
14804 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14805 if (n < buf->b_fnum)
14806 n = buf->b_fnum;
14807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014808 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014809}
14810
14811/*
14812 * "len()" function
14813 */
14814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014815f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014816{
14817 switch (argvars[0].v_type)
14818 {
14819 case VAR_STRING:
14820 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 rettv->vval.v_number = (varnumber_T)STRLEN(
14822 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014823 break;
14824 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014826 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014827 case VAR_DICT:
14828 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14829 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014830 case VAR_UNKNOWN:
14831 case VAR_SPECIAL:
14832 case VAR_FLOAT:
14833 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014834 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014835 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014836 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014837 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014838 break;
14839 }
14840}
14841
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014842static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014843
14844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014845libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014846{
14847#ifdef FEAT_LIBCALL
14848 char_u *string_in;
14849 char_u **string_result;
14850 int nr_result;
14851#endif
14852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014854 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014856
14857 if (check_restricted() || check_secure())
14858 return;
14859
14860#ifdef FEAT_LIBCALL
14861 /* The first two args must be strings, otherwise its meaningless */
14862 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14863 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014864 string_in = NULL;
14865 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014866 string_in = argvars[2].vval.v_string;
14867 if (type == VAR_NUMBER)
14868 string_result = NULL;
14869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014870 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014871 if (mch_libcall(argvars[0].vval.v_string,
14872 argvars[1].vval.v_string,
14873 string_in,
14874 argvars[2].vval.v_number,
14875 string_result,
14876 &nr_result) == OK
14877 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879 }
14880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881}
14882
14883/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014884 * "libcall()" function
14885 */
14886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014887f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014888{
14889 libcall_common(argvars, rettv, VAR_STRING);
14890}
14891
14892/*
14893 * "libcallnr()" function
14894 */
14895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014896f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014897{
14898 libcall_common(argvars, rettv, VAR_NUMBER);
14899}
14900
14901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 * "line(string)" function
14903 */
14904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014905f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906{
14907 linenr_T lnum = 0;
14908 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014909 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014911 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 if (fp != NULL)
14913 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915}
14916
14917/*
14918 * "line2byte(lnum)" function
14919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014921f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922{
14923#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014924 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925#else
14926 linenr_T lnum;
14927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014928 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014930 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14933 if (rettv->vval.v_number >= 0)
14934 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935#endif
14936}
14937
14938/*
14939 * "lispindent(lnum)" function
14940 */
14941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014942f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943{
14944#ifdef FEAT_LISP
14945 pos_T pos;
14946 linenr_T lnum;
14947
14948 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014949 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14951 {
14952 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 curwin->w_cursor = pos;
14955 }
14956 else
14957#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959}
14960
14961/*
14962 * "localtime()" function
14963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014965f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968}
14969
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014970static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971
14972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014973get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974{
14975 char_u *keys;
14976 char_u *which;
14977 char_u buf[NUMBUFLEN];
14978 char_u *keys_buf = NULL;
14979 char_u *rhs;
14980 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014981 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014982 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014983 mapblock_T *mp;
14984 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985
14986 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->v_type = VAR_STRING;
14988 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 if (*keys == NUL)
14992 return;
14993
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014994 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014996 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014997 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014998 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014999 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015000 if (argvars[3].v_type != VAR_UNKNOWN)
15001 get_dict = get_tv_number(&argvars[3]);
15002 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 else
15005 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015006 if (which == NULL)
15007 return;
15008
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 mode = get_map_mode(&which, 0);
15010
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015011 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015012 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015014
15015 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015017 /* Return a string. */
15018 if (rhs != NULL)
15019 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020
Bram Moolenaarbd743252010-10-20 21:23:33 +020015021 }
15022 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15023 {
15024 /* Return a dictionary. */
15025 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15026 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15027 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028
Bram Moolenaarbd743252010-10-20 21:23:33 +020015029 dict_add_nr_str(dict, "lhs", 0L, lhs);
15030 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15031 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15032 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15033 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15034 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15035 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015036 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015037 dict_add_nr_str(dict, "mode", 0L, mapmode);
15038
15039 vim_free(lhs);
15040 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 }
15042}
15043
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015044#ifdef FEAT_FLOAT
15045/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015046 * "log()" function
15047 */
15048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015049f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015050{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015051 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015052
15053 rettv->v_type = VAR_FLOAT;
15054 if (get_float_arg(argvars, &f) == OK)
15055 rettv->vval.v_float = log(f);
15056 else
15057 rettv->vval.v_float = 0.0;
15058}
15059
15060/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015061 * "log10()" function
15062 */
15063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015064f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015065{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015066 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015067
15068 rettv->v_type = VAR_FLOAT;
15069 if (get_float_arg(argvars, &f) == OK)
15070 rettv->vval.v_float = log10(f);
15071 else
15072 rettv->vval.v_float = 0.0;
15073}
15074#endif
15075
Bram Moolenaar1dced572012-04-05 16:54:08 +020015076#ifdef FEAT_LUA
15077/*
15078 * "luaeval()" function
15079 */
15080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015081f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015082{
15083 char_u *str;
15084 char_u buf[NUMBUFLEN];
15085
15086 str = get_tv_string_buf(&argvars[0], buf);
15087 do_luaeval(str, argvars + 1, rettv);
15088}
15089#endif
15090
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015092 * "map()" function
15093 */
15094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015095f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015096{
15097 filter_map(argvars, rettv, TRUE);
15098}
15099
15100/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015101 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102 */
15103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015104f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107}
15108
15109/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 */
15112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015113f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116}
15117
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015118static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119
15120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015121find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015123 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015124 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015125 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 char_u *pat;
15127 regmatch_T regmatch;
15128 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015129 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130 char_u *save_cpo;
15131 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015132 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015133 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015134 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 list_T *l = NULL;
15136 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015137 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015138 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139
15140 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15141 save_cpo = p_cpo;
15142 p_cpo = (char_u *)"";
15143
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015144 rettv->vval.v_number = -1;
15145 if (type == 3)
15146 {
15147 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015148 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015149 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015150 }
15151 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015153 rettv->v_type = VAR_STRING;
15154 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015157 if (argvars[0].v_type == VAR_LIST)
15158 {
15159 if ((l = argvars[0].vval.v_list) == NULL)
15160 goto theend;
15161 li = l->lv_first;
15162 }
15163 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015164 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015165 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015166 len = (long)STRLEN(str);
15167 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015169 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15170 if (pat == NULL)
15171 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015172
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015175 int error = FALSE;
15176
15177 start = get_tv_number_chk(&argvars[2], &error);
15178 if (error)
15179 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015180 if (l != NULL)
15181 {
15182 li = list_find(l, start);
15183 if (li == NULL)
15184 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015185 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015186 }
15187 else
15188 {
15189 if (start < 0)
15190 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015191 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015192 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015193 /* When "count" argument is there ignore matches before "start",
15194 * otherwise skip part of the string. Differs when pattern is "^"
15195 * or "\<". */
15196 if (argvars[3].v_type != VAR_UNKNOWN)
15197 startcol = start;
15198 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015199 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015200 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015201 len -= start;
15202 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015203 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015204
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015205 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015206 nth = get_tv_number_chk(&argvars[3], &error);
15207 if (error)
15208 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 }
15210
15211 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15212 if (regmatch.regprog != NULL)
15213 {
15214 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015215
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015216 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015217 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015218 if (l != NULL)
15219 {
15220 if (li == NULL)
15221 {
15222 match = FALSE;
15223 break;
15224 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015225 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015226 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015227 if (str == NULL)
15228 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015229 }
15230
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015231 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015232
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015233 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015234 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015235 if (l == NULL && !match)
15236 break;
15237
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015238 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015239 if (l != NULL)
15240 {
15241 li = li->li_next;
15242 ++idx;
15243 }
15244 else
15245 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015246#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015247 startcol = (colnr_T)(regmatch.startp[0]
15248 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015249#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015250 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015251#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015252 if (startcol > (colnr_T)len
15253 || str + startcol <= regmatch.startp[0])
15254 {
15255 match = FALSE;
15256 break;
15257 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015258 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015259 }
15260
15261 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015263 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015265 int i;
15266
15267 /* return list with matched string and submatches */
15268 for (i = 0; i < NSUBEXP; ++i)
15269 {
15270 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015271 {
15272 if (list_append_string(rettv->vval.v_list,
15273 (char_u *)"", 0) == FAIL)
15274 break;
15275 }
15276 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015277 regmatch.startp[i],
15278 (int)(regmatch.endp[i] - regmatch.startp[i]))
15279 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015280 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015281 }
15282 }
15283 else if (type == 2)
15284 {
15285 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015286 if (l != NULL)
15287 copy_tv(&li->li_tv, rettv);
15288 else
15289 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015291 }
15292 else if (l != NULL)
15293 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 else
15295 {
15296 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015297 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 (varnumber_T)(regmatch.startp[0] - str);
15299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015302 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 }
15304 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015305 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 }
15307
15308theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015309 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 p_cpo = save_cpo;
15311}
15312
15313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314 * "match()" function
15315 */
15316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015317f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015318{
15319 find_some_match(argvars, rettv, 1);
15320}
15321
15322/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015323 * "matchadd()" function
15324 */
15325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015326f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015327{
15328#ifdef FEAT_SEARCH_EXTRA
15329 char_u buf[NUMBUFLEN];
15330 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15331 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15332 int prio = 10; /* default priority */
15333 int id = -1;
15334 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015335 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015336
15337 rettv->vval.v_number = -1;
15338
15339 if (grp == NULL || pat == NULL)
15340 return;
15341 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015342 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015343 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015344 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015345 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015346 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015347 if (argvars[4].v_type != VAR_UNKNOWN)
15348 {
15349 if (argvars[4].v_type != VAR_DICT)
15350 {
15351 EMSG(_(e_dictreq));
15352 return;
15353 }
15354 if (dict_find(argvars[4].vval.v_dict,
15355 (char_u *)"conceal", -1) != NULL)
15356 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15357 (char_u *)"conceal", FALSE);
15358 }
15359 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015360 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015361 if (error == TRUE)
15362 return;
15363 if (id >= 1 && id <= 3)
15364 {
15365 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15366 return;
15367 }
15368
Bram Moolenaar6561d522015-07-21 15:48:27 +020015369 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15370 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015371#endif
15372}
15373
15374/*
15375 * "matchaddpos()" function
15376 */
15377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015378f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015379{
15380#ifdef FEAT_SEARCH_EXTRA
15381 char_u buf[NUMBUFLEN];
15382 char_u *group;
15383 int prio = 10;
15384 int id = -1;
15385 int error = FALSE;
15386 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015387 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015388
15389 rettv->vval.v_number = -1;
15390
15391 group = get_tv_string_buf_chk(&argvars[0], buf);
15392 if (group == NULL)
15393 return;
15394
15395 if (argvars[1].v_type != VAR_LIST)
15396 {
15397 EMSG2(_(e_listarg), "matchaddpos()");
15398 return;
15399 }
15400 l = argvars[1].vval.v_list;
15401 if (l == NULL)
15402 return;
15403
15404 if (argvars[2].v_type != VAR_UNKNOWN)
15405 {
15406 prio = get_tv_number_chk(&argvars[2], &error);
15407 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015408 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015409 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015410 if (argvars[4].v_type != VAR_UNKNOWN)
15411 {
15412 if (argvars[4].v_type != VAR_DICT)
15413 {
15414 EMSG(_(e_dictreq));
15415 return;
15416 }
15417 if (dict_find(argvars[4].vval.v_dict,
15418 (char_u *)"conceal", -1) != NULL)
15419 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15420 (char_u *)"conceal", FALSE);
15421 }
15422 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015423 }
15424 if (error == TRUE)
15425 return;
15426
15427 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15428 if (id == 1 || id == 2)
15429 {
15430 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15431 return;
15432 }
15433
Bram Moolenaar6561d522015-07-21 15:48:27 +020015434 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15435 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015436#endif
15437}
15438
15439/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015440 * "matcharg()" function
15441 */
15442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015443f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015444{
15445 if (rettv_list_alloc(rettv) == OK)
15446 {
15447#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015448 int id = get_tv_number(&argvars[0]);
15449 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015450
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015451 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015452 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015453 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15454 {
15455 list_append_string(rettv->vval.v_list,
15456 syn_id2name(m->hlg_id), -1);
15457 list_append_string(rettv->vval.v_list, m->pattern, -1);
15458 }
15459 else
15460 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015461 list_append_string(rettv->vval.v_list, NULL, -1);
15462 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015463 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015464 }
15465#endif
15466 }
15467}
15468
15469/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015470 * "matchdelete()" function
15471 */
15472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015473f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015474{
15475#ifdef FEAT_SEARCH_EXTRA
15476 rettv->vval.v_number = match_delete(curwin,
15477 (int)get_tv_number(&argvars[0]), TRUE);
15478#endif
15479}
15480
15481/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015482 * "matchend()" function
15483 */
15484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015485f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015486{
15487 find_some_match(argvars, rettv, 0);
15488}
15489
15490/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015491 * "matchlist()" function
15492 */
15493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015494f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015495{
15496 find_some_match(argvars, rettv, 3);
15497}
15498
15499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015500 * "matchstr()" function
15501 */
15502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015503f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015504{
15505 find_some_match(argvars, rettv, 2);
15506}
15507
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015508static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015509
15510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015511max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015512{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015513 long n = 0;
15514 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015516
15517 if (argvars[0].v_type == VAR_LIST)
15518 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015519 list_T *l;
15520 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015521
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015522 l = argvars[0].vval.v_list;
15523 if (l != NULL)
15524 {
15525 li = l->lv_first;
15526 if (li != NULL)
15527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015528 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015529 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015530 {
15531 li = li->li_next;
15532 if (li == NULL)
15533 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015534 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015535 if (domax ? i > n : i < n)
15536 n = i;
15537 }
15538 }
15539 }
15540 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015541 else if (argvars[0].v_type == VAR_DICT)
15542 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015543 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015544 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015545 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015546 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015547
15548 d = argvars[0].vval.v_dict;
15549 if (d != NULL)
15550 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015551 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015554 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015555 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015556 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015557 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015558 if (first)
15559 {
15560 n = i;
15561 first = FALSE;
15562 }
15563 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015564 n = i;
15565 }
15566 }
15567 }
15568 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015569 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015570 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015571 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015572}
15573
15574/*
15575 * "max()" function
15576 */
15577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015578f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015579{
15580 max_min(argvars, rettv, TRUE);
15581}
15582
15583/*
15584 * "min()" function
15585 */
15586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015587f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015588{
15589 max_min(argvars, rettv, FALSE);
15590}
15591
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015592static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015593
15594/*
15595 * Create the directory in which "dir" is located, and higher levels when
15596 * needed.
15597 */
15598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015599mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015600{
15601 char_u *p;
15602 char_u *updir;
15603 int r = FAIL;
15604
15605 /* Get end of directory name in "dir".
15606 * We're done when it's "/" or "c:/". */
15607 p = gettail_sep(dir);
15608 if (p <= get_past_head(dir))
15609 return OK;
15610
15611 /* If the directory exists we're done. Otherwise: create it.*/
15612 updir = vim_strnsave(dir, (int)(p - dir));
15613 if (updir == NULL)
15614 return FAIL;
15615 if (mch_isdir(updir))
15616 r = OK;
15617 else if (mkdir_recurse(updir, prot) == OK)
15618 r = vim_mkdir_emsg(updir, prot);
15619 vim_free(updir);
15620 return r;
15621}
15622
15623#ifdef vim_mkdir
15624/*
15625 * "mkdir()" function
15626 */
15627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015628f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015629{
15630 char_u *dir;
15631 char_u buf[NUMBUFLEN];
15632 int prot = 0755;
15633
15634 rettv->vval.v_number = FAIL;
15635 if (check_restricted() || check_secure())
15636 return;
15637
15638 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015639 if (*dir == NUL)
15640 rettv->vval.v_number = FAIL;
15641 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015642 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015643 if (*gettail(dir) == NUL)
15644 /* remove trailing slashes */
15645 *gettail_sep(dir) = NUL;
15646
15647 if (argvars[1].v_type != VAR_UNKNOWN)
15648 {
15649 if (argvars[2].v_type != VAR_UNKNOWN)
15650 prot = get_tv_number_chk(&argvars[2], NULL);
15651 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15652 mkdir_recurse(dir, prot);
15653 }
15654 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015655 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015656}
15657#endif
15658
Bram Moolenaar0d660222005-01-07 21:51:51 +000015659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 * "mode()" function
15661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015663f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015665 char_u buf[3];
15666
15667 buf[1] = NUL;
15668 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 if (VIsual_active)
15671 {
15672 if (VIsual_select)
15673 buf[0] = VIsual_mode + 's' - 'v';
15674 else
15675 buf[0] = VIsual_mode;
15676 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015677 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015678 || State == CONFIRM)
15679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015681 if (State == ASKMORE)
15682 buf[1] = 'm';
15683 else if (State == CONFIRM)
15684 buf[1] = '?';
15685 }
15686 else if (State == EXTERNCMD)
15687 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 else if (State & INSERT)
15689 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015690#ifdef FEAT_VREPLACE
15691 if (State & VREPLACE_FLAG)
15692 {
15693 buf[0] = 'R';
15694 buf[1] = 'v';
15695 }
15696 else
15697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 if (State & REPLACE_FLAG)
15699 buf[0] = 'R';
15700 else
15701 buf[0] = 'i';
15702 }
15703 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015706 if (exmode_active)
15707 buf[1] = 'v';
15708 }
15709 else if (exmode_active)
15710 {
15711 buf[0] = 'c';
15712 buf[1] = 'e';
15713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015717 if (finish_op)
15718 buf[1] = 'o';
15719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015721 /* Clear out the minor mode when the argument is not a non-zero number or
15722 * non-empty string. */
15723 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015724 buf[1] = NUL;
15725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015726 rettv->vval.v_string = vim_strsave(buf);
15727 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728}
15729
Bram Moolenaar429fa852013-04-15 12:27:36 +020015730#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015731/*
15732 * "mzeval()" function
15733 */
15734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015735f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015736{
15737 char_u *str;
15738 char_u buf[NUMBUFLEN];
15739
15740 str = get_tv_string_buf(&argvars[0], buf);
15741 do_mzeval(str, rettv);
15742}
Bram Moolenaar75676462013-01-30 14:55:42 +010015743
15744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015745mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015746{
15747 typval_T argvars[3];
15748
15749 argvars[0].v_type = VAR_STRING;
15750 argvars[0].vval.v_string = name;
15751 copy_tv(args, &argvars[1]);
15752 argvars[2].v_type = VAR_UNKNOWN;
15753 f_call(argvars, rettv);
15754 clear_tv(&argvars[1]);
15755}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015756#endif
15757
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015759 * "nextnonblank()" function
15760 */
15761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015762f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763{
15764 linenr_T lnum;
15765
15766 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015768 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769 {
15770 lnum = 0;
15771 break;
15772 }
15773 if (*skipwhite(ml_get(lnum)) != NUL)
15774 break;
15775 }
15776 rettv->vval.v_number = lnum;
15777}
15778
15779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 * "nr2char()" function
15781 */
15782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015783f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784{
15785 char_u buf[NUMBUFLEN];
15786
15787#ifdef FEAT_MBYTE
15788 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015789 {
15790 int utf8 = 0;
15791
15792 if (argvars[1].v_type != VAR_UNKNOWN)
15793 utf8 = get_tv_number_chk(&argvars[1], NULL);
15794 if (utf8)
15795 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15796 else
15797 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 else
15800#endif
15801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 buf[1] = NUL;
15804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015805 rettv->v_type = VAR_STRING;
15806 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015807}
15808
15809/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015810 * "or(expr, expr)" function
15811 */
15812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015813f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015814{
15815 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15816 | get_tv_number_chk(&argvars[1], NULL);
15817}
15818
15819/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015820 * "pathshorten()" function
15821 */
15822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015823f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015824{
15825 char_u *p;
15826
15827 rettv->v_type = VAR_STRING;
15828 p = get_tv_string_chk(&argvars[0]);
15829 if (p == NULL)
15830 rettv->vval.v_string = NULL;
15831 else
15832 {
15833 p = vim_strsave(p);
15834 rettv->vval.v_string = p;
15835 if (p != NULL)
15836 shorten_dir(p);
15837 }
15838}
15839
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015840#ifdef FEAT_PERL
15841/*
15842 * "perleval()" function
15843 */
15844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015845f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015846{
15847 char_u *str;
15848 char_u buf[NUMBUFLEN];
15849
15850 str = get_tv_string_buf(&argvars[0], buf);
15851 do_perleval(str, rettv);
15852}
15853#endif
15854
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015855#ifdef FEAT_FLOAT
15856/*
15857 * "pow()" function
15858 */
15859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015860f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015861{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015862 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015863
15864 rettv->v_type = VAR_FLOAT;
15865 if (get_float_arg(argvars, &fx) == OK
15866 && get_float_arg(&argvars[1], &fy) == OK)
15867 rettv->vval.v_float = pow(fx, fy);
15868 else
15869 rettv->vval.v_float = 0.0;
15870}
15871#endif
15872
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874 * "prevnonblank()" function
15875 */
15876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015877f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015878{
15879 linenr_T lnum;
15880
15881 lnum = get_tv_lnum(argvars);
15882 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15883 lnum = 0;
15884 else
15885 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15886 --lnum;
15887 rettv->vval.v_number = lnum;
15888}
15889
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015890/* This dummy va_list is here because:
15891 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15892 * - locally in the function results in a "used before set" warning
15893 * - using va_start() to initialize it gives "function with fixed args" error */
15894static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015895
Bram Moolenaar8c711452005-01-14 21:53:12 +000015896/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015897 * "printf()" function
15898 */
15899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015900f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015901{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015902 char_u buf[NUMBUFLEN];
15903 int len;
15904 char_u *s;
15905 int saved_did_emsg = did_emsg;
15906 char *fmt;
15907
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015908 rettv->v_type = VAR_STRING;
15909 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015910
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015911 /* Get the required length, allocate the buffer and do it for real. */
15912 did_emsg = FALSE;
15913 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15914 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15915 if (!did_emsg)
15916 {
15917 s = alloc(len + 1);
15918 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015919 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015920 rettv->vval.v_string = s;
15921 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015922 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015923 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015924 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015925}
15926
15927/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015928 * "pumvisible()" function
15929 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015931f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015932{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015933#ifdef FEAT_INS_EXPAND
15934 if (pum_visible())
15935 rettv->vval.v_number = 1;
15936#endif
15937}
15938
Bram Moolenaardb913952012-06-29 12:54:53 +020015939#ifdef FEAT_PYTHON3
15940/*
15941 * "py3eval()" function
15942 */
15943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015944f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015945{
15946 char_u *str;
15947 char_u buf[NUMBUFLEN];
15948
15949 str = get_tv_string_buf(&argvars[0], buf);
15950 do_py3eval(str, rettv);
15951}
15952#endif
15953
15954#ifdef FEAT_PYTHON
15955/*
15956 * "pyeval()" function
15957 */
15958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015959f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015960{
15961 char_u *str;
15962 char_u buf[NUMBUFLEN];
15963
15964 str = get_tv_string_buf(&argvars[0], buf);
15965 do_pyeval(str, rettv);
15966}
15967#endif
15968
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015970 * "range()" function
15971 */
15972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015973f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015974{
15975 long start;
15976 long end;
15977 long stride = 1;
15978 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015979 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015981 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015982 if (argvars[1].v_type == VAR_UNKNOWN)
15983 {
15984 end = start - 1;
15985 start = 0;
15986 }
15987 else
15988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015989 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015990 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015991 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015992 }
15993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015994 if (error)
15995 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015996 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015997 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015998 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015999 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016000 else
16001 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016002 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016003 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016004 if (list_append_number(rettv->vval.v_list,
16005 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016006 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016007 }
16008}
16009
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016010/*
16011 * "readfile()" function
16012 */
16013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016014f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016015{
16016 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016017 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016018 char_u *fname;
16019 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016020 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16021 int io_size = sizeof(buf);
16022 int readlen; /* size of last fread() */
16023 char_u *prev = NULL; /* previously read bytes, if any */
16024 long prevlen = 0; /* length of data in prev */
16025 long prevsize = 0; /* size of prev buffer */
16026 long maxline = MAXLNUM;
16027 long cnt = 0;
16028 char_u *p; /* position in buf */
16029 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016030
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016031 if (argvars[1].v_type != VAR_UNKNOWN)
16032 {
16033 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16034 binary = TRUE;
16035 if (argvars[2].v_type != VAR_UNKNOWN)
16036 maxline = get_tv_number(&argvars[2]);
16037 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016038
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016039 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016040 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016041
16042 /* Always open the file in binary mode, library functions have a mind of
16043 * their own about CR-LF conversion. */
16044 fname = get_tv_string(&argvars[0]);
16045 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16046 {
16047 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16048 return;
16049 }
16050
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016051 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016052 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016053 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016054
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016055 /* This for loop processes what was read, but is also entered at end
16056 * of file so that either:
16057 * - an incomplete line gets written
16058 * - a "binary" file gets an empty line at the end if it ends in a
16059 * newline. */
16060 for (p = buf, start = buf;
16061 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16062 ++p)
16063 {
16064 if (*p == '\n' || readlen <= 0)
16065 {
16066 listitem_T *li;
16067 char_u *s = NULL;
16068 long_u len = p - start;
16069
16070 /* Finished a line. Remove CRs before NL. */
16071 if (readlen > 0 && !binary)
16072 {
16073 while (len > 0 && start[len - 1] == '\r')
16074 --len;
16075 /* removal may cross back to the "prev" string */
16076 if (len == 0)
16077 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16078 --prevlen;
16079 }
16080 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016081 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016082 else
16083 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016084 /* Change "prev" buffer to be the right size. This way
16085 * the bytes are only copied once, and very long lines are
16086 * allocated only once. */
16087 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016089 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016090 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016091 prev = NULL; /* the list will own the string */
16092 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016093 }
16094 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016095 if (s == NULL)
16096 {
16097 do_outofmem_msg((long_u) prevlen + len + 1);
16098 failed = TRUE;
16099 break;
16100 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016101
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016102 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016103 {
16104 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016105 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016106 break;
16107 }
16108 li->li_tv.v_type = VAR_STRING;
16109 li->li_tv.v_lock = 0;
16110 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016111 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016112
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016113 start = p + 1; /* step over newline */
16114 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115 break;
16116 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016117 else if (*p == NUL)
16118 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016119#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016120 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16121 * when finding the BF and check the previous two bytes. */
16122 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016123 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016124 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16125 * + 1, these may be in the "prev" string. */
16126 char_u back1 = p >= buf + 1 ? p[-1]
16127 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16128 char_u back2 = p >= buf + 2 ? p[-2]
16129 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16130 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016131
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016132 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016133 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016134 char_u *dest = p - 2;
16135
16136 /* Usually a BOM is at the beginning of a file, and so at
16137 * the beginning of a line; then we can just step over it.
16138 */
16139 if (start == dest)
16140 start = p + 1;
16141 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016142 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016143 /* have to shuffle buf to close gap */
16144 int adjust_prevlen = 0;
16145
16146 if (dest < buf)
16147 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016148 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016149 dest = buf;
16150 }
16151 if (readlen > p - buf + 1)
16152 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16153 readlen -= 3 - adjust_prevlen;
16154 prevlen -= adjust_prevlen;
16155 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016156 }
16157 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016158 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016159#endif
16160 } /* for */
16161
16162 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16163 break;
16164 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016165 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016166 /* There's part of a line in buf, store it in "prev". */
16167 if (p - start + prevlen >= prevsize)
16168 {
16169 /* need bigger "prev" buffer */
16170 char_u *newprev;
16171
16172 /* A common use case is ordinary text files and "prev" gets a
16173 * fragment of a line, so the first allocation is made
16174 * small, to avoid repeatedly 'allocing' large and
16175 * 'reallocing' small. */
16176 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016177 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016178 else
16179 {
16180 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016181 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016182 prevsize = grow50pc > growmin ? grow50pc : growmin;
16183 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016184 newprev = prev == NULL ? alloc(prevsize)
16185 : vim_realloc(prev, prevsize);
16186 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016187 {
16188 do_outofmem_msg((long_u)prevsize);
16189 failed = TRUE;
16190 break;
16191 }
16192 prev = newprev;
16193 }
16194 /* Add the line part to end of "prev". */
16195 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016196 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016197 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016198 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016199
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016200 /*
16201 * For a negative line count use only the lines at the end of the file,
16202 * free the rest.
16203 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016204 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016205 while (cnt > -maxline)
16206 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016207 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016208 --cnt;
16209 }
16210
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016211 if (failed)
16212 {
16213 list_free(rettv->vval.v_list, TRUE);
16214 /* readfile doc says an empty list is returned on error */
16215 rettv->vval.v_list = list_alloc();
16216 }
16217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016218 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016219 fclose(fd);
16220}
16221
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016222#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016223static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016224
16225/*
16226 * Convert a List to proftime_T.
16227 * Return FAIL when there is something wrong.
16228 */
16229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016230list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016231{
16232 long n1, n2;
16233 int error = FALSE;
16234
16235 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16236 || arg->vval.v_list->lv_len != 2)
16237 return FAIL;
16238 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16239 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16240# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016241 tm->HighPart = n1;
16242 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016243# else
16244 tm->tv_sec = n1;
16245 tm->tv_usec = n2;
16246# endif
16247 return error ? FAIL : OK;
16248}
16249#endif /* FEAT_RELTIME */
16250
16251/*
16252 * "reltime()" function
16253 */
16254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016255f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016256{
16257#ifdef FEAT_RELTIME
16258 proftime_T res;
16259 proftime_T start;
16260
16261 if (argvars[0].v_type == VAR_UNKNOWN)
16262 {
16263 /* No arguments: get current time. */
16264 profile_start(&res);
16265 }
16266 else if (argvars[1].v_type == VAR_UNKNOWN)
16267 {
16268 if (list2proftime(&argvars[0], &res) == FAIL)
16269 return;
16270 profile_end(&res);
16271 }
16272 else
16273 {
16274 /* Two arguments: compute the difference. */
16275 if (list2proftime(&argvars[0], &start) == FAIL
16276 || list2proftime(&argvars[1], &res) == FAIL)
16277 return;
16278 profile_sub(&res, &start);
16279 }
16280
16281 if (rettv_list_alloc(rettv) == OK)
16282 {
16283 long n1, n2;
16284
16285# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016286 n1 = res.HighPart;
16287 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016288# else
16289 n1 = res.tv_sec;
16290 n2 = res.tv_usec;
16291# endif
16292 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16293 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16294 }
16295#endif
16296}
16297
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016298#ifdef FEAT_FLOAT
16299/*
16300 * "reltimefloat()" function
16301 */
16302 static void
16303f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16304{
16305# ifdef FEAT_RELTIME
16306 proftime_T tm;
16307# endif
16308
16309 rettv->v_type = VAR_FLOAT;
16310 rettv->vval.v_float = 0;
16311# ifdef FEAT_RELTIME
16312 if (list2proftime(&argvars[0], &tm) == OK)
16313 rettv->vval.v_float = profile_float(&tm);
16314# endif
16315}
16316#endif
16317
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016318/*
16319 * "reltimestr()" function
16320 */
16321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016322f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016323{
16324#ifdef FEAT_RELTIME
16325 proftime_T tm;
16326#endif
16327
16328 rettv->v_type = VAR_STRING;
16329 rettv->vval.v_string = NULL;
16330#ifdef FEAT_RELTIME
16331 if (list2proftime(&argvars[0], &tm) == OK)
16332 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16333#endif
16334}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016335
Bram Moolenaar0d660222005-01-07 21:51:51 +000016336#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016337static void make_connection(void);
16338static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016339
16340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016341make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016342{
16343 if (X_DISPLAY == NULL
16344# ifdef FEAT_GUI
16345 && !gui.in_use
16346# endif
16347 )
16348 {
16349 x_force_connect = TRUE;
16350 setup_term_clip();
16351 x_force_connect = FALSE;
16352 }
16353}
16354
16355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016356check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357{
16358 make_connection();
16359 if (X_DISPLAY == NULL)
16360 {
16361 EMSG(_("E240: No connection to Vim server"));
16362 return FAIL;
16363 }
16364 return OK;
16365}
16366#endif
16367
16368#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016369static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370
16371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016372remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016373{
16374 char_u *server_name;
16375 char_u *keys;
16376 char_u *r = NULL;
16377 char_u buf[NUMBUFLEN];
16378# ifdef WIN32
16379 HWND w;
16380# else
16381 Window w;
16382# endif
16383
16384 if (check_restricted() || check_secure())
16385 return;
16386
16387# ifdef FEAT_X11
16388 if (check_connection() == FAIL)
16389 return;
16390# endif
16391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016392 server_name = get_tv_string_chk(&argvars[0]);
16393 if (server_name == NULL)
16394 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 keys = get_tv_string_buf(&argvars[1], buf);
16396# ifdef WIN32
16397 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16398# else
16399 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16400 < 0)
16401# endif
16402 {
16403 if (r != NULL)
16404 EMSG(r); /* sending worked but evaluation failed */
16405 else
16406 EMSG2(_("E241: Unable to send to %s"), server_name);
16407 return;
16408 }
16409
16410 rettv->vval.v_string = r;
16411
16412 if (argvars[2].v_type != VAR_UNKNOWN)
16413 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016414 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016415 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016418 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016419 v.di_tv.v_type = VAR_STRING;
16420 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016421 idvar = get_tv_string_chk(&argvars[2]);
16422 if (idvar != NULL)
16423 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425 }
16426}
16427#endif
16428
16429/*
16430 * "remote_expr()" function
16431 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016433f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434{
16435 rettv->v_type = VAR_STRING;
16436 rettv->vval.v_string = NULL;
16437#ifdef FEAT_CLIENTSERVER
16438 remote_common(argvars, rettv, TRUE);
16439#endif
16440}
16441
16442/*
16443 * "remote_foreground()" function
16444 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016446f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448#ifdef FEAT_CLIENTSERVER
16449# ifdef WIN32
16450 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 {
16452 char_u *server_name = get_tv_string_chk(&argvars[0]);
16453
16454 if (server_name != NULL)
16455 serverForeground(server_name);
16456 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457# else
16458 /* Send a foreground() expression to the server. */
16459 argvars[1].v_type = VAR_STRING;
16460 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16461 argvars[2].v_type = VAR_UNKNOWN;
16462 remote_common(argvars, rettv, TRUE);
16463 vim_free(argvars[1].vval.v_string);
16464# endif
16465#endif
16466}
16467
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016469f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470{
16471#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016472 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016473 char_u *s = NULL;
16474# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016475 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016476# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016477 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016478
16479 if (check_restricted() || check_secure())
16480 {
16481 rettv->vval.v_number = -1;
16482 return;
16483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016484 serverid = get_tv_string_chk(&argvars[0]);
16485 if (serverid == NULL)
16486 {
16487 rettv->vval.v_number = -1;
16488 return; /* type error; errmsg already given */
16489 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016491 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 if (n == 0)
16493 rettv->vval.v_number = -1;
16494 else
16495 {
16496 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16497 rettv->vval.v_number = (s != NULL);
16498 }
16499# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500 if (check_connection() == FAIL)
16501 return;
16502
16503 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016504 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016505# endif
16506
16507 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 char_u *retvar;
16510
Bram Moolenaar33570922005-01-25 22:26:29 +000016511 v.di_tv.v_type = VAR_STRING;
16512 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016513 retvar = get_tv_string_chk(&argvars[1]);
16514 if (retvar != NULL)
16515 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016516 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 }
16518#else
16519 rettv->vval.v_number = -1;
16520#endif
16521}
16522
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016524f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525{
16526 char_u *r = NULL;
16527
16528#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016529 char_u *serverid = get_tv_string_chk(&argvars[0]);
16530
16531 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532 {
16533# ifdef WIN32
16534 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016535 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016536
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016537 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016538 if (n != 0)
16539 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16540 if (r == NULL)
16541# else
16542 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016544# endif
16545 EMSG(_("E277: Unable to read a server reply"));
16546 }
16547#endif
16548 rettv->v_type = VAR_STRING;
16549 rettv->vval.v_string = r;
16550}
16551
16552/*
16553 * "remote_send()" function
16554 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016556f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016557{
16558 rettv->v_type = VAR_STRING;
16559 rettv->vval.v_string = NULL;
16560#ifdef FEAT_CLIENTSERVER
16561 remote_common(argvars, rettv, FALSE);
16562#endif
16563}
16564
16565/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016566 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016567 */
16568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016569f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016570{
Bram Moolenaar33570922005-01-25 22:26:29 +000016571 list_T *l;
16572 listitem_T *item, *item2;
16573 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016574 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016575 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016576 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016577 dict_T *d;
16578 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016579 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016580
Bram Moolenaar8c711452005-01-14 21:53:12 +000016581 if (argvars[0].v_type == VAR_DICT)
16582 {
16583 if (argvars[2].v_type != VAR_UNKNOWN)
16584 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016585 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016586 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016588 key = get_tv_string_chk(&argvars[1]);
16589 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016591 di = dict_find(d, key, -1);
16592 if (di == NULL)
16593 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016594 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16595 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016596 {
16597 *rettv = di->di_tv;
16598 init_tv(&di->di_tv);
16599 dictitem_remove(d, di);
16600 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016602 }
16603 }
16604 else if (argvars[0].v_type != VAR_LIST)
16605 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016606 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016607 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016609 int error = FALSE;
16610
16611 idx = get_tv_number_chk(&argvars[1], &error);
16612 if (error)
16613 ; /* type error: do nothing, errmsg already given */
16614 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016615 EMSGN(_(e_listidx), idx);
16616 else
16617 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016618 if (argvars[2].v_type == VAR_UNKNOWN)
16619 {
16620 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016621 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016622 *rettv = item->li_tv;
16623 vim_free(item);
16624 }
16625 else
16626 {
16627 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016628 end = get_tv_number_chk(&argvars[2], &error);
16629 if (error)
16630 ; /* type error: do nothing */
16631 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016632 EMSGN(_(e_listidx), end);
16633 else
16634 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016635 int cnt = 0;
16636
16637 for (li = item; li != NULL; li = li->li_next)
16638 {
16639 ++cnt;
16640 if (li == item2)
16641 break;
16642 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016643 if (li == NULL) /* didn't find "item2" after "item" */
16644 EMSG(_(e_invrange));
16645 else
16646 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016647 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016648 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016649 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016650 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016651 l->lv_first = item;
16652 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016653 item->li_prev = NULL;
16654 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016655 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016656 }
16657 }
16658 }
16659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016660 }
16661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662}
16663
16664/*
16665 * "rename({from}, {to})" function
16666 */
16667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016668f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669{
16670 char_u buf[NUMBUFLEN];
16671
16672 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16676 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677}
16678
16679/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016680 * "repeat()" function
16681 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016683f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016684{
16685 char_u *p;
16686 int n;
16687 int slen;
16688 int len;
16689 char_u *r;
16690 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016691
16692 n = get_tv_number(&argvars[1]);
16693 if (argvars[0].v_type == VAR_LIST)
16694 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016695 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016696 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016697 if (list_extend(rettv->vval.v_list,
16698 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016699 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016700 }
16701 else
16702 {
16703 p = get_tv_string(&argvars[0]);
16704 rettv->v_type = VAR_STRING;
16705 rettv->vval.v_string = NULL;
16706
16707 slen = (int)STRLEN(p);
16708 len = slen * n;
16709 if (len <= 0)
16710 return;
16711
16712 r = alloc(len + 1);
16713 if (r != NULL)
16714 {
16715 for (i = 0; i < n; i++)
16716 mch_memmove(r + i * slen, p, (size_t)slen);
16717 r[len] = NUL;
16718 }
16719
16720 rettv->vval.v_string = r;
16721 }
16722}
16723
16724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 * "resolve()" function
16726 */
16727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016728f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729{
16730 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016731#ifdef HAVE_READLINK
16732 char_u *buf = NULL;
16733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016735 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736#ifdef FEAT_SHORTCUT
16737 {
16738 char_u *v = NULL;
16739
16740 v = mch_resolve_shortcut(p);
16741 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016742 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016744 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 }
16746#else
16747# ifdef HAVE_READLINK
16748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 char_u *cpy;
16750 int len;
16751 char_u *remain = NULL;
16752 char_u *q;
16753 int is_relative_to_current = FALSE;
16754 int has_trailing_pathsep = FALSE;
16755 int limit = 100;
16756
16757 p = vim_strsave(p);
16758
16759 if (p[0] == '.' && (vim_ispathsep(p[1])
16760 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16761 is_relative_to_current = TRUE;
16762
16763 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016764 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016767 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769
16770 q = getnextcomp(p);
16771 if (*q != NUL)
16772 {
16773 /* Separate the first path component in "p", and keep the
16774 * remainder (beginning with the path separator). */
16775 remain = vim_strsave(q - 1);
16776 q[-1] = NUL;
16777 }
16778
Bram Moolenaard9462e32011-04-11 21:35:11 +020016779 buf = alloc(MAXPATHL + 1);
16780 if (buf == NULL)
16781 goto fail;
16782
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 for (;;)
16784 {
16785 for (;;)
16786 {
16787 len = readlink((char *)p, (char *)buf, MAXPATHL);
16788 if (len <= 0)
16789 break;
16790 buf[len] = NUL;
16791
16792 if (limit-- == 0)
16793 {
16794 vim_free(p);
16795 vim_free(remain);
16796 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 goto fail;
16799 }
16800
16801 /* Ensure that the result will have a trailing path separator
16802 * if the argument has one. */
16803 if (remain == NULL && has_trailing_pathsep)
16804 add_pathsep(buf);
16805
16806 /* Separate the first path component in the link value and
16807 * concatenate the remainders. */
16808 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16809 if (*q != NUL)
16810 {
16811 if (remain == NULL)
16812 remain = vim_strsave(q - 1);
16813 else
16814 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016815 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 if (cpy != NULL)
16817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 vim_free(remain);
16819 remain = cpy;
16820 }
16821 }
16822 q[-1] = NUL;
16823 }
16824
16825 q = gettail(p);
16826 if (q > p && *q == NUL)
16827 {
16828 /* Ignore trailing path separator. */
16829 q[-1] = NUL;
16830 q = gettail(p);
16831 }
16832 if (q > p && !mch_isFullName(buf))
16833 {
16834 /* symlink is relative to directory of argument */
16835 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16836 if (cpy != NULL)
16837 {
16838 STRCPY(cpy, p);
16839 STRCPY(gettail(cpy), buf);
16840 vim_free(p);
16841 p = cpy;
16842 }
16843 }
16844 else
16845 {
16846 vim_free(p);
16847 p = vim_strsave(buf);
16848 }
16849 }
16850
16851 if (remain == NULL)
16852 break;
16853
16854 /* Append the first path component of "remain" to "p". */
16855 q = getnextcomp(remain + 1);
16856 len = q - remain - (*q != NUL);
16857 cpy = vim_strnsave(p, STRLEN(p) + len);
16858 if (cpy != NULL)
16859 {
16860 STRNCAT(cpy, remain, len);
16861 vim_free(p);
16862 p = cpy;
16863 }
16864 /* Shorten "remain". */
16865 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016866 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 else
16868 {
16869 vim_free(remain);
16870 remain = NULL;
16871 }
16872 }
16873
16874 /* If the result is a relative path name, make it explicitly relative to
16875 * the current directory if and only if the argument had this form. */
16876 if (!vim_ispathsep(*p))
16877 {
16878 if (is_relative_to_current
16879 && *p != NUL
16880 && !(p[0] == '.'
16881 && (p[1] == NUL
16882 || vim_ispathsep(p[1])
16883 || (p[1] == '.'
16884 && (p[2] == NUL
16885 || vim_ispathsep(p[2]))))))
16886 {
16887 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016888 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 if (cpy != NULL)
16890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 vim_free(p);
16892 p = cpy;
16893 }
16894 }
16895 else if (!is_relative_to_current)
16896 {
16897 /* Strip leading "./". */
16898 q = p;
16899 while (q[0] == '.' && vim_ispathsep(q[1]))
16900 q += 2;
16901 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016902 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 }
16904 }
16905
16906 /* Ensure that the result will have no trailing path separator
16907 * if the argument had none. But keep "/" or "//". */
16908 if (!has_trailing_pathsep)
16909 {
16910 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016911 if (after_pathsep(p, q))
16912 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 }
16914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016915 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 }
16917# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016918 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919# endif
16920#endif
16921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016922 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923
16924#ifdef HAVE_READLINK
16925fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016926 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016928 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929}
16930
16931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 */
16934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016935f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936{
Bram Moolenaar33570922005-01-25 22:26:29 +000016937 list_T *l;
16938 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940 if (argvars[0].v_type != VAR_LIST)
16941 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016942 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016943 && !tv_check_lock(l->lv_lock,
16944 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 {
16946 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016947 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016948 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 while (li != NULL)
16950 {
16951 ni = li->li_prev;
16952 list_append(l, li);
16953 li = ni;
16954 }
16955 rettv->vval.v_list = l;
16956 rettv->v_type = VAR_LIST;
16957 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016958 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960}
16961
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016962#define SP_NOMOVE 0x01 /* don't move cursor */
16963#define SP_REPEAT 0x02 /* repeat to find outer pair */
16964#define SP_RETCOUNT 0x04 /* return matchcount */
16965#define SP_SETPCMARK 0x08 /* set previous context mark */
16966#define SP_START 0x10 /* accept match at start position */
16967#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16968#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016969#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016970
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016971static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972
16973/*
16974 * Get flags for a search function.
16975 * Possibly sets "p_ws".
16976 * Returns BACKWARD, FORWARD or zero (for an error).
16977 */
16978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016979get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980{
16981 int dir = FORWARD;
16982 char_u *flags;
16983 char_u nbuf[NUMBUFLEN];
16984 int mask;
16985
16986 if (varp->v_type != VAR_UNKNOWN)
16987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 flags = get_tv_string_buf_chk(varp, nbuf);
16989 if (flags == NULL)
16990 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 while (*flags != NUL)
16992 {
16993 switch (*flags)
16994 {
16995 case 'b': dir = BACKWARD; break;
16996 case 'w': p_ws = TRUE; break;
16997 case 'W': p_ws = FALSE; break;
16998 default: mask = 0;
16999 if (flagsp != NULL)
17000 switch (*flags)
17001 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017002 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017003 case 'e': mask = SP_END; break;
17004 case 'm': mask = SP_RETCOUNT; break;
17005 case 'n': mask = SP_NOMOVE; break;
17006 case 'p': mask = SP_SUBPAT; break;
17007 case 'r': mask = SP_REPEAT; break;
17008 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017009 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010 }
17011 if (mask == 0)
17012 {
17013 EMSG2(_(e_invarg2), flags);
17014 dir = 0;
17015 }
17016 else
17017 *flagsp |= mask;
17018 }
17019 if (dir == 0)
17020 break;
17021 ++flags;
17022 }
17023 }
17024 return dir;
17025}
17026
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017028 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017031search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017033 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 char_u *pat;
17035 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017036 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 int save_p_ws = p_ws;
17038 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017039 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017040 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017041 proftime_T tm;
17042#ifdef FEAT_RELTIME
17043 long time_limit = 0;
17044#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017045 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017046 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017048 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017049 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017050 if (dir == 0)
17051 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017052 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017053 if (flags & SP_START)
17054 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017055 if (flags & SP_END)
17056 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017057 if (flags & SP_COLUMN)
17058 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017059
Bram Moolenaar76929292008-01-06 19:07:36 +000017060 /* Optional arguments: line number to stop searching and timeout. */
17061 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017062 {
17063 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17064 if (lnum_stop < 0)
17065 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017066#ifdef FEAT_RELTIME
17067 if (argvars[3].v_type != VAR_UNKNOWN)
17068 {
17069 time_limit = get_tv_number_chk(&argvars[3], NULL);
17070 if (time_limit < 0)
17071 goto theend;
17072 }
17073#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017074 }
17075
Bram Moolenaar76929292008-01-06 19:07:36 +000017076#ifdef FEAT_RELTIME
17077 /* Set the time limit, if there is one. */
17078 profile_setlimit(time_limit, &tm);
17079#endif
17080
Bram Moolenaar231334e2005-07-25 20:46:57 +000017081 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017082 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017083 * Check to make sure only those flags are set.
17084 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17085 * flags cannot be set. Check for that condition also.
17086 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017087 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017088 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017090 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017091 goto theend;
17092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017094 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017095 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017096 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017097 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017099 if (flags & SP_SUBPAT)
17100 retval = subpatnum;
17101 else
17102 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017103 if (flags & SP_SETPCMARK)
17104 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017106 if (match_pos != NULL)
17107 {
17108 /* Store the match cursor position */
17109 match_pos->lnum = pos.lnum;
17110 match_pos->col = pos.col + 1;
17111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 /* "/$" will put the cursor after the end of the line, may need to
17113 * correct that here */
17114 check_cursor();
17115 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017116
17117 /* If 'n' flag is used: restore cursor position. */
17118 if (flags & SP_NOMOVE)
17119 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017120 else
17121 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017122theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017124
17125 return retval;
17126}
17127
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017128#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017129
17130/*
17131 * round() is not in C90, use ceil() or floor() instead.
17132 */
17133 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017134vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017135{
17136 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17137}
17138
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017139/*
17140 * "round({float})" function
17141 */
17142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017143f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017144{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017145 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017146
17147 rettv->v_type = VAR_FLOAT;
17148 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017149 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017150 else
17151 rettv->vval.v_float = 0.0;
17152}
17153#endif
17154
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017155/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017156 * "screenattr()" function
17157 */
17158 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017159f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017160{
17161 int row;
17162 int col;
17163 int c;
17164
17165 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17166 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17167 if (row < 0 || row >= screen_Rows
17168 || col < 0 || col >= screen_Columns)
17169 c = -1;
17170 else
17171 c = ScreenAttrs[LineOffset[row] + col];
17172 rettv->vval.v_number = c;
17173}
17174
17175/*
17176 * "screenchar()" function
17177 */
17178 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017179f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017180{
17181 int row;
17182 int col;
17183 int off;
17184 int c;
17185
17186 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17187 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17188 if (row < 0 || row >= screen_Rows
17189 || col < 0 || col >= screen_Columns)
17190 c = -1;
17191 else
17192 {
17193 off = LineOffset[row] + col;
17194#ifdef FEAT_MBYTE
17195 if (enc_utf8 && ScreenLinesUC[off] != 0)
17196 c = ScreenLinesUC[off];
17197 else
17198#endif
17199 c = ScreenLines[off];
17200 }
17201 rettv->vval.v_number = c;
17202}
17203
17204/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017205 * "screencol()" function
17206 *
17207 * First column is 1 to be consistent with virtcol().
17208 */
17209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017210f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017211{
17212 rettv->vval.v_number = screen_screencol() + 1;
17213}
17214
17215/*
17216 * "screenrow()" function
17217 */
17218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017219f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017220{
17221 rettv->vval.v_number = screen_screenrow() + 1;
17222}
17223
17224/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017225 * "search()" function
17226 */
17227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017228f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017229{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017230 int flags = 0;
17231
17232 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233}
17234
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017236 * "searchdecl()" function
17237 */
17238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017239f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017240{
17241 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017242 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017243 int error = FALSE;
17244 char_u *name;
17245
17246 rettv->vval.v_number = 1; /* default: FAIL */
17247
17248 name = get_tv_string_chk(&argvars[0]);
17249 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017250 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017251 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017252 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17253 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17254 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017255 if (!error && name != NULL)
17256 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017257 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017258}
17259
17260/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017261 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017264searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265{
17266 char_u *spat, *mpat, *epat;
17267 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 int dir;
17270 int flags = 0;
17271 char_u nbuf1[NUMBUFLEN];
17272 char_u nbuf2[NUMBUFLEN];
17273 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017274 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017275 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017276 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 spat = get_tv_string_chk(&argvars[0]);
17280 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17281 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17282 if (spat == NULL || mpat == NULL || epat == NULL)
17283 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 /* Handle the optional fourth argument: flags */
17286 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017287 if (dir == 0)
17288 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017289
17290 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017291 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17292 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017293 if ((flags & (SP_END | SP_SUBPAT)) != 0
17294 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017295 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017296 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017297 goto theend;
17298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017300 /* Using 'r' implies 'W', otherwise it doesn't work. */
17301 if (flags & SP_REPEAT)
17302 p_ws = FALSE;
17303
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017304 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017305 if (argvars[3].v_type == VAR_UNKNOWN
17306 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 skip = (char_u *)"";
17308 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017310 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017311 if (argvars[5].v_type != VAR_UNKNOWN)
17312 {
17313 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17314 if (lnum_stop < 0)
17315 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017316#ifdef FEAT_RELTIME
17317 if (argvars[6].v_type != VAR_UNKNOWN)
17318 {
17319 time_limit = get_tv_number_chk(&argvars[6], NULL);
17320 if (time_limit < 0)
17321 goto theend;
17322 }
17323#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017324 }
17325 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017326 if (skip == NULL)
17327 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017329 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017330 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017331
17332theend:
17333 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017334
17335 return retval;
17336}
17337
17338/*
17339 * "searchpair()" function
17340 */
17341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017342f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017343{
17344 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17345}
17346
17347/*
17348 * "searchpairpos()" function
17349 */
17350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017351f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017352{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017353 pos_T match_pos;
17354 int lnum = 0;
17355 int col = 0;
17356
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017357 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017358 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017359
17360 if (searchpair_cmn(argvars, &match_pos) > 0)
17361 {
17362 lnum = match_pos.lnum;
17363 col = match_pos.col;
17364 }
17365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017366 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17367 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017368}
17369
17370/*
17371 * Search for a start/middle/end thing.
17372 * Used by searchpair(), see its documentation for the details.
17373 * Returns 0 or -1 for no match,
17374 */
17375 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017376do_searchpair(
17377 char_u *spat, /* start pattern */
17378 char_u *mpat, /* middle pattern */
17379 char_u *epat, /* end pattern */
17380 int dir, /* BACKWARD or FORWARD */
17381 char_u *skip, /* skip expression */
17382 int flags, /* SP_SETPCMARK and other SP_ values */
17383 pos_T *match_pos,
17384 linenr_T lnum_stop, /* stop at this line if not zero */
17385 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017386{
17387 char_u *save_cpo;
17388 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17389 long retval = 0;
17390 pos_T pos;
17391 pos_T firstpos;
17392 pos_T foundpos;
17393 pos_T save_cursor;
17394 pos_T save_pos;
17395 int n;
17396 int r;
17397 int nest = 1;
17398 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017399 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017400 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017401
17402 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17403 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017404 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017405
Bram Moolenaar76929292008-01-06 19:07:36 +000017406#ifdef FEAT_RELTIME
17407 /* Set the time limit, if there is one. */
17408 profile_setlimit(time_limit, &tm);
17409#endif
17410
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017411 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17412 * start/middle/end (pat3, for the top pair). */
17413 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17414 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17415 if (pat2 == NULL || pat3 == NULL)
17416 goto theend;
17417 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17418 if (*mpat == NUL)
17419 STRCPY(pat3, pat2);
17420 else
17421 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17422 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017423 if (flags & SP_START)
17424 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017425
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 save_cursor = curwin->w_cursor;
17427 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017428 clearpos(&firstpos);
17429 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 pat = pat3;
17431 for (;;)
17432 {
17433 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017434 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17436 /* didn't find it or found the first match again: FAIL */
17437 break;
17438
17439 if (firstpos.lnum == 0)
17440 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017441 if (equalpos(pos, foundpos))
17442 {
17443 /* Found the same position again. Can happen with a pattern that
17444 * has "\zs" at the end and searching backwards. Advance one
17445 * character and try again. */
17446 if (dir == BACKWARD)
17447 decl(&pos);
17448 else
17449 incl(&pos);
17450 }
17451 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017453 /* clear the start flag to avoid getting stuck here */
17454 options &= ~SEARCH_START;
17455
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 /* If the skip pattern matches, ignore this match. */
17457 if (*skip != NUL)
17458 {
17459 save_pos = curwin->w_cursor;
17460 curwin->w_cursor = pos;
17461 r = eval_to_bool(skip, &err, NULL, FALSE);
17462 curwin->w_cursor = save_pos;
17463 if (err)
17464 {
17465 /* Evaluating {skip} caused an error, break here. */
17466 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017467 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 break;
17469 }
17470 if (r)
17471 continue;
17472 }
17473
17474 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17475 {
17476 /* Found end when searching backwards or start when searching
17477 * forward: nested pair. */
17478 ++nest;
17479 pat = pat2; /* nested, don't search for middle */
17480 }
17481 else
17482 {
17483 /* Found end when searching forward or start when searching
17484 * backward: end of (nested) pair; or found middle in outer pair. */
17485 if (--nest == 1)
17486 pat = pat3; /* outer level, search for middle */
17487 }
17488
17489 if (nest == 0)
17490 {
17491 /* Found the match: return matchcount or line number. */
17492 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017493 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017495 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017496 if (flags & SP_SETPCMARK)
17497 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 curwin->w_cursor = pos;
17499 if (!(flags & SP_REPEAT))
17500 break;
17501 nest = 1; /* search for next unmatched */
17502 }
17503 }
17504
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017505 if (match_pos != NULL)
17506 {
17507 /* Store the match cursor position */
17508 match_pos->lnum = curwin->w_cursor.lnum;
17509 match_pos->col = curwin->w_cursor.col + 1;
17510 }
17511
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017513 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 curwin->w_cursor = save_cursor;
17515
17516theend:
17517 vim_free(pat2);
17518 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017519 if (p_cpo == empty_option)
17520 p_cpo = save_cpo;
17521 else
17522 /* Darn, evaluating the {skip} expression changed the value. */
17523 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017524
17525 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526}
17527
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017528/*
17529 * "searchpos()" function
17530 */
17531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017532f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017533{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017534 pos_T match_pos;
17535 int lnum = 0;
17536 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017537 int n;
17538 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017539
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017540 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017541 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017542
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017543 n = search_cmn(argvars, &match_pos, &flags);
17544 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017545 {
17546 lnum = match_pos.lnum;
17547 col = match_pos.col;
17548 }
17549
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017550 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17551 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017552 if (flags & SP_SUBPAT)
17553 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017554}
17555
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017557f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559#ifdef FEAT_CLIENTSERVER
17560 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561 char_u *server = get_tv_string_chk(&argvars[0]);
17562 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017565 if (server == NULL || reply == NULL)
17566 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 if (check_restricted() || check_secure())
17568 return;
17569# ifdef FEAT_X11
17570 if (check_connection() == FAIL)
17571 return;
17572# endif
17573
17574 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 EMSG(_("E258: Unable to send to client"));
17577 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 rettv->vval.v_number = 0;
17580#else
17581 rettv->vval.v_number = -1;
17582#endif
17583}
17584
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017586f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017587{
17588 char_u *r = NULL;
17589
17590#ifdef FEAT_CLIENTSERVER
17591# ifdef WIN32
17592 r = serverGetVimNames();
17593# else
17594 make_connection();
17595 if (X_DISPLAY != NULL)
17596 r = serverGetVimNames(X_DISPLAY);
17597# endif
17598#endif
17599 rettv->v_type = VAR_STRING;
17600 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601}
17602
17603/*
17604 * "setbufvar()" function
17605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017607f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608{
17609 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 char_u nbuf[NUMBUFLEN];
17614
17615 if (check_restricted() || check_secure())
17616 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017617 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17618 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017619 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 varp = &argvars[2];
17621
17622 if (buf != NULL && varname != NULL && varp != NULL)
17623 {
17624 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626
17627 if (*varname == '&')
17628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017629 long numval;
17630 char_u *strval;
17631 int error = FALSE;
17632
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017634 numval = get_tv_number_chk(varp, &error);
17635 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017636 if (!error && strval != NULL)
17637 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 }
17639 else
17640 {
17641 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17642 if (bufvarname != NULL)
17643 {
17644 STRCPY(bufvarname, "b:");
17645 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017646 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 vim_free(bufvarname);
17648 }
17649 }
17650
17651 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654}
17655
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017657f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017658{
17659 dict_T *d;
17660 dictitem_T *di;
17661 char_u *csearch;
17662
17663 if (argvars[0].v_type != VAR_DICT)
17664 {
17665 EMSG(_(e_dictreq));
17666 return;
17667 }
17668
17669 if ((d = argvars[0].vval.v_dict) != NULL)
17670 {
17671 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17672 if (csearch != NULL)
17673 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017674#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017675 if (enc_utf8)
17676 {
17677 int pcc[MAX_MCO];
17678 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017679
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017680 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17681 }
17682 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017683#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017684 set_last_csearch(PTR2CHAR(csearch),
17685 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017686 }
17687
17688 di = dict_find(d, (char_u *)"forward", -1);
17689 if (di != NULL)
17690 set_csearch_direction(get_tv_number(&di->di_tv)
17691 ? FORWARD : BACKWARD);
17692
17693 di = dict_find(d, (char_u *)"until", -1);
17694 if (di != NULL)
17695 set_csearch_until(!!get_tv_number(&di->di_tv));
17696 }
17697}
17698
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699/*
17700 * "setcmdpos()" function
17701 */
17702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017703f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017705 int pos = (int)get_tv_number(&argvars[0]) - 1;
17706
17707 if (pos >= 0)
17708 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709}
17710
17711/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017712 * "setfperm({fname}, {mode})" function
17713 */
17714 static void
17715f_setfperm(typval_T *argvars, typval_T *rettv)
17716{
17717 char_u *fname;
17718 char_u modebuf[NUMBUFLEN];
17719 char_u *mode_str;
17720 int i;
17721 int mask;
17722 int mode = 0;
17723
17724 rettv->vval.v_number = 0;
17725 fname = get_tv_string_chk(&argvars[0]);
17726 if (fname == NULL)
17727 return;
17728 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17729 if (mode_str == NULL)
17730 return;
17731 if (STRLEN(mode_str) != 9)
17732 {
17733 EMSG2(_(e_invarg2), mode_str);
17734 return;
17735 }
17736
17737 mask = 1;
17738 for (i = 8; i >= 0; --i)
17739 {
17740 if (mode_str[i] != '-')
17741 mode |= mask;
17742 mask = mask << 1;
17743 }
17744 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17745}
17746
17747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 * "setline()" function
17749 */
17750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017751f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752{
17753 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017754 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017755 list_T *l = NULL;
17756 listitem_T *li = NULL;
17757 long added = 0;
17758 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017760 lnum = get_tv_lnum(&argvars[0]);
17761 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017763 l = argvars[1].vval.v_list;
17764 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017766 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017768
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017769 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017770 for (;;)
17771 {
17772 if (l != NULL)
17773 {
17774 /* list argument, get next string */
17775 if (li == NULL)
17776 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017777 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017778 li = li->li_next;
17779 }
17780
17781 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017782 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017783 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017784
17785 /* When coming here from Insert mode, sync undo, so that this can be
17786 * undone separately from what was previously inserted. */
17787 if (u_sync_once == 2)
17788 {
17789 u_sync_once = 1; /* notify that u_sync() was called */
17790 u_sync(TRUE);
17791 }
17792
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017793 if (lnum <= curbuf->b_ml.ml_line_count)
17794 {
17795 /* existing line, replace it */
17796 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17797 {
17798 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017799 if (lnum == curwin->w_cursor.lnum)
17800 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017801 rettv->vval.v_number = 0; /* OK */
17802 }
17803 }
17804 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17805 {
17806 /* lnum is one past the last line, append the line */
17807 ++added;
17808 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17809 rettv->vval.v_number = 0; /* OK */
17810 }
17811
17812 if (l == NULL) /* only one string argument */
17813 break;
17814 ++lnum;
17815 }
17816
17817 if (added > 0)
17818 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819}
17820
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017821static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv);
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017822
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017824 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017825 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017827set_qf_ll_list(
17828 win_T *wp UNUSED,
17829 typval_T *list_arg UNUSED,
17830 typval_T *action_arg UNUSED,
17831 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017832{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017833#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017834 char_u *act;
17835 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017836#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017837
Bram Moolenaar2641f772005-03-25 21:58:17 +000017838 rettv->vval.v_number = -1;
17839
17840#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017841 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017842 EMSG(_(e_listreq));
17843 else
17844 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017845 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017846
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017847 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017848 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017849 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017850 if (act == NULL)
17851 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017852 if (*act == 'a' || *act == 'r')
17853 action = *act;
17854 }
17855
Bram Moolenaar81484f42012-12-05 15:16:47 +010017856 if (l != NULL && set_errorlist(wp, l, action,
17857 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017858 rettv->vval.v_number = 0;
17859 }
17860#endif
17861}
17862
17863/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017864 * "setloclist()" function
17865 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017867f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017868{
17869 win_T *win;
17870
17871 rettv->vval.v_number = -1;
17872
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017873 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017874 if (win != NULL)
17875 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17876}
17877
17878/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017879 * "setmatches()" function
17880 */
17881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017882f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017883{
17884#ifdef FEAT_SEARCH_EXTRA
17885 list_T *l;
17886 listitem_T *li;
17887 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017888 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017889
17890 rettv->vval.v_number = -1;
17891 if (argvars[0].v_type != VAR_LIST)
17892 {
17893 EMSG(_(e_listreq));
17894 return;
17895 }
17896 if ((l = argvars[0].vval.v_list) != NULL)
17897 {
17898
17899 /* To some extent make sure that we are dealing with a list from
17900 * "getmatches()". */
17901 li = l->lv_first;
17902 while (li != NULL)
17903 {
17904 if (li->li_tv.v_type != VAR_DICT
17905 || (d = li->li_tv.vval.v_dict) == NULL)
17906 {
17907 EMSG(_(e_invarg));
17908 return;
17909 }
17910 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017911 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17912 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017913 && dict_find(d, (char_u *)"priority", -1) != NULL
17914 && dict_find(d, (char_u *)"id", -1) != NULL))
17915 {
17916 EMSG(_(e_invarg));
17917 return;
17918 }
17919 li = li->li_next;
17920 }
17921
17922 clear_matches(curwin);
17923 li = l->lv_first;
17924 while (li != NULL)
17925 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017926 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017927 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017928 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017929 char_u *group;
17930 int priority;
17931 int id;
17932 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017933
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017934 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017935 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17936 {
17937 if (s == NULL)
17938 {
17939 s = list_alloc();
17940 if (s == NULL)
17941 return;
17942 }
17943
17944 /* match from matchaddpos() */
17945 for (i = 1; i < 9; i++)
17946 {
17947 sprintf((char *)buf, (char *)"pos%d", i);
17948 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17949 {
17950 if (di->di_tv.v_type != VAR_LIST)
17951 return;
17952
17953 list_append_tv(s, &di->di_tv);
17954 s->lv_refcount++;
17955 }
17956 else
17957 break;
17958 }
17959 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017960
17961 group = get_dict_string(d, (char_u *)"group", FALSE);
17962 priority = (int)get_dict_number(d, (char_u *)"priority");
17963 id = (int)get_dict_number(d, (char_u *)"id");
17964 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17965 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17966 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017967 if (i == 0)
17968 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017969 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017970 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017971 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017972 }
17973 else
17974 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017975 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017976 list_unref(s);
17977 s = NULL;
17978 }
17979
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017980 li = li->li_next;
17981 }
17982 rettv->vval.v_number = 0;
17983 }
17984#endif
17985}
17986
17987/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017988 * "setpos()" function
17989 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017991f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017992{
17993 pos_T pos;
17994 int fnum;
17995 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017996 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017997
Bram Moolenaar08250432008-02-13 11:42:46 +000017998 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017999 name = get_tv_string_chk(argvars);
18000 if (name != NULL)
18001 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018002 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018003 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018004 if (--pos.col < 0)
18005 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018006 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018007 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018008 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018009 if (fnum == curbuf->b_fnum)
18010 {
18011 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018012 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018013 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018014 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018015 curwin->w_set_curswant = FALSE;
18016 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018017 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018018 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018019 }
18020 else
18021 EMSG(_(e_invarg));
18022 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018023 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18024 {
18025 /* set mark */
18026 if (setmark_pos(name[1], &pos, fnum) == OK)
18027 rettv->vval.v_number = 0;
18028 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018029 else
18030 EMSG(_(e_invarg));
18031 }
18032 }
18033}
18034
18035/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018036 * "setqflist()" function
18037 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018039f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018040{
18041 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18042}
18043
18044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 * "setreg()" function
18046 */
18047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018048f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049{
18050 int regname;
18051 char_u *strregname;
18052 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018053 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 int append;
18055 char_u yank_type;
18056 long block_len;
18057
18058 block_len = -1;
18059 yank_type = MAUTO;
18060 append = FALSE;
18061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018062 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018063 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018065 if (strregname == NULL)
18066 return; /* type error; errmsg already given */
18067 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 if (regname == 0 || regname == '@')
18069 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018071 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018073 stropt = get_tv_string_chk(&argvars[2]);
18074 if (stropt == NULL)
18075 return; /* type error */
18076 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 switch (*stropt)
18078 {
18079 case 'a': case 'A': /* append */
18080 append = TRUE;
18081 break;
18082 case 'v': case 'c': /* character-wise selection */
18083 yank_type = MCHAR;
18084 break;
18085 case 'V': case 'l': /* line-wise selection */
18086 yank_type = MLINE;
18087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 case 'b': case Ctrl_V: /* block-wise selection */
18089 yank_type = MBLOCK;
18090 if (VIM_ISDIGIT(stropt[1]))
18091 {
18092 ++stropt;
18093 block_len = getdigits(&stropt) - 1;
18094 --stropt;
18095 }
18096 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 }
18098 }
18099
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018100 if (argvars[1].v_type == VAR_LIST)
18101 {
18102 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018103 char_u **allocval;
18104 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018105 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018106 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018107 int len = argvars[1].vval.v_list->lv_len;
18108 listitem_T *li;
18109
Bram Moolenaar7d647822014-04-05 21:28:56 +020018110 /* First half: use for pointers to result lines; second half: use for
18111 * pointers to allocated copies. */
18112 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018113 if (lstval == NULL)
18114 return;
18115 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018116 allocval = lstval + len + 2;
18117 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018118
18119 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18120 li = li->li_next)
18121 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018122 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018123 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018124 goto free_lstval;
18125 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018126 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018127 /* Need to make a copy, next get_tv_string_buf_chk() will
18128 * overwrite the string. */
18129 strval = vim_strsave(buf);
18130 if (strval == NULL)
18131 goto free_lstval;
18132 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018133 }
18134 *curval++ = strval;
18135 }
18136 *curval++ = NULL;
18137
18138 write_reg_contents_lst(regname, lstval, -1,
18139 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018140free_lstval:
18141 while (curallocval > allocval)
18142 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018143 vim_free(lstval);
18144 }
18145 else
18146 {
18147 strval = get_tv_string_chk(&argvars[1]);
18148 if (strval == NULL)
18149 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018150 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018153 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154}
18155
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018156/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018157 * "settabvar()" function
18158 */
18159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018160f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018161{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018162#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018163 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018164 tabpage_T *tp;
18165#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018166 char_u *varname, *tabvarname;
18167 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018168
18169 rettv->vval.v_number = 0;
18170
18171 if (check_restricted() || check_secure())
18172 return;
18173
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018174#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018175 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018176#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018177 varname = get_tv_string_chk(&argvars[1]);
18178 varp = &argvars[2];
18179
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018180 if (varname != NULL && varp != NULL
18181#ifdef FEAT_WINDOWS
18182 && tp != NULL
18183#endif
18184 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018185 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018186#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018187 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018188 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018189#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018190
18191 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18192 if (tabvarname != NULL)
18193 {
18194 STRCPY(tabvarname, "t:");
18195 STRCPY(tabvarname + 2, varname);
18196 set_var(tabvarname, varp, TRUE);
18197 vim_free(tabvarname);
18198 }
18199
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018200#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018201 /* Restore current tabpage */
18202 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018203 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018204#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018205 }
18206}
18207
18208/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018209 * "settabwinvar()" function
18210 */
18211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018212f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018213{
18214 setwinvar(argvars, rettv, 1);
18215}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216
18217/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018218 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018221f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018223 setwinvar(argvars, rettv, 0);
18224}
18225
18226/*
18227 * "setwinvar()" and "settabwinvar()" functions
18228 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018229
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018231setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018232{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 win_T *win;
18234#ifdef FEAT_WINDOWS
18235 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018236 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018237 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238#endif
18239 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018240 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018242 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243
18244 if (check_restricted() || check_secure())
18245 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018246
18247#ifdef FEAT_WINDOWS
18248 if (off == 1)
18249 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18250 else
18251 tp = curtab;
18252#endif
18253 win = find_win_by_nr(&argvars[off], tp);
18254 varname = get_tv_string_chk(&argvars[off + 1]);
18255 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256
18257 if (win != NULL && varname != NULL && varp != NULL)
18258 {
18259#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018260 need_switch_win = !(tp == curtab && win == curwin);
18261 if (!need_switch_win
18262 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018265 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018267 long numval;
18268 char_u *strval;
18269 int error = FALSE;
18270
18271 ++varname;
18272 numval = get_tv_number_chk(varp, &error);
18273 strval = get_tv_string_buf_chk(varp, nbuf);
18274 if (!error && strval != NULL)
18275 set_option_value(varname, numval, strval, OPT_LOCAL);
18276 }
18277 else
18278 {
18279 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18280 if (winvarname != NULL)
18281 {
18282 STRCPY(winvarname, "w:");
18283 STRCPY(winvarname + 2, varname);
18284 set_var(winvarname, varp, TRUE);
18285 vim_free(winvarname);
18286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 }
18288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018290 if (need_switch_win)
18291 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292#endif
18293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294}
18295
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018296#ifdef FEAT_CRYPT
18297/*
18298 * "sha256({string})" function
18299 */
18300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018301f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018302{
18303 char_u *p;
18304
18305 p = get_tv_string(&argvars[0]);
18306 rettv->vval.v_string = vim_strsave(
18307 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18308 rettv->v_type = VAR_STRING;
18309}
18310#endif /* FEAT_CRYPT */
18311
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018313 * "shellescape({string})" function
18314 */
18315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018316f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018317{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018318 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018319 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018320 rettv->v_type = VAR_STRING;
18321}
18322
18323/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018324 * shiftwidth() function
18325 */
18326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018327f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018328{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018329 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018330}
18331
18332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 */
18335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018336f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018338 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339
Bram Moolenaar0d660222005-01-07 21:51:51 +000018340 p = get_tv_string(&argvars[0]);
18341 rettv->vval.v_string = vim_strsave(p);
18342 simplify_filename(rettv->vval.v_string); /* simplify in place */
18343 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344}
18345
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018346#ifdef FEAT_FLOAT
18347/*
18348 * "sin()" function
18349 */
18350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018351f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018352{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018353 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018354
18355 rettv->v_type = VAR_FLOAT;
18356 if (get_float_arg(argvars, &f) == OK)
18357 rettv->vval.v_float = sin(f);
18358 else
18359 rettv->vval.v_float = 0.0;
18360}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018361
18362/*
18363 * "sinh()" function
18364 */
18365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018366f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018367{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018368 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018369
18370 rettv->v_type = VAR_FLOAT;
18371 if (get_float_arg(argvars, &f) == OK)
18372 rettv->vval.v_float = sinh(f);
18373 else
18374 rettv->vval.v_float = 0.0;
18375}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018376#endif
18377
Bram Moolenaar0d660222005-01-07 21:51:51 +000018378static int
18379#ifdef __BORLANDC__
18380 _RTLENTRYF
18381#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018382 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018383static int
18384#ifdef __BORLANDC__
18385 _RTLENTRYF
18386#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018387 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018388
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018389/* struct used in the array that's given to qsort() */
18390typedef struct
18391{
18392 listitem_T *item;
18393 int idx;
18394} sortItem_T;
18395
Bram Moolenaar0b962472016-02-22 22:51:33 +010018396/* struct storing information about current sort */
18397typedef struct
18398{
18399 int item_compare_ic;
18400 int item_compare_numeric;
18401 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018402#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018403 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018404#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018405 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018406 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018407 dict_T *item_compare_selfdict;
18408 int item_compare_func_err;
18409 int item_compare_keep_zero;
18410} sortinfo_T;
18411static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018412static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413#define ITEM_COMPARE_FAIL 999
18414
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018416 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418 static int
18419#ifdef __BORLANDC__
18420_RTLENTRYF
18421#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018422item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018424 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018425 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018427 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018428 int res;
18429 char_u numbuf1[NUMBUFLEN];
18430 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018432 si1 = (sortItem_T *)s1;
18433 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018434 tv1 = &si1->item->li_tv;
18435 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018436
Bram Moolenaar0b962472016-02-22 22:51:33 +010018437 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018438 {
18439 long v1 = get_tv_number(tv1);
18440 long v2 = get_tv_number(tv2);
18441
18442 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18443 }
18444
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018445#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018446 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018447 {
18448 float_T v1 = get_tv_float(tv1);
18449 float_T v2 = get_tv_float(tv2);
18450
18451 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18452 }
18453#endif
18454
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018455 /* tv2string() puts quotes around a string and allocates memory. Don't do
18456 * that for string variables. Use a single quote when comparing with a
18457 * non-string to do what the docs promise. */
18458 if (tv1->v_type == VAR_STRING)
18459 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018460 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018461 p1 = (char_u *)"'";
18462 else
18463 p1 = tv1->vval.v_string;
18464 }
18465 else
18466 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18467 if (tv2->v_type == VAR_STRING)
18468 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018469 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018470 p2 = (char_u *)"'";
18471 else
18472 p2 = tv2->vval.v_string;
18473 }
18474 else
18475 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018476 if (p1 == NULL)
18477 p1 = (char_u *)"";
18478 if (p2 == NULL)
18479 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018480 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018481 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018482 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018483 res = STRICMP(p1, p2);
18484 else
18485 res = STRCMP(p1, p2);
18486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018488 {
18489 double n1, n2;
18490 n1 = strtod((char *)p1, (char **)&p1);
18491 n2 = strtod((char *)p2, (char **)&p2);
18492 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18493 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018494
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018495 /* When the result would be zero, compare the item indexes. Makes the
18496 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018497 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018498 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018499
Bram Moolenaar0d660222005-01-07 21:51:51 +000018500 vim_free(tofree1);
18501 vim_free(tofree2);
18502 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503}
18504
18505 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018506#ifdef __BORLANDC__
18507_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018509item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018510{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018511 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018512 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018513 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018514 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018515 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018516 char_u *func_name;
18517 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018519 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018520 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018521 return 0;
18522
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018523 si1 = (sortItem_T *)s1;
18524 si2 = (sortItem_T *)s2;
18525
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018526 if (partial == NULL)
18527 func_name = sortinfo->item_compare_func;
18528 else
18529 func_name = partial->pt_name;
18530
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018531 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018532 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018533 copy_tv(&si1->item->li_tv, &argv[0]);
18534 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018535
18536 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018537 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018538 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018539 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540 clear_tv(&argv[0]);
18541 clear_tv(&argv[1]);
18542
18543 if (res == FAIL)
18544 res = ITEM_COMPARE_FAIL;
18545 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018546 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18547 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018548 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018549 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018550
18551 /* When the result would be zero, compare the pointers themselves. Makes
18552 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018553 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018554 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018555
Bram Moolenaar0d660222005-01-07 21:51:51 +000018556 return res;
18557}
18558
18559/*
18560 * "sort({list})" function
18561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018563do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564{
Bram Moolenaar33570922005-01-25 22:26:29 +000018565 list_T *l;
18566 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018567 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018568 sortinfo_T *old_sortinfo;
18569 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018570 long len;
18571 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572
Bram Moolenaar0b962472016-02-22 22:51:33 +010018573 /* Pointer to current info struct used in compare function. Save and
18574 * restore the current one for nested calls. */
18575 old_sortinfo = sortinfo;
18576 sortinfo = &info;
18577
Bram Moolenaar0d660222005-01-07 21:51:51 +000018578 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018579 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 else
18581 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018583 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018584 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18585 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018586 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018587 rettv->vval.v_list = l;
18588 rettv->v_type = VAR_LIST;
18589 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590
Bram Moolenaar0d660222005-01-07 21:51:51 +000018591 len = list_len(l);
18592 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018593 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594
Bram Moolenaar0b962472016-02-22 22:51:33 +010018595 info.item_compare_ic = FALSE;
18596 info.item_compare_numeric = FALSE;
18597 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018598#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018599 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018600#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018601 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018602 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018603 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 if (argvars[1].v_type != VAR_UNKNOWN)
18605 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018606 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018607 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018608 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018609 else if (argvars[1].v_type == VAR_PARTIAL)
18610 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611 else
18612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018613 int error = FALSE;
18614
18615 i = get_tv_number_chk(&argvars[1], &error);
18616 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018617 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018618 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018619 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018620 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018621 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018622 else if (i != 0)
18623 {
18624 EMSG(_(e_invarg));
18625 goto theend;
18626 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018627 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018628 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018629 if (*info.item_compare_func == NUL)
18630 {
18631 /* empty string means default sort */
18632 info.item_compare_func = NULL;
18633 }
18634 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018635 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018636 info.item_compare_func = NULL;
18637 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018638 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018639 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018640 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018641 info.item_compare_func = NULL;
18642 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018643 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018644#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018645 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018646 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018647 info.item_compare_func = NULL;
18648 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018649 }
18650#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018651 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018652 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018653 info.item_compare_func = NULL;
18654 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018655 }
18656 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018657 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018658
18659 if (argvars[2].v_type != VAR_UNKNOWN)
18660 {
18661 /* optional third argument: {dict} */
18662 if (argvars[2].v_type != VAR_DICT)
18663 {
18664 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018665 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018666 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018667 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018668 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670
Bram Moolenaar0d660222005-01-07 21:51:51 +000018671 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018672 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018673 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018674 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675
Bram Moolenaar327aa022014-03-25 18:24:23 +010018676 i = 0;
18677 if (sort)
18678 {
18679 /* sort(): ptrs will be the list to sort */
18680 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018681 {
18682 ptrs[i].item = li;
18683 ptrs[i].idx = i;
18684 ++i;
18685 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018686
Bram Moolenaar0b962472016-02-22 22:51:33 +010018687 info.item_compare_func_err = FALSE;
18688 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018689 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018690 if ((info.item_compare_func != NULL
18691 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018692 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018693 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018694 EMSG(_("E702: Sort compare function failed"));
18695 else
18696 {
18697 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018698 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018699 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018700 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018701 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018702
Bram Moolenaar0b962472016-02-22 22:51:33 +010018703 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018704 {
18705 /* Clear the List and append the items in sorted order. */
18706 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18707 l->lv_len = 0;
18708 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018709 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018710 }
18711 }
18712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018715 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018716
18717 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018718 info.item_compare_func_err = FALSE;
18719 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018720 item_compare_func_ptr = info.item_compare_func != NULL
18721 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018722 ? item_compare2 : item_compare;
18723
18724 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18725 li = li->li_next)
18726 {
18727 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18728 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018729 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018730 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018731 {
18732 EMSG(_("E882: Uniq compare function failed"));
18733 break;
18734 }
18735 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018736
Bram Moolenaar0b962472016-02-22 22:51:33 +010018737 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018738 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018739 while (--i >= 0)
18740 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018741 li = ptrs[i].item->li_next;
18742 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018743 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018744 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018745 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018746 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018747 list_fix_watch(l, li);
18748 listitem_free(li);
18749 l->lv_len--;
18750 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018752 }
18753
18754 vim_free(ptrs);
18755 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018756theend:
18757 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018758}
18759
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018760/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018761 * "sort({list})" function
18762 */
18763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018764f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018765{
18766 do_sort_uniq(argvars, rettv, TRUE);
18767}
18768
18769/*
18770 * "uniq({list})" function
18771 */
18772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018773f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018774{
18775 do_sort_uniq(argvars, rettv, FALSE);
18776}
18777
18778/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018779 * "soundfold({word})" function
18780 */
18781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018782f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018783{
18784 char_u *s;
18785
18786 rettv->v_type = VAR_STRING;
18787 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018788#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018789 rettv->vval.v_string = eval_soundfold(s);
18790#else
18791 rettv->vval.v_string = vim_strsave(s);
18792#endif
18793}
18794
18795/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018796 * "spellbadword()" function
18797 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018799f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018800{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018801 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018802 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018803 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018804
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018805 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018806 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018807
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018808#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018809 if (argvars[0].v_type == VAR_UNKNOWN)
18810 {
18811 /* Find the start and length of the badly spelled word. */
18812 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18813 if (len != 0)
18814 word = ml_get_cursor();
18815 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018816 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018817 {
18818 char_u *str = get_tv_string_chk(&argvars[0]);
18819 int capcol = -1;
18820
18821 if (str != NULL)
18822 {
18823 /* Check the argument for spelling. */
18824 while (*str != NUL)
18825 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018826 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018827 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018828 {
18829 word = str;
18830 break;
18831 }
18832 str += len;
18833 }
18834 }
18835 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018836#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018837
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018838 list_append_string(rettv->vval.v_list, word, len);
18839 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018840 attr == HLF_SPB ? "bad" :
18841 attr == HLF_SPR ? "rare" :
18842 attr == HLF_SPL ? "local" :
18843 attr == HLF_SPC ? "caps" :
18844 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845}
18846
18847/*
18848 * "spellsuggest()" function
18849 */
18850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018851f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018852{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018853#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018854 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018855 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018856 int maxcount;
18857 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018858 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018859 listitem_T *li;
18860 int need_capital = FALSE;
18861#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018862
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018863 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018864 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018865
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018866#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018867 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018868 {
18869 str = get_tv_string(&argvars[0]);
18870 if (argvars[1].v_type != VAR_UNKNOWN)
18871 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018872 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018873 if (maxcount <= 0)
18874 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018875 if (argvars[2].v_type != VAR_UNKNOWN)
18876 {
18877 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18878 if (typeerr)
18879 return;
18880 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018881 }
18882 else
18883 maxcount = 25;
18884
Bram Moolenaar4770d092006-01-12 23:22:24 +000018885 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018886
18887 for (i = 0; i < ga.ga_len; ++i)
18888 {
18889 str = ((char_u **)ga.ga_data)[i];
18890
18891 li = listitem_alloc();
18892 if (li == NULL)
18893 vim_free(str);
18894 else
18895 {
18896 li->li_tv.v_type = VAR_STRING;
18897 li->li_tv.v_lock = 0;
18898 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018899 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018900 }
18901 }
18902 ga_clear(&ga);
18903 }
18904#endif
18905}
18906
Bram Moolenaar0d660222005-01-07 21:51:51 +000018907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018908f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909{
18910 char_u *str;
18911 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018912 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018913 regmatch_T regmatch;
18914 char_u patbuf[NUMBUFLEN];
18915 char_u *save_cpo;
18916 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018917 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018918 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018919 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018920
18921 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18922 save_cpo = p_cpo;
18923 p_cpo = (char_u *)"";
18924
18925 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018926 if (argvars[1].v_type != VAR_UNKNOWN)
18927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018928 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18929 if (pat == NULL)
18930 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018931 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018932 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018933 }
18934 if (pat == NULL || *pat == NUL)
18935 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018936
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018937 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018939 if (typeerr)
18940 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941
Bram Moolenaar0d660222005-01-07 21:51:51 +000018942 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18943 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018945 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018946 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018947 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018948 if (*str == NUL)
18949 match = FALSE; /* empty item at the end */
18950 else
18951 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018952 if (match)
18953 end = regmatch.startp[0];
18954 else
18955 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018956 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18957 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018959 if (list_append_string(rettv->vval.v_list, str,
18960 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018962 }
18963 if (!match)
18964 break;
18965 /* Advance to just after the match. */
18966 if (regmatch.endp[0] > str)
18967 col = 0;
18968 else
18969 {
18970 /* Don't get stuck at the same match. */
18971#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018972 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973#else
18974 col = 1;
18975#endif
18976 }
18977 str = regmatch.endp[0];
18978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979
Bram Moolenaar473de612013-06-08 18:19:48 +020018980 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984}
18985
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018986#ifdef FEAT_FLOAT
18987/*
18988 * "sqrt()" function
18989 */
18990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018991f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018992{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018993 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018994
18995 rettv->v_type = VAR_FLOAT;
18996 if (get_float_arg(argvars, &f) == OK)
18997 rettv->vval.v_float = sqrt(f);
18998 else
18999 rettv->vval.v_float = 0.0;
19000}
19001
19002/*
19003 * "str2float()" function
19004 */
19005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019006f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019007{
19008 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19009
19010 if (*p == '+')
19011 p = skipwhite(p + 1);
19012 (void)string2float(p, &rettv->vval.v_float);
19013 rettv->v_type = VAR_FLOAT;
19014}
19015#endif
19016
Bram Moolenaar2c932302006-03-18 21:42:09 +000019017/*
19018 * "str2nr()" function
19019 */
19020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019021f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019022{
19023 int base = 10;
19024 char_u *p;
19025 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019026 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019027
19028 if (argvars[1].v_type != VAR_UNKNOWN)
19029 {
19030 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019031 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019032 {
19033 EMSG(_(e_invarg));
19034 return;
19035 }
19036 }
19037
19038 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019039 if (*p == '+')
19040 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019041 switch (base)
19042 {
19043 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19044 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19045 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19046 default: what = 0;
19047 }
19048 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019049 rettv->vval.v_number = n;
19050}
19051
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052#ifdef HAVE_STRFTIME
19053/*
19054 * "strftime({format}[, {time}])" function
19055 */
19056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019057f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058{
19059 char_u result_buf[256];
19060 struct tm *curtime;
19061 time_t seconds;
19062 char_u *p;
19063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019064 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019066 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019067 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 seconds = time(NULL);
19069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019070 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 curtime = localtime(&seconds);
19072 /* MSVC returns NULL for an invalid value of seconds. */
19073 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019074 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 else
19076 {
19077# ifdef FEAT_MBYTE
19078 vimconv_T conv;
19079 char_u *enc;
19080
19081 conv.vc_type = CONV_NONE;
19082 enc = enc_locale();
19083 convert_setup(&conv, p_enc, enc);
19084 if (conv.vc_type != CONV_NONE)
19085 p = string_convert(&conv, p, NULL);
19086# endif
19087 if (p != NULL)
19088 (void)strftime((char *)result_buf, sizeof(result_buf),
19089 (char *)p, curtime);
19090 else
19091 result_buf[0] = NUL;
19092
19093# ifdef FEAT_MBYTE
19094 if (conv.vc_type != CONV_NONE)
19095 vim_free(p);
19096 convert_setup(&conv, enc, p_enc);
19097 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 else
19100# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102
19103# ifdef FEAT_MBYTE
19104 /* Release conversion descriptors */
19105 convert_setup(&conv, NULL, NULL);
19106 vim_free(enc);
19107# endif
19108 }
19109}
19110#endif
19111
19112/*
19113 * "stridx()" function
19114 */
19115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019116f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117{
19118 char_u buf[NUMBUFLEN];
19119 char_u *needle;
19120 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019121 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019123 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019125 needle = get_tv_string_chk(&argvars[1]);
19126 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019127 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019128 if (needle == NULL || haystack == NULL)
19129 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130
Bram Moolenaar33570922005-01-25 22:26:29 +000019131 if (argvars[2].v_type != VAR_UNKNOWN)
19132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019133 int error = FALSE;
19134
19135 start_idx = get_tv_number_chk(&argvars[2], &error);
19136 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019137 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019138 if (start_idx >= 0)
19139 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019140 }
19141
19142 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19143 if (pos != NULL)
19144 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145}
19146
19147/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019148 * "string()" function
19149 */
19150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019151f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019152{
19153 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019154 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019156 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019157 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019158 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019159 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019160 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161}
19162
19163/*
19164 * "strlen()" function
19165 */
19166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019167f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 rettv->vval.v_number = (varnumber_T)(STRLEN(
19170 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171}
19172
19173/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019174 * "strchars()" function
19175 */
19176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019177f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019178{
19179 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019180 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019181#ifdef FEAT_MBYTE
19182 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019183 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019184#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019185
19186 if (argvars[1].v_type != VAR_UNKNOWN)
19187 skipcc = get_tv_number_chk(&argvars[1], NULL);
19188 if (skipcc < 0 || skipcc > 1)
19189 EMSG(_(e_invarg));
19190 else
19191 {
19192#ifdef FEAT_MBYTE
19193 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19194 while (*s != NUL)
19195 {
19196 func_mb_ptr2char_adv(&s);
19197 ++len;
19198 }
19199 rettv->vval.v_number = len;
19200#else
19201 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19202#endif
19203 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019204}
19205
19206/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019207 * "strdisplaywidth()" function
19208 */
19209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019210f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019211{
19212 char_u *s = get_tv_string(&argvars[0]);
19213 int col = 0;
19214
19215 if (argvars[1].v_type != VAR_UNKNOWN)
19216 col = get_tv_number(&argvars[1]);
19217
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019218 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019219}
19220
19221/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019222 * "strwidth()" function
19223 */
19224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019225f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019226{
19227 char_u *s = get_tv_string(&argvars[0]);
19228
19229 rettv->vval.v_number = (varnumber_T)(
19230#ifdef FEAT_MBYTE
19231 mb_string2cells(s, -1)
19232#else
19233 STRLEN(s)
19234#endif
19235 );
19236}
19237
19238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 * "strpart()" function
19240 */
19241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019242f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243{
19244 char_u *p;
19245 int n;
19246 int len;
19247 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019248 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019250 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 slen = (int)STRLEN(p);
19252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019253 n = get_tv_number_chk(&argvars[1], &error);
19254 if (error)
19255 len = 0;
19256 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019257 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 else
19259 len = slen - n; /* default len: all bytes that are available. */
19260
19261 /*
19262 * Only return the overlap between the specified part and the actual
19263 * string.
19264 */
19265 if (n < 0)
19266 {
19267 len += n;
19268 n = 0;
19269 }
19270 else if (n > slen)
19271 n = slen;
19272 if (len < 0)
19273 len = 0;
19274 else if (n + len > slen)
19275 len = slen - n;
19276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019277 rettv->v_type = VAR_STRING;
19278 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279}
19280
19281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282 * "strridx()" function
19283 */
19284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019285f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019286{
19287 char_u buf[NUMBUFLEN];
19288 char_u *needle;
19289 char_u *haystack;
19290 char_u *rest;
19291 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019292 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019294 needle = get_tv_string_chk(&argvars[1]);
19295 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019296
19297 rettv->vval.v_number = -1;
19298 if (needle == NULL || haystack == NULL)
19299 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019300
19301 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019302 if (argvars[2].v_type != VAR_UNKNOWN)
19303 {
19304 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019305 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019306 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019307 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019308 }
19309 else
19310 end_idx = haystack_len;
19311
Bram Moolenaar0d660222005-01-07 21:51:51 +000019312 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019313 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019314 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019315 lastmatch = haystack + end_idx;
19316 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019318 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019319 for (rest = haystack; *rest != '\0'; ++rest)
19320 {
19321 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019322 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019323 break;
19324 lastmatch = rest;
19325 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019326 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019327
19328 if (lastmatch == NULL)
19329 rettv->vval.v_number = -1;
19330 else
19331 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19332}
19333
19334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 * "strtrans()" function
19336 */
19337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019338f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019340 rettv->v_type = VAR_STRING;
19341 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342}
19343
19344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019345 * "submatch()" function
19346 */
19347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019348f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019349{
Bram Moolenaar41571762014-04-02 19:00:58 +020019350 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019351 int no;
19352 int retList = 0;
19353
19354 no = (int)get_tv_number_chk(&argvars[0], &error);
19355 if (error)
19356 return;
19357 error = FALSE;
19358 if (argvars[1].v_type != VAR_UNKNOWN)
19359 retList = get_tv_number_chk(&argvars[1], &error);
19360 if (error)
19361 return;
19362
19363 if (retList == 0)
19364 {
19365 rettv->v_type = VAR_STRING;
19366 rettv->vval.v_string = reg_submatch(no);
19367 }
19368 else
19369 {
19370 rettv->v_type = VAR_LIST;
19371 rettv->vval.v_list = reg_submatch_list(no);
19372 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019373}
19374
19375/*
19376 * "substitute()" function
19377 */
19378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019379f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019380{
19381 char_u patbuf[NUMBUFLEN];
19382 char_u subbuf[NUMBUFLEN];
19383 char_u flagsbuf[NUMBUFLEN];
19384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 char_u *str = get_tv_string_chk(&argvars[0]);
19386 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19387 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19388 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19389
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019391 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19392 rettv->vval.v_string = NULL;
19393 else
19394 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395}
19396
19397/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019398 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019401f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402{
19403 int id = 0;
19404#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019405 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 long col;
19407 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019408 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019410 lnum = get_tv_lnum(argvars); /* -1 on type error */
19411 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19412 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019414 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019415 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019416 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417#endif
19418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420}
19421
19422/*
19423 * "synIDattr(id, what [, mode])" function
19424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019426f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427{
19428 char_u *p = NULL;
19429#ifdef FEAT_SYN_HL
19430 int id;
19431 char_u *what;
19432 char_u *mode;
19433 char_u modebuf[NUMBUFLEN];
19434 int modec;
19435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019436 id = get_tv_number(&argvars[0]);
19437 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019440 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019442 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 modec = 0; /* replace invalid with current */
19444 }
19445 else
19446 {
19447#ifdef FEAT_GUI
19448 if (gui.in_use)
19449 modec = 'g';
19450 else
19451#endif
19452 if (t_colors > 1)
19453 modec = 'c';
19454 else
19455 modec = 't';
19456 }
19457
19458
19459 switch (TOLOWER_ASC(what[0]))
19460 {
19461 case 'b':
19462 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19463 p = highlight_color(id, what, modec);
19464 else /* bold */
19465 p = highlight_has_attr(id, HL_BOLD, modec);
19466 break;
19467
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019468 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 p = highlight_color(id, what, modec);
19470 break;
19471
19472 case 'i':
19473 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19474 p = highlight_has_attr(id, HL_INVERSE, modec);
19475 else /* italic */
19476 p = highlight_has_attr(id, HL_ITALIC, modec);
19477 break;
19478
19479 case 'n': /* name */
19480 p = get_highlight_name(NULL, id - 1);
19481 break;
19482
19483 case 'r': /* reverse */
19484 p = highlight_has_attr(id, HL_INVERSE, modec);
19485 break;
19486
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019487 case 's':
19488 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19489 p = highlight_color(id, what, modec);
19490 else /* standout */
19491 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 break;
19493
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019494 case 'u':
19495 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19496 /* underline */
19497 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19498 else
19499 /* undercurl */
19500 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 break;
19502 }
19503
19504 if (p != NULL)
19505 p = vim_strsave(p);
19506#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 rettv->v_type = VAR_STRING;
19508 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509}
19510
19511/*
19512 * "synIDtrans(id)" function
19513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019515f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516{
19517 int id;
19518
19519#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019520 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521
19522 if (id > 0)
19523 id = syn_get_final_id(id);
19524 else
19525#endif
19526 id = 0;
19527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529}
19530
19531/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019532 * "synconcealed(lnum, col)" function
19533 */
19534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019535f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019536{
19537#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19538 long lnum;
19539 long col;
19540 int syntax_flags = 0;
19541 int cchar;
19542 int matchid = 0;
19543 char_u str[NUMBUFLEN];
19544#endif
19545
19546 rettv->v_type = VAR_LIST;
19547 rettv->vval.v_list = NULL;
19548
19549#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19550 lnum = get_tv_lnum(argvars); /* -1 on type error */
19551 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19552
19553 vim_memset(str, NUL, sizeof(str));
19554
19555 if (rettv_list_alloc(rettv) != FAIL)
19556 {
19557 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19558 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19559 && curwin->w_p_cole > 0)
19560 {
19561 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19562 syntax_flags = get_syntax_info(&matchid);
19563
19564 /* get the conceal character */
19565 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19566 {
19567 cchar = syn_get_sub_char();
19568 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19569 cchar = lcs_conceal;
19570 if (cchar != NUL)
19571 {
19572# ifdef FEAT_MBYTE
19573 if (has_mbyte)
19574 (*mb_char2bytes)(cchar, str);
19575 else
19576# endif
19577 str[0] = cchar;
19578 }
19579 }
19580 }
19581
19582 list_append_number(rettv->vval.v_list,
19583 (syntax_flags & HL_CONCEAL) != 0);
19584 /* -1 to auto-determine strlen */
19585 list_append_string(rettv->vval.v_list, str, -1);
19586 list_append_number(rettv->vval.v_list, matchid);
19587 }
19588#endif
19589}
19590
19591/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019592 * "synstack(lnum, col)" function
19593 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019595f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019596{
19597#ifdef FEAT_SYN_HL
19598 long lnum;
19599 long col;
19600 int i;
19601 int id;
19602#endif
19603
19604 rettv->v_type = VAR_LIST;
19605 rettv->vval.v_list = NULL;
19606
19607#ifdef FEAT_SYN_HL
19608 lnum = get_tv_lnum(argvars); /* -1 on type error */
19609 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19610
19611 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019612 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019613 && rettv_list_alloc(rettv) != FAIL)
19614 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019615 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019616 for (i = 0; ; ++i)
19617 {
19618 id = syn_get_stack_item(i);
19619 if (id < 0)
19620 break;
19621 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19622 break;
19623 }
19624 }
19625#endif
19626}
19627
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019629get_cmd_output_as_rettv(
19630 typval_T *argvars,
19631 typval_T *rettv,
19632 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019634 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019636 char_u *infile = NULL;
19637 char_u buf[NUMBUFLEN];
19638 int err = FALSE;
19639 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019640 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019641 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019643 rettv->v_type = VAR_STRING;
19644 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019645 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019646 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019647
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019648 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019649 {
19650 /*
19651 * Write the string to a temp file, to be used for input of the shell
19652 * command.
19653 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019654 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019655 {
19656 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019657 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019658 }
19659
19660 fd = mch_fopen((char *)infile, WRITEBIN);
19661 if (fd == NULL)
19662 {
19663 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019664 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019665 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019666 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019667 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019668 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19669 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019670 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019671 else
19672 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019673 size_t len;
19674
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019675 p = get_tv_string_buf_chk(&argvars[1], buf);
19676 if (p == NULL)
19677 {
19678 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019679 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019680 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019681 len = STRLEN(p);
19682 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019683 err = TRUE;
19684 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019685 if (fclose(fd) != 0)
19686 err = TRUE;
19687 if (err)
19688 {
19689 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019690 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019691 }
19692 }
19693
Bram Moolenaar52a72462014-08-29 15:53:52 +020019694 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19695 * echoes typeahead, that messes up the display. */
19696 if (!msg_silent)
19697 flags += SHELL_COOKED;
19698
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019699 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019701 int len;
19702 listitem_T *li;
19703 char_u *s = NULL;
19704 char_u *start;
19705 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019706 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707
Bram Moolenaar52a72462014-08-29 15:53:52 +020019708 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019709 if (res == NULL)
19710 goto errret;
19711
19712 list = list_alloc();
19713 if (list == NULL)
19714 goto errret;
19715
19716 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019718 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019719 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019720 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019721 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019722
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019723 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019724 if (s == NULL)
19725 goto errret;
19726
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019727 for (p = s; start < end; ++p, ++start)
19728 *p = *start == NUL ? NL : *start;
19729 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019730
19731 li = listitem_alloc();
19732 if (li == NULL)
19733 {
19734 vim_free(s);
19735 goto errret;
19736 }
19737 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019738 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019739 li->li_tv.vval.v_string = s;
19740 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019742
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019743 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019744 rettv->v_type = VAR_LIST;
19745 rettv->vval.v_list = list;
19746 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019748 else
19749 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019750 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019751#ifdef USE_CR
19752 /* translate <CR> into <NL> */
19753 if (res != NULL)
19754 {
19755 char_u *s;
19756
19757 for (s = res; *s; ++s)
19758 {
19759 if (*s == CAR)
19760 *s = NL;
19761 }
19762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763#else
19764# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019765 /* translate <CR><NL> into <NL> */
19766 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019768 char_u *s, *d;
19769
19770 d = res;
19771 for (s = res; *s; ++s)
19772 {
19773 if (s[0] == CAR && s[1] == NL)
19774 ++s;
19775 *d++ = *s;
19776 }
19777 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779# endif
19780#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019781 rettv->vval.v_string = res;
19782 res = NULL;
19783 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019784
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019785errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019786 if (infile != NULL)
19787 {
19788 mch_remove(infile);
19789 vim_free(infile);
19790 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019791 if (res != NULL)
19792 vim_free(res);
19793 if (list != NULL)
19794 list_free(list, TRUE);
19795}
19796
19797/*
19798 * "system()" function
19799 */
19800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019801f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019802{
19803 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19804}
19805
19806/*
19807 * "systemlist()" function
19808 */
19809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019810f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019811{
19812 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813}
19814
19815/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019816 * "tabpagebuflist()" function
19817 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019819f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019820{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019821#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019822 tabpage_T *tp;
19823 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019824
19825 if (argvars[0].v_type == VAR_UNKNOWN)
19826 wp = firstwin;
19827 else
19828 {
19829 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19830 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019831 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019832 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019833 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019834 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019835 for (; wp != NULL; wp = wp->w_next)
19836 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019837 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019838 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019839 }
19840#endif
19841}
19842
19843
19844/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019845 * "tabpagenr()" function
19846 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019848f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019849{
19850 int nr = 1;
19851#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019852 char_u *arg;
19853
19854 if (argvars[0].v_type != VAR_UNKNOWN)
19855 {
19856 arg = get_tv_string_chk(&argvars[0]);
19857 nr = 0;
19858 if (arg != NULL)
19859 {
19860 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019861 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019862 else
19863 EMSG2(_(e_invexpr2), arg);
19864 }
19865 }
19866 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019867 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019868#endif
19869 rettv->vval.v_number = nr;
19870}
19871
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019872
19873#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019874static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019875
19876/*
19877 * Common code for tabpagewinnr() and winnr().
19878 */
19879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019880get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019881{
19882 win_T *twin;
19883 int nr = 1;
19884 win_T *wp;
19885 char_u *arg;
19886
19887 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19888 if (argvar->v_type != VAR_UNKNOWN)
19889 {
19890 arg = get_tv_string_chk(argvar);
19891 if (arg == NULL)
19892 nr = 0; /* type error; errmsg already given */
19893 else if (STRCMP(arg, "$") == 0)
19894 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19895 else if (STRCMP(arg, "#") == 0)
19896 {
19897 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19898 if (twin == NULL)
19899 nr = 0;
19900 }
19901 else
19902 {
19903 EMSG2(_(e_invexpr2), arg);
19904 nr = 0;
19905 }
19906 }
19907
19908 if (nr > 0)
19909 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19910 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019911 {
19912 if (wp == NULL)
19913 {
19914 /* didn't find it in this tabpage */
19915 nr = 0;
19916 break;
19917 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019918 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019919 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019920 return nr;
19921}
19922#endif
19923
19924/*
19925 * "tabpagewinnr()" function
19926 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019928f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019929{
19930 int nr = 1;
19931#ifdef FEAT_WINDOWS
19932 tabpage_T *tp;
19933
19934 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19935 if (tp == NULL)
19936 nr = 0;
19937 else
19938 nr = get_winnr(tp, &argvars[1]);
19939#endif
19940 rettv->vval.v_number = nr;
19941}
19942
19943
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019944/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019945 * "tagfiles()" function
19946 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019948f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019949{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019950 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019951 tagname_T tn;
19952 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019953
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019954 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019955 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019956 fname = alloc(MAXPATHL);
19957 if (fname == NULL)
19958 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019959
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019960 for (first = TRUE; ; first = FALSE)
19961 if (get_tagfname(&tn, first, fname) == FAIL
19962 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019963 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019964 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019965 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019966}
19967
19968/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019969 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019970 */
19971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019972f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019973{
19974 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019975
19976 tag_pattern = get_tv_string(&argvars[0]);
19977
19978 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019979 if (*tag_pattern == NUL)
19980 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019981
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019982 if (rettv_list_alloc(rettv) == OK)
19983 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019984}
19985
19986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 * "tempname()" function
19988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019990f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991{
19992 static int x = 'A';
19993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019995 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996
19997 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19998 * names. Skip 'I' and 'O', they are used for shell redirection. */
19999 do
20000 {
20001 if (x == 'Z')
20002 x = '0';
20003 else if (x == '9')
20004 x = 'A';
20005 else
20006 {
20007#ifdef EBCDIC
20008 if (x == 'I')
20009 x = 'J';
20010 else if (x == 'R')
20011 x = 'S';
20012 else
20013#endif
20014 ++x;
20015 }
20016 } while (x == 'I' || x == 'O');
20017}
20018
20019/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020020 * "test(list)" function: Just checking the walls...
20021 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020023f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020024{
20025 /* Used for unit testing. Change the code below to your liking. */
20026#if 0
20027 listitem_T *li;
20028 list_T *l;
20029 char_u *bad, *good;
20030
20031 if (argvars[0].v_type != VAR_LIST)
20032 return;
20033 l = argvars[0].vval.v_list;
20034 if (l == NULL)
20035 return;
20036 li = l->lv_first;
20037 if (li == NULL)
20038 return;
20039 bad = get_tv_string(&li->li_tv);
20040 li = li->li_next;
20041 if (li == NULL)
20042 return;
20043 good = get_tv_string(&li->li_tv);
20044 rettv->vval.v_number = test_edit_score(bad, good);
20045#endif
20046}
20047
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020048#ifdef FEAT_FLOAT
20049/*
20050 * "tan()" function
20051 */
20052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020053f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020054{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020055 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020056
20057 rettv->v_type = VAR_FLOAT;
20058 if (get_float_arg(argvars, &f) == OK)
20059 rettv->vval.v_float = tan(f);
20060 else
20061 rettv->vval.v_float = 0.0;
20062}
20063
20064/*
20065 * "tanh()" function
20066 */
20067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020068f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020069{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020070 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020071
20072 rettv->v_type = VAR_FLOAT;
20073 if (get_float_arg(argvars, &f) == OK)
20074 rettv->vval.v_float = tanh(f);
20075 else
20076 rettv->vval.v_float = 0.0;
20077}
20078#endif
20079
Bram Moolenaard52d9742005-08-21 22:20:28 +000020080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 * "tolower(string)" function
20082 */
20083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020084f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085{
20086 char_u *p;
20087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088 p = vim_strsave(get_tv_string(&argvars[0]));
20089 rettv->v_type = VAR_STRING;
20090 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091
20092 if (p != NULL)
20093 while (*p != NUL)
20094 {
20095#ifdef FEAT_MBYTE
20096 int l;
20097
20098 if (enc_utf8)
20099 {
20100 int c, lc;
20101
20102 c = utf_ptr2char(p);
20103 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020104 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 /* TODO: reallocate string when byte count changes. */
20106 if (utf_char2len(lc) == l)
20107 utf_char2bytes(lc, p);
20108 p += l;
20109 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020110 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 p += l; /* skip multi-byte character */
20112 else
20113#endif
20114 {
20115 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20116 ++p;
20117 }
20118 }
20119}
20120
20121/*
20122 * "toupper(string)" function
20123 */
20124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020125f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020127 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020128 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129}
20130
20131/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020132 * "tr(string, fromstr, tostr)" function
20133 */
20134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020135f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020136{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020137 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020138 char_u *fromstr;
20139 char_u *tostr;
20140 char_u *p;
20141#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020142 int inlen;
20143 int fromlen;
20144 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020145 int idx;
20146 char_u *cpstr;
20147 int cplen;
20148 int first = TRUE;
20149#endif
20150 char_u buf[NUMBUFLEN];
20151 char_u buf2[NUMBUFLEN];
20152 garray_T ga;
20153
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020154 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020155 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20156 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020157
20158 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020159 rettv->v_type = VAR_STRING;
20160 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020161 if (fromstr == NULL || tostr == NULL)
20162 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020163 ga_init2(&ga, (int)sizeof(char), 80);
20164
20165#ifdef FEAT_MBYTE
20166 if (!has_mbyte)
20167#endif
20168 /* not multi-byte: fromstr and tostr must be the same length */
20169 if (STRLEN(fromstr) != STRLEN(tostr))
20170 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020171#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020172error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020173#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020174 EMSG2(_(e_invarg2), fromstr);
20175 ga_clear(&ga);
20176 return;
20177 }
20178
20179 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020180 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020181 {
20182#ifdef FEAT_MBYTE
20183 if (has_mbyte)
20184 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020185 inlen = (*mb_ptr2len)(in_str);
20186 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020187 cplen = inlen;
20188 idx = 0;
20189 for (p = fromstr; *p != NUL; p += fromlen)
20190 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020191 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020192 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020193 {
20194 for (p = tostr; *p != NUL; p += tolen)
20195 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020196 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020197 if (idx-- == 0)
20198 {
20199 cplen = tolen;
20200 cpstr = p;
20201 break;
20202 }
20203 }
20204 if (*p == NUL) /* tostr is shorter than fromstr */
20205 goto error;
20206 break;
20207 }
20208 ++idx;
20209 }
20210
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020211 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020212 {
20213 /* Check that fromstr and tostr have the same number of
20214 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020215 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020216 first = FALSE;
20217 for (p = tostr; *p != NUL; p += tolen)
20218 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020219 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020220 --idx;
20221 }
20222 if (idx != 0)
20223 goto error;
20224 }
20225
Bram Moolenaarcde88542015-08-11 19:14:00 +020020226 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020227 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020228 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020229
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020230 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020231 }
20232 else
20233#endif
20234 {
20235 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020236 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020237 if (p != NULL)
20238 ga_append(&ga, tostr[p - fromstr]);
20239 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020240 ga_append(&ga, *in_str);
20241 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020242 }
20243 }
20244
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020245 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020246 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020247 ga_append(&ga, NUL);
20248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020250}
20251
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020252#ifdef FEAT_FLOAT
20253/*
20254 * "trunc({float})" function
20255 */
20256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020257f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020258{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020259 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020260
20261 rettv->v_type = VAR_FLOAT;
20262 if (get_float_arg(argvars, &f) == OK)
20263 /* trunc() is not in C90, use floor() or ceil() instead. */
20264 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20265 else
20266 rettv->vval.v_float = 0.0;
20267}
20268#endif
20269
Bram Moolenaar8299df92004-07-10 09:47:34 +000020270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 * "type(expr)" function
20272 */
20273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020274f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020276 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020277
20278 switch (argvars[0].v_type)
20279 {
20280 case VAR_NUMBER: n = 0; break;
20281 case VAR_STRING: n = 1; break;
20282 case VAR_FUNC: n = 2; break;
20283 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020284 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020285 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020286 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020287 if (argvars[0].vval.v_number == VVAL_FALSE
20288 || argvars[0].vval.v_number == VVAL_TRUE)
20289 n = 6;
20290 else
20291 n = 7;
20292 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020293 case VAR_JOB: n = 8; break;
20294 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020295 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020296 case VAR_UNKNOWN:
20297 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20298 n = -1;
20299 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020300 }
20301 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302}
20303
20304/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020305 * "undofile(name)" function
20306 */
20307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020308f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020309{
20310 rettv->v_type = VAR_STRING;
20311#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020312 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020313 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020314
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020315 if (*fname == NUL)
20316 {
20317 /* If there is no file name there will be no undo file. */
20318 rettv->vval.v_string = NULL;
20319 }
20320 else
20321 {
20322 char_u *ffname = FullName_save(fname, FALSE);
20323
20324 if (ffname != NULL)
20325 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20326 vim_free(ffname);
20327 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020328 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020329#else
20330 rettv->vval.v_string = NULL;
20331#endif
20332}
20333
20334/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020335 * "undotree()" function
20336 */
20337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020338f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020339{
20340 if (rettv_dict_alloc(rettv) == OK)
20341 {
20342 dict_T *dict = rettv->vval.v_dict;
20343 list_T *list;
20344
Bram Moolenaar730cde92010-06-27 05:18:54 +020020345 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020346 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020347 dict_add_nr_str(dict, "save_last",
20348 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020349 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20350 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020351 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020352
20353 list = list_alloc();
20354 if (list != NULL)
20355 {
20356 u_eval_tree(curbuf->b_u_oldhead, list);
20357 dict_add_list(dict, "entries", list);
20358 }
20359 }
20360}
20361
20362/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020363 * "values(dict)" function
20364 */
20365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020366f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020367{
20368 dict_list(argvars, rettv, 1);
20369}
20370
20371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 * "virtcol(string)" function
20373 */
20374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020375f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376{
20377 colnr_T vcol = 0;
20378 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020379 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020381 fp = var2fpos(&argvars[0], FALSE, &fnum);
20382 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20383 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 {
20385 getvvcol(curwin, fp, NULL, NULL, &vcol);
20386 ++vcol;
20387 }
20388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020389 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390}
20391
20392/*
20393 * "visualmode()" function
20394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020396f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 char_u str[2];
20399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020400 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 str[0] = curbuf->b_visual_mode_eval;
20402 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020403 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404
20405 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020406 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408}
20409
20410/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020411 * "wildmenumode()" function
20412 */
20413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020414f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020415{
20416#ifdef FEAT_WILDMENU
20417 if (wild_menu_showing)
20418 rettv->vval.v_number = 1;
20419#endif
20420}
20421
20422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 * "winbufnr(nr)" function
20424 */
20425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020426f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427{
20428 win_T *wp;
20429
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020430 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020432 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020434 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435}
20436
20437/*
20438 * "wincol()" function
20439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020441f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442{
20443 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020444 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445}
20446
20447/*
20448 * "winheight(nr)" function
20449 */
20450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452{
20453 win_T *wp;
20454
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020455 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020459 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460}
20461
20462/*
20463 * "winline()" function
20464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467{
20468 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020469 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470}
20471
20472/*
20473 * "winnr()" function
20474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020476f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477{
20478 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020479
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020481 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020483 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484}
20485
20486/*
20487 * "winrestcmd()" function
20488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020490f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491{
20492#ifdef FEAT_WINDOWS
20493 win_T *wp;
20494 int winnr = 1;
20495 garray_T ga;
20496 char_u buf[50];
20497
20498 ga_init2(&ga, (int)sizeof(char), 70);
20499 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20500 {
20501 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20502 ga_concat(&ga, buf);
20503# ifdef FEAT_VERTSPLIT
20504 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20505 ga_concat(&ga, buf);
20506# endif
20507 ++winnr;
20508 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020509 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020511 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020513 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020515 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516}
20517
20518/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020519 * "winrestview()" function
20520 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020522f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020523{
20524 dict_T *dict;
20525
20526 if (argvars[0].v_type != VAR_DICT
20527 || (dict = argvars[0].vval.v_dict) == NULL)
20528 EMSG(_(e_invarg));
20529 else
20530 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020531 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20532 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20533 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20534 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020535#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020536 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20537 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020538#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020539 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20540 {
20541 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20542 curwin->w_set_curswant = FALSE;
20543 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020544
Bram Moolenaar82c25852014-05-28 16:47:16 +020020545 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20546 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020547#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020548 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20549 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020550#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020551 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20552 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20553 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20554 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020555
20556 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020557 win_new_height(curwin, curwin->w_height);
20558# ifdef FEAT_VERTSPLIT
20559 win_new_width(curwin, W_WIDTH(curwin));
20560# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020561 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020562
Bram Moolenaarb851a962014-10-31 15:45:52 +010020563 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020564 curwin->w_topline = 1;
20565 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20566 curwin->w_topline = curbuf->b_ml.ml_line_count;
20567#ifdef FEAT_DIFF
20568 check_topfill(curwin, TRUE);
20569#endif
20570 }
20571}
20572
20573/*
20574 * "winsaveview()" function
20575 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020577f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020578{
20579 dict_T *dict;
20580
Bram Moolenaara800b422010-06-27 01:15:55 +020020581 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020582 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020583 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020584
20585 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20586 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20587#ifdef FEAT_VIRTUALEDIT
20588 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20589#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020590 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020591 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20592
20593 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20594#ifdef FEAT_DIFF
20595 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20596#endif
20597 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20598 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20599}
20600
20601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 * "winwidth(nr)" function
20603 */
20604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020605f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606{
20607 win_T *wp;
20608
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020609 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 else
20613#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020614 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020616 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617#endif
20618}
20619
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020621 * "wordcount()" function
20622 */
20623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020624f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020625{
20626 if (rettv_dict_alloc(rettv) == FAIL)
20627 return;
20628 cursor_pos_info(rettv->vval.v_dict);
20629}
20630
20631/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020632 * Write list of strings to file
20633 */
20634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020635write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020636{
20637 listitem_T *li;
20638 int c;
20639 int ret = OK;
20640 char_u *s;
20641
20642 for (li = list->lv_first; li != NULL; li = li->li_next)
20643 {
20644 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20645 {
20646 if (*s == '\n')
20647 c = putc(NUL, fd);
20648 else
20649 c = putc(*s, fd);
20650 if (c == EOF)
20651 {
20652 ret = FAIL;
20653 break;
20654 }
20655 }
20656 if (!binary || li->li_next != NULL)
20657 if (putc('\n', fd) == EOF)
20658 {
20659 ret = FAIL;
20660 break;
20661 }
20662 if (ret == FAIL)
20663 {
20664 EMSG(_(e_write));
20665 break;
20666 }
20667 }
20668 return ret;
20669}
20670
20671/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020672 * "writefile()" function
20673 */
20674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020675f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020676{
20677 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020678 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020679 char_u *fname;
20680 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020681 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020682
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020683 if (check_restricted() || check_secure())
20684 return;
20685
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020686 if (argvars[0].v_type != VAR_LIST)
20687 {
20688 EMSG2(_(e_listarg), "writefile()");
20689 return;
20690 }
20691 if (argvars[0].vval.v_list == NULL)
20692 return;
20693
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020694 if (argvars[2].v_type != VAR_UNKNOWN)
20695 {
20696 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20697 binary = TRUE;
20698 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20699 append = TRUE;
20700 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020701
20702 /* Always open the file in binary mode, library functions have a mind of
20703 * their own about CR-LF conversion. */
20704 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020705 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20706 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020707 {
20708 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20709 ret = -1;
20710 }
20711 else
20712 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020713 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20714 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020715 fclose(fd);
20716 }
20717
20718 rettv->vval.v_number = ret;
20719}
20720
20721/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020722 * "xor(expr, expr)" function
20723 */
20724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020725f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020726{
20727 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20728 ^ get_tv_number_chk(&argvars[1], NULL);
20729}
20730
20731
20732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020734 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 */
20736 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020737var2fpos(
20738 typval_T *varp,
20739 int dollar_lnum, /* TRUE when $ is last line */
20740 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020742 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020744 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745
Bram Moolenaara5525202006-03-02 22:52:09 +000020746 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020747 if (varp->v_type == VAR_LIST)
20748 {
20749 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020750 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020751 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020752 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020753
20754 l = varp->vval.v_list;
20755 if (l == NULL)
20756 return NULL;
20757
20758 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020759 pos.lnum = list_find_nr(l, 0L, &error);
20760 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020761 return NULL; /* invalid line number */
20762
20763 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020764 pos.col = list_find_nr(l, 1L, &error);
20765 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020766 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020767 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020768
20769 /* We accept "$" for the column number: last column. */
20770 li = list_find(l, 1L);
20771 if (li != NULL && li->li_tv.v_type == VAR_STRING
20772 && li->li_tv.vval.v_string != NULL
20773 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20774 pos.col = len + 1;
20775
Bram Moolenaara5525202006-03-02 22:52:09 +000020776 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020777 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020778 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020779 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020780
Bram Moolenaara5525202006-03-02 22:52:09 +000020781#ifdef FEAT_VIRTUALEDIT
20782 /* Get the virtual offset. Defaults to zero. */
20783 pos.coladd = list_find_nr(l, 2L, &error);
20784 if (error)
20785 pos.coladd = 0;
20786#endif
20787
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020788 return &pos;
20789 }
20790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020791 name = get_tv_string_chk(varp);
20792 if (name == NULL)
20793 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020794 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020796 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20797 {
20798 if (VIsual_active)
20799 return &VIsual;
20800 return &curwin->w_cursor;
20801 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020802 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020804 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20806 return NULL;
20807 return pp;
20808 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020809
20810#ifdef FEAT_VIRTUALEDIT
20811 pos.coladd = 0;
20812#endif
20813
Bram Moolenaar477933c2007-07-17 14:32:23 +000020814 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020815 {
20816 pos.col = 0;
20817 if (name[1] == '0') /* "w0": first visible line */
20818 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020819 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020820 pos.lnum = curwin->w_topline;
20821 return &pos;
20822 }
20823 else if (name[1] == '$') /* "w$": last visible line */
20824 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020825 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020826 pos.lnum = curwin->w_botline - 1;
20827 return &pos;
20828 }
20829 }
20830 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020832 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 {
20834 pos.lnum = curbuf->b_ml.ml_line_count;
20835 pos.col = 0;
20836 }
20837 else
20838 {
20839 pos.lnum = curwin->w_cursor.lnum;
20840 pos.col = (colnr_T)STRLEN(ml_get_curline());
20841 }
20842 return &pos;
20843 }
20844 return NULL;
20845}
20846
20847/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020848 * Convert list in "arg" into a position and optional file number.
20849 * When "fnump" is NULL there is no file number, only 3 items.
20850 * Note that the column is passed on as-is, the caller may want to decrement
20851 * it to use 1 for the first column.
20852 * Return FAIL when conversion is not possible, doesn't check the position for
20853 * validity.
20854 */
20855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020856list2fpos(
20857 typval_T *arg,
20858 pos_T *posp,
20859 int *fnump,
20860 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020861{
20862 list_T *l = arg->vval.v_list;
20863 long i = 0;
20864 long n;
20865
Bram Moolenaar493c1782014-05-28 14:34:46 +020020866 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20867 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020868 if (arg->v_type != VAR_LIST
20869 || l == NULL
20870 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020871 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020872 return FAIL;
20873
20874 if (fnump != NULL)
20875 {
20876 n = list_find_nr(l, i++, NULL); /* fnum */
20877 if (n < 0)
20878 return FAIL;
20879 if (n == 0)
20880 n = curbuf->b_fnum; /* current buffer */
20881 *fnump = n;
20882 }
20883
20884 n = list_find_nr(l, i++, NULL); /* lnum */
20885 if (n < 0)
20886 return FAIL;
20887 posp->lnum = n;
20888
20889 n = list_find_nr(l, i++, NULL); /* col */
20890 if (n < 0)
20891 return FAIL;
20892 posp->col = n;
20893
20894#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020895 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020896 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020897 posp->coladd = 0;
20898 else
20899 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020900#endif
20901
Bram Moolenaar493c1782014-05-28 14:34:46 +020020902 if (curswantp != NULL)
20903 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20904
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020905 return OK;
20906}
20907
20908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 * Get the length of an environment variable name.
20910 * Advance "arg" to the first character after the name.
20911 * Return 0 for error.
20912 */
20913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020914get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915{
20916 char_u *p;
20917 int len;
20918
20919 for (p = *arg; vim_isIDc(*p); ++p)
20920 ;
20921 if (p == *arg) /* no name found */
20922 return 0;
20923
20924 len = (int)(p - *arg);
20925 *arg = p;
20926 return len;
20927}
20928
20929/*
20930 * Get the length of the name of a function or internal variable.
20931 * "arg" is advanced to the first non-white character after the name.
20932 * Return 0 if something is wrong.
20933 */
20934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020935get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936{
20937 char_u *p;
20938 int len;
20939
20940 /* Find the end of the name. */
20941 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020942 {
20943 if (*p == ':')
20944 {
20945 /* "s:" is start of "s:var", but "n:" is not and can be used in
20946 * slice "[n:]". Also "xx:" is not a namespace. */
20947 len = (int)(p - *arg);
20948 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20949 || len > 1)
20950 break;
20951 }
20952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 if (p == *arg) /* no name found */
20954 return 0;
20955
20956 len = (int)(p - *arg);
20957 *arg = skipwhite(p);
20958
20959 return len;
20960}
20961
20962/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020963 * Get the length of the name of a variable or function.
20964 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020966 * Return -1 if curly braces expansion failed.
20967 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 * If the name contains 'magic' {}'s, expand them and return the
20969 * expanded name in an allocated string via 'alias' - caller must free.
20970 */
20971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020972get_name_len(
20973 char_u **arg,
20974 char_u **alias,
20975 int evaluate,
20976 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977{
20978 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 char_u *p;
20980 char_u *expr_start;
20981 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982
20983 *alias = NULL; /* default to no alias */
20984
20985 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20986 && (*arg)[2] == (int)KE_SNR)
20987 {
20988 /* hard coded <SNR>, already translated */
20989 *arg += 3;
20990 return get_id_len(arg) + 3;
20991 }
20992 len = eval_fname_script(*arg);
20993 if (len > 0)
20994 {
20995 /* literal "<SID>", "s:" or "<SNR>" */
20996 *arg += len;
20997 }
20998
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021000 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021002 p = find_name_end(*arg, &expr_start, &expr_end,
21003 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 if (expr_start != NULL)
21005 {
21006 char_u *temp_string;
21007
21008 if (!evaluate)
21009 {
21010 len += (int)(p - *arg);
21011 *arg = skipwhite(p);
21012 return len;
21013 }
21014
21015 /*
21016 * Include any <SID> etc in the expanded string:
21017 * Thus the -len here.
21018 */
21019 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21020 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021021 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 *alias = temp_string;
21023 *arg = skipwhite(p);
21024 return (int)STRLEN(temp_string);
21025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026
21027 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021028 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 EMSG2(_(e_invexpr2), *arg);
21030
21031 return len;
21032}
21033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034/*
21035 * Find the end of a variable or function name, taking care of magic braces.
21036 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21037 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021038 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021039 * Return a pointer to just after the name. Equal to "arg" if there is no
21040 * valid name.
21041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021043find_name_end(
21044 char_u *arg,
21045 char_u **expr_start,
21046 char_u **expr_end,
21047 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021049 int mb_nest = 0;
21050 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021052 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021054 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021056 *expr_start = NULL;
21057 *expr_end = NULL;
21058 }
21059
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021060 /* Quick check for valid starting character. */
21061 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21062 return arg;
21063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021064 for (p = arg; *p != NUL
21065 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021066 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021067 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021068 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021069 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021070 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021071 if (*p == '\'')
21072 {
21073 /* skip over 'string' to avoid counting [ and ] inside it. */
21074 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21075 ;
21076 if (*p == NUL)
21077 break;
21078 }
21079 else if (*p == '"')
21080 {
21081 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21082 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21083 if (*p == '\\' && p[1] != NUL)
21084 ++p;
21085 if (*p == NUL)
21086 break;
21087 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021088 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21089 {
21090 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021091 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021092 len = (int)(p - arg);
21093 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021094 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021095 break;
21096 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021098 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021100 if (*p == '[')
21101 ++br_nest;
21102 else if (*p == ']')
21103 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021106 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021108 if (*p == '{')
21109 {
21110 mb_nest++;
21111 if (expr_start != NULL && *expr_start == NULL)
21112 *expr_start = p;
21113 }
21114 else if (*p == '}')
21115 {
21116 mb_nest--;
21117 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21118 *expr_end = p;
21119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 }
21122
21123 return p;
21124}
21125
21126/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021127 * Expands out the 'magic' {}'s in a variable/function name.
21128 * Note that this can call itself recursively, to deal with
21129 * constructs like foo{bar}{baz}{bam}
21130 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21131 * "in_start" ^
21132 * "expr_start" ^
21133 * "expr_end" ^
21134 * "in_end" ^
21135 *
21136 * Returns a new allocated string, which the caller must free.
21137 * Returns NULL for failure.
21138 */
21139 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021140make_expanded_name(
21141 char_u *in_start,
21142 char_u *expr_start,
21143 char_u *expr_end,
21144 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021145{
21146 char_u c1;
21147 char_u *retval = NULL;
21148 char_u *temp_result;
21149 char_u *nextcmd = NULL;
21150
21151 if (expr_end == NULL || in_end == NULL)
21152 return NULL;
21153 *expr_start = NUL;
21154 *expr_end = NUL;
21155 c1 = *in_end;
21156 *in_end = NUL;
21157
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021158 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021159 if (temp_result != NULL && nextcmd == NULL)
21160 {
21161 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21162 + (in_end - expr_end) + 1));
21163 if (retval != NULL)
21164 {
21165 STRCPY(retval, in_start);
21166 STRCAT(retval, temp_result);
21167 STRCAT(retval, expr_end + 1);
21168 }
21169 }
21170 vim_free(temp_result);
21171
21172 *in_end = c1; /* put char back for error messages */
21173 *expr_start = '{';
21174 *expr_end = '}';
21175
21176 if (retval != NULL)
21177 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021178 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021179 if (expr_start != NULL)
21180 {
21181 /* Further expansion! */
21182 temp_result = make_expanded_name(retval, expr_start,
21183 expr_end, temp_result);
21184 vim_free(retval);
21185 retval = temp_result;
21186 }
21187 }
21188
21189 return retval;
21190}
21191
21192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021194 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 */
21196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021197eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021199 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21200}
21201
21202/*
21203 * Return TRUE if character "c" can be used as the first character in a
21204 * variable or function name (excluding '{' and '}').
21205 */
21206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021207eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021208{
21209 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210}
21211
21212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 * Set number v: variable to "val".
21214 */
21215 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021216set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021218 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219}
21220
21221/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021222 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 */
21224 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021225get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021227 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228}
21229
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021230/*
21231 * Get string v: variable value. Uses a static buffer, can only be used once.
21232 */
21233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021234get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021235{
21236 return get_tv_string(&vimvars[idx].vv_tv);
21237}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021238
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021240 * Get List v: variable value. Caller must take care of reference count when
21241 * needed.
21242 */
21243 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021244get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021245{
21246 return vimvars[idx].vv_list;
21247}
21248
21249/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021250 * Set v:char to character "c".
21251 */
21252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021253set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021254{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021255 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021256
21257#ifdef FEAT_MBYTE
21258 if (has_mbyte)
21259 buf[(*mb_char2bytes)(c, buf)] = NUL;
21260 else
21261#endif
21262 {
21263 buf[0] = c;
21264 buf[1] = NUL;
21265 }
21266 set_vim_var_string(VV_CHAR, buf, -1);
21267}
21268
21269/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021270 * Set v:count to "count" and v:count1 to "count1".
21271 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 */
21273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021274set_vcount(
21275 long count,
21276 long count1,
21277 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021279 if (set_prevcount)
21280 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021281 vimvars[VV_COUNT].vv_nr = count;
21282 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283}
21284
21285/*
21286 * Set string v: variable to a copy of "val".
21287 */
21288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021289set_vim_var_string(
21290 int idx,
21291 char_u *val,
21292 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293{
Bram Moolenaara542c682016-01-31 16:28:04 +010021294 clear_tv(&vimvars[idx].vv_di.di_tv);
21295 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021297 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021299 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021301 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302}
21303
21304/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021305 * Set List v: variable to "val".
21306 */
21307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021308set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021309{
Bram Moolenaara542c682016-01-31 16:28:04 +010021310 clear_tv(&vimvars[idx].vv_di.di_tv);
21311 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021312 vimvars[idx].vv_list = val;
21313 if (val != NULL)
21314 ++val->lv_refcount;
21315}
21316
21317/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021318 * Set Dictionary v: variable to "val".
21319 */
21320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021321set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021322{
21323 int todo;
21324 hashitem_T *hi;
21325
Bram Moolenaara542c682016-01-31 16:28:04 +010021326 clear_tv(&vimvars[idx].vv_di.di_tv);
21327 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021328 vimvars[idx].vv_dict = val;
21329 if (val != NULL)
21330 {
21331 ++val->dv_refcount;
21332
21333 /* Set readonly */
21334 todo = (int)val->dv_hashtab.ht_used;
21335 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21336 {
21337 if (HASHITEM_EMPTY(hi))
21338 continue;
21339 --todo;
21340 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21341 }
21342 }
21343}
21344
21345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 * Set v:register if needed.
21347 */
21348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021349set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350{
21351 char_u regname;
21352
21353 if (c == 0 || c == ' ')
21354 regname = '"';
21355 else
21356 regname = c;
21357 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021358 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 set_vim_var_string(VV_REG, &regname, 1);
21360}
21361
21362/*
21363 * Get or set v:exception. If "oldval" == NULL, return the current value.
21364 * Otherwise, restore the value to "oldval" and return NULL.
21365 * Must always be called in pairs to save and restore v:exception! Does not
21366 * take care of memory allocations.
21367 */
21368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021369v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370{
21371 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021372 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373
Bram Moolenaare9a41262005-01-15 22:18:47 +000021374 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 return NULL;
21376}
21377
21378/*
21379 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21380 * Otherwise, restore the value to "oldval" and return NULL.
21381 * Must always be called in pairs to save and restore v:throwpoint! Does not
21382 * take care of memory allocations.
21383 */
21384 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021385v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386{
21387 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021388 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389
Bram Moolenaare9a41262005-01-15 22:18:47 +000021390 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 return NULL;
21392}
21393
21394#if defined(FEAT_AUTOCMD) || defined(PROTO)
21395/*
21396 * Set v:cmdarg.
21397 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21398 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21399 * Must always be called in pairs!
21400 */
21401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021402set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403{
21404 char_u *oldval;
21405 char_u *newval;
21406 unsigned len;
21407
Bram Moolenaare9a41262005-01-15 22:18:47 +000021408 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021409 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021411 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021412 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021413 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 }
21415
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021416 if (eap->force_bin == FORCE_BIN)
21417 len = 6;
21418 else if (eap->force_bin == FORCE_NOBIN)
21419 len = 8;
21420 else
21421 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021422
21423 if (eap->read_edit)
21424 len += 7;
21425
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021426 if (eap->force_ff != 0)
21427 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21428# ifdef FEAT_MBYTE
21429 if (eap->force_enc != 0)
21430 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021431 if (eap->bad_char != 0)
21432 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021433# endif
21434
21435 newval = alloc(len + 1);
21436 if (newval == NULL)
21437 return NULL;
21438
21439 if (eap->force_bin == FORCE_BIN)
21440 sprintf((char *)newval, " ++bin");
21441 else if (eap->force_bin == FORCE_NOBIN)
21442 sprintf((char *)newval, " ++nobin");
21443 else
21444 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021445
21446 if (eap->read_edit)
21447 STRCAT(newval, " ++edit");
21448
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021449 if (eap->force_ff != 0)
21450 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21451 eap->cmd + eap->force_ff);
21452# ifdef FEAT_MBYTE
21453 if (eap->force_enc != 0)
21454 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21455 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021456 if (eap->bad_char == BAD_KEEP)
21457 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21458 else if (eap->bad_char == BAD_DROP)
21459 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21460 else if (eap->bad_char != 0)
21461 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021462# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021463 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021464 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465}
21466#endif
21467
21468/*
21469 * Get the value of internal variable "name".
21470 * Return OK or FAIL.
21471 */
21472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021473get_var_tv(
21474 char_u *name,
21475 int len, /* length of "name" */
21476 typval_T *rettv, /* NULL when only checking existence */
21477 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21478 int verbose, /* may give error message */
21479 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480{
21481 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021482 typval_T *tv = NULL;
21483 typval_T atv;
21484 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486
21487 /* truncate the name, so that we can use strcmp() */
21488 cc = name[len];
21489 name[len] = NUL;
21490
21491 /*
21492 * Check for "b:changedtick".
21493 */
21494 if (STRCMP(name, "b:changedtick") == 0)
21495 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021496 atv.v_type = VAR_NUMBER;
21497 atv.vval.v_number = curbuf->b_changedtick;
21498 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 }
21500
21501 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 * Check for user-defined variables.
21503 */
21504 else
21505 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021506 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021508 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021509 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021510 if (dip != NULL)
21511 *dip = v;
21512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 }
21514
Bram Moolenaare9a41262005-01-15 22:18:47 +000021515 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021517 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021518 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 ret = FAIL;
21520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021521 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021522 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523
21524 name[len] = cc;
21525
21526 return ret;
21527}
21528
21529/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021530 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21531 * Also handle function call with Funcref variable: func(expr)
21532 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21533 */
21534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021535handle_subscript(
21536 char_u **arg,
21537 typval_T *rettv,
21538 int evaluate, /* do more than finding the end */
21539 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021540{
21541 int ret = OK;
21542 dict_T *selfdict = NULL;
21543 char_u *s;
21544 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021545 typval_T functv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021546 partial_T *pt = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021547
21548 while (ret == OK
21549 && (**arg == '['
21550 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021551 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21552 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021553 && !vim_iswhite(*(*arg - 1)))
21554 {
21555 if (**arg == '(')
21556 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021557 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021558 if (evaluate)
21559 {
21560 functv = *rettv;
21561 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021562
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021563 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021564 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021565 {
21566 pt = functv.vval.v_partial;
21567 s = pt->pt_name;
21568 }
21569 else
21570 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021571 }
21572 else
21573 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021574 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021575 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021576 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021577
21578 /* Clear the funcref afterwards, so that deleting it while
21579 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021580 if (evaluate)
21581 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021582
21583 /* Stop the expression evaluation when immediately aborting on
21584 * error, or when an interrupt occurred or an exception was thrown
21585 * but not caught. */
21586 if (aborting())
21587 {
21588 if (ret == OK)
21589 clear_tv(rettv);
21590 ret = FAIL;
21591 }
21592 dict_unref(selfdict);
21593 selfdict = NULL;
21594 }
21595 else /* **arg == '[' || **arg == '.' */
21596 {
21597 dict_unref(selfdict);
21598 if (rettv->v_type == VAR_DICT)
21599 {
21600 selfdict = rettv->vval.v_dict;
21601 if (selfdict != NULL)
21602 ++selfdict->dv_refcount;
21603 }
21604 else
21605 selfdict = NULL;
21606 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21607 {
21608 clear_tv(rettv);
21609 ret = FAIL;
21610 }
21611 }
21612 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021613
21614 if (rettv->v_type == VAR_FUNC && selfdict != NULL)
21615 {
21616 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21617
21618 /* Turn "dict.Func" into a partial for "Func" with "dict". */
21619 if (pt != NULL)
21620 {
21621 pt->pt_dict = selfdict;
21622 selfdict = NULL;
21623 pt->pt_name = rettv->vval.v_string;
21624 func_ref(pt->pt_name);
21625 rettv->v_type = VAR_PARTIAL;
21626 rettv->vval.v_partial = pt;
21627 }
21628 }
21629
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021630 dict_unref(selfdict);
21631 return ret;
21632}
21633
21634/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021635 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021636 * value).
21637 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021638 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021639alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640{
Bram Moolenaar33570922005-01-25 22:26:29 +000021641 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021642}
21643
21644/*
21645 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 * The string "s" must have been allocated, it is consumed.
21647 * Return NULL for out of memory, the variable otherwise.
21648 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021649 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021650alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651{
Bram Moolenaar33570922005-01-25 22:26:29 +000021652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021654 rettv = alloc_tv();
21655 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021657 rettv->v_type = VAR_STRING;
21658 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 }
21660 else
21661 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021662 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663}
21664
21665/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021666 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021669free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670{
21671 if (varp != NULL)
21672 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021673 switch (varp->v_type)
21674 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021675 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021676 func_unref(varp->vval.v_string);
21677 /*FALLTHROUGH*/
21678 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021679 vim_free(varp->vval.v_string);
21680 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021681 case VAR_PARTIAL:
21682 partial_unref(varp->vval.v_partial);
21683 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021684 case VAR_LIST:
21685 list_unref(varp->vval.v_list);
21686 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021687 case VAR_DICT:
21688 dict_unref(varp->vval.v_dict);
21689 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021690 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021691#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021692 job_unref(varp->vval.v_job);
21693 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021694#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021695 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021696#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021697 channel_unref(varp->vval.v_channel);
21698 break;
21699#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021700 case VAR_NUMBER:
21701 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021702 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021703 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021704 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 vim_free(varp);
21707 }
21708}
21709
21710/*
21711 * Free the memory for a variable value and set the value to NULL or 0.
21712 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021714clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715{
21716 if (varp != NULL)
21717 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021718 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021720 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021721 func_unref(varp->vval.v_string);
21722 /*FALLTHROUGH*/
21723 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021724 vim_free(varp->vval.v_string);
21725 varp->vval.v_string = NULL;
21726 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021727 case VAR_PARTIAL:
21728 partial_unref(varp->vval.v_partial);
21729 varp->vval.v_partial = NULL;
21730 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021731 case VAR_LIST:
21732 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021733 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021734 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021735 case VAR_DICT:
21736 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021737 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021738 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021739 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021740 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021741 varp->vval.v_number = 0;
21742 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021743 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021744#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021745 varp->vval.v_float = 0.0;
21746 break;
21747#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021748 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021749#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021750 job_unref(varp->vval.v_job);
21751 varp->vval.v_job = NULL;
21752#endif
21753 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021754 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021755#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021756 channel_unref(varp->vval.v_channel);
21757 varp->vval.v_channel = NULL;
21758#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021759 case VAR_UNKNOWN:
21760 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021762 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 }
21764}
21765
21766/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021767 * Set the value of a variable to NULL without freeing items.
21768 */
21769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021770init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021771{
21772 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021773 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021774}
21775
21776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 * Get the number value of a variable.
21778 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021779 * For incompatible types, return 0.
21780 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21781 * caller of incompatible types: it sets *denote to TRUE if "denote"
21782 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021784 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021785get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021787 int error = FALSE;
21788
21789 return get_tv_number_chk(varp, &error); /* return 0L on error */
21790}
21791
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021792 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021793get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021794{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021795 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021797 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021799 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021800 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021801 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021802#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021803 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021804 break;
21805#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021806 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021807 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021808 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021809 break;
21810 case VAR_STRING:
21811 if (varp->vval.v_string != NULL)
21812 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021813 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021814 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021815 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021816 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021817 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021818 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021819 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021820 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021821 case VAR_SPECIAL:
21822 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21823 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021824 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021825#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021826 EMSG(_("E910: Using a Job as a Number"));
21827 break;
21828#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021829 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021830#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021831 EMSG(_("E913: Using a Channel as a Number"));
21832 break;
21833#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021834 case VAR_UNKNOWN:
21835 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021838 if (denote == NULL) /* useful for values that must be unsigned */
21839 n = -1;
21840 else
21841 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021842 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843}
21844
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021845#ifdef FEAT_FLOAT
21846 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021847get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021848{
21849 switch (varp->v_type)
21850 {
21851 case VAR_NUMBER:
21852 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021853 case VAR_FLOAT:
21854 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021855 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021856 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021857 EMSG(_("E891: Using a Funcref as a Float"));
21858 break;
21859 case VAR_STRING:
21860 EMSG(_("E892: Using a String as a Float"));
21861 break;
21862 case VAR_LIST:
21863 EMSG(_("E893: Using a List as a Float"));
21864 break;
21865 case VAR_DICT:
21866 EMSG(_("E894: Using a Dictionary as a Float"));
21867 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021868 case VAR_SPECIAL:
21869 EMSG(_("E907: Using a special value as a Float"));
21870 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021871 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021872# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021873 EMSG(_("E911: Using a Job as a Float"));
21874 break;
21875# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021876 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021877# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021878 EMSG(_("E914: Using a Channel as a Float"));
21879 break;
21880# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021881 case VAR_UNKNOWN:
21882 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021883 break;
21884 }
21885 return 0;
21886}
21887#endif
21888
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021890 * Get the lnum from the first argument.
21891 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021892 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 */
21894 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021895get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896{
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 linenr_T lnum;
21899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021900 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 if (lnum == 0) /* no valid number, try using line() */
21902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021903 rettv.v_type = VAR_NUMBER;
21904 f_line(argvars, &rettv);
21905 lnum = rettv.vval.v_number;
21906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 }
21908 return lnum;
21909}
21910
21911/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021912 * Get the lnum from the first argument.
21913 * Also accepts "$", then "buf" is used.
21914 * Returns 0 on error.
21915 */
21916 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021917get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021918{
21919 if (argvars[0].v_type == VAR_STRING
21920 && argvars[0].vval.v_string != NULL
21921 && argvars[0].vval.v_string[0] == '$'
21922 && buf != NULL)
21923 return buf->b_ml.ml_line_count;
21924 return get_tv_number_chk(&argvars[0], NULL);
21925}
21926
21927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 * Get the string value of a variable.
21929 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021930 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21931 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932 * If the String variable has never been set, return an empty string.
21933 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021934 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21935 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021937 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021938get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939{
21940 static char_u mybuf[NUMBUFLEN];
21941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021942 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943}
21944
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021945 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021948 char_u *res = get_tv_string_buf_chk(varp, buf);
21949
21950 return res != NULL ? res : (char_u *)"";
21951}
21952
Bram Moolenaar7d647822014-04-05 21:28:56 +020021953/*
21954 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21955 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021956 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021957get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021958{
21959 static char_u mybuf[NUMBUFLEN];
21960
21961 return get_tv_string_buf_chk(varp, mybuf);
21962}
21963
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021964 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021965get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021966{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021967 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021969 case VAR_NUMBER:
21970 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21971 return buf;
21972 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021973 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021974 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021975 break;
21976 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021977 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021978 break;
21979 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021980 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021981 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021982 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021983#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021984 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021985 break;
21986#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021987 case VAR_STRING:
21988 if (varp->vval.v_string != NULL)
21989 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021990 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021991 case VAR_SPECIAL:
21992 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21993 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021994 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021995#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021996 {
21997 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010021998 char *status;
21999
22000 if (job == NULL)
22001 return (char_u *)"no process";
22002 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022003 : job->jv_status == JOB_ENDED ? "dead"
22004 : "run";
22005# ifdef UNIX
22006 vim_snprintf((char *)buf, NUMBUFLEN,
22007 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022008# elif defined(WIN32)
22009 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022010 "process %ld %s",
22011 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022012 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022013# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022014 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022015 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22016# endif
22017 return buf;
22018 }
22019#endif
22020 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022021 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022022#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022023 {
22024 channel_T *channel = varp->vval.v_channel;
22025 char *status = channel_status(channel);
22026
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022027 if (channel == NULL)
22028 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22029 else
22030 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022031 "channel %d %s", channel->ch_id, status);
22032 return buf;
22033 }
22034#endif
22035 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022036 case VAR_UNKNOWN:
22037 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022040 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041}
22042
22043/*
22044 * Find variable "name" in the list of variables.
22045 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022046 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022047 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022048 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022050 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022051find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022054 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055
Bram Moolenaara7043832005-01-21 11:56:39 +000022056 ht = find_var_ht(name, &varname);
22057 if (htp != NULL)
22058 *htp = ht;
22059 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022061 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062}
22063
22064/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022065 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022066 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022068 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022069find_var_in_ht(
22070 hashtab_T *ht,
22071 int htname,
22072 char_u *varname,
22073 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022074{
Bram Moolenaar33570922005-01-25 22:26:29 +000022075 hashitem_T *hi;
22076
22077 if (*varname == NUL)
22078 {
22079 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022080 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022081 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022082 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022083 case 'g': return &globvars_var;
22084 case 'v': return &vimvars_var;
22085 case 'b': return &curbuf->b_bufvar;
22086 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022087#ifdef FEAT_WINDOWS
22088 case 't': return &curtab->tp_winvar;
22089#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022090 case 'l': return current_funccal == NULL
22091 ? NULL : &current_funccal->l_vars_var;
22092 case 'a': return current_funccal == NULL
22093 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022094 }
22095 return NULL;
22096 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022097
22098 hi = hash_find(ht, varname);
22099 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022100 {
22101 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022102 * worked find the variable again. Don't auto-load a script if it was
22103 * loaded already, otherwise it would be loaded every time when
22104 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022105 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022106 {
22107 /* Note: script_autoload() may make "hi" invalid. It must either
22108 * be obtained again or not used. */
22109 if (!script_autoload(varname, FALSE) || aborting())
22110 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022111 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022112 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022113 if (HASHITEM_EMPTY(hi))
22114 return NULL;
22115 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022116 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022117}
22118
22119/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022120 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022121 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022122 * Set "varname" to the start of name without ':'.
22123 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022124 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022125find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022127 hashitem_T *hi;
22128
Bram Moolenaar73627d02015-08-11 15:46:09 +020022129 if (name[0] == NUL)
22130 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 if (name[1] != ':')
22132 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022133 /* The name must not start with a colon or #. */
22134 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 return NULL;
22136 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022137
22138 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022139 hi = hash_find(&compat_hashtab, name);
22140 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022141 return &compat_hashtab;
22142
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022144 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022145 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 }
22147 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022148 if (*name == 'g') /* global variable */
22149 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022150 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22151 */
22152 if (vim_strchr(name + 2, ':') != NULL
22153 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022154 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022156 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022158 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022159#ifdef FEAT_WINDOWS
22160 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022161 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022162#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 if (*name == 'v') /* v: variable */
22164 return &vimvarht;
22165 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022166 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022167 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022168 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 if (*name == 's' /* script variable */
22170 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22171 return &SCRIPT_VARS(current_SID);
22172 return NULL;
22173}
22174
22175/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022176 * Get function call environment based on bactrace debug level
22177 */
22178 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022179get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022180{
22181 int i;
22182 funccall_T *funccal;
22183 funccall_T *temp_funccal;
22184
22185 funccal = current_funccal;
22186 if (debug_backtrace_level > 0)
22187 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022188 for (i = 0; i < debug_backtrace_level; i++)
22189 {
22190 temp_funccal = funccal->caller;
22191 if (temp_funccal)
22192 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022193 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022194 /* backtrace level overflow. reset to max */
22195 debug_backtrace_level = i;
22196 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022197 }
22198 return funccal;
22199}
22200
22201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022203 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 * Returns NULL when it doesn't exist.
22205 */
22206 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022207get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208{
Bram Moolenaar33570922005-01-25 22:26:29 +000022209 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022211 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 if (v == NULL)
22213 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022214 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215}
22216
22217/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022218 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 * sourcing this script and when executing functions defined in the script.
22220 */
22221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022222new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223{
Bram Moolenaara7043832005-01-21 11:56:39 +000022224 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022225 hashtab_T *ht;
22226 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022227
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22229 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022230 /* Re-allocating ga_data means that an ht_array pointing to
22231 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022233 for (i = 1; i <= ga_scripts.ga_len; ++i)
22234 {
22235 ht = &SCRIPT_VARS(i);
22236 if (ht->ht_mask == HT_INIT_SIZE - 1)
22237 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022238 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022239 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022240 }
22241
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 while (ga_scripts.ga_len < id)
22243 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022244 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022245 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022246 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 }
22249 }
22250}
22251
22252/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22254 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 */
22256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022257init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258{
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022260 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022261 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022262 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022263 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022264 dict_var->di_tv.vval.v_dict = dict;
22265 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022266 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022267 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22268 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269}
22270
22271/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022272 * Unreference a dictionary initialized by init_var_dict().
22273 */
22274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022275unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022276{
22277 /* Now the dict needs to be freed if no one else is using it, go back to
22278 * normal reference counting. */
22279 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22280 dict_unref(dict);
22281}
22282
22283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022285 * Frees all allocated variables and the value they contain.
22286 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 */
22288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022289vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022290{
22291 vars_clear_ext(ht, TRUE);
22292}
22293
22294/*
22295 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22296 */
22297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022298vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299{
Bram Moolenaara7043832005-01-21 11:56:39 +000022300 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 hashitem_T *hi;
22302 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303
Bram Moolenaar33570922005-01-25 22:26:29 +000022304 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022305 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022306 for (hi = ht->ht_array; todo > 0; ++hi)
22307 {
22308 if (!HASHITEM_EMPTY(hi))
22309 {
22310 --todo;
22311
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022313 * ht_array might change then. hash_clear() takes care of it
22314 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022315 v = HI2DI(hi);
22316 if (free_val)
22317 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022318 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022319 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022320 }
22321 }
22322 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022323 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324}
22325
Bram Moolenaara7043832005-01-21 11:56:39 +000022326/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 * Delete a variable from hashtab "ht" at item "hi".
22328 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022331delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332{
Bram Moolenaar33570922005-01-25 22:26:29 +000022333 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022334
22335 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022336 clear_tv(&di->di_tv);
22337 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338}
22339
22340/*
22341 * List the value of one internal variable.
22342 */
22343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022344list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022346 char_u *tofree;
22347 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022349
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022350 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022351 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022352 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022353 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354}
22355
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022357list_one_var_a(
22358 char_u *prefix,
22359 char_u *name,
22360 int type,
22361 char_u *string,
22362 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363{
Bram Moolenaar31859182007-08-14 20:41:13 +000022364 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22365 msg_start();
22366 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 if (name != NULL) /* "a:" vars don't have a name stored */
22368 msg_puts(name);
22369 msg_putchar(' ');
22370 msg_advance(22);
22371 if (type == VAR_NUMBER)
22372 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022373 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022374 msg_putchar('*');
22375 else if (type == VAR_LIST)
22376 {
22377 msg_putchar('[');
22378 if (*string == '[')
22379 ++string;
22380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022381 else if (type == VAR_DICT)
22382 {
22383 msg_putchar('{');
22384 if (*string == '{')
22385 ++string;
22386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 else
22388 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022389
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022391
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022392 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022393 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022394 if (*first)
22395 {
22396 msg_clr_eos();
22397 *first = FALSE;
22398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399}
22400
22401/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022402 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 * If the variable already exists, the value is updated.
22404 * Otherwise the variable is created.
22405 */
22406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022407set_var(
22408 char_u *name,
22409 typval_T *tv,
22410 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411{
Bram Moolenaar33570922005-01-25 22:26:29 +000022412 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022414 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022416 ht = find_var_ht(name, &varname);
22417 if (ht == NULL || *varname == NUL)
22418 {
22419 EMSG2(_(e_illvar), name);
22420 return;
22421 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022422 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022423
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022424 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22425 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022426 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427
Bram Moolenaar33570922005-01-25 22:26:29 +000022428 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022430 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022431 if (var_check_ro(v->di_flags, name, FALSE)
22432 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022433 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022434
22435 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022436 * Handle setting internal v: variables separately where needed to
22437 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 */
22439 if (ht == &vimvarht)
22440 {
22441 if (v->di_tv.v_type == VAR_STRING)
22442 {
22443 vim_free(v->di_tv.vval.v_string);
22444 if (copy || tv->v_type != VAR_STRING)
22445 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22446 else
22447 {
22448 /* Take over the string to avoid an extra alloc/free. */
22449 v->di_tv.vval.v_string = tv->vval.v_string;
22450 tv->vval.v_string = NULL;
22451 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022452 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022453 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022454 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022455 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022457 if (STRCMP(varname, "searchforward") == 0)
22458 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022459#ifdef FEAT_SEARCH_EXTRA
22460 else if (STRCMP(varname, "hlsearch") == 0)
22461 {
22462 no_hlsearch = !v->di_tv.vval.v_number;
22463 redraw_all_later(SOME_VALID);
22464 }
22465#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022466 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022467 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022468 else if (v->di_tv.v_type != tv->v_type)
22469 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022470 }
22471
22472 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 }
22474 else /* add a new variable */
22475 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022476 /* Can't add "v:" variable. */
22477 if (ht == &vimvarht)
22478 {
22479 EMSG2(_(e_illvar), name);
22480 return;
22481 }
22482
Bram Moolenaar92124a32005-06-17 22:03:40 +000022483 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022484 if (!valid_varname(varname))
22485 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022486
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022487 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22488 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022489 if (v == NULL)
22490 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022491 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022494 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 return;
22496 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022497 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022499
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022500 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022501 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022502 else
22503 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022504 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022505 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022506 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508}
22509
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022510/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022511 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022512 * Also give an error message.
22513 */
22514 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022515var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022516{
22517 if (flags & DI_FLAGS_RO)
22518 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022519 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022520 return TRUE;
22521 }
22522 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22523 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022524 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022525 return TRUE;
22526 }
22527 return FALSE;
22528}
22529
22530/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022531 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22532 * Also give an error message.
22533 */
22534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022535var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022536{
22537 if (flags & DI_FLAGS_FIX)
22538 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022539 EMSG2(_("E795: Cannot delete variable %s"),
22540 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022541 return TRUE;
22542 }
22543 return FALSE;
22544}
22545
22546/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022547 * Check if a funcref is assigned to a valid variable name.
22548 * Return TRUE and give an error if not.
22549 */
22550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022551var_check_func_name(
22552 char_u *name, /* points to start of variable name */
22553 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022554{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022555 /* Allow for w: b: s: and t:. */
22556 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022557 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22558 ? name[2] : name[0]))
22559 {
22560 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22561 name);
22562 return TRUE;
22563 }
22564 /* Don't allow hiding a function. When "v" is not NULL we might be
22565 * assigning another function to the same var, the type is checked
22566 * below. */
22567 if (new_var && function_exists(name))
22568 {
22569 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22570 name);
22571 return TRUE;
22572 }
22573 return FALSE;
22574}
22575
22576/*
22577 * Check if a variable name is valid.
22578 * Return FALSE and give an error if not.
22579 */
22580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022581valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022582{
22583 char_u *p;
22584
22585 for (p = varname; *p != NUL; ++p)
22586 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22587 && *p != AUTOLOAD_CHAR)
22588 {
22589 EMSG2(_(e_illvar), varname);
22590 return FALSE;
22591 }
22592 return TRUE;
22593}
22594
22595/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022596 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022597 * Also give an error message, using "name" or _("name") when use_gettext is
22598 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022599 */
22600 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022601tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022602{
22603 if (lock & VAR_LOCKED)
22604 {
22605 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022606 name == NULL ? (char_u *)_("Unknown")
22607 : use_gettext ? (char_u *)_(name)
22608 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022609 return TRUE;
22610 }
22611 if (lock & VAR_FIXED)
22612 {
22613 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022614 name == NULL ? (char_u *)_("Unknown")
22615 : use_gettext ? (char_u *)_(name)
22616 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022617 return TRUE;
22618 }
22619 return FALSE;
22620}
22621
22622/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022623 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022624 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022625 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022626 * It is OK for "from" and "to" to point to the same item. This is used to
22627 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022628 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022630copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022632 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022633 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022634 switch (from->v_type)
22635 {
22636 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022637 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022638 to->vval.v_number = from->vval.v_number;
22639 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022640 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022641#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022642 to->vval.v_float = from->vval.v_float;
22643 break;
22644#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022645 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022646#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022647 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022648 if (to->vval.v_job != NULL)
22649 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022650 break;
22651#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022652 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022653#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022654 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022655 if (to->vval.v_channel != NULL)
22656 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022657 break;
22658#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022659 case VAR_STRING:
22660 case VAR_FUNC:
22661 if (from->vval.v_string == NULL)
22662 to->vval.v_string = NULL;
22663 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022665 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022666 if (from->v_type == VAR_FUNC)
22667 func_ref(to->vval.v_string);
22668 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022669 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022670 case VAR_PARTIAL:
22671 if (from->vval.v_partial == NULL)
22672 to->vval.v_partial = NULL;
22673 else
22674 {
22675 to->vval.v_partial = from->vval.v_partial;
22676 ++to->vval.v_partial->pt_refcount;
22677 }
22678 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022679 case VAR_LIST:
22680 if (from->vval.v_list == NULL)
22681 to->vval.v_list = NULL;
22682 else
22683 {
22684 to->vval.v_list = from->vval.v_list;
22685 ++to->vval.v_list->lv_refcount;
22686 }
22687 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022688 case VAR_DICT:
22689 if (from->vval.v_dict == NULL)
22690 to->vval.v_dict = NULL;
22691 else
22692 {
22693 to->vval.v_dict = from->vval.v_dict;
22694 ++to->vval.v_dict->dv_refcount;
22695 }
22696 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022697 case VAR_UNKNOWN:
22698 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022699 break;
22700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701}
22702
22703/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022704 * Make a copy of an item.
22705 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022706 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22707 * reference to an already copied list/dict can be used.
22708 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022709 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022711item_copy(
22712 typval_T *from,
22713 typval_T *to,
22714 int deep,
22715 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022716{
22717 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022718 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022719
Bram Moolenaar33570922005-01-25 22:26:29 +000022720 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022721 {
22722 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022723 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022724 }
22725 ++recurse;
22726
22727 switch (from->v_type)
22728 {
22729 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022730 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022731 case VAR_STRING:
22732 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022733 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022734 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022735 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022736 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022737 copy_tv(from, to);
22738 break;
22739 case VAR_LIST:
22740 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022741 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022742 if (from->vval.v_list == NULL)
22743 to->vval.v_list = NULL;
22744 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22745 {
22746 /* use the copy made earlier */
22747 to->vval.v_list = from->vval.v_list->lv_copylist;
22748 ++to->vval.v_list->lv_refcount;
22749 }
22750 else
22751 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22752 if (to->vval.v_list == NULL)
22753 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022754 break;
22755 case VAR_DICT:
22756 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022757 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022758 if (from->vval.v_dict == NULL)
22759 to->vval.v_dict = NULL;
22760 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22761 {
22762 /* use the copy made earlier */
22763 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22764 ++to->vval.v_dict->dv_refcount;
22765 }
22766 else
22767 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22768 if (to->vval.v_dict == NULL)
22769 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022770 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022771 case VAR_UNKNOWN:
22772 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022773 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022774 }
22775 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022776 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022777}
22778
22779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 * ":echo expr1 ..." print each argument separated with a space, add a
22781 * newline at the end.
22782 * ":echon expr1 ..." print each argument plain.
22783 */
22784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022785ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786{
22787 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022789 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 char_u *p;
22791 int needclr = TRUE;
22792 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022793 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794
22795 if (eap->skip)
22796 ++emsg_skip;
22797 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22798 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022799 /* If eval1() causes an error message the text from the command may
22800 * still need to be cleared. E.g., "echo 22,44". */
22801 need_clr_eos = needclr;
22802
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022804 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 {
22806 /*
22807 * Report the invalid expression unless the expression evaluation
22808 * has been cancelled due to an aborting error, an interrupt, or an
22809 * exception.
22810 */
22811 if (!aborting())
22812 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022813 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 break;
22815 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022816 need_clr_eos = FALSE;
22817
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 if (!eap->skip)
22819 {
22820 if (atstart)
22821 {
22822 atstart = FALSE;
22823 /* Call msg_start() after eval1(), evaluating the expression
22824 * may cause a message to appear. */
22825 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022826 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022827 /* Mark the saved text as finishing the line, so that what
22828 * follows is displayed on a new line when scrolling back
22829 * at the more prompt. */
22830 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 }
22834 else if (eap->cmdidx == CMD_echo)
22835 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022836 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022837 if (p != NULL)
22838 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022840 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022842 if (*p != TAB && needclr)
22843 {
22844 /* remove any text still there from the command */
22845 msg_clr_eos();
22846 needclr = FALSE;
22847 }
22848 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 }
22850 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022851 {
22852#ifdef FEAT_MBYTE
22853 if (has_mbyte)
22854 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022855 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022856
22857 (void)msg_outtrans_len_attr(p, i, echo_attr);
22858 p += i - 1;
22859 }
22860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022862 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022865 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022867 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 arg = skipwhite(arg);
22869 }
22870 eap->nextcmd = check_nextcmd(arg);
22871
22872 if (eap->skip)
22873 --emsg_skip;
22874 else
22875 {
22876 /* remove text that may still be there from the command */
22877 if (needclr)
22878 msg_clr_eos();
22879 if (eap->cmdidx == CMD_echo)
22880 msg_end();
22881 }
22882}
22883
22884/*
22885 * ":echohl {name}".
22886 */
22887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022888ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889{
22890 int id;
22891
22892 id = syn_name2id(eap->arg);
22893 if (id == 0)
22894 echo_attr = 0;
22895 else
22896 echo_attr = syn_id2attr(id);
22897}
22898
22899/*
22900 * ":execute expr1 ..." execute the result of an expression.
22901 * ":echomsg expr1 ..." Print a message
22902 * ":echoerr expr1 ..." Print an error
22903 * Each gets spaces around each argument and a newline at the end for
22904 * echo commands
22905 */
22906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022907ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908{
22909 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 int ret = OK;
22912 char_u *p;
22913 garray_T ga;
22914 int len;
22915 int save_did_emsg;
22916
22917 ga_init2(&ga, 1, 80);
22918
22919 if (eap->skip)
22920 ++emsg_skip;
22921 while (*arg != NUL && *arg != '|' && *arg != '\n')
22922 {
22923 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022924 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 {
22926 /*
22927 * Report the invalid expression unless the expression evaluation
22928 * has been cancelled due to an aborting error, an interrupt, or an
22929 * exception.
22930 */
22931 if (!aborting())
22932 EMSG2(_(e_invexpr2), p);
22933 ret = FAIL;
22934 break;
22935 }
22936
22937 if (!eap->skip)
22938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022939 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 len = (int)STRLEN(p);
22941 if (ga_grow(&ga, len + 2) == FAIL)
22942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022943 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 ret = FAIL;
22945 break;
22946 }
22947 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 ga.ga_len += len;
22951 }
22952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022953 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 arg = skipwhite(arg);
22955 }
22956
22957 if (ret != FAIL && ga.ga_data != NULL)
22958 {
22959 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022962 out_flush();
22963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 else if (eap->cmdidx == CMD_echoerr)
22965 {
22966 /* We don't want to abort following commands, restore did_emsg. */
22967 save_did_emsg = did_emsg;
22968 EMSG((char_u *)ga.ga_data);
22969 if (!force_abort)
22970 did_emsg = save_did_emsg;
22971 }
22972 else if (eap->cmdidx == CMD_execute)
22973 do_cmdline((char_u *)ga.ga_data,
22974 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22975 }
22976
22977 ga_clear(&ga);
22978
22979 if (eap->skip)
22980 --emsg_skip;
22981
22982 eap->nextcmd = check_nextcmd(arg);
22983}
22984
22985/*
22986 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22987 * "arg" points to the "&" or '+' when called, to "option" when returning.
22988 * Returns NULL when no option name found. Otherwise pointer to the char
22989 * after the option name.
22990 */
22991 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022992find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993{
22994 char_u *p = *arg;
22995
22996 ++p;
22997 if (*p == 'g' && p[1] == ':')
22998 {
22999 *opt_flags = OPT_GLOBAL;
23000 p += 2;
23001 }
23002 else if (*p == 'l' && p[1] == ':')
23003 {
23004 *opt_flags = OPT_LOCAL;
23005 p += 2;
23006 }
23007 else
23008 *opt_flags = 0;
23009
23010 if (!ASCII_ISALPHA(*p))
23011 return NULL;
23012 *arg = p;
23013
23014 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23015 p += 4; /* termcap option */
23016 else
23017 while (ASCII_ISALPHA(*p))
23018 ++p;
23019 return p;
23020}
23021
23022/*
23023 * ":function"
23024 */
23025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023026ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027{
23028 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023029 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 int j;
23031 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023033 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 char_u *name = NULL;
23035 char_u *p;
23036 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023037 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 garray_T newargs;
23039 garray_T newlines;
23040 int varargs = FALSE;
23041 int mustend = FALSE;
23042 int flags = 0;
23043 ufunc_T *fp;
23044 int indent;
23045 int nesting;
23046 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 dictitem_T *v;
23048 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023049 static int func_nr = 0; /* number for nameless function */
23050 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023051 hashtab_T *ht;
23052 int todo;
23053 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023054 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055
23056 /*
23057 * ":function" without argument: list functions.
23058 */
23059 if (ends_excmd(*eap->arg))
23060 {
23061 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023062 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023063 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023064 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023065 {
23066 if (!HASHITEM_EMPTY(hi))
23067 {
23068 --todo;
23069 fp = HI2UF(hi);
23070 if (!isdigit(*fp->uf_name))
23071 list_func_head(fp, FALSE);
23072 }
23073 }
23074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 eap->nextcmd = check_nextcmd(eap->arg);
23076 return;
23077 }
23078
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023079 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023080 * ":function /pat": list functions matching pattern.
23081 */
23082 if (*eap->arg == '/')
23083 {
23084 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23085 if (!eap->skip)
23086 {
23087 regmatch_T regmatch;
23088
23089 c = *p;
23090 *p = NUL;
23091 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23092 *p = c;
23093 if (regmatch.regprog != NULL)
23094 {
23095 regmatch.rm_ic = p_ic;
23096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023097 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023098 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23099 {
23100 if (!HASHITEM_EMPTY(hi))
23101 {
23102 --todo;
23103 fp = HI2UF(hi);
23104 if (!isdigit(*fp->uf_name)
23105 && vim_regexec(&regmatch, fp->uf_name, 0))
23106 list_func_head(fp, FALSE);
23107 }
23108 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023109 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023110 }
23111 }
23112 if (*p == '/')
23113 ++p;
23114 eap->nextcmd = check_nextcmd(p);
23115 return;
23116 }
23117
23118 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023119 * Get the function name. There are these situations:
23120 * func normal function name
23121 * "name" == func, "fudi.fd_dict" == NULL
23122 * dict.func new dictionary entry
23123 * "name" == NULL, "fudi.fd_dict" set,
23124 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23125 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023126 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023127 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23128 * dict.func existing dict entry that's not a Funcref
23129 * "name" == NULL, "fudi.fd_dict" set,
23130 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023131 * s:func script-local function name
23132 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023135 name = trans_function_name(&p, eap->skip, 0, &fudi);
23136 paren = (vim_strchr(p, '(') != NULL);
23137 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 {
23139 /*
23140 * Return on an invalid expression in braces, unless the expression
23141 * evaluation has been cancelled due to an aborting error, an
23142 * interrupt, or an exception.
23143 */
23144 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023146 if (!eap->skip && fudi.fd_newkey != NULL)
23147 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023148 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 else
23152 eap->skip = TRUE;
23153 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023154
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 /* An error in a function call during evaluation of an expression in magic
23156 * braces should not cause the function not to be defined. */
23157 saved_did_emsg = did_emsg;
23158 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159
23160 /*
23161 * ":function func" with only function name: list function.
23162 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023163 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 {
23165 if (!ends_excmd(*skipwhite(p)))
23166 {
23167 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023168 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 }
23170 eap->nextcmd = check_nextcmd(p);
23171 if (eap->nextcmd != NULL)
23172 *p = NUL;
23173 if (!eap->skip && !got_int)
23174 {
23175 fp = find_func(name);
23176 if (fp != NULL)
23177 {
23178 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023179 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023181 if (FUNCLINE(fp, j) == NULL)
23182 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 msg_putchar('\n');
23184 msg_outnum((long)(j + 1));
23185 if (j < 9)
23186 msg_putchar(' ');
23187 if (j < 99)
23188 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023189 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 out_flush(); /* show a line at a time */
23191 ui_breakcheck();
23192 }
23193 if (!got_int)
23194 {
23195 msg_putchar('\n');
23196 msg_puts((char_u *)" endfunction");
23197 }
23198 }
23199 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023200 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023202 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 }
23204
23205 /*
23206 * ":function name(arg1, arg2)" Define function.
23207 */
23208 p = skipwhite(p);
23209 if (*p != '(')
23210 {
23211 if (!eap->skip)
23212 {
23213 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023214 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 }
23216 /* attempt to continue by skipping some text */
23217 if (vim_strchr(p, '(') != NULL)
23218 p = vim_strchr(p, '(');
23219 }
23220 p = skipwhite(p + 1);
23221
23222 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23223 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23224
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023225 if (!eap->skip)
23226 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023227 /* Check the name of the function. Unless it's a dictionary function
23228 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023229 if (name != NULL)
23230 arg = name;
23231 else
23232 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023233 if (arg != NULL && (fudi.fd_di == NULL
23234 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023235 {
23236 if (*arg == K_SPECIAL)
23237 j = 3;
23238 else
23239 j = 0;
23240 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23241 : eval_isnamec(arg[j])))
23242 ++j;
23243 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023244 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023245 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023246 /* Disallow using the g: dict. */
23247 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23248 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023249 }
23250
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 /*
23252 * Isolate the arguments: "arg1, arg2, ...)"
23253 */
23254 while (*p != ')')
23255 {
23256 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23257 {
23258 varargs = TRUE;
23259 p += 3;
23260 mustend = TRUE;
23261 }
23262 else
23263 {
23264 arg = p;
23265 while (ASCII_ISALNUM(*p) || *p == '_')
23266 ++p;
23267 if (arg == p || isdigit(*arg)
23268 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23269 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23270 {
23271 if (!eap->skip)
23272 EMSG2(_("E125: Illegal argument: %s"), arg);
23273 break;
23274 }
23275 if (ga_grow(&newargs, 1) == FAIL)
23276 goto erret;
23277 c = *p;
23278 *p = NUL;
23279 arg = vim_strsave(arg);
23280 if (arg == NULL)
23281 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023282
23283 /* Check for duplicate argument name. */
23284 for (i = 0; i < newargs.ga_len; ++i)
23285 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23286 {
23287 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023288 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023289 goto erret;
23290 }
23291
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23293 *p = c;
23294 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 if (*p == ',')
23296 ++p;
23297 else
23298 mustend = TRUE;
23299 }
23300 p = skipwhite(p);
23301 if (mustend && *p != ')')
23302 {
23303 if (!eap->skip)
23304 EMSG2(_(e_invarg2), eap->arg);
23305 break;
23306 }
23307 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023308 if (*p != ')')
23309 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 ++p; /* skip the ')' */
23311
Bram Moolenaare9a41262005-01-15 22:18:47 +000023312 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 for (;;)
23314 {
23315 p = skipwhite(p);
23316 if (STRNCMP(p, "range", 5) == 0)
23317 {
23318 flags |= FC_RANGE;
23319 p += 5;
23320 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023321 else if (STRNCMP(p, "dict", 4) == 0)
23322 {
23323 flags |= FC_DICT;
23324 p += 4;
23325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 else if (STRNCMP(p, "abort", 5) == 0)
23327 {
23328 flags |= FC_ABORT;
23329 p += 5;
23330 }
23331 else
23332 break;
23333 }
23334
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023335 /* When there is a line break use what follows for the function body.
23336 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23337 if (*p == '\n')
23338 line_arg = p + 1;
23339 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 EMSG(_(e_trailing));
23341
23342 /*
23343 * Read the body of the function, until ":endfunction" is found.
23344 */
23345 if (KeyTyped)
23346 {
23347 /* Check if the function already exists, don't let the user type the
23348 * whole function before telling him it doesn't work! For a script we
23349 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023350 if (!eap->skip && !eap->forceit)
23351 {
23352 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23353 EMSG(_(e_funcdict));
23354 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023355 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023358 if (!eap->skip && did_emsg)
23359 goto erret;
23360
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 msg_putchar('\n'); /* don't overwrite the function name */
23362 cmdline_row = msg_row;
23363 }
23364
23365 indent = 2;
23366 nesting = 0;
23367 for (;;)
23368 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023369 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023370 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023371 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023372 saved_wait_return = FALSE;
23373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023375 sourcing_lnum_off = sourcing_lnum;
23376
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023377 if (line_arg != NULL)
23378 {
23379 /* Use eap->arg, split up in parts by line breaks. */
23380 theline = line_arg;
23381 p = vim_strchr(theline, '\n');
23382 if (p == NULL)
23383 line_arg += STRLEN(line_arg);
23384 else
23385 {
23386 *p = NUL;
23387 line_arg = p + 1;
23388 }
23389 }
23390 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 theline = getcmdline(':', 0L, indent);
23392 else
23393 theline = eap->getline(':', eap->cookie, indent);
23394 if (KeyTyped)
23395 lines_left = Rows - 1;
23396 if (theline == NULL)
23397 {
23398 EMSG(_("E126: Missing :endfunction"));
23399 goto erret;
23400 }
23401
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023402 /* Detect line continuation: sourcing_lnum increased more than one. */
23403 if (sourcing_lnum > sourcing_lnum_off + 1)
23404 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23405 else
23406 sourcing_lnum_off = 0;
23407
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 if (skip_until != NULL)
23409 {
23410 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23411 * don't check for ":endfunc". */
23412 if (STRCMP(theline, skip_until) == 0)
23413 {
23414 vim_free(skip_until);
23415 skip_until = NULL;
23416 }
23417 }
23418 else
23419 {
23420 /* skip ':' and blanks*/
23421 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23422 ;
23423
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023424 /* Check for "endfunction". */
23425 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023427 if (line_arg == NULL)
23428 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 break;
23430 }
23431
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023432 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433 * at "end". */
23434 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23435 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023436 else if (STRNCMP(p, "if", 2) == 0
23437 || STRNCMP(p, "wh", 2) == 0
23438 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 || STRNCMP(p, "try", 3) == 0)
23440 indent += 2;
23441
23442 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023443 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023445 if (*p == '!')
23446 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023448 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23449 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023451 ++nesting;
23452 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 }
23454 }
23455
23456 /* Check for ":append" or ":insert". */
23457 p = skip_range(p, NULL);
23458 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23459 || (p[0] == 'i'
23460 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23461 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23462 skip_until = vim_strsave((char_u *)".");
23463
23464 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23465 arg = skipwhite(skiptowhite(p));
23466 if (arg[0] == '<' && arg[1] =='<'
23467 && ((p[0] == 'p' && p[1] == 'y'
23468 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23469 || (p[0] == 'p' && p[1] == 'e'
23470 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23471 || (p[0] == 't' && p[1] == 'c'
23472 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023473 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23474 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23476 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023477 || (p[0] == 'm' && p[1] == 'z'
23478 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 ))
23480 {
23481 /* ":python <<" continues until a dot, like ":append" */
23482 p = skipwhite(arg + 2);
23483 if (*p == NUL)
23484 skip_until = vim_strsave((char_u *)".");
23485 else
23486 skip_until = vim_strsave(p);
23487 }
23488 }
23489
23490 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023491 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023492 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023493 if (line_arg == NULL)
23494 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023496 }
23497
23498 /* Copy the line to newly allocated memory. get_one_sourceline()
23499 * allocates 250 bytes per line, this saves 80% on average. The cost
23500 * is an extra alloc/free. */
23501 p = vim_strsave(theline);
23502 if (p != NULL)
23503 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023504 if (line_arg == NULL)
23505 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023506 theline = p;
23507 }
23508
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023509 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23510
23511 /* Add NULL lines for continuation lines, so that the line count is
23512 * equal to the index in the growarray. */
23513 while (sourcing_lnum_off-- > 0)
23514 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023515
23516 /* Check for end of eap->arg. */
23517 if (line_arg != NULL && *line_arg == NUL)
23518 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 }
23520
23521 /* Don't define the function when skipping commands or when an error was
23522 * detected. */
23523 if (eap->skip || did_emsg)
23524 goto erret;
23525
23526 /*
23527 * If there are no errors, add the function
23528 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023529 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023530 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023531 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023532 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023533 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023534 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023535 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023536 goto erret;
23537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023538
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023539 fp = find_func(name);
23540 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023542 if (!eap->forceit)
23543 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023544 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023545 goto erret;
23546 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023548 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023549 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023550 name);
23551 goto erret;
23552 }
23553 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023554 ga_clear_strings(&(fp->uf_args));
23555 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023556 vim_free(name);
23557 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 }
23560 else
23561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023562 char numbuf[20];
23563
23564 fp = NULL;
23565 if (fudi.fd_newkey == NULL && !eap->forceit)
23566 {
23567 EMSG(_(e_funcdict));
23568 goto erret;
23569 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023570 if (fudi.fd_di == NULL)
23571 {
23572 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023573 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023574 goto erret;
23575 }
23576 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023577 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023578 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023579
23580 /* Give the function a sequential number. Can only be used with a
23581 * Funcref! */
23582 vim_free(name);
23583 sprintf(numbuf, "%d", ++func_nr);
23584 name = vim_strsave((char_u *)numbuf);
23585 if (name == NULL)
23586 goto erret;
23587 }
23588
23589 if (fp == NULL)
23590 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023591 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023592 {
23593 int slen, plen;
23594 char_u *scriptname;
23595
23596 /* Check that the autoload name matches the script name. */
23597 j = FAIL;
23598 if (sourcing_name != NULL)
23599 {
23600 scriptname = autoload_name(name);
23601 if (scriptname != NULL)
23602 {
23603 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023604 plen = (int)STRLEN(p);
23605 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023606 if (slen > plen && fnamecmp(p,
23607 sourcing_name + slen - plen) == 0)
23608 j = OK;
23609 vim_free(scriptname);
23610 }
23611 }
23612 if (j == FAIL)
23613 {
23614 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23615 goto erret;
23616 }
23617 }
23618
23619 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 if (fp == NULL)
23621 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023622
23623 if (fudi.fd_dict != NULL)
23624 {
23625 if (fudi.fd_di == NULL)
23626 {
23627 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023628 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023629 if (fudi.fd_di == NULL)
23630 {
23631 vim_free(fp);
23632 goto erret;
23633 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023634 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23635 {
23636 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023637 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023638 goto erret;
23639 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 }
23641 else
23642 /* overwrite existing dict entry */
23643 clear_tv(&fudi.fd_di->di_tv);
23644 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023645 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023646 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023647 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023648
23649 /* behave like "dict" was used */
23650 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023651 }
23652
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023654 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023655 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23656 {
23657 vim_free(fp);
23658 goto erret;
23659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023661 fp->uf_args = newargs;
23662 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023663#ifdef FEAT_PROFILE
23664 fp->uf_tml_count = NULL;
23665 fp->uf_tml_total = NULL;
23666 fp->uf_tml_self = NULL;
23667 fp->uf_profiling = FALSE;
23668 if (prof_def_func())
23669 func_do_profile(fp);
23670#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023671 fp->uf_varargs = varargs;
23672 fp->uf_flags = flags;
23673 fp->uf_calls = 0;
23674 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676
23677erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678 ga_clear_strings(&newargs);
23679 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680ret_free:
23681 vim_free(skip_until);
23682 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023685 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686}
23687
23688/*
23689 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023690 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023692 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023693 * TFN_INT: internal function name OK
23694 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023695 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696 * Advances "pp" to just after the function name (if no error).
23697 */
23698 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023699trans_function_name(
23700 char_u **pp,
23701 int skip, /* only find the end, don't evaluate */
23702 int flags,
23703 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023705 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 char_u *start;
23707 char_u *end;
23708 int lead;
23709 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023711 lval_T lv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023712 partial_T *partial;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023713
23714 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023715 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023717
23718 /* Check for hard coded <SNR>: already translated function ID (from a user
23719 * command). */
23720 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23721 && (*pp)[2] == (int)KE_SNR)
23722 {
23723 *pp += 3;
23724 len = get_id_len(pp) + 3;
23725 return vim_strnsave(start, len);
23726 }
23727
23728 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23729 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023731 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023733
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023734 /* Note that TFN_ flags use the same values as GLV_ flags. */
23735 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023736 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023737 if (end == start)
23738 {
23739 if (!skip)
23740 EMSG(_("E129: Function name required"));
23741 goto theend;
23742 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023743 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023744 {
23745 /*
23746 * Report an invalid expression in braces, unless the expression
23747 * evaluation has been cancelled due to an aborting error, an
23748 * interrupt, or an exception.
23749 */
23750 if (!aborting())
23751 {
23752 if (end != NULL)
23753 EMSG2(_(e_invarg2), start);
23754 }
23755 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023756 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023757 goto theend;
23758 }
23759
23760 if (lv.ll_tv != NULL)
23761 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023762 if (fdp != NULL)
23763 {
23764 fdp->fd_dict = lv.ll_dict;
23765 fdp->fd_newkey = lv.ll_newkey;
23766 lv.ll_newkey = NULL;
23767 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023768 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023769 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23770 {
23771 name = vim_strsave(lv.ll_tv->vval.v_string);
23772 *pp = end;
23773 }
23774 else
23775 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023776 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23777 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023778 EMSG(_(e_funcref));
23779 else
23780 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023781 name = NULL;
23782 }
23783 goto theend;
23784 }
23785
23786 if (lv.ll_name == NULL)
23787 {
23788 /* Error found, but continue after the function name. */
23789 *pp = end;
23790 goto theend;
23791 }
23792
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023793 /* Check if the name is a Funcref. If so, use the value. */
23794 if (lv.ll_exp_name != NULL)
23795 {
23796 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023797 name = deref_func_name(lv.ll_exp_name, &len, &partial,
23798 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023799 if (name == lv.ll_exp_name)
23800 name = NULL;
23801 }
23802 else
23803 {
23804 len = (int)(end - *pp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023805 name = deref_func_name(*pp, &len, &partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023806 if (name == *pp)
23807 name = NULL;
23808 }
23809 if (name != NULL)
23810 {
23811 name = vim_strsave(name);
23812 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023813 if (STRNCMP(name, "<SNR>", 5) == 0)
23814 {
23815 /* Change "<SNR>" to the byte sequence. */
23816 name[0] = K_SPECIAL;
23817 name[1] = KS_EXTRA;
23818 name[2] = (int)KE_SNR;
23819 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23820 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023821 goto theend;
23822 }
23823
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023824 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023825 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023826 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023827 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23828 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23829 {
23830 /* When there was "s:" already or the name expanded to get a
23831 * leading "s:" then remove it. */
23832 lv.ll_name += 2;
23833 len -= 2;
23834 lead = 2;
23835 }
23836 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023837 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023838 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023839 /* skip over "s:" and "g:" */
23840 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023841 lv.ll_name += 2;
23842 len = (int)(end - lv.ll_name);
23843 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023844
23845 /*
23846 * Copy the function name to allocated memory.
23847 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23848 * Accept <SNR>123_name() outside a script.
23849 */
23850 if (skip)
23851 lead = 0; /* do nothing */
23852 else if (lead > 0)
23853 {
23854 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023855 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23856 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023857 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023858 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023859 if (current_SID <= 0)
23860 {
23861 EMSG(_(e_usingsid));
23862 goto theend;
23863 }
23864 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23865 lead += (int)STRLEN(sid_buf);
23866 }
23867 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023868 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023869 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023870 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023871 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023872 goto theend;
23873 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023874 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023875 {
23876 char_u *cp = vim_strchr(lv.ll_name, ':');
23877
23878 if (cp != NULL && cp < end)
23879 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023880 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023881 goto theend;
23882 }
23883 }
23884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023885 name = alloc((unsigned)(len + lead + 1));
23886 if (name != NULL)
23887 {
23888 if (lead > 0)
23889 {
23890 name[0] = K_SPECIAL;
23891 name[1] = KS_EXTRA;
23892 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023893 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023894 STRCPY(name + 3, sid_buf);
23895 }
23896 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023897 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023898 }
23899 *pp = end;
23900
23901theend:
23902 clear_lval(&lv);
23903 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904}
23905
23906/*
23907 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23908 * Return 2 if "p" starts with "s:".
23909 * Return 0 otherwise.
23910 */
23911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023912eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023914 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23915 * the standard library function. */
23916 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23917 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 return 5;
23919 if (p[0] == 's' && p[1] == ':')
23920 return 2;
23921 return 0;
23922}
23923
23924/*
23925 * Return TRUE if "p" starts with "<SID>" or "s:".
23926 * Only works if eval_fname_script() returned non-zero for "p"!
23927 */
23928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023929eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930{
23931 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23932}
23933
23934/*
23935 * List the head of the function: "name(arg1, arg2)".
23936 */
23937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023938list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939{
23940 int j;
23941
23942 msg_start();
23943 if (indent)
23944 MSG_PUTS(" ");
23945 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023946 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 {
23948 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023949 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 }
23951 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023952 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023954 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 {
23956 if (j)
23957 MSG_PUTS(", ");
23958 msg_puts(FUNCARG(fp, j));
23959 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023960 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 {
23962 if (j)
23963 MSG_PUTS(", ");
23964 MSG_PUTS("...");
23965 }
23966 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023967 if (fp->uf_flags & FC_ABORT)
23968 MSG_PUTS(" abort");
23969 if (fp->uf_flags & FC_RANGE)
23970 MSG_PUTS(" range");
23971 if (fp->uf_flags & FC_DICT)
23972 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023973 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023974 if (p_verbose > 0)
23975 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976}
23977
23978/*
23979 * Find a function by name, return pointer to it in ufuncs.
23980 * Return NULL for unknown function.
23981 */
23982 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023983find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023985 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023987 hi = hash_find(&func_hashtab, name);
23988 if (!HASHITEM_EMPTY(hi))
23989 return HI2UF(hi);
23990 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991}
23992
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023993#if defined(EXITFREE) || defined(PROTO)
23994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023995free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023996{
23997 hashitem_T *hi;
23998
23999 /* Need to start all over every time, because func_free() may change the
24000 * hash table. */
24001 while (func_hashtab.ht_used > 0)
24002 for (hi = func_hashtab.ht_array; ; ++hi)
24003 if (!HASHITEM_EMPTY(hi))
24004 {
24005 func_free(HI2UF(hi));
24006 break;
24007 }
24008}
24009#endif
24010
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024012translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024013{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024014 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024015 return find_internal_func(name) >= 0;
24016 return find_func(name) != NULL;
24017}
24018
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024019/*
24020 * Return TRUE if a function "name" exists.
24021 */
24022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024023function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024024{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024025 char_u *nm = name;
24026 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024027 int n = FALSE;
24028
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024029 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24030 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024031 nm = skipwhite(nm);
24032
24033 /* Only accept "funcname", "funcname ", "funcname (..." and
24034 * "funcname(...", not "funcname!...". */
24035 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024036 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024037 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024038 return n;
24039}
24040
Bram Moolenaara1544c02013-05-30 12:35:52 +020024041 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024042get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024043{
24044 char_u *nm = name;
24045 char_u *p;
24046
24047 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24048
24049 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024050 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024051 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024052
Bram Moolenaara1544c02013-05-30 12:35:52 +020024053 vim_free(p);
24054 return NULL;
24055}
24056
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024057/*
24058 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024059 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24060 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024061 */
24062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024063builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024064{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024065 char_u *p;
24066
24067 if (!ASCII_ISLOWER(name[0]))
24068 return FALSE;
24069 p = vim_strchr(name, AUTOLOAD_CHAR);
24070 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024071}
24072
Bram Moolenaar05159a02005-02-26 23:04:13 +000024073#if defined(FEAT_PROFILE) || defined(PROTO)
24074/*
24075 * Start profiling function "fp".
24076 */
24077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024078func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024079{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024080 int len = fp->uf_lines.ga_len;
24081
24082 if (len == 0)
24083 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024084 fp->uf_tm_count = 0;
24085 profile_zero(&fp->uf_tm_self);
24086 profile_zero(&fp->uf_tm_total);
24087 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024088 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024089 if (fp->uf_tml_total == NULL)
24090 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024091 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024092 if (fp->uf_tml_self == NULL)
24093 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024094 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024095 fp->uf_tml_idx = -1;
24096 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24097 || fp->uf_tml_self == NULL)
24098 return; /* out of memory */
24099
24100 fp->uf_profiling = TRUE;
24101}
24102
24103/*
24104 * Dump the profiling results for all functions in file "fd".
24105 */
24106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024107func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024108{
24109 hashitem_T *hi;
24110 int todo;
24111 ufunc_T *fp;
24112 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024113 ufunc_T **sorttab;
24114 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024115
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024116 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024117 if (todo == 0)
24118 return; /* nothing to dump */
24119
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024120 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024121
Bram Moolenaar05159a02005-02-26 23:04:13 +000024122 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24123 {
24124 if (!HASHITEM_EMPTY(hi))
24125 {
24126 --todo;
24127 fp = HI2UF(hi);
24128 if (fp->uf_profiling)
24129 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024130 if (sorttab != NULL)
24131 sorttab[st_len++] = fp;
24132
Bram Moolenaar05159a02005-02-26 23:04:13 +000024133 if (fp->uf_name[0] == K_SPECIAL)
24134 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24135 else
24136 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24137 if (fp->uf_tm_count == 1)
24138 fprintf(fd, "Called 1 time\n");
24139 else
24140 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24141 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24142 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24143 fprintf(fd, "\n");
24144 fprintf(fd, "count total (s) self (s)\n");
24145
24146 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24147 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024148 if (FUNCLINE(fp, i) == NULL)
24149 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024150 prof_func_line(fd, fp->uf_tml_count[i],
24151 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024152 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24153 }
24154 fprintf(fd, "\n");
24155 }
24156 }
24157 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024158
24159 if (sorttab != NULL && st_len > 0)
24160 {
24161 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24162 prof_total_cmp);
24163 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24164 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24165 prof_self_cmp);
24166 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24167 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024168
24169 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024170}
Bram Moolenaar73830342005-02-28 22:48:19 +000024171
24172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024173prof_sort_list(
24174 FILE *fd,
24175 ufunc_T **sorttab,
24176 int st_len,
24177 char *title,
24178 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024179{
24180 int i;
24181 ufunc_T *fp;
24182
24183 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24184 fprintf(fd, "count total (s) self (s) function\n");
24185 for (i = 0; i < 20 && i < st_len; ++i)
24186 {
24187 fp = sorttab[i];
24188 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24189 prefer_self);
24190 if (fp->uf_name[0] == K_SPECIAL)
24191 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24192 else
24193 fprintf(fd, " %s()\n", fp->uf_name);
24194 }
24195 fprintf(fd, "\n");
24196}
24197
24198/*
24199 * Print the count and times for one function or function line.
24200 */
24201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024202prof_func_line(
24203 FILE *fd,
24204 int count,
24205 proftime_T *total,
24206 proftime_T *self,
24207 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024208{
24209 if (count > 0)
24210 {
24211 fprintf(fd, "%5d ", count);
24212 if (prefer_self && profile_equal(total, self))
24213 fprintf(fd, " ");
24214 else
24215 fprintf(fd, "%s ", profile_msg(total));
24216 if (!prefer_self && profile_equal(total, self))
24217 fprintf(fd, " ");
24218 else
24219 fprintf(fd, "%s ", profile_msg(self));
24220 }
24221 else
24222 fprintf(fd, " ");
24223}
24224
24225/*
24226 * Compare function for total time sorting.
24227 */
24228 static int
24229#ifdef __BORLANDC__
24230_RTLENTRYF
24231#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024232prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024233{
24234 ufunc_T *p1, *p2;
24235
24236 p1 = *(ufunc_T **)s1;
24237 p2 = *(ufunc_T **)s2;
24238 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24239}
24240
24241/*
24242 * Compare function for self time sorting.
24243 */
24244 static int
24245#ifdef __BORLANDC__
24246_RTLENTRYF
24247#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024248prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024249{
24250 ufunc_T *p1, *p2;
24251
24252 p1 = *(ufunc_T **)s1;
24253 p2 = *(ufunc_T **)s2;
24254 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24255}
24256
Bram Moolenaar05159a02005-02-26 23:04:13 +000024257#endif
24258
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024259/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024260 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024261 * Return TRUE if a package was loaded.
24262 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024264script_autoload(
24265 char_u *name,
24266 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024267{
24268 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024269 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024270 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024271 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024272
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024273 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024274 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024275 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024276 return FALSE;
24277
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024278 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024279
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024280 /* Find the name in the list of previously loaded package names. Skip
24281 * "autoload/", it's always the same. */
24282 for (i = 0; i < ga_loaded.ga_len; ++i)
24283 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24284 break;
24285 if (!reload && i < ga_loaded.ga_len)
24286 ret = FALSE; /* was loaded already */
24287 else
24288 {
24289 /* Remember the name if it wasn't loaded already. */
24290 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24291 {
24292 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24293 tofree = NULL;
24294 }
24295
24296 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024297 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024298 ret = TRUE;
24299 }
24300
24301 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024302 return ret;
24303}
24304
24305/*
24306 * Return the autoload script name for a function or variable name.
24307 * Returns NULL when out of memory.
24308 */
24309 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024310autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024311{
24312 char_u *p;
24313 char_u *scriptname;
24314
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024315 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024316 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24317 if (scriptname == NULL)
24318 return FALSE;
24319 STRCPY(scriptname, "autoload/");
24320 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024321 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024322 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024323 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024324 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024325 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024326}
24327
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24329
24330/*
24331 * Function given to ExpandGeneric() to obtain the list of user defined
24332 * function names.
24333 */
24334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024335get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024337 static long_u done;
24338 static hashitem_T *hi;
24339 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340
24341 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024343 done = 0;
24344 hi = func_hashtab.ht_array;
24345 }
24346 if (done < func_hashtab.ht_used)
24347 {
24348 if (done++ > 0)
24349 ++hi;
24350 while (HASHITEM_EMPTY(hi))
24351 ++hi;
24352 fp = HI2UF(hi);
24353
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024354 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024355 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024356
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24358 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359
24360 cat_func_name(IObuff, fp);
24361 if (xp->xp_context != EXPAND_USER_FUNC)
24362 {
24363 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024364 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 STRCAT(IObuff, ")");
24366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 return IObuff;
24368 }
24369 return NULL;
24370}
24371
24372#endif /* FEAT_CMDL_COMPL */
24373
24374/*
24375 * Copy the function name of "fp" to buffer "buf".
24376 * "buf" must be able to hold the function name plus three bytes.
24377 * Takes care of script-local function names.
24378 */
24379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024380cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024382 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 {
24384 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024385 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 }
24387 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024388 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389}
24390
24391/*
24392 * ":delfunction {name}"
24393 */
24394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024395ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024397 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 char_u *p;
24399 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024400 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401
24402 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024403 name = trans_function_name(&p, eap->skip, 0, &fudi);
24404 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024406 {
24407 if (fudi.fd_dict != NULL && !eap->skip)
24408 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411 if (!ends_excmd(*skipwhite(p)))
24412 {
24413 vim_free(name);
24414 EMSG(_(e_trailing));
24415 return;
24416 }
24417 eap->nextcmd = check_nextcmd(p);
24418 if (eap->nextcmd != NULL)
24419 *p = NUL;
24420
24421 if (!eap->skip)
24422 fp = find_func(name);
24423 vim_free(name);
24424
24425 if (!eap->skip)
24426 {
24427 if (fp == NULL)
24428 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024429 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024430 return;
24431 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433 {
24434 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24435 return;
24436 }
24437
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024438 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024440 /* Delete the dict item that refers to the function, it will
24441 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024442 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024444 else
24445 func_free(fp);
24446 }
24447}
24448
24449/*
24450 * Free a function and remove it from the list of functions.
24451 */
24452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024453func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024454{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024455 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024456
24457 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024458 ga_clear_strings(&(fp->uf_args));
24459 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024460#ifdef FEAT_PROFILE
24461 vim_free(fp->uf_tml_count);
24462 vim_free(fp->uf_tml_total);
24463 vim_free(fp->uf_tml_self);
24464#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024466 /* remove the function from the function hashtable */
24467 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24468 if (HASHITEM_EMPTY(hi))
24469 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024470 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024471 hash_remove(&func_hashtab, hi);
24472
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024473 vim_free(fp);
24474}
24475
24476/*
24477 * Unreference a Function: decrement the reference count and free it when it
24478 * becomes zero. Only for numbered functions.
24479 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024481func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024482{
24483 ufunc_T *fp;
24484
24485 if (name != NULL && isdigit(*name))
24486 {
24487 fp = find_func(name);
24488 if (fp == NULL)
24489 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024490 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024491 {
24492 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024493 * when "uf_calls" becomes zero. */
24494 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024495 func_free(fp);
24496 }
24497 }
24498}
24499
24500/*
24501 * Count a reference to a Function.
24502 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024503 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024504func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024505{
24506 ufunc_T *fp;
24507
24508 if (name != NULL && isdigit(*name))
24509 {
24510 fp = find_func(name);
24511 if (fp == NULL)
24512 EMSG2(_(e_intern2), "func_ref()");
24513 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024514 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 }
24516}
24517
24518/*
24519 * Call a user function.
24520 */
24521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024522call_user_func(
24523 ufunc_T *fp, /* pointer to function */
24524 int argcount, /* nr of args */
24525 typval_T *argvars, /* arguments */
24526 typval_T *rettv, /* return value */
24527 linenr_T firstline, /* first line of range */
24528 linenr_T lastline, /* last line of range */
24529 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530{
Bram Moolenaar33570922005-01-25 22:26:29 +000024531 char_u *save_sourcing_name;
24532 linenr_T save_sourcing_lnum;
24533 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024534 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024535 int save_did_emsg;
24536 static int depth = 0;
24537 dictitem_T *v;
24538 int fixvar_idx = 0; /* index in fixvar[] */
24539 int i;
24540 int ai;
24541 char_u numbuf[NUMBUFLEN];
24542 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024543 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024544#ifdef FEAT_PROFILE
24545 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024546 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548
24549 /* If depth of calling is getting too high, don't execute the function */
24550 if (depth >= p_mfd)
24551 {
24552 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024553 rettv->v_type = VAR_NUMBER;
24554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 return;
24556 }
24557 ++depth;
24558
24559 line_breakcheck(); /* check for CTRL-C hit */
24560
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024561 fc = (funccall_T *)alloc(sizeof(funccall_T));
24562 fc->caller = current_funccal;
24563 current_funccal = fc;
24564 fc->func = fp;
24565 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024566 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024567 fc->linenr = 0;
24568 fc->returned = FALSE;
24569 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024571 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24572 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573
Bram Moolenaar33570922005-01-25 22:26:29 +000024574 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024575 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024576 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24577 * each argument variable and saves a lot of time.
24578 */
24579 /*
24580 * Init l: variables.
24581 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024582 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024583 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024584 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024585 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24586 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024587 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024588 name = v->di_key;
24589 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024590 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024591 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024592 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024593 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024594 v->di_tv.vval.v_dict = selfdict;
24595 ++selfdict->dv_refcount;
24596 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024597
Bram Moolenaar33570922005-01-25 22:26:29 +000024598 /*
24599 * Init a: variables.
24600 * Set a:0 to "argcount".
24601 * Set a:000 to a list with room for the "..." arguments.
24602 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024603 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024604 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024605 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024606 /* Use "name" to avoid a warning from some compiler that checks the
24607 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024608 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024609 name = v->di_key;
24610 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024611 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024612 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024613 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024614 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024615 v->di_tv.vval.v_list = &fc->l_varlist;
24616 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24617 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24618 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024619
24620 /*
24621 * Set a:firstline to "firstline" and a:lastline to "lastline".
24622 * Set a:name to named arguments.
24623 * Set a:N to the "..." arguments.
24624 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024625 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024626 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024627 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024628 (varnumber_T)lastline);
24629 for (i = 0; i < argcount; ++i)
24630 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024631 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024632 if (ai < 0)
24633 /* named argument a:name */
24634 name = FUNCARG(fp, i);
24635 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024636 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024637 /* "..." argument a:1, a:2, etc. */
24638 sprintf((char *)numbuf, "%d", ai + 1);
24639 name = numbuf;
24640 }
24641 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24642 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024643 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024644 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24645 }
24646 else
24647 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024648 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24649 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024650 if (v == NULL)
24651 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024652 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024653 }
24654 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024655 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024656
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024657 /* Note: the values are copied directly to avoid alloc/free.
24658 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024659 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024660 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024661
24662 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24663 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024664 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24665 fc->l_listitems[ai].li_tv = argvars[i];
24666 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024667 }
24668 }
24669
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 /* Don't redraw while executing the function. */
24671 ++RedrawingDisabled;
24672 save_sourcing_name = sourcing_name;
24673 save_sourcing_lnum = sourcing_lnum;
24674 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024675 /* need space for function name + ("function " + 3) or "[number]" */
24676 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24677 + STRLEN(fp->uf_name) + 20;
24678 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 if (sourcing_name != NULL)
24680 {
24681 if (save_sourcing_name != NULL
24682 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024683 sprintf((char *)sourcing_name, "%s[%d]..",
24684 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 else
24686 STRCPY(sourcing_name, "function ");
24687 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24688
24689 if (p_verbose >= 12)
24690 {
24691 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024692 verbose_enter_scroll();
24693
Bram Moolenaar555b2802005-05-19 21:08:39 +000024694 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 if (p_verbose >= 14)
24696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024698 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024699 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024700 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701
24702 msg_puts((char_u *)"(");
24703 for (i = 0; i < argcount; ++i)
24704 {
24705 if (i > 0)
24706 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024707 if (argvars[i].v_type == VAR_NUMBER)
24708 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 else
24710 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024711 /* Do not want errors such as E724 here. */
24712 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024713 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024714 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024715 if (s != NULL)
24716 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024717 if (vim_strsize(s) > MSG_BUF_CLEN)
24718 {
24719 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24720 s = buf;
24721 }
24722 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024723 vim_free(tofree);
24724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725 }
24726 }
24727 msg_puts((char_u *)")");
24728 }
24729 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024730
24731 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732 --no_wait_return;
24733 }
24734 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024735#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024736 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024737 {
24738 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24739 func_do_profile(fp);
24740 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024741 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024742 {
24743 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024744 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024745 profile_zero(&fp->uf_tm_children);
24746 }
24747 script_prof_save(&wait_start);
24748 }
24749#endif
24750
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024752 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 save_did_emsg = did_emsg;
24754 did_emsg = FALSE;
24755
24756 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024757 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24759
24760 --RedrawingDisabled;
24761
24762 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024763 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024765 clear_tv(rettv);
24766 rettv->v_type = VAR_NUMBER;
24767 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 }
24769
Bram Moolenaar05159a02005-02-26 23:04:13 +000024770#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024771 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024772 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024773 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024774 profile_end(&call_start);
24775 profile_sub_wait(&wait_start, &call_start);
24776 profile_add(&fp->uf_tm_total, &call_start);
24777 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024778 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024779 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024780 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24781 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024782 }
24783 }
24784#endif
24785
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786 /* when being verbose, mention the return value */
24787 if (p_verbose >= 12)
24788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024790 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024793 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024794 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024795 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024796 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024799 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024800 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024801 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024802 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024803
Bram Moolenaar555b2802005-05-19 21:08:39 +000024804 /* The value may be very long. Skip the middle part, so that we
24805 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024806 * truncate it at the end. Don't want errors such as E724 here. */
24807 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024808 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024809 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024810 if (s != NULL)
24811 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024812 if (vim_strsize(s) > MSG_BUF_CLEN)
24813 {
24814 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24815 s = buf;
24816 }
24817 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024818 vim_free(tofree);
24819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 }
24821 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024822
24823 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824 --no_wait_return;
24825 }
24826
24827 vim_free(sourcing_name);
24828 sourcing_name = save_sourcing_name;
24829 sourcing_lnum = save_sourcing_lnum;
24830 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024831#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024832 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024833 script_prof_restore(&wait_start);
24834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835
24836 if (p_verbose >= 12 && sourcing_name != NULL)
24837 {
24838 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024839 verbose_enter_scroll();
24840
Bram Moolenaar555b2802005-05-19 21:08:39 +000024841 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024843
24844 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845 --no_wait_return;
24846 }
24847
24848 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024849 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024851
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024852 /* If the a:000 list and the l: and a: dicts are not referenced we can
24853 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024854 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24855 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24856 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24857 {
24858 free_funccal(fc, FALSE);
24859 }
24860 else
24861 {
24862 hashitem_T *hi;
24863 listitem_T *li;
24864 int todo;
24865
24866 /* "fc" is still in use. This can happen when returning "a:000" or
24867 * assigning "l:" to a global variable.
24868 * Link "fc" in the list for garbage collection later. */
24869 fc->caller = previous_funccal;
24870 previous_funccal = fc;
24871
24872 /* Make a copy of the a: variables, since we didn't do that above. */
24873 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24874 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24875 {
24876 if (!HASHITEM_EMPTY(hi))
24877 {
24878 --todo;
24879 v = HI2DI(hi);
24880 copy_tv(&v->di_tv, &v->di_tv);
24881 }
24882 }
24883
24884 /* Make a copy of the a:000 items, since we didn't do that above. */
24885 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24886 copy_tv(&li->li_tv, &li->li_tv);
24887 }
24888}
24889
24890/*
24891 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024892 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024893 */
24894 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024895can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024896{
24897 return (fc->l_varlist.lv_copyID != copyID
24898 && fc->l_vars.dv_copyID != copyID
24899 && fc->l_avars.dv_copyID != copyID);
24900}
24901
24902/*
24903 * Free "fc" and what it contains.
24904 */
24905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024906free_funccal(
24907 funccall_T *fc,
24908 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024909{
24910 listitem_T *li;
24911
24912 /* The a: variables typevals may not have been allocated, only free the
24913 * allocated variables. */
24914 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24915
24916 /* free all l: variables */
24917 vars_clear(&fc->l_vars.dv_hashtab);
24918
24919 /* Free the a:000 variables if they were allocated. */
24920 if (free_val)
24921 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24922 clear_tv(&li->li_tv);
24923
24924 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925}
24926
24927/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024928 * Add a number variable "name" to dict "dp" with value "nr".
24929 */
24930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024931add_nr_var(
24932 dict_T *dp,
24933 dictitem_T *v,
24934 char *name,
24935 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024936{
24937 STRCPY(v->di_key, name);
24938 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24939 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24940 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024941 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024942 v->di_tv.vval.v_number = nr;
24943}
24944
24945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946 * ":return [expr]"
24947 */
24948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024949ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950{
24951 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024952 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 int returning = FALSE;
24954
24955 if (current_funccal == NULL)
24956 {
24957 EMSG(_("E133: :return not inside a function"));
24958 return;
24959 }
24960
24961 if (eap->skip)
24962 ++emsg_skip;
24963
24964 eap->nextcmd = NULL;
24965 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024966 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 {
24968 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024969 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024971 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972 }
24973 /* It's safer to return also on error. */
24974 else if (!eap->skip)
24975 {
24976 /*
24977 * Return unless the expression evaluation has been cancelled due to an
24978 * aborting error, an interrupt, or an exception.
24979 */
24980 if (!aborting())
24981 returning = do_return(eap, FALSE, TRUE, NULL);
24982 }
24983
24984 /* When skipping or the return gets pending, advance to the next command
24985 * in this line (!returning). Otherwise, ignore the rest of the line.
24986 * Following lines will be ignored by get_func_line(). */
24987 if (returning)
24988 eap->nextcmd = NULL;
24989 else if (eap->nextcmd == NULL) /* no argument */
24990 eap->nextcmd = check_nextcmd(arg);
24991
24992 if (eap->skip)
24993 --emsg_skip;
24994}
24995
24996/*
24997 * Return from a function. Possibly makes the return pending. Also called
24998 * for a pending return at the ":endtry" or after returning from an extra
24999 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025000 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025001 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002 * FALSE when the return gets pending.
25003 */
25004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025005do_return(
25006 exarg_T *eap,
25007 int reanimate,
25008 int is_cmd,
25009 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010{
25011 int idx;
25012 struct condstack *cstack = eap->cstack;
25013
25014 if (reanimate)
25015 /* Undo the return. */
25016 current_funccal->returned = FALSE;
25017
25018 /*
25019 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25020 * not in its finally clause (which then is to be executed next) is found.
25021 * In this case, make the ":return" pending for execution at the ":endtry".
25022 * Otherwise, return normally.
25023 */
25024 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25025 if (idx >= 0)
25026 {
25027 cstack->cs_pending[idx] = CSTP_RETURN;
25028
25029 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025030 /* A pending return again gets pending. "rettv" points to an
25031 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025033 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 else
25035 {
25036 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025037 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025039 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025041 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 {
25043 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025044 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025045 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 else
25047 EMSG(_(e_outofmem));
25048 }
25049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025050 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051
25052 if (reanimate)
25053 {
25054 /* The pending return value could be overwritten by a ":return"
25055 * without argument in a finally clause; reset the default
25056 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025057 current_funccal->rettv->v_type = VAR_NUMBER;
25058 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059 }
25060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025061 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 }
25063 else
25064 {
25065 current_funccal->returned = TRUE;
25066
25067 /* If the return is carried out now, store the return value. For
25068 * a return immediately after reanimation, the value is already
25069 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025070 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025072 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025073 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025075 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 }
25077 }
25078
25079 return idx < 0;
25080}
25081
25082/*
25083 * Free the variable with a pending return value.
25084 */
25085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025086discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087{
Bram Moolenaar33570922005-01-25 22:26:29 +000025088 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089}
25090
25091/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025092 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 * is an allocated string. Used by report_pending() for verbose messages.
25094 */
25095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025096get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025098 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025099 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025100 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025102 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025103 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025104 if (s == NULL)
25105 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025106
25107 STRCPY(IObuff, ":return ");
25108 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25109 if (STRLEN(s) + 8 >= IOSIZE)
25110 STRCPY(IObuff + IOSIZE - 4, "...");
25111 vim_free(tofree);
25112 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113}
25114
25115/*
25116 * Get next function line.
25117 * Called by do_cmdline() to get the next line.
25118 * Returns allocated string, or NULL for end of function.
25119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025121get_func_line(
25122 int c UNUSED,
25123 void *cookie,
25124 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125{
Bram Moolenaar33570922005-01-25 22:26:29 +000025126 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025127 ufunc_T *fp = fcp->func;
25128 char_u *retval;
25129 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130
25131 /* If breakpoints have been added/deleted need to check for it. */
25132 if (fcp->dbg_tick != debug_tick)
25133 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025134 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 sourcing_lnum);
25136 fcp->dbg_tick = debug_tick;
25137 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025138#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025139 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025140 func_line_end(cookie);
25141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142
Bram Moolenaar05159a02005-02-26 23:04:13 +000025143 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025144 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25145 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 retval = NULL;
25147 else
25148 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025149 /* Skip NULL lines (continuation lines). */
25150 while (fcp->linenr < gap->ga_len
25151 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25152 ++fcp->linenr;
25153 if (fcp->linenr >= gap->ga_len)
25154 retval = NULL;
25155 else
25156 {
25157 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25158 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025159#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025160 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025161 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025162#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164 }
25165
25166 /* Did we encounter a breakpoint? */
25167 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25168 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025169 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025171 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172 sourcing_lnum);
25173 fcp->dbg_tick = debug_tick;
25174 }
25175
25176 return retval;
25177}
25178
Bram Moolenaar05159a02005-02-26 23:04:13 +000025179#if defined(FEAT_PROFILE) || defined(PROTO)
25180/*
25181 * Called when starting to read a function line.
25182 * "sourcing_lnum" must be correct!
25183 * When skipping lines it may not actually be executed, but we won't find out
25184 * until later and we need to store the time now.
25185 */
25186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025187func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025188{
25189 funccall_T *fcp = (funccall_T *)cookie;
25190 ufunc_T *fp = fcp->func;
25191
25192 if (fp->uf_profiling && sourcing_lnum >= 1
25193 && sourcing_lnum <= fp->uf_lines.ga_len)
25194 {
25195 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025196 /* Skip continuation lines. */
25197 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25198 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025199 fp->uf_tml_execed = FALSE;
25200 profile_start(&fp->uf_tml_start);
25201 profile_zero(&fp->uf_tml_children);
25202 profile_get_wait(&fp->uf_tml_wait);
25203 }
25204}
25205
25206/*
25207 * Called when actually executing a function line.
25208 */
25209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025210func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025211{
25212 funccall_T *fcp = (funccall_T *)cookie;
25213 ufunc_T *fp = fcp->func;
25214
25215 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25216 fp->uf_tml_execed = TRUE;
25217}
25218
25219/*
25220 * Called when done with a function line.
25221 */
25222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025223func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025224{
25225 funccall_T *fcp = (funccall_T *)cookie;
25226 ufunc_T *fp = fcp->func;
25227
25228 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25229 {
25230 if (fp->uf_tml_execed)
25231 {
25232 ++fp->uf_tml_count[fp->uf_tml_idx];
25233 profile_end(&fp->uf_tml_start);
25234 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025235 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025236 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25237 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025238 }
25239 fp->uf_tml_idx = -1;
25240 }
25241}
25242#endif
25243
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244/*
25245 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025246 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 */
25248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025249func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250{
Bram Moolenaar33570922005-01-25 22:26:29 +000025251 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252
25253 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25254 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025255 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 || fcp->returned);
25257}
25258
25259/*
25260 * return TRUE if cookie indicates a function which "abort"s on errors.
25261 */
25262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025263func_has_abort(
25264 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025266 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267}
25268
25269#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25270typedef enum
25271{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025272 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25273 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25274 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275} var_flavour_T;
25276
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025277static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278
25279 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025280var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281{
25282 char_u *p = varname;
25283
25284 if (ASCII_ISUPPER(*p))
25285 {
25286 while (*(++p))
25287 if (ASCII_ISLOWER(*p))
25288 return VAR_FLAVOUR_SESSION;
25289 return VAR_FLAVOUR_VIMINFO;
25290 }
25291 else
25292 return VAR_FLAVOUR_DEFAULT;
25293}
25294#endif
25295
25296#if defined(FEAT_VIMINFO) || defined(PROTO)
25297/*
25298 * Restore global vars that start with a capital from the viminfo file
25299 */
25300 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025301read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302{
25303 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025304 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025305 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025306 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025307
25308 if (!writing && (find_viminfo_parameter('!') != NULL))
25309 {
25310 tab = vim_strchr(virp->vir_line + 1, '\t');
25311 if (tab != NULL)
25312 {
25313 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025314 switch (*tab)
25315 {
25316 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025317#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025318 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025319#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025320 case 'D': type = VAR_DICT; break;
25321 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025322 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324
25325 tab = vim_strchr(tab, '\t');
25326 if (tab != NULL)
25327 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025328 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025329 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025330 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025332#ifdef FEAT_FLOAT
25333 else if (type == VAR_FLOAT)
25334 (void)string2float(tab + 1, &tv.vval.v_float);
25335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025337 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025338 if (type == VAR_DICT || type == VAR_LIST)
25339 {
25340 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25341
25342 if (etv == NULL)
25343 /* Failed to parse back the dict or list, use it as a
25344 * string. */
25345 tv.v_type = VAR_STRING;
25346 else
25347 {
25348 vim_free(tv.vval.v_string);
25349 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025350 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025351 }
25352 }
25353
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025354 /* when in a function use global variables */
25355 save_funccal = current_funccal;
25356 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025357 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025358 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025359
25360 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025361 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025362 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25363 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 }
25365 }
25366 }
25367
25368 return viminfo_readline(virp);
25369}
25370
25371/*
25372 * Write global vars that start with a capital to the viminfo file
25373 */
25374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025375write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376{
Bram Moolenaar33570922005-01-25 22:26:29 +000025377 hashitem_T *hi;
25378 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025379 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025380 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025381 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025382 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025383 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384
25385 if (find_viminfo_parameter('!') == NULL)
25386 return;
25387
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025388 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025389
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025390 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025391 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025393 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025395 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025396 this_var = HI2DI(hi);
25397 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025398 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025399 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025400 {
25401 case VAR_STRING: s = "STR"; break;
25402 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025403 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025404 case VAR_DICT: s = "DIC"; break;
25405 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025406 case VAR_SPECIAL: s = "XPL"; break;
25407
25408 case VAR_UNKNOWN:
25409 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025410 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025411 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025412 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025413 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025414 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025415 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025416 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025417 if (p != NULL)
25418 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025419 vim_free(tofree);
25420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 }
25422 }
25423}
25424#endif
25425
25426#if defined(FEAT_SESSION) || defined(PROTO)
25427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025428store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429{
Bram Moolenaar33570922005-01-25 22:26:29 +000025430 hashitem_T *hi;
25431 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025432 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 char_u *p, *t;
25434
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025435 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025436 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025438 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025440 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025441 this_var = HI2DI(hi);
25442 if ((this_var->di_tv.v_type == VAR_NUMBER
25443 || this_var->di_tv.v_type == VAR_STRING)
25444 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025445 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025446 /* Escape special characters with a backslash. Turn a LF and
25447 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025448 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025449 (char_u *)"\\\"\n\r");
25450 if (p == NULL) /* out of memory */
25451 break;
25452 for (t = p; *t != NUL; ++t)
25453 if (*t == '\n')
25454 *t = 'n';
25455 else if (*t == '\r')
25456 *t = 'r';
25457 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025458 this_var->di_key,
25459 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25460 : ' ',
25461 p,
25462 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25463 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025464 || put_eol(fd) == FAIL)
25465 {
25466 vim_free(p);
25467 return FAIL;
25468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469 vim_free(p);
25470 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025471#ifdef FEAT_FLOAT
25472 else if (this_var->di_tv.v_type == VAR_FLOAT
25473 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25474 {
25475 float_T f = this_var->di_tv.vval.v_float;
25476 int sign = ' ';
25477
25478 if (f < 0)
25479 {
25480 f = -f;
25481 sign = '-';
25482 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025483 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025484 this_var->di_key, sign, f) < 0)
25485 || put_eol(fd) == FAIL)
25486 return FAIL;
25487 }
25488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 }
25490 }
25491 return OK;
25492}
25493#endif
25494
Bram Moolenaar661b1822005-07-28 22:36:45 +000025495/*
25496 * Display script name where an item was last set.
25497 * Should only be invoked when 'verbose' is non-zero.
25498 */
25499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025500last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025501{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025502 char_u *p;
25503
Bram Moolenaar661b1822005-07-28 22:36:45 +000025504 if (scriptID != 0)
25505 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025506 p = home_replace_save(NULL, get_scriptname(scriptID));
25507 if (p != NULL)
25508 {
25509 verbose_enter();
25510 MSG_PUTS(_("\n\tLast set from "));
25511 MSG_PUTS(p);
25512 vim_free(p);
25513 verbose_leave();
25514 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025515 }
25516}
25517
Bram Moolenaard812df62008-11-09 12:46:09 +000025518/*
25519 * List v:oldfiles in a nice way.
25520 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025522ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025523{
25524 list_T *l = vimvars[VV_OLDFILES].vv_list;
25525 listitem_T *li;
25526 int nr = 0;
25527
25528 if (l == NULL)
25529 msg((char_u *)_("No old files"));
25530 else
25531 {
25532 msg_start();
25533 msg_scroll = TRUE;
25534 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25535 {
25536 msg_outnum((long)++nr);
25537 MSG_PUTS(": ");
25538 msg_outtrans(get_tv_string(&li->li_tv));
25539 msg_putchar('\n');
25540 out_flush(); /* output one line at a time */
25541 ui_breakcheck();
25542 }
25543 /* Assume "got_int" was set to truncate the listing. */
25544 got_int = FALSE;
25545
25546#ifdef FEAT_BROWSE_CMD
25547 if (cmdmod.browse)
25548 {
25549 quit_more = FALSE;
25550 nr = prompt_for_number(FALSE);
25551 msg_starthere();
25552 if (nr > 0)
25553 {
25554 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25555 (long)nr);
25556
25557 if (p != NULL)
25558 {
25559 p = expand_env_save(p);
25560 eap->arg = p;
25561 eap->cmdidx = CMD_edit;
25562 cmdmod.browse = FALSE;
25563 do_exedit(eap, NULL);
25564 vim_free(p);
25565 }
25566 }
25567 }
25568#endif
25569 }
25570}
25571
Bram Moolenaar53744302015-07-17 17:38:22 +020025572/* reset v:option_new, v:option_old and v:option_type */
25573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025574reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025575{
25576 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25577 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25578 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25579}
25580
25581
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582#endif /* FEAT_EVAL */
25583
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025585#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586
25587#ifdef WIN3264
25588/*
25589 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25590 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025591static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25592static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25593static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594
25595/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025596 * Get the short path (8.3) for the filename in "fnamep".
25597 * Only works for a valid file name.
25598 * When the path gets longer "fnamep" is changed and the allocated buffer
25599 * is put in "bufp".
25600 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25601 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 */
25603 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025604get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025606 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 char_u *newbuf;
25608
25609 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025610 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 if (l > len - 1)
25612 {
25613 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025614 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615 newbuf = vim_strnsave(*fnamep, l);
25616 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025617 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618
25619 vim_free(*bufp);
25620 *fnamep = *bufp = newbuf;
25621
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025622 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025623 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624 }
25625
25626 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025627 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628}
25629
25630/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025631 * Get the short path (8.3) for the filename in "fname". The converted
25632 * path is returned in "bufp".
25633 *
25634 * Some of the directories specified in "fname" may not exist. This function
25635 * will shorten the existing directories at the beginning of the path and then
25636 * append the remaining non-existing path.
25637 *
25638 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025639 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025640 * bufp - Pointer to an allocated buffer for the filename.
25641 * fnamelen - Length of the filename pointed to by fname
25642 *
25643 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644 */
25645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025646shortpath_for_invalid_fname(
25647 char_u **fname,
25648 char_u **bufp,
25649 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025651 char_u *short_fname, *save_fname, *pbuf_unused;
25652 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025654 int old_len, len;
25655 int new_len, sfx_len;
25656 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657
25658 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025659 old_len = *fnamelen;
25660 save_fname = vim_strnsave(*fname, old_len);
25661 pbuf_unused = NULL;
25662 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025664 endp = save_fname + old_len - 1; /* Find the end of the copy */
25665 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025667 /*
25668 * Try shortening the supplied path till it succeeds by removing one
25669 * directory at a time from the tail of the path.
25670 */
25671 len = 0;
25672 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025674 /* go back one path-separator */
25675 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25676 --endp;
25677 if (endp <= save_fname)
25678 break; /* processed the complete path */
25679
25680 /*
25681 * Replace the path separator with a NUL and try to shorten the
25682 * resulting path.
25683 */
25684 ch = *endp;
25685 *endp = 0;
25686 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025687 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025688 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25689 {
25690 retval = FAIL;
25691 goto theend;
25692 }
25693 *endp = ch; /* preserve the string */
25694
25695 if (len > 0)
25696 break; /* successfully shortened the path */
25697
25698 /* failed to shorten the path. Skip the path separator */
25699 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700 }
25701
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025702 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025704 /*
25705 * Succeeded in shortening the path. Now concatenate the shortened
25706 * path with the remaining path at the tail.
25707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025708
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025709 /* Compute the length of the new path. */
25710 sfx_len = (int)(save_endp - endp) + 1;
25711 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025713 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025714 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025715 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025717 /* There is not enough space in the currently allocated string,
25718 * copy it to a buffer big enough. */
25719 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025721 {
25722 retval = FAIL;
25723 goto theend;
25724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 }
25726 else
25727 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025728 /* Transfer short_fname to the main buffer (it's big enough),
25729 * unless get_short_pathname() did its work in-place. */
25730 *fname = *bufp = save_fname;
25731 if (short_fname != save_fname)
25732 vim_strncpy(save_fname, short_fname, len);
25733 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025735
25736 /* concat the not-shortened part of the path */
25737 vim_strncpy(*fname + len, endp, sfx_len);
25738 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025739 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025740
25741theend:
25742 vim_free(pbuf_unused);
25743 vim_free(save_fname);
25744
25745 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746}
25747
25748/*
25749 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025750 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751 */
25752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025753shortpath_for_partial(
25754 char_u **fnamep,
25755 char_u **bufp,
25756 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025757{
25758 int sepcount, len, tflen;
25759 char_u *p;
25760 char_u *pbuf, *tfname;
25761 int hasTilde;
25762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025763 /* Count up the path separators from the RHS.. so we know which part
25764 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025766 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767 if (vim_ispathsep(*p))
25768 ++sepcount;
25769
25770 /* Need full path first (use expand_env() to remove a "~/") */
25771 hasTilde = (**fnamep == '~');
25772 if (hasTilde)
25773 pbuf = tfname = expand_env_save(*fnamep);
25774 else
25775 pbuf = tfname = FullName_save(*fnamep, FALSE);
25776
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025777 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025779 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25780 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781
25782 if (len == 0)
25783 {
25784 /* Don't have a valid filename, so shorten the rest of the
25785 * path if we can. This CAN give us invalid 8.3 filenames, but
25786 * there's not a lot of point in guessing what it might be.
25787 */
25788 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025789 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25790 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791 }
25792
25793 /* Count the paths backward to find the beginning of the desired string. */
25794 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025795 {
25796#ifdef FEAT_MBYTE
25797 if (has_mbyte)
25798 p -= mb_head_off(tfname, p);
25799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800 if (vim_ispathsep(*p))
25801 {
25802 if (sepcount == 0 || (hasTilde && sepcount == 1))
25803 break;
25804 else
25805 sepcount --;
25806 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808 if (hasTilde)
25809 {
25810 --p;
25811 if (p >= tfname)
25812 *p = '~';
25813 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025814 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815 }
25816 else
25817 ++p;
25818
25819 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25820 vim_free(*bufp);
25821 *fnamelen = (int)STRLEN(p);
25822 *bufp = pbuf;
25823 *fnamep = p;
25824
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025825 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826}
25827#endif /* WIN3264 */
25828
25829/*
25830 * Adjust a filename, according to a string of modifiers.
25831 * *fnamep must be NUL terminated when called. When returning, the length is
25832 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025833 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 * When there is an error, *fnamep is set to NULL.
25835 */
25836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025837modify_fname(
25838 char_u *src, /* string with modifiers */
25839 int *usedlen, /* characters after src that are used */
25840 char_u **fnamep, /* file name so far */
25841 char_u **bufp, /* buffer for allocated file name or NULL */
25842 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843{
25844 int valid = 0;
25845 char_u *tail;
25846 char_u *s, *p, *pbuf;
25847 char_u dirname[MAXPATHL];
25848 int c;
25849 int has_fullname = 0;
25850#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025851 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 int has_shortname = 0;
25853#endif
25854
25855repeat:
25856 /* ":p" - full path/file_name */
25857 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25858 {
25859 has_fullname = 1;
25860
25861 valid |= VALID_PATH;
25862 *usedlen += 2;
25863
25864 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25865 if ((*fnamep)[0] == '~'
25866#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25867 && ((*fnamep)[1] == '/'
25868# ifdef BACKSLASH_IN_FILENAME
25869 || (*fnamep)[1] == '\\'
25870# endif
25871 || (*fnamep)[1] == NUL)
25872
25873#endif
25874 )
25875 {
25876 *fnamep = expand_env_save(*fnamep);
25877 vim_free(*bufp); /* free any allocated file name */
25878 *bufp = *fnamep;
25879 if (*fnamep == NULL)
25880 return -1;
25881 }
25882
25883 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025884 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885 {
25886 if (vim_ispathsep(*p)
25887 && p[1] == '.'
25888 && (p[2] == NUL
25889 || vim_ispathsep(p[2])
25890 || (p[2] == '.'
25891 && (p[3] == NUL || vim_ispathsep(p[3])))))
25892 break;
25893 }
25894
25895 /* FullName_save() is slow, don't use it when not needed. */
25896 if (*p != NUL || !vim_isAbsName(*fnamep))
25897 {
25898 *fnamep = FullName_save(*fnamep, *p != NUL);
25899 vim_free(*bufp); /* free any allocated file name */
25900 *bufp = *fnamep;
25901 if (*fnamep == NULL)
25902 return -1;
25903 }
25904
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025905#ifdef WIN3264
25906# if _WIN32_WINNT >= 0x0500
25907 if (vim_strchr(*fnamep, '~') != NULL)
25908 {
25909 /* Expand 8.3 filename to full path. Needed to make sure the same
25910 * file does not have two different names.
25911 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25912 p = alloc(_MAX_PATH + 1);
25913 if (p != NULL)
25914 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025915 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025916 {
25917 vim_free(*bufp);
25918 *bufp = *fnamep = p;
25919 }
25920 else
25921 vim_free(p);
25922 }
25923 }
25924# endif
25925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 /* Append a path separator to a directory. */
25927 if (mch_isdir(*fnamep))
25928 {
25929 /* Make room for one or two extra characters. */
25930 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25931 vim_free(*bufp); /* free any allocated file name */
25932 *bufp = *fnamep;
25933 if (*fnamep == NULL)
25934 return -1;
25935 add_pathsep(*fnamep);
25936 }
25937 }
25938
25939 /* ":." - path relative to the current directory */
25940 /* ":~" - path relative to the home directory */
25941 /* ":8" - shortname path - postponed till after */
25942 while (src[*usedlen] == ':'
25943 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25944 {
25945 *usedlen += 2;
25946 if (c == '8')
25947 {
25948#ifdef WIN3264
25949 has_shortname = 1; /* Postpone this. */
25950#endif
25951 continue;
25952 }
25953 pbuf = NULL;
25954 /* Need full path first (use expand_env() to remove a "~/") */
25955 if (!has_fullname)
25956 {
25957 if (c == '.' && **fnamep == '~')
25958 p = pbuf = expand_env_save(*fnamep);
25959 else
25960 p = pbuf = FullName_save(*fnamep, FALSE);
25961 }
25962 else
25963 p = *fnamep;
25964
25965 has_fullname = 0;
25966
25967 if (p != NULL)
25968 {
25969 if (c == '.')
25970 {
25971 mch_dirname(dirname, MAXPATHL);
25972 s = shorten_fname(p, dirname);
25973 if (s != NULL)
25974 {
25975 *fnamep = s;
25976 if (pbuf != NULL)
25977 {
25978 vim_free(*bufp); /* free any allocated file name */
25979 *bufp = pbuf;
25980 pbuf = NULL;
25981 }
25982 }
25983 }
25984 else
25985 {
25986 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25987 /* Only replace it when it starts with '~' */
25988 if (*dirname == '~')
25989 {
25990 s = vim_strsave(dirname);
25991 if (s != NULL)
25992 {
25993 *fnamep = s;
25994 vim_free(*bufp);
25995 *bufp = s;
25996 }
25997 }
25998 }
25999 vim_free(pbuf);
26000 }
26001 }
26002
26003 tail = gettail(*fnamep);
26004 *fnamelen = (int)STRLEN(*fnamep);
26005
26006 /* ":h" - head, remove "/file_name", can be repeated */
26007 /* Don't remove the first "/" or "c:\" */
26008 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26009 {
26010 valid |= VALID_HEAD;
26011 *usedlen += 2;
26012 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026013 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026014 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 *fnamelen = (int)(tail - *fnamep);
26016#ifdef VMS
26017 if (*fnamelen > 0)
26018 *fnamelen += 1; /* the path separator is part of the path */
26019#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026020 if (*fnamelen == 0)
26021 {
26022 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26023 p = vim_strsave((char_u *)".");
26024 if (p == NULL)
26025 return -1;
26026 vim_free(*bufp);
26027 *bufp = *fnamep = tail = p;
26028 *fnamelen = 1;
26029 }
26030 else
26031 {
26032 while (tail > s && !after_pathsep(s, tail))
26033 mb_ptr_back(*fnamep, tail);
26034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 }
26036
26037 /* ":8" - shortname */
26038 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26039 {
26040 *usedlen += 2;
26041#ifdef WIN3264
26042 has_shortname = 1;
26043#endif
26044 }
26045
26046#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026047 /*
26048 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 */
26050 if (has_shortname)
26051 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026052 /* Copy the string if it is shortened by :h and when it wasn't copied
26053 * yet, because we are going to change it in place. Avoids changing
26054 * the buffer name for "%:8". */
26055 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056 {
26057 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026058 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 return -1;
26060 vim_free(*bufp);
26061 *bufp = *fnamep = p;
26062 }
26063
26064 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026065 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066 if (!has_fullname && !vim_isAbsName(*fnamep))
26067 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026068 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069 return -1;
26070 }
26071 else
26072 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026073 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074
Bram Moolenaardc935552011-08-17 15:23:23 +020026075 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026077 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026078 return -1;
26079
26080 if (l == 0)
26081 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026082 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026084 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085 return -1;
26086 }
26087 *fnamelen = l;
26088 }
26089 }
26090#endif /* WIN3264 */
26091
26092 /* ":t" - tail, just the basename */
26093 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26094 {
26095 *usedlen += 2;
26096 *fnamelen -= (int)(tail - *fnamep);
26097 *fnamep = tail;
26098 }
26099
26100 /* ":e" - extension, can be repeated */
26101 /* ":r" - root, without extension, can be repeated */
26102 while (src[*usedlen] == ':'
26103 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26104 {
26105 /* find a '.' in the tail:
26106 * - for second :e: before the current fname
26107 * - otherwise: The last '.'
26108 */
26109 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26110 s = *fnamep - 2;
26111 else
26112 s = *fnamep + *fnamelen - 1;
26113 for ( ; s > tail; --s)
26114 if (s[0] == '.')
26115 break;
26116 if (src[*usedlen + 1] == 'e') /* :e */
26117 {
26118 if (s > tail)
26119 {
26120 *fnamelen += (int)(*fnamep - (s + 1));
26121 *fnamep = s + 1;
26122#ifdef VMS
26123 /* cut version from the extension */
26124 s = *fnamep + *fnamelen - 1;
26125 for ( ; s > *fnamep; --s)
26126 if (s[0] == ';')
26127 break;
26128 if (s > *fnamep)
26129 *fnamelen = s - *fnamep;
26130#endif
26131 }
26132 else if (*fnamep <= tail)
26133 *fnamelen = 0;
26134 }
26135 else /* :r */
26136 {
26137 if (s > tail) /* remove one extension */
26138 *fnamelen = (int)(s - *fnamep);
26139 }
26140 *usedlen += 2;
26141 }
26142
26143 /* ":s?pat?foo?" - substitute */
26144 /* ":gs?pat?foo?" - global substitute */
26145 if (src[*usedlen] == ':'
26146 && (src[*usedlen + 1] == 's'
26147 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26148 {
26149 char_u *str;
26150 char_u *pat;
26151 char_u *sub;
26152 int sep;
26153 char_u *flags;
26154 int didit = FALSE;
26155
26156 flags = (char_u *)"";
26157 s = src + *usedlen + 2;
26158 if (src[*usedlen + 1] == 'g')
26159 {
26160 flags = (char_u *)"g";
26161 ++s;
26162 }
26163
26164 sep = *s++;
26165 if (sep)
26166 {
26167 /* find end of pattern */
26168 p = vim_strchr(s, sep);
26169 if (p != NULL)
26170 {
26171 pat = vim_strnsave(s, (int)(p - s));
26172 if (pat != NULL)
26173 {
26174 s = p + 1;
26175 /* find end of substitution */
26176 p = vim_strchr(s, sep);
26177 if (p != NULL)
26178 {
26179 sub = vim_strnsave(s, (int)(p - s));
26180 str = vim_strnsave(*fnamep, *fnamelen);
26181 if (sub != NULL && str != NULL)
26182 {
26183 *usedlen = (int)(p + 1 - src);
26184 s = do_string_sub(str, pat, sub, flags);
26185 if (s != NULL)
26186 {
26187 *fnamep = s;
26188 *fnamelen = (int)STRLEN(s);
26189 vim_free(*bufp);
26190 *bufp = s;
26191 didit = TRUE;
26192 }
26193 }
26194 vim_free(sub);
26195 vim_free(str);
26196 }
26197 vim_free(pat);
26198 }
26199 }
26200 /* after using ":s", repeat all the modifiers */
26201 if (didit)
26202 goto repeat;
26203 }
26204 }
26205
Bram Moolenaar26df0922014-02-23 23:39:13 +010026206 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26207 {
26208 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26209 if (p == NULL)
26210 return -1;
26211 vim_free(*bufp);
26212 *bufp = *fnamep = p;
26213 *fnamelen = (int)STRLEN(p);
26214 *usedlen += 2;
26215 }
26216
Bram Moolenaar071d4272004-06-13 20:20:40 +000026217 return valid;
26218}
26219
26220/*
26221 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26222 * "flags" can be "g" to do a global substitute.
26223 * Returns an allocated string, NULL for error.
26224 */
26225 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026226do_string_sub(
26227 char_u *str,
26228 char_u *pat,
26229 char_u *sub,
26230 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026231{
26232 int sublen;
26233 regmatch_T regmatch;
26234 int i;
26235 int do_all;
26236 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026237 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238 garray_T ga;
26239 char_u *ret;
26240 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026241 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026242
26243 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26244 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026245 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246
26247 ga_init2(&ga, 1, 200);
26248
26249 do_all = (flags[0] == 'g');
26250
26251 regmatch.rm_ic = p_ic;
26252 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26253 if (regmatch.regprog != NULL)
26254 {
26255 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026256 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26258 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026259 /* Skip empty match except for first match. */
26260 if (regmatch.startp[0] == regmatch.endp[0])
26261 {
26262 if (zero_width == regmatch.startp[0])
26263 {
26264 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026265 i = MB_PTR2LEN(tail);
26266 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26267 (size_t)i);
26268 ga.ga_len += i;
26269 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026270 continue;
26271 }
26272 zero_width = regmatch.startp[0];
26273 }
26274
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275 /*
26276 * Get some space for a temporary buffer to do the substitution
26277 * into. It will contain:
26278 * - The text up to where the match is.
26279 * - The substituted text.
26280 * - The text after the match.
26281 */
26282 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026283 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26285 {
26286 ga_clear(&ga);
26287 break;
26288 }
26289
26290 /* copy the text up to where the match is */
26291 i = (int)(regmatch.startp[0] - tail);
26292 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26293 /* add the substituted text */
26294 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26295 + ga.ga_len + i, TRUE, TRUE, FALSE);
26296 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026297 tail = regmatch.endp[0];
26298 if (*tail == NUL)
26299 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300 if (!do_all)
26301 break;
26302 }
26303
26304 if (ga.ga_data != NULL)
26305 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26306
Bram Moolenaar473de612013-06-08 18:19:48 +020026307 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308 }
26309
26310 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26311 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026312 if (p_cpo == empty_option)
26313 p_cpo = save_cpo;
26314 else
26315 /* Darn, evaluating {sub} expression changed the value. */
26316 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317
26318 return ret;
26319}
26320
26321#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */