blob: 10f57e8b10333baa2befbf13f4e64c9d09faca42 [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 Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static 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, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
507static void f_ch_close(typval_T *argvars, typval_T *rettv);
508static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
509static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
510#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100511static void f_changenr(typval_T *argvars, typval_T *rettv);
512static void f_char2nr(typval_T *argvars, typval_T *rettv);
513static void f_cindent(typval_T *argvars, typval_T *rettv);
514static void f_clearmatches(typval_T *argvars, typval_T *rettv);
515static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000516#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_complete(typval_T *argvars, typval_T *rettv);
518static void f_complete_add(typval_T *argvars, typval_T *rettv);
519static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_confirm(typval_T *argvars, typval_T *rettv);
522static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_cos(typval_T *argvars, typval_T *rettv);
525static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_count(typval_T *argvars, typval_T *rettv);
528static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
529static void f_cursor(typval_T *argsvars, typval_T *rettv);
530static void f_deepcopy(typval_T *argvars, typval_T *rettv);
531static void f_delete(typval_T *argvars, typval_T *rettv);
532static void f_did_filetype(typval_T *argvars, typval_T *rettv);
533static void f_diff_filler(typval_T *argvars, typval_T *rettv);
534static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100535static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_empty(typval_T *argvars, typval_T *rettv);
537static void f_escape(typval_T *argvars, typval_T *rettv);
538static void f_eval(typval_T *argvars, typval_T *rettv);
539static void f_eventhandler(typval_T *argvars, typval_T *rettv);
540static void f_executable(typval_T *argvars, typval_T *rettv);
541static void f_exepath(typval_T *argvars, typval_T *rettv);
542static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100546static void f_expand(typval_T *argvars, typval_T *rettv);
547static void f_extend(typval_T *argvars, typval_T *rettv);
548static void f_feedkeys(typval_T *argvars, typval_T *rettv);
549static void f_filereadable(typval_T *argvars, typval_T *rettv);
550static void f_filewritable(typval_T *argvars, typval_T *rettv);
551static void f_filter(typval_T *argvars, typval_T *rettv);
552static void f_finddir(typval_T *argvars, typval_T *rettv);
553static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_float2nr(typval_T *argvars, typval_T *rettv);
556static void f_floor(typval_T *argvars, typval_T *rettv);
557static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000558#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_fnameescape(typval_T *argvars, typval_T *rettv);
560static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
561static void f_foldclosed(typval_T *argvars, typval_T *rettv);
562static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
563static void f_foldlevel(typval_T *argvars, typval_T *rettv);
564static void f_foldtext(typval_T *argvars, typval_T *rettv);
565static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
566static void f_foreground(typval_T *argvars, typval_T *rettv);
567static void f_function(typval_T *argvars, typval_T *rettv);
568static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
569static void f_get(typval_T *argvars, typval_T *rettv);
570static void f_getbufline(typval_T *argvars, typval_T *rettv);
571static void f_getbufvar(typval_T *argvars, typval_T *rettv);
572static void f_getchar(typval_T *argvars, typval_T *rettv);
573static void f_getcharmod(typval_T *argvars, typval_T *rettv);
574static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
575static void f_getcmdline(typval_T *argvars, typval_T *rettv);
576static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
577static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
578static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
579static void f_getcwd(typval_T *argvars, typval_T *rettv);
580static void f_getfontname(typval_T *argvars, typval_T *rettv);
581static void f_getfperm(typval_T *argvars, typval_T *rettv);
582static void f_getfsize(typval_T *argvars, typval_T *rettv);
583static void f_getftime(typval_T *argvars, typval_T *rettv);
584static void f_getftype(typval_T *argvars, typval_T *rettv);
585static void f_getline(typval_T *argvars, typval_T *rettv);
586static void f_getmatches(typval_T *argvars, typval_T *rettv);
587static void f_getpid(typval_T *argvars, typval_T *rettv);
588static void f_getcurpos(typval_T *argvars, typval_T *rettv);
589static void f_getpos(typval_T *argvars, typval_T *rettv);
590static void f_getqflist(typval_T *argvars, typval_T *rettv);
591static void f_getreg(typval_T *argvars, typval_T *rettv);
592static void f_getregtype(typval_T *argvars, typval_T *rettv);
593static void f_gettabvar(typval_T *argvars, typval_T *rettv);
594static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
595static void f_getwinposx(typval_T *argvars, typval_T *rettv);
596static void f_getwinposy(typval_T *argvars, typval_T *rettv);
597static void f_getwinvar(typval_T *argvars, typval_T *rettv);
598static void f_glob(typval_T *argvars, typval_T *rettv);
599static void f_globpath(typval_T *argvars, typval_T *rettv);
600static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
601static void f_has(typval_T *argvars, typval_T *rettv);
602static void f_has_key(typval_T *argvars, typval_T *rettv);
603static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
604static void f_hasmapto(typval_T *argvars, typval_T *rettv);
605static void f_histadd(typval_T *argvars, typval_T *rettv);
606static void f_histdel(typval_T *argvars, typval_T *rettv);
607static void f_histget(typval_T *argvars, typval_T *rettv);
608static void f_histnr(typval_T *argvars, typval_T *rettv);
609static void f_hlID(typval_T *argvars, typval_T *rettv);
610static void f_hlexists(typval_T *argvars, typval_T *rettv);
611static void f_hostname(typval_T *argvars, typval_T *rettv);
612static void f_iconv(typval_T *argvars, typval_T *rettv);
613static void f_indent(typval_T *argvars, typval_T *rettv);
614static void f_index(typval_T *argvars, typval_T *rettv);
615static void f_input(typval_T *argvars, typval_T *rettv);
616static void f_inputdialog(typval_T *argvars, typval_T *rettv);
617static void f_inputlist(typval_T *argvars, typval_T *rettv);
618static void f_inputrestore(typval_T *argvars, typval_T *rettv);
619static void f_inputsave(typval_T *argvars, typval_T *rettv);
620static void f_inputsecret(typval_T *argvars, typval_T *rettv);
621static void f_insert(typval_T *argvars, typval_T *rettv);
622static void f_invert(typval_T *argvars, typval_T *rettv);
623static void f_isdirectory(typval_T *argvars, typval_T *rettv);
624static void f_islocked(typval_T *argvars, typval_T *rettv);
625static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100626#ifdef FEAT_JOB
627static void f_job_start(typval_T *argvars, typval_T *rettv);
628static void f_job_stop(typval_T *argvars, typval_T *rettv);
629static void f_job_status(typval_T *argvars, typval_T *rettv);
630#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100631static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100632static void f_jsdecode(typval_T *argvars, typval_T *rettv);
633static void f_jsencode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_jsondecode(typval_T *argvars, typval_T *rettv);
635static void f_jsonencode(typval_T *argvars, typval_T *rettv);
636static void f_keys(typval_T *argvars, typval_T *rettv);
637static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
638static void f_len(typval_T *argvars, typval_T *rettv);
639static void f_libcall(typval_T *argvars, typval_T *rettv);
640static void f_libcallnr(typval_T *argvars, typval_T *rettv);
641static void f_line(typval_T *argvars, typval_T *rettv);
642static void f_line2byte(typval_T *argvars, typval_T *rettv);
643static void f_lispindent(typval_T *argvars, typval_T *rettv);
644static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_log(typval_T *argvars, typval_T *rettv);
647static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200649#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100650static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200651#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_map(typval_T *argvars, typval_T *rettv);
653static void f_maparg(typval_T *argvars, typval_T *rettv);
654static void f_mapcheck(typval_T *argvars, typval_T *rettv);
655static void f_match(typval_T *argvars, typval_T *rettv);
656static void f_matchadd(typval_T *argvars, typval_T *rettv);
657static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
658static void f_matcharg(typval_T *argvars, typval_T *rettv);
659static void f_matchdelete(typval_T *argvars, typval_T *rettv);
660static void f_matchend(typval_T *argvars, typval_T *rettv);
661static void f_matchlist(typval_T *argvars, typval_T *rettv);
662static void f_matchstr(typval_T *argvars, typval_T *rettv);
663static void f_max(typval_T *argvars, typval_T *rettv);
664static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000665#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000667#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100668static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100669#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100671#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
673static void f_nr2char(typval_T *argvars, typval_T *rettv);
674static void f_or(typval_T *argvars, typval_T *rettv);
675static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100676#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100678#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000679#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000681#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
683static void f_printf(typval_T *argvars, typval_T *rettv);
684static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200685#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200687#endif
688#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_range(typval_T *argvars, typval_T *rettv);
692static void f_readfile(typval_T *argvars, typval_T *rettv);
693static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100694#ifdef FEAT_FLOAT
695static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_reltimestr(typval_T *argvars, typval_T *rettv);
698static void f_remote_expr(typval_T *argvars, typval_T *rettv);
699static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
700static void f_remote_peek(typval_T *argvars, typval_T *rettv);
701static void f_remote_read(typval_T *argvars, typval_T *rettv);
702static void f_remote_send(typval_T *argvars, typval_T *rettv);
703static void f_remove(typval_T *argvars, typval_T *rettv);
704static void f_rename(typval_T *argvars, typval_T *rettv);
705static void f_repeat(typval_T *argvars, typval_T *rettv);
706static void f_resolve(typval_T *argvars, typval_T *rettv);
707static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000708#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000710#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_screenattr(typval_T *argvars, typval_T *rettv);
712static void f_screenchar(typval_T *argvars, typval_T *rettv);
713static void f_screencol(typval_T *argvars, typval_T *rettv);
714static void f_screenrow(typval_T *argvars, typval_T *rettv);
715static void f_search(typval_T *argvars, typval_T *rettv);
716static void f_searchdecl(typval_T *argvars, typval_T *rettv);
717static void f_searchpair(typval_T *argvars, typval_T *rettv);
718static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
719static void f_searchpos(typval_T *argvars, typval_T *rettv);
720static void f_server2client(typval_T *argvars, typval_T *rettv);
721static void f_serverlist(typval_T *argvars, typval_T *rettv);
722static void f_setbufvar(typval_T *argvars, typval_T *rettv);
723static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
724static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
725static void f_setline(typval_T *argvars, typval_T *rettv);
726static void f_setloclist(typval_T *argvars, typval_T *rettv);
727static void f_setmatches(typval_T *argvars, typval_T *rettv);
728static void f_setpos(typval_T *argvars, typval_T *rettv);
729static void f_setqflist(typval_T *argvars, typval_T *rettv);
730static void f_setreg(typval_T *argvars, typval_T *rettv);
731static void f_settabvar(typval_T *argvars, typval_T *rettv);
732static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
733static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100734#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100736#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100737static void f_shellescape(typval_T *argvars, typval_T *rettv);
738static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
739static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000740#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_sin(typval_T *argvars, typval_T *rettv);
742static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000743#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_sort(typval_T *argvars, typval_T *rettv);
745static void f_soundfold(typval_T *argvars, typval_T *rettv);
746static void f_spellbadword(typval_T *argvars, typval_T *rettv);
747static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
748static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000749#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_sqrt(typval_T *argvars, typval_T *rettv);
751static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000752#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_str2nr(typval_T *argvars, typval_T *rettv);
754static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000755#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000757#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_stridx(typval_T *argvars, typval_T *rettv);
759static void f_string(typval_T *argvars, typval_T *rettv);
760static void f_strlen(typval_T *argvars, typval_T *rettv);
761static void f_strpart(typval_T *argvars, typval_T *rettv);
762static void f_strridx(typval_T *argvars, typval_T *rettv);
763static void f_strtrans(typval_T *argvars, typval_T *rettv);
764static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
765static void f_strwidth(typval_T *argvars, typval_T *rettv);
766static void f_submatch(typval_T *argvars, typval_T *rettv);
767static void f_substitute(typval_T *argvars, typval_T *rettv);
768static void f_synID(typval_T *argvars, typval_T *rettv);
769static void f_synIDattr(typval_T *argvars, typval_T *rettv);
770static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
771static void f_synstack(typval_T *argvars, typval_T *rettv);
772static void f_synconcealed(typval_T *argvars, typval_T *rettv);
773static void f_system(typval_T *argvars, typval_T *rettv);
774static void f_systemlist(typval_T *argvars, typval_T *rettv);
775static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
776static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
777static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
778static void f_taglist(typval_T *argvars, typval_T *rettv);
779static void f_tagfiles(typval_T *argvars, typval_T *rettv);
780static void f_tempname(typval_T *argvars, typval_T *rettv);
781static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200782#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100783static void f_tan(typval_T *argvars, typval_T *rettv);
784static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200785#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_tolower(typval_T *argvars, typval_T *rettv);
787static void f_toupper(typval_T *argvars, typval_T *rettv);
788static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000789#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000791#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100792static void f_type(typval_T *argvars, typval_T *rettv);
793static void f_undofile(typval_T *argvars, typval_T *rettv);
794static void f_undotree(typval_T *argvars, typval_T *rettv);
795static void f_uniq(typval_T *argvars, typval_T *rettv);
796static void f_values(typval_T *argvars, typval_T *rettv);
797static void f_virtcol(typval_T *argvars, typval_T *rettv);
798static void f_visualmode(typval_T *argvars, typval_T *rettv);
799static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
800static void f_winbufnr(typval_T *argvars, typval_T *rettv);
801static void f_wincol(typval_T *argvars, typval_T *rettv);
802static void f_winheight(typval_T *argvars, typval_T *rettv);
803static void f_winline(typval_T *argvars, typval_T *rettv);
804static void f_winnr(typval_T *argvars, typval_T *rettv);
805static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
806static void f_winrestview(typval_T *argvars, typval_T *rettv);
807static void f_winsaveview(typval_T *argvars, typval_T *rettv);
808static void f_winwidth(typval_T *argvars, typval_T *rettv);
809static void f_writefile(typval_T *argvars, typval_T *rettv);
810static void f_wordcount(typval_T *argvars, typval_T *rettv);
811static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000812
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
814static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
815static int get_env_len(char_u **arg);
816static int get_id_len(char_u **arg);
817static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
818static 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 +0000819#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
820#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
821 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
823static int eval_isnamec(int c);
824static int eval_isnamec1(int c);
825static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
826static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static typval_T *alloc_string_tv(char_u *string);
828static void init_tv(typval_T *varp);
829static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100830#ifdef FEAT_FLOAT
831static float_T get_tv_float(typval_T *varp);
832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static linenr_T get_tv_lnum(typval_T *argvars);
834static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
835static char_u *get_tv_string(typval_T *varp);
836static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
837static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
838static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
839static hashtab_T *find_var_ht(char_u *name, char_u **varname);
840static funccall_T *get_funccal(void);
841static void vars_clear_ext(hashtab_T *ht, int free_val);
842static void delete_var(hashtab_T *ht, hashitem_T *hi);
843static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
844static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
845static void set_var(char_u *name, typval_T *varp, int copy);
846static int var_check_ro(int flags, char_u *name, int use_gettext);
847static int var_check_fixed(int flags, char_u *name, int use_gettext);
848static int var_check_func_name(char_u *name, int new_var);
849static int valid_varname(char_u *varname);
850static int tv_check_lock(int lock, char_u *name, int use_gettext);
851static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
852static char_u *find_option_end(char_u **arg, int *opt_flags);
853static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
854static int eval_fname_script(char_u *p);
855static int eval_fname_sid(char_u *p);
856static void list_func_head(ufunc_T *fp, int indent);
857static ufunc_T *find_func(char_u *name);
858static int function_exists(char_u *name);
859static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100861static void func_do_profile(ufunc_T *fp);
862static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
863static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000864static int
865# ifdef __BORLANDC__
866 _RTLENTRYF
867# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000869static int
870# ifdef __BORLANDC__
871 _RTLENTRYF
872# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100875static int script_autoload(char_u *name, int reload);
876static char_u *autoload_name(char_u *name);
877static void cat_func_name(char_u *buf, ufunc_T *fp);
878static void func_free(ufunc_T *fp);
879static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
880static int can_free_funccal(funccall_T *fc, int copyID) ;
881static void free_funccal(funccall_T *fc, int free_val);
882static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
883static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
884static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
885static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
886static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
887static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
888static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
889static int write_list(FILE *fd, list_T *list, int binary);
890static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000891
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100894static int compare_func_name(const void *s1, const void *s2);
895static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896#endif
897
Bram Moolenaar33570922005-01-25 22:26:29 +0000898/*
899 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000900 */
901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100902eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000903{
Bram Moolenaar33570922005-01-25 22:26:29 +0000904 int i;
905 struct vimvar *p;
906
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200907 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
908 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200909 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000910 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000911 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000912
913 for (i = 0; i < VV_LEN; ++i)
914 {
915 p = &vimvars[i];
916 STRCPY(p->vv_di.di_key, p->vv_name);
917 if (p->vv_flags & VV_RO)
918 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
919 else if (p->vv_flags & VV_RO_SBX)
920 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
921 else
922 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000923
924 /* add to v: scope dict, unless the value is not always available */
925 if (p->vv_type != VAR_UNKNOWN)
926 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000927 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000928 /* add to compat scope dict */
929 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000930 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100931 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
932
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000933 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100934 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200935 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100936 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100937
938 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
939 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
940 set_vim_var_nr(VV_NONE, VVAL_NONE);
941 set_vim_var_nr(VV_NULL, VVAL_NULL);
942
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200943 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200944
945#ifdef EBCDIC
946 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100947 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200948 */
949 sortFunctions();
950#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000951}
952
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000953#if defined(EXITFREE) || defined(PROTO)
954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100955eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000956{
957 int i;
958 struct vimvar *p;
959
960 for (i = 0; i < VV_LEN; ++i)
961 {
962 p = &vimvars[i];
963 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000964 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000965 vim_free(p->vv_str);
966 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000967 }
968 else if (p->vv_di.di_tv.v_type == VAR_LIST)
969 {
970 list_unref(p->vv_list);
971 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000972 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000973 }
974 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000975 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000976 hash_clear(&compat_hashtab);
977
Bram Moolenaard9fba312005-06-26 22:34:35 +0000978 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100979# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200980 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100981# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000982
983 /* global variables */
984 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000986 /* autoloaded script names */
987 ga_clear_strings(&ga_loaded);
988
Bram Moolenaarcca74132013-09-25 21:00:28 +0200989 /* Script-local variables. First clear all the variables and in a second
990 * loop free the scriptvar_T, because a variable in one script might hold
991 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200992 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200993 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200994 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200995 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200996 ga_clear(&ga_scripts);
997
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000998 /* unreferenced lists and dicts */
999 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001000
1001 /* functions */
1002 free_all_functions();
1003 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004}
1005#endif
1006
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 * Return the name of the executed function.
1009 */
1010 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001011func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014}
1015
1016/*
1017 * Return the address holding the next breakpoint line for a funccall cookie.
1018 */
1019 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001020func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023}
1024
1025/*
1026 * Return the address holding the debug tick for a funccall cookie.
1027 */
1028 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001029func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
Bram Moolenaar33570922005-01-25 22:26:29 +00001031 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
1033
1034/*
1035 * Return the nesting level for a funccall cookie.
1036 */
1037 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001038func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
Bram Moolenaar33570922005-01-25 22:26:29 +00001040 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041}
1042
1043/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001044funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001046/* pointer to list of previously used funccal, still around because some
1047 * item in it is still being used. */
1048funccall_T *previous_funccal = NULL;
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050/*
1051 * Return TRUE when a function was ended by a ":return" command.
1052 */
1053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001054current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056 return current_funccal->returned;
1057}
1058
1059
1060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 * Set an internal variable to a string value. Creates the variable if it does
1062 * not already exist.
1063 */
1064 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001065set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001067 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001068 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
1070 val = vim_strsave(value);
1071 if (val != NULL)
1072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001073 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001074 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001076 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001077 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 }
1079 }
1080}
1081
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001083static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084static char_u *redir_endp = NULL;
1085static char_u *redir_varname = NULL;
1086
1087/*
1088 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001089 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 * Returns OK if successfully completed the setup. FAIL otherwise.
1091 */
1092 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001093var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094{
1095 int save_emsg;
1096 int err;
1097 typval_T tv;
1098
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001099 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001100 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 {
1102 EMSG(_(e_invarg));
1103 return FAIL;
1104 }
1105
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001106 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 redir_varname = vim_strsave(name);
1108 if (redir_varname == NULL)
1109 return FAIL;
1110
1111 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1112 if (redir_lval == NULL)
1113 {
1114 var_redir_stop();
1115 return FAIL;
1116 }
1117
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 /* The output is stored in growarray "redir_ga" until redirection ends. */
1119 ga_init2(&redir_ga, (int)sizeof(char), 500);
1120
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001122 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001123 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1125 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001126 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 if (redir_endp != NULL && *redir_endp != NUL)
1128 /* Trailing characters are present after the variable name */
1129 EMSG(_(e_trailing));
1130 else
1131 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 var_redir_stop();
1134 return FAIL;
1135 }
1136
1137 /* check if we can write to the variable: set it to or append an empty
1138 * string */
1139 save_emsg = did_emsg;
1140 did_emsg = FALSE;
1141 tv.v_type = VAR_STRING;
1142 tv.vval.v_string = (char_u *)"";
1143 if (append)
1144 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1145 else
1146 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001147 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001148 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001149 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (err)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 var_redir_stop();
1154 return FAIL;
1155 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156
1157 return OK;
1158}
1159
1160/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161 * Append "value[value_len]" to the variable set by var_redir_start().
1162 * The actual appending is postponed until redirection ends, because the value
1163 * appended may in fact be the string we write to, changing it may cause freed
1164 * memory to be used:
1165 * :redir => foo
1166 * :let foo
1167 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 */
1169 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001170var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001172 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173
1174 if (redir_lval == NULL)
1175 return;
1176
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001178 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001180 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001182 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001183 {
1184 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001186 }
1187 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189}
1190
1191/*
1192 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001193 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 */
1195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001196var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001198 typval_T tv;
1199
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 if (redir_lval != NULL)
1201 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001202 /* If there was no error: assign the text to the variable. */
1203 if (redir_endp != NULL)
1204 {
1205 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1206 tv.v_type = VAR_STRING;
1207 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001208 /* Call get_lval() again, if it's inside a Dict or List it may
1209 * have changed. */
1210 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001211 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001212 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1213 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1214 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001215 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001217 /* free the collected output */
1218 vim_free(redir_ga.ga_data);
1219 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221 vim_free(redir_lval);
1222 redir_lval = NULL;
1223 }
1224 vim_free(redir_varname);
1225 redir_varname = NULL;
1226}
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228# if defined(FEAT_MBYTE) || defined(PROTO)
1229 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001230eval_charconvert(
1231 char_u *enc_from,
1232 char_u *enc_to,
1233 char_u *fname_from,
1234 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1239 set_vim_var_string(VV_CC_TO, enc_to, -1);
1240 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1241 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1242 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1243 err = TRUE;
1244 set_vim_var_string(VV_CC_FROM, NULL, -1);
1245 set_vim_var_string(VV_CC_TO, NULL, -1);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1248
1249 if (err)
1250 return FAIL;
1251 return OK;
1252}
1253# endif
1254
1255# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1256 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001257eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
1259 int err = FALSE;
1260
1261 set_vim_var_string(VV_FNAME_IN, fname, -1);
1262 set_vim_var_string(VV_CMDARG, args, -1);
1263 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1264 err = TRUE;
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_CMDARG, NULL, -1);
1267
1268 if (err)
1269 {
1270 mch_remove(fname);
1271 return FAIL;
1272 }
1273 return OK;
1274}
1275# endif
1276
1277# if defined(FEAT_DIFF) || defined(PROTO)
1278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279eval_diff(
1280 char_u *origfile,
1281 char_u *newfile,
1282 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int err = FALSE;
1285
1286 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1287 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1288 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1289 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1290 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1291 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1292 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1293}
1294
1295 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001296eval_patch(
1297 char_u *origfile,
1298 char_u *difffile,
1299 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int err;
1302
1303 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1304 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1305 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1306 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1307 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1308 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1309 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1310}
1311# endif
1312
1313/*
1314 * Top level evaluation function, returning a boolean.
1315 * Sets "error" to TRUE if there was an error.
1316 * Return TRUE or FALSE.
1317 */
1318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001319eval_to_bool(
1320 char_u *arg,
1321 int *error,
1322 char_u **nextcmd,
1323 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 int retval = FALSE;
1327
1328 if (skip)
1329 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 else
1333 {
1334 *error = FALSE;
1335 if (!skip)
1336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001337 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340 }
1341 if (skip)
1342 --emsg_skip;
1343
1344 return retval;
1345}
1346
1347/*
1348 * Top level evaluation function, returning a string. If "skip" is TRUE,
1349 * only parsing to "nextcmd" is done, without reporting errors. Return
1350 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1351 */
1352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001353eval_to_string_skip(
1354 char_u *arg,
1355 char_u **nextcmd,
1356 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
Bram Moolenaar33570922005-01-25 22:26:29 +00001358 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 char_u *retval;
1360
1361 if (skip)
1362 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 retval = NULL;
1365 else
1366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 retval = vim_strsave(get_tv_string(&tv));
1368 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370 if (skip)
1371 --emsg_skip;
1372
1373 return retval;
1374}
1375
1376/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001377 * Skip over an expression at "*pp".
1378 * Return FAIL for an error, OK otherwise.
1379 */
1380 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001381skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001382{
Bram Moolenaar33570922005-01-25 22:26:29 +00001383 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001384
1385 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001387}
1388
1389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001391 * When "convert" is TRUE convert a List into a sequence of lines and convert
1392 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 * Return pointer to allocated memory, or NULL for failure.
1394 */
1395 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001396eval_to_string(
1397 char_u *arg,
1398 char_u **nextcmd,
1399 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
Bram Moolenaar33570922005-01-25 22:26:29 +00001401 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001404#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001405 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 retval = NULL;
1410 else
1411 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001412 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001413 {
1414 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001415 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001416 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001417 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001418 if (tv.vval.v_list->lv_len > 0)
1419 ga_append(&ga, NL);
1420 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001421 ga_append(&ga, NUL);
1422 retval = (char_u *)ga.ga_data;
1423 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001424#ifdef FEAT_FLOAT
1425 else if (convert && tv.v_type == VAR_FLOAT)
1426 {
1427 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1428 retval = vim_strsave(numbuf);
1429 }
1430#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 else
1432 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435
1436 return retval;
1437}
1438
1439/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001440 * Call eval_to_string() without using current local variables and using
1441 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 */
1443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444eval_to_string_safe(
1445 char_u *arg,
1446 char_u **nextcmd,
1447 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 char_u *retval;
1450 void *save_funccalp;
1451
1452 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001453 if (use_sandbox)
1454 ++sandbox;
1455 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001456 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001457 if (use_sandbox)
1458 --sandbox;
1459 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 restore_funccal(save_funccalp);
1461 return retval;
1462}
1463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464/*
1465 * Top level evaluation function, returning a number.
1466 * Evaluates "expr" silently.
1467 * Returns -1 for an error.
1468 */
1469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001470eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
Bram Moolenaar33570922005-01-25 22:26:29 +00001472 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001474 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 ++emsg_off;
1477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001478 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 retval = -1;
1480 else
1481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001482 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485 --emsg_off;
1486
1487 return retval;
1488}
1489
Bram Moolenaara40058a2005-07-11 22:42:07 +00001490/*
1491 * Prepare v: variable "idx" to be used.
1492 * Save the current typeval in "save_tv".
1493 * When not used yet add the variable to the v: hashtable.
1494 */
1495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001496prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001497{
1498 *save_tv = vimvars[idx].vv_tv;
1499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1500 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1501}
1502
1503/*
1504 * Restore v: variable "idx" to typeval "save_tv".
1505 * When no longer defined, remove the variable from the v: hashtable.
1506 */
1507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001509{
1510 hashitem_T *hi;
1511
Bram Moolenaara40058a2005-07-11 22:42:07 +00001512 vimvars[idx].vv_tv = *save_tv;
1513 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1514 {
1515 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1516 if (HASHITEM_EMPTY(hi))
1517 EMSG2(_(e_intern2), "restore_vimvar()");
1518 else
1519 hash_remove(&vimvarht, hi);
1520 }
1521}
1522
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001523#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001524/*
1525 * Evaluate an expression to a list with suggestions.
1526 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001527 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001528 */
1529 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001530eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001531{
1532 typval_T save_val;
1533 typval_T rettv;
1534 list_T *list = NULL;
1535 char_u *p = skipwhite(expr);
1536
1537 /* Set "v:val" to the bad word. */
1538 prepare_vimvar(VV_VAL, &save_val);
1539 vimvars[VV_VAL].vv_type = VAR_STRING;
1540 vimvars[VV_VAL].vv_str = badword;
1541 if (p_verbose == 0)
1542 ++emsg_off;
1543
1544 if (eval1(&p, &rettv, TRUE) == OK)
1545 {
1546 if (rettv.v_type != VAR_LIST)
1547 clear_tv(&rettv);
1548 else
1549 list = rettv.vval.v_list;
1550 }
1551
1552 if (p_verbose == 0)
1553 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001554 restore_vimvar(VV_VAL, &save_val);
1555
1556 return list;
1557}
1558
1559/*
1560 * "list" is supposed to contain two items: a word and a number. Return the
1561 * word in "pp" and the number as the return value.
1562 * Return -1 if anything isn't right.
1563 * Used to get the good word and score from the eval_spell_expr() result.
1564 */
1565 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001566get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567{
1568 listitem_T *li;
1569
1570 li = list->lv_first;
1571 if (li == NULL)
1572 return -1;
1573 *pp = get_tv_string(&li->li_tv);
1574
1575 li = li->li_next;
1576 if (li == NULL)
1577 return -1;
1578 return get_tv_number(&li->li_tv);
1579}
1580#endif
1581
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001582/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001583 * Top level evaluation function.
1584 * Returns an allocated typval_T with the result.
1585 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001586 */
1587 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001588eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001589{
1590 typval_T *tv;
1591
1592 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001593 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001594 {
1595 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001596 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001597 }
1598
1599 return tv;
1600}
1601
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001604 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605 * Uses argv[argc] for the function arguments. Only Number and String
1606 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001610call_vim_function(
1611 char_u *func,
1612 int argc,
1613 char_u **argv,
1614 int safe, /* use the sandbox */
1615 int str_arg_only, /* all arguments are strings */
1616 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
Bram Moolenaar33570922005-01-25 22:26:29 +00001618 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 long n;
1620 int len;
1621 int i;
1622 int doesrange;
1623 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001626 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 for (i = 0; i < argc; i++)
1631 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001632 /* Pass a NULL or empty argument as an empty string */
1633 if (argv[i] == NULL || *argv[i] == NUL)
1634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001635 argvars[i].v_type = VAR_STRING;
1636 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001637 continue;
1638 }
1639
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001640 if (str_arg_only)
1641 len = 0;
1642 else
1643 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001644 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (len != 0 && len == (int)STRLEN(argv[i]))
1646 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001647 argvars[i].v_type = VAR_NUMBER;
1648 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
1650 else
1651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001652 argvars[i].v_type = VAR_STRING;
1653 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
1655 }
1656
1657 if (safe)
1658 {
1659 save_funccalp = save_funccal();
1660 ++sandbox;
1661 }
1662
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1664 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (safe)
1668 {
1669 --sandbox;
1670 restore_funccal(save_funccalp);
1671 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 vim_free(argvars);
1673
1674 if (ret == FAIL)
1675 clear_tv(rettv);
1676
1677 return ret;
1678}
1679
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001680/*
1681 * Call vimL function "func" and return the result as a number.
1682 * Returns -1 when calling the function fails.
1683 * Uses argv[argc] for the function arguments.
1684 */
1685 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001686call_func_retnr(
1687 char_u *func,
1688 int argc,
1689 char_u **argv,
1690 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001691{
1692 typval_T rettv;
1693 long retval;
1694
1695 /* All arguments are passed as strings, no conversion to number. */
1696 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1697 return -1;
1698
1699 retval = get_tv_number_chk(&rettv, NULL);
1700 clear_tv(&rettv);
1701 return retval;
1702}
1703
1704#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1705 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1706
Bram Moolenaar4f688582007-07-24 12:34:30 +00001707# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001709 * Call vimL function "func" and return the result as a string.
1710 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 * Uses argv[argc] for the function arguments.
1712 */
1713 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001714call_func_retstr(
1715 char_u *func,
1716 int argc,
1717 char_u **argv,
1718 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719{
1720 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001721 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001723 /* All arguments are passed as strings, no conversion to number. */
1724 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 return NULL;
1726
1727 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 return retval;
1730}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001731# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001733/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001734 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001736 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 */
1738 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001739call_func_retlist(
1740 char_u *func,
1741 int argc,
1742 char_u **argv,
1743 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744{
1745 typval_T rettv;
1746
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001747 /* All arguments are passed as strings, no conversion to number. */
1748 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749 return NULL;
1750
1751 if (rettv.v_type != VAR_LIST)
1752 {
1753 clear_tv(&rettv);
1754 return NULL;
1755 }
1756
1757 return rettv.vval.v_list;
1758}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#endif
1760
1761/*
1762 * Save the current function call pointer, and set it to NULL.
1763 * Used when executing autocommands and for ":source".
1764 */
1765 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 current_funccal = NULL;
1771 return (void *)fc;
1772}
1773
1774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001777 funccall_T *fc = (funccall_T *)vfc;
1778
1779 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780}
1781
Bram Moolenaar05159a02005-02-26 23:04:13 +00001782#if defined(FEAT_PROFILE) || defined(PROTO)
1783/*
1784 * Prepare profiling for entering a child or something else that is not
1785 * counted for the script/function itself.
1786 * Should always be called in pair with prof_child_exit().
1787 */
1788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001789prof_child_enter(
1790 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001791{
1792 funccall_T *fc = current_funccal;
1793
1794 if (fc != NULL && fc->func->uf_profiling)
1795 profile_start(&fc->prof_child);
1796 script_prof_save(tm);
1797}
1798
1799/*
1800 * Take care of time spent in a child.
1801 * Should always be called after prof_child_enter().
1802 */
1803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001804prof_child_exit(
1805 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001806{
1807 funccall_T *fc = current_funccal;
1808
1809 if (fc != NULL && fc->func->uf_profiling)
1810 {
1811 profile_end(&fc->prof_child);
1812 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1813 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1814 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1815 }
1816 script_prof_restore(tm);
1817}
1818#endif
1819
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#ifdef FEAT_FOLDING
1822/*
1823 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1824 * it in "*cp". Doesn't give error messages.
1825 */
1826 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 int retval;
1831 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001832 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1833 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001836 if (use_sandbox)
1837 ++sandbox;
1838 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 retval = 0;
1842 else
1843 {
1844 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 if (tv.v_type == VAR_NUMBER)
1846 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001847 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 retval = 0;
1849 else
1850 {
1851 /* If the result is a string, check if there is a non-digit before
1852 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (!VIM_ISDIGIT(*s) && *s != '-')
1855 *cp = *s++;
1856 retval = atol((char *)s);
1857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001861 if (use_sandbox)
1862 --sandbox;
1863 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865 return retval;
1866}
1867#endif
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 * ":let" list all variable values
1871 * ":let var1 var2" list variable values
1872 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 * ":let var += expr" assignment command.
1874 * ":let var -= expr" assignment command.
1875 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 */
1878 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001879ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001883 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 int var_count = 0;
1886 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001888 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001889 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
Bram Moolenaardb552d602006-03-23 22:59:57 +00001891 argend = skip_var_list(arg, &var_count, &semicolon);
1892 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001894 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1895 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001896 expr = skipwhite(argend);
1897 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1898 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001900 /*
1901 * ":let" without "=": list variables
1902 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 if (*arg == '[')
1904 EMSG(_(e_invarg));
1905 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001907 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001911 list_glob_vars(&first);
1912 list_buf_vars(&first);
1913 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001914#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001915 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001916#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 list_script_vars(&first);
1918 list_func_vars(&first);
1919 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 eap->nextcmd = check_nextcmd(arg);
1922 }
1923 else
1924 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 op[0] = '=';
1926 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001927 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001929 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1930 op[0] = *expr; /* +=, -= or .= */
1931 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001933 else
1934 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (eap->skip)
1937 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (eap->skip)
1940 {
1941 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 --emsg_skip;
1944 }
1945 else if (i != FAIL)
1946 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951 }
1952}
1953
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954/*
1955 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1956 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 * When "nextchars" is not NULL it points to a string with characters that
1958 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1959 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 * Returns OK or FAIL;
1961 */
1962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001963ex_let_vars(
1964 char_u *arg_start,
1965 typval_T *tv,
1966 int copy, /* copy values from "tv", don't move */
1967 int semicolon, /* from skip_var_list() */
1968 int var_count, /* from skip_var_list() */
1969 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970{
1971 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001972 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001974 listitem_T *item;
1975 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976
1977 if (*arg != '[')
1978 {
1979 /*
1980 * ":let var = expr" or ":for var in list"
1981 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 return FAIL;
1984 return OK;
1985 }
1986
1987 /*
1988 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1989 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001990 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 {
1992 EMSG(_(e_listreq));
1993 return FAIL;
1994 }
1995
1996 i = list_len(l);
1997 if (semicolon == 0 && var_count < i)
1998 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001999 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 return FAIL;
2001 }
2002 if (var_count - semicolon > i)
2003 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002004 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 return FAIL;
2006 }
2007
2008 item = l->lv_first;
2009 while (*arg != ']')
2010 {
2011 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002012 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 item = item->li_next;
2014 if (arg == NULL)
2015 return FAIL;
2016
2017 arg = skipwhite(arg);
2018 if (*arg == ';')
2019 {
2020 /* Put the rest of the list (may be empty) in the var after ';'.
2021 * Create a new list for this. */
2022 l = list_alloc();
2023 if (l == NULL)
2024 return FAIL;
2025 while (item != NULL)
2026 {
2027 list_append_tv(l, &item->li_tv);
2028 item = item->li_next;
2029 }
2030
2031 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002032 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 ltv.vval.v_list = l;
2034 l->lv_refcount = 1;
2035
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002036 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2037 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 clear_tv(&ltv);
2039 if (arg == NULL)
2040 return FAIL;
2041 break;
2042 }
2043 else if (*arg != ',' && *arg != ']')
2044 {
2045 EMSG2(_(e_intern2), "ex_let_vars()");
2046 return FAIL;
2047 }
2048 }
2049
2050 return OK;
2051}
2052
2053/*
2054 * Skip over assignable variable "var" or list of variables "[var, var]".
2055 * Used for ":let varvar = expr" and ":for varvar in expr".
2056 * For "[var, var]" increment "*var_count" for each variable.
2057 * for "[var, var; var]" set "semicolon".
2058 * Return NULL for an error.
2059 */
2060 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002061skip_var_list(
2062 char_u *arg,
2063 int *var_count,
2064 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065{
2066 char_u *p, *s;
2067
2068 if (*arg == '[')
2069 {
2070 /* "[var, var]": find the matching ']'. */
2071 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002072 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073 {
2074 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2075 s = skip_var_one(p);
2076 if (s == p)
2077 {
2078 EMSG2(_(e_invarg2), p);
2079 return NULL;
2080 }
2081 ++*var_count;
2082
2083 p = skipwhite(s);
2084 if (*p == ']')
2085 break;
2086 else if (*p == ';')
2087 {
2088 if (*semicolon == 1)
2089 {
2090 EMSG(_("Double ; in list of variables"));
2091 return NULL;
2092 }
2093 *semicolon = 1;
2094 }
2095 else if (*p != ',')
2096 {
2097 EMSG2(_(e_invarg2), p);
2098 return NULL;
2099 }
2100 }
2101 return p + 1;
2102 }
2103 else
2104 return skip_var_one(arg);
2105}
2106
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002108 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002109 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002112skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002114 if (*arg == '@' && arg[1] != NUL)
2115 return arg + 2;
2116 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2117 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118}
2119
Bram Moolenaara7043832005-01-21 11:56:39 +00002120/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 * List variables for hashtab "ht" with prefix "prefix".
2122 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002123 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125list_hashtable_vars(
2126 hashtab_T *ht,
2127 char_u *prefix,
2128 int empty,
2129 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 hashitem_T *hi;
2132 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002133 int todo;
2134
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002135 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2137 {
2138 if (!HASHITEM_EMPTY(hi))
2139 {
2140 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002141 di = HI2DI(hi);
2142 if (empty || di->di_tv.v_type != VAR_STRING
2143 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145 }
2146 }
2147}
2148
2149/*
2150 * List global variables.
2151 */
2152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002153list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002156}
2157
2158/*
2159 * List buffer variables.
2160 */
2161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002163{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002164 char_u numbuf[NUMBUFLEN];
2165
Bram Moolenaar429fa852013-04-15 12:27:36 +02002166 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002168
2169 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2171 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002172}
2173
2174/*
2175 * List window variables.
2176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002180 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002182}
2183
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002184#ifdef FEAT_WINDOWS
2185/*
2186 * List tab page variables.
2187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002190{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002191 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002193}
2194#endif
2195
Bram Moolenaara7043832005-01-21 11:56:39 +00002196/*
2197 * List Vim variables.
2198 */
2199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002200list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203}
2204
2205/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002206 * List script-local variables, if there is a script.
2207 */
2208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210{
2211 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002212 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2213 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214}
2215
2216/*
2217 * List function variables, if there is a function.
2218 */
2219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002220list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221{
2222 if (current_funccal != NULL)
2223 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002224 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225}
2226
2227/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 * List variables in "arg".
2229 */
2230 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002231list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232{
2233 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 char_u *name_start;
2237 char_u *arg_subsc;
2238 char_u *tofree;
2239 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240
2241 while (!ends_excmd(*arg) && !got_int)
2242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002245 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2247 {
2248 emsg_severe = TRUE;
2249 EMSG(_(e_trailing));
2250 break;
2251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 /* get_name_len() takes care of expanding curly braces */
2256 name_start = name = arg;
2257 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2258 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 /* This is mainly to keep test 49 working: when expanding
2261 * curly braces fails overrule the exception error message. */
2262 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 emsg_severe = TRUE;
2265 EMSG2(_(e_invarg2), arg);
2266 break;
2267 }
2268 error = TRUE;
2269 }
2270 else
2271 {
2272 if (tofree != NULL)
2273 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002274 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
2277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 /* handle d.key, l[idx], f(expr) */
2279 arg_subsc = arg;
2280 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'g': list_glob_vars(first); break;
2289 case 'b': list_buf_vars(first); break;
2290 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002291#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 'v': list_vim_vars(first); break;
2295 case 's': list_script_vars(first); break;
2296 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 default:
2298 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 }
2301 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 {
2303 char_u numbuf[NUMBUFLEN];
2304 char_u *tf;
2305 int c;
2306 char_u *s;
2307
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002308 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 c = *arg;
2310 *arg = NUL;
2311 list_one_var_a((char_u *)"",
2312 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 tv.v_type,
2314 s == NULL ? (char_u *)"" : s,
2315 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 *arg = c;
2317 vim_free(tf);
2318 }
2319 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
2322 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323
2324 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326
2327 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 }
2329
2330 return arg;
2331}
2332
2333/*
2334 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2335 * Returns a pointer to the char just after the var name.
2336 * Returns NULL if there is an error.
2337 */
2338 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002339ex_let_one(
2340 char_u *arg, /* points to variable name */
2341 typval_T *tv, /* value to assign to variable */
2342 int copy, /* copy value from "tv" */
2343 char_u *endchars, /* valid chars after variable name or NULL */
2344 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345{
2346 int c1;
2347 char_u *name;
2348 char_u *p;
2349 char_u *arg_end = NULL;
2350 int len;
2351 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353
2354 /*
2355 * ":let $VAR = expr": Set environment variable.
2356 */
2357 if (*arg == '$')
2358 {
2359 /* Find the end of the name. */
2360 ++arg;
2361 name = arg;
2362 len = get_env_len(&arg);
2363 if (len == 0)
2364 EMSG2(_(e_invarg2), name - 1);
2365 else
2366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 if (op != NULL && (*op == '+' || *op == '-'))
2368 EMSG2(_(e_letwrong), op);
2369 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002370 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002372 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 {
2374 c1 = name[len];
2375 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 p = get_tv_string_chk(tv);
2377 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 {
2379 int mustfree = FALSE;
2380 char_u *s = vim_getenv(name, &mustfree);
2381
2382 if (s != NULL)
2383 {
2384 p = tofree = concat_str(s, p);
2385 if (mustfree)
2386 vim_free(s);
2387 }
2388 }
2389 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 if (STRICMP(name, "HOME") == 0)
2393 init_homedir();
2394 else if (didset_vim && STRICMP(name, "VIM") == 0)
2395 didset_vim = FALSE;
2396 else if (didset_vimruntime
2397 && STRICMP(name, "VIMRUNTIME") == 0)
2398 didset_vimruntime = FALSE;
2399 arg_end = arg;
2400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 }
2404 }
2405 }
2406
2407 /*
2408 * ":let &option = expr": Set option value.
2409 * ":let &l:option = expr": Set local option value.
2410 * ":let &g:option = expr": Set global option value.
2411 */
2412 else if (*arg == '&')
2413 {
2414 /* Find the end of the name. */
2415 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002416 if (p == NULL || (endchars != NULL
2417 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 EMSG(_(e_letunexp));
2419 else
2420 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 long n;
2422 int opt_type;
2423 long numval;
2424 char_u *stringval = NULL;
2425 char_u *s;
2426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 c1 = *p;
2428 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429
2430 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 s = get_tv_string_chk(tv); /* != NULL if number or string */
2432 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 {
2434 opt_type = get_option_value(arg, &numval,
2435 &stringval, opt_flags);
2436 if ((opt_type == 1 && *op == '.')
2437 || (opt_type == 0 && *op != '.'))
2438 EMSG2(_(e_letwrong), op);
2439 else
2440 {
2441 if (opt_type == 1) /* number */
2442 {
2443 if (*op == '+')
2444 n = numval + n;
2445 else
2446 n = numval - n;
2447 }
2448 else if (opt_type == 0 && stringval != NULL) /* string */
2449 {
2450 s = concat_str(stringval, s);
2451 vim_free(stringval);
2452 stringval = s;
2453 }
2454 }
2455 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 if (s != NULL)
2457 {
2458 set_option_value(arg, n, s, opt_flags);
2459 arg_end = p;
2460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 }
2464 }
2465
2466 /*
2467 * ":let @r = expr": Set register contents.
2468 */
2469 else if (*arg == '@')
2470 {
2471 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (op != NULL && (*op == '+' || *op == '-'))
2473 EMSG2(_(e_letwrong), op);
2474 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002479 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 char_u *s;
2481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 p = get_tv_string_chk(tv);
2483 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002485 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 if (s != NULL)
2487 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002488 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 vim_free(s);
2490 }
2491 }
2492 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 arg_end = arg + 1;
2496 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
2499 }
2500
2501 /*
2502 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002505 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002509 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2513 EMSG(_(e_letunexp));
2514 else
2515 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 arg_end = p;
2518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 }
2522
2523 else
2524 EMSG2(_(e_invarg2), arg);
2525
2526 return arg_end;
2527}
2528
2529/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2531 */
2532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002533check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534{
2535 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2536 {
2537 EMSG2(_(e_readonlyvar), arg);
2538 return TRUE;
2539 }
2540 return FALSE;
2541}
2542
2543/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Get an lval: variable, Dict item or List item that can be assigned a value
2545 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2546 * "name.key", "name.key[expr]" etc.
2547 * Indexing only works if "name" is an existing List or Dictionary.
2548 * "name" points to the start of the name.
2549 * If "rettv" is not NULL it points to the value to be assigned.
2550 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2551 * wrong; must end in space or cmd separator.
2552 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 * flags:
2554 * GLV_QUIET: do not give error messages
2555 * GLV_NO_AUTOLOAD: do not use script autoloading
2556 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002558 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 */
2561 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002562get_lval(
2563 char_u *name,
2564 typval_T *rettv,
2565 lval_T *lp,
2566 int unlet,
2567 int skip,
2568 int flags, /* GLV_ values */
2569 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 char_u *expr_start, *expr_end;
2573 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 dictitem_T *v;
2575 typval_T var1;
2576 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002582 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586
2587 if (skip)
2588 {
2589 /* When skipping just find the end of the name. */
2590 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002591 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 }
2593
2594 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002595 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (expr_start != NULL)
2597 {
2598 /* Don't expand the name when we already know there is an error. */
2599 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2600 && *p != '[' && *p != '.')
2601 {
2602 EMSG(_(e_trailing));
2603 return NULL;
2604 }
2605
2606 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2607 if (lp->ll_exp_name == NULL)
2608 {
2609 /* Report an invalid expression in braces, unless the
2610 * expression evaluation has been cancelled due to an
2611 * aborting error, an interrupt, or an exception. */
2612 if (!aborting() && !quiet)
2613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002614 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 EMSG2(_(e_invarg2), name);
2616 return NULL;
2617 }
2618 }
2619 lp->ll_name = lp->ll_exp_name;
2620 }
2621 else
2622 lp->ll_name = name;
2623
2624 /* Without [idx] or .key we are done. */
2625 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2626 return p;
2627
2628 cc = *p;
2629 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (v == NULL && !quiet)
2632 EMSG2(_(e_undefvar), lp->ll_name);
2633 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634 if (v == NULL)
2635 return NULL;
2636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 /*
2638 * Loop until no more [idx] or .key is following.
2639 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002640 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2644 && !(lp->ll_tv->v_type == VAR_DICT
2645 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (!quiet)
2648 EMSG(_("E689: Can only index a List or Dictionary"));
2649 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E708: [:] must come last"));
2655 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 len = -1;
2659 if (*p == '.')
2660 {
2661 key = p + 1;
2662 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2663 ;
2664 if (len == 0)
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_(e_emptykey));
2668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = key + len;
2671 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 else
2673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (*p == ':')
2677 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 else
2679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 empty1 = FALSE;
2681 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002683 if (get_tv_string_chk(&var1) == NULL)
2684 {
2685 /* not a number or string */
2686 clear_tv(&var1);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690
2691 /* Optionally get the second index [ :expr]. */
2692 if (*p == ':')
2693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 if (!empty1)
2699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (rettv != NULL && (rettv->v_type != VAR_LIST
2703 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 if (!empty1)
2708 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 }
2711 p = skipwhite(p + 1);
2712 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (!empty1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002723 if (get_tv_string_chk(&var2) == NULL)
2724 {
2725 /* not a number or string */
2726 if (!empty1)
2727 clear_tv(&var1);
2728 clear_tv(&var2);
2729 return NULL;
2730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (!quiet)
2740 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (!empty1)
2742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747
2748 /* Skip to past ']'. */
2749 ++p;
2750 }
2751
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
2754 if (len == -1)
2755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (*key == NUL)
2759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (!quiet)
2761 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
2765 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002766 lp->ll_list = NULL;
2767 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002768 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 /* When assigning to a scope dictionary check that a function and
2771 * variable name is valid (only variable name unless it is l: or
2772 * g: dictionary). Disallow overwriting a builtin function. */
2773 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 int prevval;
2776 int wrong;
2777
2778 if (len != -1)
2779 {
2780 prevval = key[len];
2781 key[len] = NUL;
2782 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002783 else
2784 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002785 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2786 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 || !valid_varname(key);
2789 if (len != -1)
2790 key[len] = prevval;
2791 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 return NULL;
2793 }
2794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002797 /* Can't add "v:" variable. */
2798 if (lp->ll_dict == &vimvardict)
2799 {
2800 EMSG2(_(e_illvar), name);
2801 return NULL;
2802 }
2803
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002804 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002808 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 if (len == -1)
2810 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 }
2813 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 p = NULL;
2821 break;
2822 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002824 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 return NULL;
2826
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 if (len == -1)
2828 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 }
2831 else
2832 {
2833 /*
2834 * Get the number and item for the only or first index of the List.
2835 */
2836 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 else
2839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002840 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 clear_tv(&var1);
2842 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_list = lp->ll_tv->vval.v_list;
2845 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2846 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002848 if (lp->ll_n1 < 0)
2849 {
2850 lp->ll_n1 = 0;
2851 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2852 }
2853 }
2854 if (lp->ll_li == NULL)
2855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 }
2862
2863 /*
2864 * May need to find the item or absolute index for the second
2865 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 * When no index given: "lp->ll_empty2" is TRUE.
2867 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002871 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 {
2878 if (!quiet)
2879 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002881 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2886 if (lp->ll_n1 < 0)
2887 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2888 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 {
2890 if (!quiet)
2891 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 }
2899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return p;
2901}
2902
2903/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002907clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908{
2909 vim_free(lp->ll_exp_name);
2910 vim_free(lp->ll_newkey);
2911}
2912
2913/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002916 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 */
2918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002919set_var_lval(
2920 lval_T *lp,
2921 char_u *endp,
2922 typval_T *rettv,
2923 int copy,
2924 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925{
2926 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 listitem_T *ri;
2928 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929
2930 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 cc = *endp;
2935 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 if (op != NULL && *op != '=')
2937 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002942 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002943 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 if ((di == NULL
2946 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2947 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2948 FALSE)))
2949 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 set_var(lp->ll_name, &tv, FALSE);
2951 clear_tv(&tv);
2952 }
2953 }
2954 else
2955 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 else if (tv_check_lock(lp->ll_newkey == NULL
2960 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002961 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 else if (lp->ll_range)
2964 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002965 listitem_T *ll_li = lp->ll_li;
2966 int ll_n1 = lp->ll_n1;
2967
2968 /*
2969 * Check whether any of the list items is locked
2970 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002971 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002972 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002973 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002974 return;
2975 ri = ri->li_next;
2976 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2977 break;
2978 ll_li = ll_li->li_next;
2979 ++ll_n1;
2980 }
2981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /*
2983 * Assign the List values to the list items.
2984 */
2985 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002986 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 if (op != NULL && *op != '=')
2988 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2989 else
2990 {
2991 clear_tv(&lp->ll_li->li_tv);
2992 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 ri = ri->li_next;
2995 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2996 break;
2997 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003000 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 ri = NULL;
3003 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 lp->ll_li = lp->ll_li->li_next;
3007 ++lp->ll_n1;
3008 }
3009 if (ri != NULL)
3010 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 else if (lp->ll_empty2
3012 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 : lp->ll_n1 != lp->ll_n2)
3014 EMSG(_("E711: List value has not enough items"));
3015 }
3016 else
3017 {
3018 /*
3019 * Assign to a List or Dictionary item.
3020 */
3021 if (lp->ll_newkey != NULL)
3022 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 if (op != NULL && *op != '=')
3024 {
3025 EMSG2(_(e_letwrong), op);
3026 return;
3027 }
3028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003030 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 if (di == NULL)
3032 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003033 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3034 {
3035 vim_free(di);
3036 return;
3037 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003038 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 else if (op != NULL && *op != '=')
3041 {
3042 tv_op(lp->ll_tv, rettv, op);
3043 return;
3044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 /*
3049 * Assign the value to the variable or list item.
3050 */
3051 if (copy)
3052 copy_tv(rettv, lp->ll_tv);
3053 else
3054 {
3055 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003056 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 }
3059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060}
3061
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003062/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3064 * Returns OK or FAIL.
3065 */
3066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003067tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068{
3069 long n;
3070 char_u numbuf[NUMBUFLEN];
3071 char_u *s;
3072
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003073 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3074 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3075 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 {
3077 switch (tv1->v_type)
3078 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003079 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 case VAR_DICT:
3081 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003082 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003083 case VAR_JOB:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 break;
3085
3086 case VAR_LIST:
3087 if (*op != '+' || tv2->v_type != VAR_LIST)
3088 break;
3089 /* List += List */
3090 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3091 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3092 return OK;
3093
3094 case VAR_NUMBER:
3095 case VAR_STRING:
3096 if (tv2->v_type == VAR_LIST)
3097 break;
3098 if (*op == '+' || *op == '-')
3099 {
3100 /* nr += nr or nr -= nr*/
3101 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102#ifdef FEAT_FLOAT
3103 if (tv2->v_type == VAR_FLOAT)
3104 {
3105 float_T f = n;
3106
3107 if (*op == '+')
3108 f += tv2->vval.v_float;
3109 else
3110 f -= tv2->vval.v_float;
3111 clear_tv(tv1);
3112 tv1->v_type = VAR_FLOAT;
3113 tv1->vval.v_float = f;
3114 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#endif
3117 {
3118 if (*op == '+')
3119 n += get_tv_number(tv2);
3120 else
3121 n -= get_tv_number(tv2);
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_NUMBER;
3124 tv1->vval.v_number = n;
3125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 }
3127 else
3128 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129 if (tv2->v_type == VAR_FLOAT)
3130 break;
3131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 /* str .= str */
3133 s = get_tv_string(tv1);
3134 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3135 clear_tv(tv1);
3136 tv1->v_type = VAR_STRING;
3137 tv1->vval.v_string = s;
3138 }
3139 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140
3141#ifdef FEAT_FLOAT
3142 case VAR_FLOAT:
3143 {
3144 float_T f;
3145
3146 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3147 && tv2->v_type != VAR_NUMBER
3148 && tv2->v_type != VAR_STRING))
3149 break;
3150 if (tv2->v_type == VAR_FLOAT)
3151 f = tv2->vval.v_float;
3152 else
3153 f = get_tv_number(tv2);
3154 if (*op == '+')
3155 tv1->vval.v_float += f;
3156 else
3157 tv1->vval.v_float -= f;
3158 }
3159 return OK;
3160#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003161 }
3162 }
3163
3164 EMSG2(_(e_letwrong), op);
3165 return FAIL;
3166}
3167
3168/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 * Add a watcher to a list.
3170 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003172list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173{
3174 lw->lw_next = l->lv_watch;
3175 l->lv_watch = lw;
3176}
3177
3178/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003179 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 * No warning when it isn't found...
3181 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003183list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184{
Bram Moolenaar33570922005-01-25 22:26:29 +00003185 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186
3187 lwp = &l->lv_watch;
3188 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3189 {
3190 if (lw == lwrem)
3191 {
3192 *lwp = lw->lw_next;
3193 break;
3194 }
3195 lwp = &lw->lw_next;
3196 }
3197}
3198
3199/*
3200 * Just before removing an item from a list: advance watchers to the next
3201 * item.
3202 */
3203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205{
Bram Moolenaar33570922005-01-25 22:26:29 +00003206 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207
3208 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3209 if (lw->lw_item == item)
3210 lw->lw_item = item->li_next;
3211}
3212
3213/*
3214 * Evaluate the expression used in a ":for var in expr" command.
3215 * "arg" points to "var".
3216 * Set "*errp" to TRUE for an error, FALSE otherwise;
3217 * Return a pointer that holds the info. Null when there is an error.
3218 */
3219 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220eval_for_line(
3221 char_u *arg,
3222 int *errp,
3223 char_u **nextcmdp,
3224 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225{
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 typval_T tv;
3229 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230
3231 *errp = TRUE; /* default: there is an error */
3232
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 if (fi == NULL)
3235 return NULL;
3236
3237 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3238 if (expr == NULL)
3239 return fi;
3240
3241 expr = skipwhite(expr);
3242 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3243 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003244 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 return fi;
3246 }
3247
3248 if (skip)
3249 ++emsg_skip;
3250 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3251 {
3252 *errp = FALSE;
3253 if (!skip)
3254 {
3255 l = tv.vval.v_list;
3256 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003257 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 clear_tv(&tv);
3260 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 else
3262 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003263 /* No need to increment the refcount, it's already set for the
3264 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 fi->fi_list = l;
3266 list_add_watch(l, &fi->fi_lw);
3267 fi->fi_lw.lw_item = l->lv_first;
3268 }
3269 }
3270 }
3271 if (skip)
3272 --emsg_skip;
3273
3274 return fi;
3275}
3276
3277/*
3278 * Use the first item in a ":for" list. Advance to the next.
3279 * Assign the values to the variable (list). "arg" points to the first one.
3280 * Return TRUE when a valid item was found, FALSE when at end of list or
3281 * something wrong.
3282 */
3283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003284next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003286 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003288 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289
3290 item = fi->fi_lw.lw_item;
3291 if (item == NULL)
3292 result = FALSE;
3293 else
3294 {
3295 fi->fi_lw.lw_item = item->li_next;
3296 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3297 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3298 }
3299 return result;
3300}
3301
3302/*
3303 * Free the structure used to store info used by ":for".
3304 */
3305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003306free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307{
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003310 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003311 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003313 list_unref(fi->fi_list);
3314 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 vim_free(fi);
3316}
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3319
3320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321set_context_for_expression(
3322 expand_T *xp,
3323 char_u *arg,
3324 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 int got_eq = FALSE;
3327 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 if (cmdidx == CMD_let)
3331 {
3332 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003333 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 {
3335 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003336 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 {
3338 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003339 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 if (vim_iswhite(*p))
3341 break;
3342 }
3343 return;
3344 }
3345 }
3346 else
3347 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3348 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 while ((xp->xp_pattern = vim_strpbrk(arg,
3350 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3351 {
3352 c = *xp->xp_pattern;
3353 if (c == '&')
3354 {
3355 c = xp->xp_pattern[1];
3356 if (c == '&')
3357 {
3358 ++xp->xp_pattern;
3359 xp->xp_context = cmdidx != CMD_let || got_eq
3360 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3361 }
3362 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003365 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3366 xp->xp_pattern += 2;
3367
3368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370 else if (c == '$')
3371 {
3372 /* environment variable */
3373 xp->xp_context = EXPAND_ENV_VARS;
3374 }
3375 else if (c == '=')
3376 {
3377 got_eq = TRUE;
3378 xp->xp_context = EXPAND_EXPRESSION;
3379 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003380 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 && xp->xp_context == EXPAND_FUNCTIONS
3382 && vim_strchr(xp->xp_pattern, '(') == NULL)
3383 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003384 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 break;
3386 }
3387 else if (cmdidx != CMD_let || got_eq)
3388 {
3389 if (c == '"') /* string */
3390 {
3391 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3392 if (c == '\\' && xp->xp_pattern[1] != NUL)
3393 ++xp->xp_pattern;
3394 xp->xp_context = EXPAND_NOTHING;
3395 }
3396 else if (c == '\'') /* literal string */
3397 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003398 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3400 /* skip */ ;
3401 xp->xp_context = EXPAND_NOTHING;
3402 }
3403 else if (c == '|')
3404 {
3405 if (xp->xp_pattern[1] == '|')
3406 {
3407 ++xp->xp_pattern;
3408 xp->xp_context = EXPAND_EXPRESSION;
3409 }
3410 else
3411 xp->xp_context = EXPAND_COMMANDS;
3412 }
3413 else
3414 xp->xp_context = EXPAND_EXPRESSION;
3415 }
3416 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003417 /* Doesn't look like something valid, expand as an expression
3418 * anyway. */
3419 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 arg = xp->xp_pattern;
3421 if (*arg != NUL)
3422 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3423 /* skip */ ;
3424 }
3425 xp->xp_pattern = arg;
3426}
3427
3428#endif /* FEAT_CMDL_COMPL */
3429
3430/*
3431 * ":1,25call func(arg1, arg2)" function call.
3432 */
3433 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003434ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
3436 char_u *arg = eap->arg;
3437 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003439 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 linenr_T lnum;
3443 int doesrange;
3444 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003445 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003447 if (eap->skip)
3448 {
3449 /* trans_function_name() doesn't work well when skipping, use eval0()
3450 * instead to skip to any following command, e.g. for:
3451 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003452 ++emsg_skip;
3453 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3454 clear_tv(&rettv);
3455 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003456 return;
3457 }
3458
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003459 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003460 if (fudi.fd_newkey != NULL)
3461 {
3462 /* Still need to give an error message for missing key. */
3463 EMSG2(_(e_dictkey), fudi.fd_newkey);
3464 vim_free(fudi.fd_newkey);
3465 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003466 if (tofree == NULL)
3467 return;
3468
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003469 /* Increase refcount on dictionary, it could get deleted when evaluating
3470 * the arguments. */
3471 if (fudi.fd_dict != NULL)
3472 ++fudi.fd_dict->dv_refcount;
3473
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003474 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003475 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003476 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
Bram Moolenaar532c7802005-01-27 14:44:31 +00003478 /* Skip white space to allow ":call func ()". Not good, but required for
3479 * backward compatibility. */
3480 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 if (*startarg != '(')
3484 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003485 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 goto end;
3487 }
3488
3489 /*
3490 * When skipping, evaluate the function once, to find the end of the
3491 * arguments.
3492 * When the function takes a range, this is discovered after the first
3493 * call, and the loop is broken.
3494 */
3495 if (eap->skip)
3496 {
3497 ++emsg_skip;
3498 lnum = eap->line2; /* do it once, also with an invalid range */
3499 }
3500 else
3501 lnum = eap->line1;
3502 for ( ; lnum <= eap->line2; ++lnum)
3503 {
3504 if (!eap->skip && eap->addr_count > 0)
3505 {
3506 curwin->w_cursor.lnum = lnum;
3507 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003508#ifdef FEAT_VIRTUALEDIT
3509 curwin->w_cursor.coladd = 0;
3510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003513 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 eap->line1, eap->line2, &doesrange,
3515 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 failed = TRUE;
3518 break;
3519 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003520
3521 /* Handle a function returning a Funcref, Dictionary or List. */
3522 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3523 {
3524 failed = TRUE;
3525 break;
3526 }
3527
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003528 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (doesrange || eap->skip)
3530 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003533 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003535 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (aborting())
3537 break;
3538 }
3539 if (eap->skip)
3540 --emsg_skip;
3541
3542 if (!failed)
3543 {
3544 /* Check for trailing illegal characters and a following command. */
3545 if (!ends_excmd(*arg))
3546 {
3547 emsg_severe = TRUE;
3548 EMSG(_(e_trailing));
3549 }
3550 else
3551 eap->nextcmd = check_nextcmd(arg);
3552 }
3553
3554end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003555 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003556 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557}
3558
3559/*
3560 * ":unlet[!] var1 ... " command.
3561 */
3562 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003563ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003565 ex_unletlock(eap, eap->arg, 0);
3566}
3567
3568/*
3569 * ":lockvar" and ":unlockvar" commands
3570 */
3571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003572ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 int deep = 2;
3576
3577 if (eap->forceit)
3578 deep = -1;
3579 else if (vim_isdigit(*arg))
3580 {
3581 deep = getdigits(&arg);
3582 arg = skipwhite(arg);
3583 }
3584
3585 ex_unletlock(eap, arg, deep);
3586}
3587
3588/*
3589 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3590 */
3591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003592ex_unletlock(
3593 exarg_T *eap,
3594 char_u *argstart,
3595 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003596{
3597 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 do
3603 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003605 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003606 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (lv.ll_name == NULL)
3608 error = TRUE; /* error but continue parsing */
3609 if (name_end == NULL || (!vim_iswhite(*name_end)
3610 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 if (name_end != NULL)
3613 {
3614 emsg_severe = TRUE;
3615 EMSG(_(e_trailing));
3616 }
3617 if (!(eap->skip || error))
3618 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 break;
3620 }
3621
3622 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 {
3624 if (eap->cmdidx == CMD_unlet)
3625 {
3626 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3627 error = TRUE;
3628 }
3629 else
3630 {
3631 if (do_lock_var(&lv, name_end, deep,
3632 eap->cmdidx == CMD_lockvar) == FAIL)
3633 error = TRUE;
3634 }
3635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 if (!eap->skip)
3638 clear_lval(&lv);
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 arg = skipwhite(name_end);
3641 } while (!ends_excmd(*arg));
3642
3643 eap->nextcmd = check_nextcmd(arg);
3644}
3645
Bram Moolenaar8c711452005-01-14 21:53:12 +00003646 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003647do_unlet_var(
3648 lval_T *lp,
3649 char_u *name_end,
3650 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 int ret = OK;
3653 int cc;
3654
3655 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 cc = *name_end;
3658 *name_end = NUL;
3659
3660 /* Normal name or expanded name. */
3661 if (check_changedtick(lp->ll_name))
3662 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003667 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003668 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003669 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003670 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 else if (lp->ll_range)
3673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003675 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003676 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003677
3678 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3679 {
3680 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 return FAIL;
3683 ll_li = li;
3684 ++ll_n1;
3685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686
3687 /* Delete a range of List items. */
3688 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3689 {
3690 li = lp->ll_li->li_next;
3691 listitem_remove(lp->ll_list, lp->ll_li);
3692 lp->ll_li = li;
3693 ++lp->ll_n1;
3694 }
3695 }
3696 else
3697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003699 /* unlet a List item. */
3700 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003703 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 }
3705
3706 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003707}
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709/*
3710 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003711 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 */
3713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 hashtab_T *ht;
3717 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003718 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003719 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003720 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
Bram Moolenaar33570922005-01-25 22:26:29 +00003722 ht = find_var_ht(name, &varname);
3723 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003725 if (ht == &globvarht)
3726 d = &globvardict;
3727 else if (current_funccal != NULL
3728 && ht == &current_funccal->l_vars.dv_hashtab)
3729 d = &current_funccal->l_vars;
3730 else if (ht == &compat_hashtab)
3731 d = &vimvardict;
3732 else
3733 {
3734 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3735 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3736 }
3737 if (d == NULL)
3738 {
3739 EMSG2(_(e_intern2), "do_unlet()");
3740 return FAIL;
3741 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 hi = hash_find(ht, varname);
3743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003744 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003745 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003746 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003747 || var_check_ro(di->di_flags, name, FALSE)
3748 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003749 return FAIL;
3750
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 delete_var(ht, hi);
3752 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003755 if (forceit)
3756 return OK;
3757 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 return FAIL;
3759}
3760
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003761/*
3762 * Lock or unlock variable indicated by "lp".
3763 * "deep" is the levels to go (-1 for unlimited);
3764 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3765 */
3766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003767do_lock_var(
3768 lval_T *lp,
3769 char_u *name_end,
3770 int deep,
3771 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772{
3773 int ret = OK;
3774 int cc;
3775 dictitem_T *di;
3776
3777 if (deep == 0) /* nothing to do */
3778 return OK;
3779
3780 if (lp->ll_tv == NULL)
3781 {
3782 cc = *name_end;
3783 *name_end = NUL;
3784
3785 /* Normal name or expanded name. */
3786 if (check_changedtick(lp->ll_name))
3787 ret = FAIL;
3788 else
3789 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003790 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003791 if (di == NULL)
3792 ret = FAIL;
3793 else
3794 {
3795 if (lock)
3796 di->di_flags |= DI_FLAGS_LOCK;
3797 else
3798 di->di_flags &= ~DI_FLAGS_LOCK;
3799 item_lock(&di->di_tv, deep, lock);
3800 }
3801 }
3802 *name_end = cc;
3803 }
3804 else if (lp->ll_range)
3805 {
3806 listitem_T *li = lp->ll_li;
3807
3808 /* (un)lock a range of List items. */
3809 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3810 {
3811 item_lock(&li->li_tv, deep, lock);
3812 li = li->li_next;
3813 ++lp->ll_n1;
3814 }
3815 }
3816 else if (lp->ll_list != NULL)
3817 /* (un)lock a List item. */
3818 item_lock(&lp->ll_li->li_tv, deep, lock);
3819 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003820 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 item_lock(&lp->ll_di->di_tv, deep, lock);
3822
3823 return ret;
3824}
3825
3826/*
3827 * Lock or unlock an item. "deep" is nr of levels to go.
3828 */
3829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003830item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003831{
3832 static int recurse = 0;
3833 list_T *l;
3834 listitem_T *li;
3835 dict_T *d;
3836 hashitem_T *hi;
3837 int todo;
3838
3839 if (recurse >= DICT_MAXNEST)
3840 {
3841 EMSG(_("E743: variable nested too deep for (un)lock"));
3842 return;
3843 }
3844 if (deep == 0)
3845 return;
3846 ++recurse;
3847
3848 /* lock/unlock the item itself */
3849 if (lock)
3850 tv->v_lock |= VAR_LOCKED;
3851 else
3852 tv->v_lock &= ~VAR_LOCKED;
3853
3854 switch (tv->v_type)
3855 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003856 case VAR_UNKNOWN:
3857 case VAR_NUMBER:
3858 case VAR_STRING:
3859 case VAR_FUNC:
3860 case VAR_FLOAT:
3861 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003862 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003863 break;
3864
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003865 case VAR_LIST:
3866 if ((l = tv->vval.v_list) != NULL)
3867 {
3868 if (lock)
3869 l->lv_lock |= VAR_LOCKED;
3870 else
3871 l->lv_lock &= ~VAR_LOCKED;
3872 if (deep < 0 || deep > 1)
3873 /* recursive: lock/unlock the items the List contains */
3874 for (li = l->lv_first; li != NULL; li = li->li_next)
3875 item_lock(&li->li_tv, deep - 1, lock);
3876 }
3877 break;
3878 case VAR_DICT:
3879 if ((d = tv->vval.v_dict) != NULL)
3880 {
3881 if (lock)
3882 d->dv_lock |= VAR_LOCKED;
3883 else
3884 d->dv_lock &= ~VAR_LOCKED;
3885 if (deep < 0 || deep > 1)
3886 {
3887 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003888 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003889 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3890 {
3891 if (!HASHITEM_EMPTY(hi))
3892 {
3893 --todo;
3894 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3895 }
3896 }
3897 }
3898 }
3899 }
3900 --recurse;
3901}
3902
Bram Moolenaara40058a2005-07-11 22:42:07 +00003903/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003904 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3905 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003906 */
3907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003908tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003909{
3910 return (tv->v_lock & VAR_LOCKED)
3911 || (tv->v_type == VAR_LIST
3912 && tv->vval.v_list != NULL
3913 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3914 || (tv->v_type == VAR_DICT
3915 && tv->vval.v_dict != NULL
3916 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3917}
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3920/*
3921 * Delete all "menutrans_" variables.
3922 */
3923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003924del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003927 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003930 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003931 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 {
3933 if (!HASHITEM_EMPTY(hi))
3934 {
3935 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3937 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 }
3939 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941}
3942#endif
3943
3944#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3945
3946/*
3947 * Local string buffer for the next two functions to store a variable name
3948 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3949 * get_user_var_name().
3950 */
3951
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003952static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954static char_u *varnamebuf = NULL;
3955static int varnamebuflen = 0;
3956
3957/*
3958 * Function to concatenate a prefix and a variable name.
3959 */
3960 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003961cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963 int len;
3964
3965 len = (int)STRLEN(name) + 3;
3966 if (len > varnamebuflen)
3967 {
3968 vim_free(varnamebuf);
3969 len += 10; /* some additional space */
3970 varnamebuf = alloc(len);
3971 if (varnamebuf == NULL)
3972 {
3973 varnamebuflen = 0;
3974 return NULL;
3975 }
3976 varnamebuflen = len;
3977 }
3978 *varnamebuf = prefix;
3979 varnamebuf[1] = ':';
3980 STRCPY(varnamebuf + 2, name);
3981 return varnamebuf;
3982}
3983
3984/*
3985 * Function given to ExpandGeneric() to obtain the list of user defined
3986 * (global/buffer/window/built-in) variable names.
3987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003989get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 static long_u gdone;
3992 static long_u bdone;
3993 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994#ifdef FEAT_WINDOWS
3995 static long_u tdone;
3996#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 static int vidx;
3998 static hashitem_T *hi;
3999 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 tdone = 0;
4006#endif
4007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004008
4009 /* Global variables */
4010 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 else
4015 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 while (HASHITEM_EMPTY(hi))
4017 ++hi;
4018 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4019 return cat_prefix_varname('g', hi->hi_key);
4020 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022
4023 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004024 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return (char_u *)"b:changedtick";
4039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
4041 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004042 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004045 if (wdone++ == 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('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004054#ifdef FEAT_WINDOWS
4055 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004056 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 if (tdone < ht->ht_used)
4058 {
4059 if (tdone++ == 0)
4060 hi = ht->ht_array;
4061 else
4062 ++hi;
4063 while (HASHITEM_EMPTY(hi))
4064 ++hi;
4065 return cat_prefix_varname('t', hi->hi_key);
4066 }
4067#endif
4068
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 /* v: variables */
4070 if (vidx < VV_LEN)
4071 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 vim_free(varnamebuf);
4074 varnamebuf = NULL;
4075 varnamebuflen = 0;
4076 return NULL;
4077}
4078
4079#endif /* FEAT_CMDL_COMPL */
4080
4081/*
4082 * types for expressions.
4083 */
4084typedef enum
4085{
4086 TYPE_UNKNOWN = 0
4087 , TYPE_EQUAL /* == */
4088 , TYPE_NEQUAL /* != */
4089 , TYPE_GREATER /* > */
4090 , TYPE_GEQUAL /* >= */
4091 , TYPE_SMALLER /* < */
4092 , TYPE_SEQUAL /* <= */
4093 , TYPE_MATCH /* =~ */
4094 , TYPE_NOMATCH /* !~ */
4095} exptype_T;
4096
4097/*
4098 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4101 */
4102
4103/*
4104 * Handle zero level expression.
4105 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004107 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * Return OK or FAIL.
4109 */
4110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004111eval0(
4112 char_u *arg,
4113 typval_T *rettv,
4114 char_u **nextcmd,
4115 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
4117 int ret;
4118 char_u *p;
4119
4120 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (ret == FAIL || !ends_excmd(*p))
4123 {
4124 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /*
4127 * Report the invalid expression unless the expression evaluation has
4128 * been cancelled due to an aborting error, an interrupt, or an
4129 * exception.
4130 */
4131 if (!aborting())
4132 EMSG2(_(e_invexpr2), arg);
4133 ret = FAIL;
4134 }
4135 if (nextcmd != NULL)
4136 *nextcmd = check_nextcmd(p);
4137
4138 return ret;
4139}
4140
4141/*
4142 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004143 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004148 * Note: "rettv.v_lock" is not set.
4149 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004153eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
4155 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 /*
4159 * Get the first variable.
4160 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 return FAIL;
4163
4164 if ((*arg)[0] == '?')
4165 {
4166 result = FALSE;
4167 if (evaluate)
4168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 int error = FALSE;
4170
4171 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004174 if (error)
4175 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177
4178 /*
4179 * Get the second variable.
4180 */
4181 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184
4185 /*
4186 * Check for the ":".
4187 */
4188 if ((*arg)[0] != ':')
4189 {
4190 EMSG(_("E109: Missing ':' after '?'"));
4191 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return FAIL;
4194 }
4195
4196 /*
4197 * Get the third variable.
4198 */
4199 *arg = skipwhite(*arg + 1);
4200 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4201 {
4202 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 return FAIL;
4205 }
4206 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209
4210 return OK;
4211}
4212
4213/*
4214 * Handle first level expression:
4215 * expr2 || expr2 || expr2 logical OR
4216 *
4217 * "arg" must point to the first non-white of the expression.
4218 * "arg" is advanced to the next non-white after the recognized expression.
4219 *
4220 * Return OK or FAIL.
4221 */
4222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004223eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
Bram Moolenaar33570922005-01-25 22:26:29 +00004225 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 long result;
4227 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004228 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 /*
4231 * Get the first variable.
4232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return FAIL;
4235
4236 /*
4237 * Repeat until there is no following "||".
4238 */
4239 first = TRUE;
4240 result = FALSE;
4241 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4242 {
4243 if (evaluate && first)
4244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (error)
4249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 first = FALSE;
4251 }
4252
4253 /*
4254 * Get the second variable.
4255 */
4256 *arg = skipwhite(*arg + 2);
4257 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4258 return FAIL;
4259
4260 /*
4261 * Compute the result.
4262 */
4263 if (evaluate && !result)
4264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (error)
4269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 if (evaluate)
4272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 rettv->v_type = VAR_NUMBER;
4274 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 }
4277
4278 return OK;
4279}
4280
4281/*
4282 * Handle second level expression:
4283 * expr3 && expr3 && expr3 logical AND
4284 *
4285 * "arg" must point to the first non-white of the expression.
4286 * "arg" is advanced to the next non-white after the recognized expression.
4287 *
4288 * Return OK or FAIL.
4289 */
4290 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004291eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292{
Bram Moolenaar33570922005-01-25 22:26:29 +00004293 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 long result;
4295 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
4298 /*
4299 * Get the first variable.
4300 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return FAIL;
4303
4304 /*
4305 * Repeat until there is no following "&&".
4306 */
4307 first = TRUE;
4308 result = TRUE;
4309 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4310 {
4311 if (evaluate && first)
4312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004316 if (error)
4317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 first = FALSE;
4319 }
4320
4321 /*
4322 * Get the second variable.
4323 */
4324 *arg = skipwhite(*arg + 2);
4325 if (eval4(arg, &var2, evaluate && result) == FAIL)
4326 return FAIL;
4327
4328 /*
4329 * Compute the result.
4330 */
4331 if (evaluate && result)
4332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 if (error)
4337 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 if (evaluate)
4340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 rettv->v_type = VAR_NUMBER;
4342 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 }
4345
4346 return OK;
4347}
4348
4349/*
4350 * Handle third level expression:
4351 * var1 == var2
4352 * var1 =~ var2
4353 * var1 != var2
4354 * var1 !~ var2
4355 * var1 > var2
4356 * var1 >= var2
4357 * var1 < var2
4358 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004359 * var1 is var2
4360 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 *
4362 * "arg" must point to the first non-white of the expression.
4363 * "arg" is advanced to the next non-white after the recognized expression.
4364 *
4365 * Return OK or FAIL.
4366 */
4367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004368eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
Bram Moolenaar33570922005-01-25 22:26:29 +00004370 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 char_u *p;
4372 int i;
4373 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 int len = 2;
4376 long n1, n2;
4377 char_u *s1, *s2;
4378 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4379 regmatch_T regmatch;
4380 int ic;
4381 char_u *save_cpo;
4382
4383 /*
4384 * Get the first variable.
4385 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 return FAIL;
4388
4389 p = *arg;
4390 switch (p[0])
4391 {
4392 case '=': if (p[1] == '=')
4393 type = TYPE_EQUAL;
4394 else if (p[1] == '~')
4395 type = TYPE_MATCH;
4396 break;
4397 case '!': if (p[1] == '=')
4398 type = TYPE_NEQUAL;
4399 else if (p[1] == '~')
4400 type = TYPE_NOMATCH;
4401 break;
4402 case '>': if (p[1] != '=')
4403 {
4404 type = TYPE_GREATER;
4405 len = 1;
4406 }
4407 else
4408 type = TYPE_GEQUAL;
4409 break;
4410 case '<': if (p[1] != '=')
4411 {
4412 type = TYPE_SMALLER;
4413 len = 1;
4414 }
4415 else
4416 type = TYPE_SEQUAL;
4417 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418 case 'i': if (p[1] == 's')
4419 {
4420 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4421 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004422 i = p[len];
4423 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 {
4425 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4426 type_is = TRUE;
4427 }
4428 }
4429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431
4432 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004433 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 */
4435 if (type != TYPE_UNKNOWN)
4436 {
4437 /* extra question mark appended: ignore case */
4438 if (p[len] == '?')
4439 {
4440 ic = TRUE;
4441 ++len;
4442 }
4443 /* extra '#' appended: match case */
4444 else if (p[len] == '#')
4445 {
4446 ic = FALSE;
4447 ++len;
4448 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004449 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 else
4451 ic = p_ic;
4452
4453 /*
4454 * Get the second variable.
4455 */
4456 *arg = skipwhite(p + len);
4457 if (eval5(arg, &var2, evaluate) == FAIL)
4458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 return FAIL;
4461 }
4462
4463 if (evaluate)
4464 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 if (type_is && rettv->v_type != var2.v_type)
4466 {
4467 /* For "is" a different type always means FALSE, for "notis"
4468 * it means TRUE. */
4469 n1 = (type == TYPE_NEQUAL);
4470 }
4471 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4472 {
4473 if (type_is)
4474 {
4475 n1 = (rettv->v_type == var2.v_type
4476 && rettv->vval.v_list == var2.vval.v_list);
4477 if (type == TYPE_NEQUAL)
4478 n1 = !n1;
4479 }
4480 else if (rettv->v_type != var2.v_type
4481 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4482 {
4483 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004484 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004485 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004486 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 clear_tv(rettv);
4488 clear_tv(&var2);
4489 return FAIL;
4490 }
4491 else
4492 {
4493 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004494 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4495 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 if (type == TYPE_NEQUAL)
4497 n1 = !n1;
4498 }
4499 }
4500
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004501 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4502 {
4503 if (type_is)
4504 {
4505 n1 = (rettv->v_type == var2.v_type
4506 && rettv->vval.v_dict == var2.vval.v_dict);
4507 if (type == TYPE_NEQUAL)
4508 n1 = !n1;
4509 }
4510 else if (rettv->v_type != var2.v_type
4511 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4512 {
4513 if (rettv->v_type != var2.v_type)
4514 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4515 else
4516 EMSG(_("E736: Invalid operation for Dictionary"));
4517 clear_tv(rettv);
4518 clear_tv(&var2);
4519 return FAIL;
4520 }
4521 else
4522 {
4523 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004524 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4525 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004526 if (type == TYPE_NEQUAL)
4527 n1 = !n1;
4528 }
4529 }
4530
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004531 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4532 {
4533 if (rettv->v_type != var2.v_type
4534 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4535 {
4536 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004537 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004538 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004539 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004540 clear_tv(rettv);
4541 clear_tv(&var2);
4542 return FAIL;
4543 }
4544 else
4545 {
4546 /* Compare two Funcrefs for being equal or unequal. */
4547 if (rettv->vval.v_string == NULL
4548 || var2.vval.v_string == NULL)
4549 n1 = FALSE;
4550 else
4551 n1 = STRCMP(rettv->vval.v_string,
4552 var2.vval.v_string) == 0;
4553 if (type == TYPE_NEQUAL)
4554 n1 = !n1;
4555 }
4556 }
4557
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004558#ifdef FEAT_FLOAT
4559 /*
4560 * If one of the two variables is a float, compare as a float.
4561 * When using "=~" or "!~", always compare as string.
4562 */
4563 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4564 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4565 {
4566 float_T f1, f2;
4567
4568 if (rettv->v_type == VAR_FLOAT)
4569 f1 = rettv->vval.v_float;
4570 else
4571 f1 = get_tv_number(rettv);
4572 if (var2.v_type == VAR_FLOAT)
4573 f2 = var2.vval.v_float;
4574 else
4575 f2 = get_tv_number(&var2);
4576 n1 = FALSE;
4577 switch (type)
4578 {
4579 case TYPE_EQUAL: n1 = (f1 == f2); break;
4580 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4581 case TYPE_GREATER: n1 = (f1 > f2); break;
4582 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4583 case TYPE_SMALLER: n1 = (f1 < f2); break;
4584 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4585 case TYPE_UNKNOWN:
4586 case TYPE_MATCH:
4587 case TYPE_NOMATCH: break; /* avoid gcc warning */
4588 }
4589 }
4590#endif
4591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 /*
4593 * If one of the two variables is a number, compare as a number.
4594 * When using "=~" or "!~", always compare as string.
4595 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004596 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 n1 = get_tv_number(rettv);
4600 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 switch (type)
4602 {
4603 case TYPE_EQUAL: n1 = (n1 == n2); break;
4604 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4605 case TYPE_GREATER: n1 = (n1 > n2); break;
4606 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4607 case TYPE_SMALLER: n1 = (n1 < n2); break;
4608 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4609 case TYPE_UNKNOWN:
4610 case TYPE_MATCH:
4611 case TYPE_NOMATCH: break; /* avoid gcc warning */
4612 }
4613 }
4614 else
4615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 s1 = get_tv_string_buf(rettv, buf1);
4617 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4619 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4620 else
4621 i = 0;
4622 n1 = FALSE;
4623 switch (type)
4624 {
4625 case TYPE_EQUAL: n1 = (i == 0); break;
4626 case TYPE_NEQUAL: n1 = (i != 0); break;
4627 case TYPE_GREATER: n1 = (i > 0); break;
4628 case TYPE_GEQUAL: n1 = (i >= 0); break;
4629 case TYPE_SMALLER: n1 = (i < 0); break;
4630 case TYPE_SEQUAL: n1 = (i <= 0); break;
4631
4632 case TYPE_MATCH:
4633 case TYPE_NOMATCH:
4634 /* avoid 'l' flag in 'cpoptions' */
4635 save_cpo = p_cpo;
4636 p_cpo = (char_u *)"";
4637 regmatch.regprog = vim_regcomp(s2,
4638 RE_MAGIC + RE_STRING);
4639 regmatch.rm_ic = ic;
4640 if (regmatch.regprog != NULL)
4641 {
4642 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004643 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (type == TYPE_NOMATCH)
4645 n1 = !n1;
4646 }
4647 p_cpo = save_cpo;
4648 break;
4649
4650 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4651 }
4652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 clear_tv(rettv);
4654 clear_tv(&var2);
4655 rettv->v_type = VAR_NUMBER;
4656 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
4658 }
4659
4660 return OK;
4661}
4662
4663/*
4664 * Handle fourth level expression:
4665 * + number addition
4666 * - number subtraction
4667 * . string concatenation
4668 *
4669 * "arg" must point to the first non-white of the expression.
4670 * "arg" is advanced to the next non-white after the recognized expression.
4671 *
4672 * Return OK or FAIL.
4673 */
4674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004675eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
Bram Moolenaar33570922005-01-25 22:26:29 +00004677 typval_T var2;
4678 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 int op;
4680 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004681#ifdef FEAT_FLOAT
4682 float_T f1 = 0, f2 = 0;
4683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 char_u *s1, *s2;
4685 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4686 char_u *p;
4687
4688 /*
4689 * Get the first variable.
4690 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004691 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 return FAIL;
4693
4694 /*
4695 * Repeat computing, until no '+', '-' or '.' is following.
4696 */
4697 for (;;)
4698 {
4699 op = **arg;
4700 if (op != '+' && op != '-' && op != '.')
4701 break;
4702
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004703 if ((op != '+' || rettv->v_type != VAR_LIST)
4704#ifdef FEAT_FLOAT
4705 && (op == '.' || rettv->v_type != VAR_FLOAT)
4706#endif
4707 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004708 {
4709 /* For "list + ...", an illegal use of the first operand as
4710 * a number cannot be determined before evaluating the 2nd
4711 * operand: if this is also a list, all is ok.
4712 * For "something . ...", "something - ..." or "non-list + ...",
4713 * we know that the first operand needs to be a string or number
4714 * without evaluating the 2nd operand. So check before to avoid
4715 * side effects after an error. */
4716 if (evaluate && get_tv_string_chk(rettv) == NULL)
4717 {
4718 clear_tv(rettv);
4719 return FAIL;
4720 }
4721 }
4722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 /*
4724 * Get the second variable.
4725 */
4726 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004727 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 return FAIL;
4731 }
4732
4733 if (evaluate)
4734 {
4735 /*
4736 * Compute the result.
4737 */
4738 if (op == '.')
4739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4741 s2 = get_tv_string_buf_chk(&var2, buf2);
4742 if (s2 == NULL) /* type error ? */
4743 {
4744 clear_tv(rettv);
4745 clear_tv(&var2);
4746 return FAIL;
4747 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004748 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(rettv);
4750 rettv->v_type = VAR_STRING;
4751 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004753 else if (op == '+' && rettv->v_type == VAR_LIST
4754 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004755 {
4756 /* concatenate Lists */
4757 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4758 &var3) == FAIL)
4759 {
4760 clear_tv(rettv);
4761 clear_tv(&var2);
4762 return FAIL;
4763 }
4764 clear_tv(rettv);
4765 *rettv = var3;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 else
4768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004769 int error = FALSE;
4770
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#ifdef FEAT_FLOAT
4772 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774 f1 = rettv->vval.v_float;
4775 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004776 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777 else
4778#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780 n1 = get_tv_number_chk(rettv, &error);
4781 if (error)
4782 {
4783 /* This can only happen for "list + non-list". For
4784 * "non-list + ..." or "something - ...", we returned
4785 * before evaluating the 2nd operand. */
4786 clear_tv(rettv);
4787 return FAIL;
4788 }
4789#ifdef FEAT_FLOAT
4790 if (var2.v_type == VAR_FLOAT)
4791 f1 = n1;
4792#endif
4793 }
4794#ifdef FEAT_FLOAT
4795 if (var2.v_type == VAR_FLOAT)
4796 {
4797 f2 = var2.vval.v_float;
4798 n2 = 0;
4799 }
4800 else
4801#endif
4802 {
4803 n2 = get_tv_number_chk(&var2, &error);
4804 if (error)
4805 {
4806 clear_tv(rettv);
4807 clear_tv(&var2);
4808 return FAIL;
4809 }
4810#ifdef FEAT_FLOAT
4811 if (rettv->v_type == VAR_FLOAT)
4812 f2 = n2;
4813#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004816
4817#ifdef FEAT_FLOAT
4818 /* If there is a float on either side the result is a float. */
4819 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4820 {
4821 if (op == '+')
4822 f1 = f1 + f2;
4823 else
4824 f1 = f1 - f2;
4825 rettv->v_type = VAR_FLOAT;
4826 rettv->vval.v_float = f1;
4827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829#endif
4830 {
4831 if (op == '+')
4832 n1 = n1 + n2;
4833 else
4834 n1 = n1 - n2;
4835 rettv->v_type = VAR_NUMBER;
4836 rettv->vval.v_number = n1;
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 }
4842 return OK;
4843}
4844
4845/*
4846 * Handle fifth level expression:
4847 * * number multiplication
4848 * / number division
4849 * % number modulo
4850 *
4851 * "arg" must point to the first non-white of the expression.
4852 * "arg" is advanced to the next non-white after the recognized expression.
4853 *
4854 * Return OK or FAIL.
4855 */
4856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004857eval6(
4858 char_u **arg,
4859 typval_T *rettv,
4860 int evaluate,
4861 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
Bram Moolenaar33570922005-01-25 22:26:29 +00004863 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 int op;
4865 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#ifdef FEAT_FLOAT
4867 int use_float = FALSE;
4868 float_T f1 = 0, f2;
4869#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 /*
4873 * Get the first variable.
4874 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004875 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 return FAIL;
4877
4878 /*
4879 * Repeat computing, until no '*', '/' or '%' is following.
4880 */
4881 for (;;)
4882 {
4883 op = **arg;
4884 if (op != '*' && op != '/' && op != '%')
4885 break;
4886
4887 if (evaluate)
4888 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889#ifdef FEAT_FLOAT
4890 if (rettv->v_type == VAR_FLOAT)
4891 {
4892 f1 = rettv->vval.v_float;
4893 use_float = TRUE;
4894 n1 = 0;
4895 }
4896 else
4897#endif
4898 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004900 if (error)
4901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 else
4904 n1 = 0;
4905
4906 /*
4907 * Get the second variable.
4908 */
4909 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004910 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 return FAIL;
4912
4913 if (evaluate)
4914 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915#ifdef FEAT_FLOAT
4916 if (var2.v_type == VAR_FLOAT)
4917 {
4918 if (!use_float)
4919 {
4920 f1 = n1;
4921 use_float = TRUE;
4922 }
4923 f2 = var2.vval.v_float;
4924 n2 = 0;
4925 }
4926 else
4927#endif
4928 {
4929 n2 = get_tv_number_chk(&var2, &error);
4930 clear_tv(&var2);
4931 if (error)
4932 return FAIL;
4933#ifdef FEAT_FLOAT
4934 if (use_float)
4935 f2 = n2;
4936#endif
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 /*
4940 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004941 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943#ifdef FEAT_FLOAT
4944 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946 if (op == '*')
4947 f1 = f1 * f2;
4948 else if (op == '/')
4949 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004950# ifdef VMS
4951 /* VMS crashes on divide by zero, work around it */
4952 if (f2 == 0.0)
4953 {
4954 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004955 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004956 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004957 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004958 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004959 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004960 }
4961 else
4962 f1 = f1 / f2;
4963# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964 /* We rely on the floating point library to handle divide
4965 * by zero to result in "inf" and not a crash. */
4966 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004967# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004971 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 return FAIL;
4973 }
4974 rettv->v_type = VAR_FLOAT;
4975 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 if (op == '*')
4981 n1 = n1 * n2;
4982 else if (op == '/')
4983 {
4984 if (n2 == 0) /* give an error message? */
4985 {
4986 if (n1 == 0)
4987 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4988 else if (n1 < 0)
4989 n1 = -0x7fffffffL;
4990 else
4991 n1 = 0x7fffffffL;
4992 }
4993 else
4994 n1 = n1 / n2;
4995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 {
4998 if (n2 == 0) /* give an error message? */
4999 n1 = 0;
5000 else
5001 n1 = n1 % n2;
5002 }
5003 rettv->v_type = VAR_NUMBER;
5004 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007 }
5008
5009 return OK;
5010}
5011
5012/*
5013 * Handle sixth level expression:
5014 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005015 * "string" string constant
5016 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 * &option-name option value
5018 * @r register contents
5019 * identifier variable value
5020 * function() function call
5021 * $VAR environment variable
5022 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005023 * [expr, expr] List
5024 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 *
5026 * Also handle:
5027 * ! in front logical NOT
5028 * - in front unary minus
5029 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 * trailing [] subscript in String or List
5031 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 *
5033 * "arg" must point to the first non-white of the expression.
5034 * "arg" is advanced to the next non-white after the recognized expression.
5035 *
5036 * Return OK or FAIL.
5037 */
5038 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005039eval7(
5040 char_u **arg,
5041 typval_T *rettv,
5042 int evaluate,
5043 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 long n;
5046 int len;
5047 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 char_u *start_leader, *end_leader;
5049 int ret = OK;
5050 char_u *alias;
5051
5052 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005054 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 /*
5059 * Skip '!' and '-' characters. They are handled later.
5060 */
5061 start_leader = *arg;
5062 while (**arg == '!' || **arg == '-' || **arg == '+')
5063 *arg = skipwhite(*arg + 1);
5064 end_leader = *arg;
5065
5066 switch (**arg)
5067 {
5068 /*
5069 * Number constant.
5070 */
5071 case '0':
5072 case '1':
5073 case '2':
5074 case '3':
5075 case '4':
5076 case '5':
5077 case '6':
5078 case '7':
5079 case '8':
5080 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005081 {
5082#ifdef FEAT_FLOAT
5083 char_u *p = skipdigits(*arg + 1);
5084 int get_float = FALSE;
5085
5086 /* We accept a float when the format matches
5087 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005088 * strict to avoid backwards compatibility problems.
5089 * Don't look for a float after the "." operator, so that
5090 * ":let vers = 1.2.3" doesn't fail. */
5091 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 get_float = TRUE;
5094 p = skipdigits(p + 2);
5095 if (*p == 'e' || *p == 'E')
5096 {
5097 ++p;
5098 if (*p == '-' || *p == '+')
5099 ++p;
5100 if (!vim_isdigit(*p))
5101 get_float = FALSE;
5102 else
5103 p = skipdigits(p + 1);
5104 }
5105 if (ASCII_ISALPHA(*p) || *p == '.')
5106 get_float = FALSE;
5107 }
5108 if (get_float)
5109 {
5110 float_T f;
5111
5112 *arg += string2float(*arg, &f);
5113 if (evaluate)
5114 {
5115 rettv->v_type = VAR_FLOAT;
5116 rettv->vval.v_float = f;
5117 }
5118 }
5119 else
5120#endif
5121 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005122 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005123 *arg += len;
5124 if (evaluate)
5125 {
5126 rettv->v_type = VAR_NUMBER;
5127 rettv->vval.v_number = n;
5128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 /*
5134 * String constant: "string".
5135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 break;
5138
5139 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 break;
5144
5145 /*
5146 * List: [expr, expr]
5147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150
5151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 * Dictionary: {key: val, key: val}
5153 */
5154 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5155 break;
5156
5157 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005158 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005160 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
5164 * Environment variable: $VAR.
5165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168
5169 /*
5170 * Register contents: @r.
5171 */
5172 case '@': ++*arg;
5173 if (evaluate)
5174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005176 rettv->vval.v_string = get_reg_contents(**arg,
5177 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179 if (**arg != NUL)
5180 ++*arg;
5181 break;
5182
5183 /*
5184 * nested expression: (expression).
5185 */
5186 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if (**arg == ')')
5189 ++*arg;
5190 else if (ret == OK)
5191 {
5192 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 ret = FAIL;
5195 }
5196 break;
5197
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 break;
5200 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201
5202 if (ret == NOTDONE)
5203 {
5204 /*
5205 * Must be a variable or function name.
5206 * Can also be a curly-braces kind of name: {expr}.
5207 */
5208 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005209 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 if (alias != NULL)
5211 s = alias;
5212
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005213 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 ret = FAIL;
5215 else
5216 {
5217 if (**arg == '(') /* recursive! */
5218 {
5219 /* If "s" is the name of a variable of type VAR_FUNC
5220 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005221 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222
5223 /* Invoke the function. */
5224 ret = get_func_tv(s, len, rettv, arg,
5225 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005226 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005227
5228 /* If evaluate is FALSE rettv->v_type was not set in
5229 * get_func_tv, but it's needed in handle_subscript() to parse
5230 * what follows. So set it here. */
5231 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5232 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005233 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005234 rettv->v_type = VAR_FUNC;
5235 }
5236
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 /* Stop the expression evaluation when immediately
5238 * aborting on error, or when an interrupt occurred or
5239 * an exception was thrown but not caught. */
5240 if (aborting())
5241 {
5242 if (ret == OK)
5243 clear_tv(rettv);
5244 ret = FAIL;
5245 }
5246 }
5247 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005248 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005249 else
5250 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005252 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 }
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 *arg = skipwhite(*arg);
5256
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5258 * expr(expr). */
5259 if (ret == OK)
5260 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262 /*
5263 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5264 */
5265 if (ret == OK && evaluate && end_leader > start_leader)
5266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005267 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005268 int val = 0;
5269#ifdef FEAT_FLOAT
5270 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005272 if (rettv->v_type == VAR_FLOAT)
5273 f = rettv->vval.v_float;
5274 else
5275#endif
5276 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279 clear_tv(rettv);
5280 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 else
5283 {
5284 while (end_leader > start_leader)
5285 {
5286 --end_leader;
5287 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 {
5289#ifdef FEAT_FLOAT
5290 if (rettv->v_type == VAR_FLOAT)
5291 f = !f;
5292 else
5293#endif
5294 val = !val;
5295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005297 {
5298#ifdef FEAT_FLOAT
5299 if (rettv->v_type == VAR_FLOAT)
5300 f = -f;
5301 else
5302#endif
5303 val = -val;
5304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005306#ifdef FEAT_FLOAT
5307 if (rettv->v_type == VAR_FLOAT)
5308 {
5309 clear_tv(rettv);
5310 rettv->vval.v_float = f;
5311 }
5312 else
5313#endif
5314 {
5315 clear_tv(rettv);
5316 rettv->v_type = VAR_NUMBER;
5317 rettv->vval.v_number = val;
5318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321
5322 return ret;
5323}
5324
5325/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005326 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5327 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5329 */
5330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005331eval_index(
5332 char_u **arg,
5333 typval_T *rettv,
5334 int evaluate,
5335 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336{
5337 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005338 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340 long len = -1;
5341 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344
Bram Moolenaara03f2332016-02-06 18:09:59 +01005345 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005347 case VAR_FUNC:
5348 if (verbose)
5349 EMSG(_("E695: Cannot index a Funcref"));
5350 return FAIL;
5351 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005352#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 if (verbose)
5354 EMSG(_(e_float_as_string));
5355 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005356#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005358 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 if (verbose)
5360 EMSG(_("E909: Cannot index a special variable"));
5361 return FAIL;
5362 case VAR_UNKNOWN:
5363 if (evaluate)
5364 return FAIL;
5365 /* FALLTHROUGH */
5366
5367 case VAR_STRING:
5368 case VAR_NUMBER:
5369 case VAR_LIST:
5370 case VAR_DICT:
5371 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005372 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005374 init_tv(&var1);
5375 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 /*
5379 * dict.name
5380 */
5381 key = *arg + 1;
5382 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5383 ;
5384 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 /*
5391 * something[idx]
5392 *
5393 * Get the (first) variable from inside the [].
5394 */
5395 *arg = skipwhite(*arg + 1);
5396 if (**arg == ':')
5397 empty1 = TRUE;
5398 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5399 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005400 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5401 {
5402 /* not a number or string */
5403 clear_tv(&var1);
5404 return FAIL;
5405 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406
5407 /*
5408 * Get the second variable from inside the [:].
5409 */
5410 if (**arg == ':')
5411 {
5412 range = TRUE;
5413 *arg = skipwhite(*arg + 1);
5414 if (**arg == ']')
5415 empty2 = TRUE;
5416 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005418 if (!empty1)
5419 clear_tv(&var1);
5420 return FAIL;
5421 }
5422 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5423 {
5424 /* not a number or string */
5425 if (!empty1)
5426 clear_tv(&var1);
5427 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428 return FAIL;
5429 }
5430 }
5431
5432 /* Check for the ']'. */
5433 if (**arg != ']')
5434 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005435 if (verbose)
5436 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 clear_tv(&var1);
5438 if (range)
5439 clear_tv(&var2);
5440 return FAIL;
5441 }
5442 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 }
5444
5445 if (evaluate)
5446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 n1 = 0;
5448 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005450 n1 = get_tv_number(&var1);
5451 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453 if (range)
5454 {
5455 if (empty2)
5456 n2 = -1;
5457 else
5458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 n2 = get_tv_number(&var2);
5460 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 }
5463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005466 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005467 case VAR_FUNC:
5468 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469 case VAR_SPECIAL:
5470 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005471 break; /* not evaluating, skipping over subscript */
5472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 case VAR_NUMBER:
5474 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 len = (long)STRLEN(s);
5477 if (range)
5478 {
5479 /* The resulting variable is a substring. If the indexes
5480 * are out of range the result is empty. */
5481 if (n1 < 0)
5482 {
5483 n1 = len + n1;
5484 if (n1 < 0)
5485 n1 = 0;
5486 }
5487 if (n2 < 0)
5488 n2 = len + n2;
5489 else if (n2 >= len)
5490 n2 = len;
5491 if (n1 >= len || n2 < 0 || n1 > n2)
5492 s = NULL;
5493 else
5494 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5495 }
5496 else
5497 {
5498 /* The resulting variable is a string of a single
5499 * character. If the index is too big or negative the
5500 * result is empty. */
5501 if (n1 >= len || n1 < 0)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, 1);
5505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 clear_tv(rettv);
5507 rettv->v_type = VAR_STRING;
5508 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 break;
5510
5511 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 if (n1 < 0)
5514 n1 = len + n1;
5515 if (!empty1 && (n1 < 0 || n1 >= len))
5516 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005517 /* For a range we allow invalid values and return an empty
5518 * list. A list index out of range is an error. */
5519 if (!range)
5520 {
5521 if (verbose)
5522 EMSGN(_(e_listidx), n1);
5523 return FAIL;
5524 }
5525 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 }
5527 if (range)
5528 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005529 list_T *l;
5530 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531
5532 if (n2 < 0)
5533 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005534 else if (n2 >= len)
5535 n2 = len - 1;
5536 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005537 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 l = list_alloc();
5539 if (l == NULL)
5540 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 n1 <= n2; ++n1)
5543 {
5544 if (list_append_tv(l, &item->li_tv) == FAIL)
5545 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005546 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 return FAIL;
5548 }
5549 item = item->li_next;
5550 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 clear_tv(rettv);
5552 rettv->v_type = VAR_LIST;
5553 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005554 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556 else
5557 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005558 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 clear_tv(rettv);
5560 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 }
5562 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563
5564 case VAR_DICT:
5565 if (range)
5566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005567 if (verbose)
5568 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 if (len == -1)
5570 clear_tv(&var1);
5571 return FAIL;
5572 }
5573 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005574 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575
5576 if (len == -1)
5577 {
5578 key = get_tv_string(&var1);
5579 if (*key == NUL)
5580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 if (verbose)
5582 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 clear_tv(&var1);
5584 return FAIL;
5585 }
5586 }
5587
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005588 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005590 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005591 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 if (len == -1)
5593 clear_tv(&var1);
5594 if (item == NULL)
5595 return FAIL;
5596
5597 copy_tv(&item->di_tv, &var1);
5598 clear_tv(rettv);
5599 *rettv = var1;
5600 }
5601 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 }
5603 }
5604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 return OK;
5606}
5607
5608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 * Get an option value.
5610 * "arg" points to the '&' or '+' before the option name.
5611 * "arg" is advanced to character after the option name.
5612 * Return OK or FAIL.
5613 */
5614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005615get_option_tv(
5616 char_u **arg,
5617 typval_T *rettv, /* when NULL, only check if option exists */
5618 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
5620 char_u *option_end;
5621 long numval;
5622 char_u *stringval;
5623 int opt_type;
5624 int c;
5625 int working = (**arg == '+'); /* has("+option") */
5626 int ret = OK;
5627 int opt_flags;
5628
5629 /*
5630 * Isolate the option name and find its value.
5631 */
5632 option_end = find_option_end(arg, &opt_flags);
5633 if (option_end == NULL)
5634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 EMSG2(_("E112: Option name missing: %s"), *arg);
5637 return FAIL;
5638 }
5639
5640 if (!evaluate)
5641 {
5642 *arg = option_end;
5643 return OK;
5644 }
5645
5646 c = *option_end;
5647 *option_end = NUL;
5648 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 if (opt_type == -3) /* invalid name */
5652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 EMSG2(_("E113: Unknown option: %s"), *arg);
5655 ret = FAIL;
5656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
5659 if (opt_type == -2) /* hidden string option */
5660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 rettv->v_type = VAR_STRING;
5662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664 else if (opt_type == -1) /* hidden number option */
5665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 rettv->v_type = VAR_NUMBER;
5667 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669 else if (opt_type == 1) /* number option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_NUMBER;
5672 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else /* string option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_STRING;
5677 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 }
5680 else if (working && (opt_type == -2 || opt_type == -1))
5681 ret = FAIL;
5682
5683 *option_end = c; /* put back for error messages */
5684 *arg = option_end;
5685
5686 return ret;
5687}
5688
5689/*
5690 * Allocate a variable for a string constant.
5691 * Return OK or FAIL.
5692 */
5693 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005694get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
5696 char_u *p;
5697 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 int extra = 0;
5699
5700 /*
5701 * Find the end of the string, skipping backslashed characters.
5702 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005703 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 if (*p == '\\' && p[1] != NUL)
5706 {
5707 ++p;
5708 /* A "\<x>" form occupies at least 4 characters, and produces up
5709 * to 6 characters: reserve space for 2 extra */
5710 if (*p == '<')
5711 extra += 2;
5712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714
5715 if (*p != '"')
5716 {
5717 EMSG2(_("E114: Missing quote: %s"), *arg);
5718 return FAIL;
5719 }
5720
5721 /* If only parsing, set *arg and return here */
5722 if (!evaluate)
5723 {
5724 *arg = p + 1;
5725 return OK;
5726 }
5727
5728 /*
5729 * Copy the string into allocated memory, handling backslashed
5730 * characters.
5731 */
5732 name = alloc((unsigned)(p - *arg + extra));
5733 if (name == NULL)
5734 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 rettv->v_type = VAR_STRING;
5736 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
5740 if (*p == '\\')
5741 {
5742 switch (*++p)
5743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 case 'b': *name++ = BS; ++p; break;
5745 case 'e': *name++ = ESC; ++p; break;
5746 case 'f': *name++ = FF; ++p; break;
5747 case 'n': *name++ = NL; ++p; break;
5748 case 'r': *name++ = CAR; ++p; break;
5749 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
5751 case 'X': /* hex: "\x1", "\x12" */
5752 case 'x':
5753 case 'u': /* Unicode: "\u0023" */
5754 case 'U':
5755 if (vim_isxdigit(p[1]))
5756 {
5757 int n, nr;
5758 int c = toupper(*p);
5759
5760 if (c == 'X')
5761 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005762 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005764 else
5765 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 nr = 0;
5767 while (--n >= 0 && vim_isxdigit(p[1]))
5768 {
5769 ++p;
5770 nr = (nr << 4) + hex2nr(*p);
5771 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773#ifdef FEAT_MBYTE
5774 /* For "\u" store the number according to
5775 * 'encoding'. */
5776 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 else
5779#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 break;
5783
5784 /* octal: "\1", "\12", "\123" */
5785 case '0':
5786 case '1':
5787 case '2':
5788 case '3':
5789 case '4':
5790 case '5':
5791 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 case '7': *name = *p++ - '0';
5793 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 *name = (*name << 3) + *p++ - '0';
5796 if (*p >= '0' && *p <= '7')
5797 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 break;
5801
5802 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 if (extra != 0)
5805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 break;
5808 }
5809 /* FALLTHROUGH */
5810
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813 }
5814 }
5815 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 *arg = p + 1;
5821
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 return OK;
5823}
5824
5825/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 * Return OK or FAIL.
5828 */
5829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005830get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 int reduce = 0;
5835
5836 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 */
5839 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 break;
5845 ++reduce;
5846 ++p;
5847 }
5848 }
5849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 return FAIL;
5854 }
5855
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 if (!evaluate)
5858 {
5859 *arg = p + 1;
5860 return OK;
5861 }
5862
5863 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 */
5866 str = alloc((unsigned)((p - *arg) - reduce));
5867 if (str == NULL)
5868 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 rettv->v_type = VAR_STRING;
5870 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 break;
5878 ++p;
5879 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 *arg = p + 1;
5884
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 return OK;
5886}
5887
5888/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 * Allocate a variable for a List and fill it from "*arg".
5890 * Return OK or FAIL.
5891 */
5892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005893get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894{
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 list_T *l = NULL;
5896 typval_T tv;
5897 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898
5899 if (evaluate)
5900 {
5901 l = list_alloc();
5902 if (l == NULL)
5903 return FAIL;
5904 }
5905
5906 *arg = skipwhite(*arg + 1);
5907 while (**arg != ']' && **arg != NUL)
5908 {
5909 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5910 goto failret;
5911 if (evaluate)
5912 {
5913 item = listitem_alloc();
5914 if (item != NULL)
5915 {
5916 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005917 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 list_append(l, item);
5919 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005920 else
5921 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 }
5923
5924 if (**arg == ']')
5925 break;
5926 if (**arg != ',')
5927 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 goto failret;
5930 }
5931 *arg = skipwhite(*arg + 1);
5932 }
5933
5934 if (**arg != ']')
5935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937failret:
5938 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 return FAIL;
5941 }
5942
5943 *arg = skipwhite(*arg + 1);
5944 if (evaluate)
5945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005946 rettv->v_type = VAR_LIST;
5947 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 ++l->lv_refcount;
5949 }
5950
5951 return OK;
5952}
5953
5954/*
5955 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005956 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005958 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005959list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005961 list_T *l;
5962
5963 l = (list_T *)alloc_clear(sizeof(list_T));
5964 if (l != NULL)
5965 {
5966 /* Prepend the list to the list of lists for garbage collection. */
5967 if (first_list != NULL)
5968 first_list->lv_used_prev = l;
5969 l->lv_used_prev = NULL;
5970 l->lv_used_next = first_list;
5971 first_list = l;
5972 }
5973 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974}
5975
5976/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005977 * Allocate an empty list for a return value.
5978 * Returns OK or FAIL.
5979 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005981rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005982{
5983 list_T *l = list_alloc();
5984
5985 if (l == NULL)
5986 return FAIL;
5987
5988 rettv->vval.v_list = l;
5989 rettv->v_type = VAR_LIST;
5990 ++l->lv_refcount;
5991 return OK;
5992}
5993
5994/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 * Unreference a list: decrement the reference count and free it when it
5996 * becomes zero.
5997 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005998 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005999list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006001 if (l != NULL && --l->lv_refcount <= 0)
6002 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003}
6004
6005/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006006 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Ignores the reference count.
6008 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006010list_free(
6011 list_T *l,
6012 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006016 /* Remove the list from the list of lists for garbage collection. */
6017 if (l->lv_used_prev == NULL)
6018 first_list = l->lv_used_next;
6019 else
6020 l->lv_used_prev->lv_used_next = l->lv_used_next;
6021 if (l->lv_used_next != NULL)
6022 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6023
Bram Moolenaard9fba312005-06-26 22:34:35 +00006024 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006026 /* Remove the item before deleting it. */
6027 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006028 if (recurse || (item->li_tv.v_type != VAR_LIST
6029 && item->li_tv.v_type != VAR_DICT))
6030 clear_tv(&item->li_tv);
6031 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032 }
6033 vim_free(l);
6034}
6035
6036/*
6037 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006038 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006040 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006041listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042{
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044}
6045
6046/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006047 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006050listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006052 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 vim_free(item);
6054}
6055
6056/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006057 * Remove a list item from a List and free it. Also clears the value.
6058 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006061{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006062 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006063 listitem_free(item);
6064}
6065
6066/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 * Get the number of items in a list.
6068 */
6069 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 if (l == NULL)
6073 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006074 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075}
6076
6077/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 * Return TRUE when two lists have exactly the same values.
6079 */
6080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006081list_equal(
6082 list_T *l1,
6083 list_T *l2,
6084 int ic, /* ignore case for strings */
6085 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086{
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006089 if (l1 == NULL || l2 == NULL)
6090 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 if (l1 == l2)
6092 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006093 if (list_len(l1) != list_len(l2))
6094 return FALSE;
6095
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 for (item1 = l1->lv_first, item2 = l2->lv_first;
6097 item1 != NULL && item2 != NULL;
6098 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 return FALSE;
6101 return item1 == NULL && item2 == NULL;
6102}
6103
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006104/*
6105 * Return the dictitem that an entry in a hashtable points to.
6106 */
6107 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006108dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006109{
6110 return HI2DI(hi);
6111}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006112
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006114 * Return TRUE when two dictionaries have exactly the same key/values.
6115 */
6116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006117dict_equal(
6118 dict_T *d1,
6119 dict_T *d2,
6120 int ic, /* ignore case for strings */
6121 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006122{
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 hashitem_T *hi;
6124 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006125 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006127 if (d1 == NULL || d2 == NULL)
6128 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006129 if (d1 == d2)
6130 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 if (dict_len(d1) != dict_len(d2))
6132 return FALSE;
6133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006134 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 if (!HASHITEM_EMPTY(hi))
6138 {
6139 item2 = dict_find(d2, hi->hi_key, -1);
6140 if (item2 == NULL)
6141 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 return FALSE;
6144 --todo;
6145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 }
6147 return TRUE;
6148}
6149
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150static int tv_equal_recurse_limit;
6151
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006153 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006154 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006155 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156 */
6157 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006158tv_equal(
6159 typval_T *tv1,
6160 typval_T *tv2,
6161 int ic, /* ignore case */
6162 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163{
6164 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006165 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006167 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006169 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006170 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006172 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006173 * recursiveness to a limit. We guess they are equal then.
6174 * A fixed limit has the problem of still taking an awful long time.
6175 * Reduce the limit every time running into it. That should work fine for
6176 * deeply linked structures that are not recursively linked and catch
6177 * recursiveness quickly. */
6178 if (!recursive)
6179 tv_equal_recurse_limit = 1000;
6180 if (recursive_cnt >= tv_equal_recurse_limit)
6181 {
6182 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006185
6186 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006187 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 ++recursive_cnt;
6190 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6191 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006192 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193
6194 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 ++recursive_cnt;
6196 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6197 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006198 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199
6200 case VAR_FUNC:
6201 return (tv1->vval.v_string != NULL
6202 && tv2->vval.v_string != NULL
6203 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6204
6205 case VAR_NUMBER:
6206 return tv1->vval.v_number == tv2->vval.v_number;
6207
6208 case VAR_STRING:
6209 s1 = get_tv_string_buf(tv1, buf1);
6210 s2 = get_tv_string_buf(tv2, buf2);
6211 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006212
6213 case VAR_SPECIAL:
6214 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006215
6216 case VAR_FLOAT:
6217#ifdef FEAT_FLOAT
6218 return tv1->vval.v_float == tv2->vval.v_float;
6219#endif
6220 case VAR_JOB:
6221#ifdef FEAT_JOB
6222 return tv1->vval.v_job == tv2->vval.v_job;
6223#endif
6224 case VAR_UNKNOWN:
6225 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006226 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006227
Bram Moolenaara03f2332016-02-06 18:09:59 +01006228 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6229 * does not equal anything, not even itself. */
6230 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006231}
6232
6233/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234 * Locate item with index "n" in list "l" and return it.
6235 * A negative index is counted from the end; -1 is the last item.
6236 * Returns NULL when "n" is out of range.
6237 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006239list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006240{
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 long idx;
6243
6244 if (l == NULL)
6245 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006246
6247 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006249 n = l->lv_len + n;
6250
6251 /* Check for index out of range. */
6252 if (n < 0 || n >= l->lv_len)
6253 return NULL;
6254
6255 /* When there is a cached index may start search from there. */
6256 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006258 if (n < l->lv_idx / 2)
6259 {
6260 /* closest to the start of the list */
6261 item = l->lv_first;
6262 idx = 0;
6263 }
6264 else if (n > (l->lv_idx + l->lv_len) / 2)
6265 {
6266 /* closest to the end of the list */
6267 item = l->lv_last;
6268 idx = l->lv_len - 1;
6269 }
6270 else
6271 {
6272 /* closest to the cached index */
6273 item = l->lv_idx_item;
6274 idx = l->lv_idx;
6275 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 }
6277 else
6278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_len / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006292
6293 while (n > idx)
6294 {
6295 /* search forward */
6296 item = item->li_next;
6297 ++idx;
6298 }
6299 while (n < idx)
6300 {
6301 /* search backward */
6302 item = item->li_prev;
6303 --idx;
6304 }
6305
6306 /* cache the used index */
6307 l->lv_idx = idx;
6308 l->lv_idx_item = item;
6309
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 return item;
6311}
6312
6313/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006314 * Get list item "l[idx]" as a number.
6315 */
6316 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317list_find_nr(
6318 list_T *l,
6319 long idx,
6320 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006321{
6322 listitem_T *li;
6323
6324 li = list_find(l, idx);
6325 if (li == NULL)
6326 {
6327 if (errorp != NULL)
6328 *errorp = TRUE;
6329 return -1L;
6330 }
6331 return get_tv_number_chk(&li->li_tv, errorp);
6332}
6333
6334/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006335 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6336 */
6337 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006339{
6340 listitem_T *li;
6341
6342 li = list_find(l, idx - 1);
6343 if (li == NULL)
6344 {
6345 EMSGN(_(e_listidx), idx);
6346 return NULL;
6347 }
6348 return get_tv_string(&li->li_tv);
6349}
6350
6351/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006352 * Locate "item" list "l" and return its index.
6353 * Returns -1 when "item" is not in the list.
6354 */
6355 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006357{
6358 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006360
6361 if (l == NULL)
6362 return -1;
6363 idx = 0;
6364 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6365 ++idx;
6366 if (li == NULL)
6367 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006368 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006369}
6370
6371/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 * Append item "item" to the end of list "l".
6373 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376{
6377 if (l->lv_last == NULL)
6378 {
6379 /* empty list */
6380 l->lv_first = item;
6381 l->lv_last = item;
6382 item->li_prev = NULL;
6383 }
6384 else
6385 {
6386 l->lv_last->li_next = item;
6387 item->li_prev = l->lv_last;
6388 l->lv_last = item;
6389 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 item->li_next = NULL;
6392}
6393
6394/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006396 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006399list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006401 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402
Bram Moolenaar05159a02005-02-26 23:04:13 +00006403 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006405 copy_tv(tv, &li->li_tv);
6406 list_append(l, li);
6407 return OK;
6408}
6409
6410/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006411 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006412 * Return FAIL when out of memory.
6413 */
6414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006415list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006416{
6417 listitem_T *li = listitem_alloc();
6418
6419 if (li == NULL)
6420 return FAIL;
6421 li->li_tv.v_type = VAR_DICT;
6422 li->li_tv.v_lock = 0;
6423 li->li_tv.vval.v_dict = dict;
6424 list_append(list, li);
6425 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 return OK;
6427}
6428
6429/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006430 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006431 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006432 * Returns FAIL when out of memory.
6433 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006435list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006436{
6437 listitem_T *li = listitem_alloc();
6438
6439 if (li == NULL)
6440 return FAIL;
6441 list_append(l, li);
6442 li->li_tv.v_type = VAR_STRING;
6443 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006444 if (str == NULL)
6445 li->li_tv.vval.v_string = NULL;
6446 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006447 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 return FAIL;
6449 return OK;
6450}
6451
6452/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 * Append "n" to list "l".
6454 * Returns FAIL when out of memory.
6455 */
6456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006457list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006458{
6459 listitem_T *li;
6460
6461 li = listitem_alloc();
6462 if (li == NULL)
6463 return FAIL;
6464 li->li_tv.v_type = VAR_NUMBER;
6465 li->li_tv.v_lock = 0;
6466 li->li_tv.vval.v_number = n;
6467 list_append(l, li);
6468 return OK;
6469}
6470
6471/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473 * If "item" is NULL append at the end.
6474 * Return FAIL when out of memory.
6475 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006477list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478{
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480
6481 if (ni == NULL)
6482 return FAIL;
6483 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006484 list_insert(l, ni, item);
6485 return OK;
6486}
6487
6488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006490{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491 if (item == NULL)
6492 /* Append new item at end of list. */
6493 list_append(l, ni);
6494 else
6495 {
6496 /* Insert new item before existing item. */
6497 ni->li_prev = item->li_prev;
6498 ni->li_next = item;
6499 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006500 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006502 ++l->lv_idx;
6503 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006505 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006507 l->lv_idx_item = NULL;
6508 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512}
6513
6514/*
6515 * Extend "l1" with "l2".
6516 * If "bef" is NULL append at the end, otherwise insert before this item.
6517 * Returns FAIL when out of memory.
6518 */
6519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521{
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006523 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006525 /* We also quit the loop when we have inserted the original item count of
6526 * the list, avoid a hang when we extend a list with itself. */
6527 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6529 return FAIL;
6530 return OK;
6531}
6532
6533/*
6534 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6535 * Return FAIL when out of memory.
6536 */
6537 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539{
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006542 if (l1 == NULL || l2 == NULL)
6543 return FAIL;
6544
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006546 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 if (l == NULL)
6548 return FAIL;
6549 tv->v_type = VAR_LIST;
6550 tv->vval.v_list = l;
6551
6552 /* append all items from the second list */
6553 return list_extend(l, l2, NULL);
6554}
6555
6556/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006559 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 * Returns NULL when out of memory.
6561 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006563list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564{
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 list_T *copy;
6566 listitem_T *item;
6567 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568
6569 if (orig == NULL)
6570 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571
6572 copy = list_alloc();
6573 if (copy != NULL)
6574 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 if (copyID != 0)
6576 {
6577 /* Do this before adding the items, because one of the items may
6578 * refer back to this list. */
6579 orig->lv_copyID = copyID;
6580 orig->lv_copylist = copy;
6581 }
6582 for (item = orig->lv_first; item != NULL && !got_int;
6583 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 {
6585 ni = listitem_alloc();
6586 if (ni == NULL)
6587 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006588 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 {
6590 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6591 {
6592 vim_free(ni);
6593 break;
6594 }
6595 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006597 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 list_append(copy, ni);
6599 }
6600 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 if (item != NULL)
6602 {
6603 list_unref(copy);
6604 copy = NULL;
6605 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 }
6607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 return copy;
6609}
6610
6611/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006613 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006614 * This used to be called list_remove, but that conflicts with a Sun header
6615 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006618vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006619{
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622 /* notify watchers */
6623 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006625 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 list_fix_watch(l, ip);
6627 if (ip == item2)
6628 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630
6631 if (item2->li_next == NULL)
6632 l->lv_last = item->li_prev;
6633 else
6634 item2->li_next->li_prev = item->li_prev;
6635 if (item->li_prev == NULL)
6636 l->lv_first = item2->li_next;
6637 else
6638 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006639 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640}
6641
6642/*
6643 * Return an allocated string with the string representation of a list.
6644 * May return NULL.
6645 */
6646 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006647list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648{
6649 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650
6651 if (tv->vval.v_list == NULL)
6652 return NULL;
6653 ga_init2(&ga, (int)sizeof(char), 80);
6654 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006655 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 {
6657 vim_free(ga.ga_data);
6658 return NULL;
6659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 ga_append(&ga, ']');
6661 ga_append(&ga, NUL);
6662 return (char_u *)ga.ga_data;
6663}
6664
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006665typedef struct join_S {
6666 char_u *s;
6667 char_u *tofree;
6668} join_T;
6669
6670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006671list_join_inner(
6672 garray_T *gap, /* to store the result in */
6673 list_T *l,
6674 char_u *sep,
6675 int echo_style,
6676 int copyID,
6677 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006678{
6679 int i;
6680 join_T *p;
6681 int len;
6682 int sumlen = 0;
6683 int first = TRUE;
6684 char_u *tofree;
6685 char_u numbuf[NUMBUFLEN];
6686 listitem_T *item;
6687 char_u *s;
6688
6689 /* Stringify each item in the list. */
6690 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6691 {
6692 if (echo_style)
6693 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6694 else
6695 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6696 if (s == NULL)
6697 return FAIL;
6698
6699 len = (int)STRLEN(s);
6700 sumlen += len;
6701
Bram Moolenaarcde88542015-08-11 19:14:00 +02006702 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006703 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6704 if (tofree != NULL || s != numbuf)
6705 {
6706 p->s = s;
6707 p->tofree = tofree;
6708 }
6709 else
6710 {
6711 p->s = vim_strnsave(s, len);
6712 p->tofree = p->s;
6713 }
6714
6715 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006716 if (did_echo_string_emsg) /* recursion error, bail out */
6717 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 }
6719
6720 /* Allocate result buffer with its total size, avoid re-allocation and
6721 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6722 if (join_gap->ga_len >= 2)
6723 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6724 if (ga_grow(gap, sumlen + 2) == FAIL)
6725 return FAIL;
6726
6727 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6728 {
6729 if (first)
6730 first = FALSE;
6731 else
6732 ga_concat(gap, sep);
6733 p = ((join_T *)join_gap->ga_data) + i;
6734
6735 if (p->s != NULL)
6736 ga_concat(gap, p->s);
6737 line_breakcheck();
6738 }
6739
6740 return OK;
6741}
6742
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006743/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006745 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006746 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006747 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006748 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006749list_join(
6750 garray_T *gap,
6751 list_T *l,
6752 char_u *sep,
6753 int echo_style,
6754 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006755{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006756 garray_T join_ga;
6757 int retval;
6758 join_T *p;
6759 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760
Bram Moolenaard39a7512015-04-16 22:51:22 +02006761 if (l->lv_len < 1)
6762 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006763 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6764 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6765
6766 /* Dispose each item in join_ga. */
6767 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006769 p = (join_T *)join_ga.ga_data;
6770 for (i = 0; i < join_ga.ga_len; ++i)
6771 {
6772 vim_free(p->tofree);
6773 ++p;
6774 }
6775 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777
6778 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779}
6780
6781/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006782 * Return the next (unique) copy ID.
6783 * Used for serializing nested structures.
6784 */
6785 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006786get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006787{
6788 current_copyID += COPYID_INC;
6789 return current_copyID;
6790}
6791
6792/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006793 * Garbage collection for lists and dictionaries.
6794 *
6795 * We use reference counts to be able to free most items right away when they
6796 * are no longer used. But for composite items it's possible that it becomes
6797 * unused while the reference count is > 0: When there is a recursive
6798 * reference. Example:
6799 * :let l = [1, 2, 3]
6800 * :let d = {9: l}
6801 * :let l[1] = d
6802 *
6803 * Since this is quite unusual we handle this with garbage collection: every
6804 * once in a while find out which lists and dicts are not referenced from any
6805 * variable.
6806 *
6807 * Here is a good reference text about garbage collection (refers to Python
6808 * but it applies to all reference-counting mechanisms):
6809 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006810 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811
6812/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813 * Do garbage collection for lists and dicts.
6814 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006815 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006817garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006818{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006819 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006820 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006821 buf_T *buf;
6822 win_T *wp;
6823 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006824 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006825 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006826 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006827#ifdef FEAT_WINDOWS
6828 tabpage_T *tp;
6829#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006831 /* Only do this once. */
6832 want_garbage_collect = FALSE;
6833 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006834 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006835
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006836 /* We advance by two because we add one for items referenced through
6837 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006838 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 /*
6841 * 1. Go through all accessible variables and mark all lists and dicts
6842 * with copyID.
6843 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844
6845 /* Don't free variables in the previous_funccal list unless they are only
6846 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006847 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6849 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006850 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6851 NULL);
6852 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6853 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 }
6855
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 /* script-local variables */
6857 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859
6860 /* buffer-local variables */
6861 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6863 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864
6865 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006866 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006867 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6868 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006869#ifdef FEAT_AUTOCMD
6870 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006871 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6872 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006873#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006875#ifdef FEAT_WINDOWS
6876 /* tabpage-local variables */
6877 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006878 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6879 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006880#endif
6881
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884
6885 /* function-local variables */
6886 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6887 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6889 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 }
6891
Bram Moolenaard812df62008-11-09 12:46:09 +00006892 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006894
Bram Moolenaar1dced572012-04-05 16:54:08 +02006895#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006897#endif
6898
Bram Moolenaardb913952012-06-29 12:54:53 +02006899#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006901#endif
6902
6903#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006905#endif
6906
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006907#ifdef FEAT_CHANNEL
6908 abort = abort || set_ref_in_channel(copyID);
6909#endif
6910
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006912 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 /*
6914 * 2. Free lists and dictionaries that are not referenced.
6915 */
6916 did_free = free_unref_items(copyID);
6917
6918 /*
6919 * 3. Check if any funccal can be freed now.
6920 */
6921 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006922 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 if (can_free_funccal(*pfc, copyID))
6924 {
6925 fc = *pfc;
6926 *pfc = fc->caller;
6927 free_funccal(fc, TRUE);
6928 did_free = TRUE;
6929 did_free_funccal = TRUE;
6930 }
6931 else
6932 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 if (did_free_funccal)
6935 /* When a funccal was freed some more items might be garbage
6936 * collected, so run again. */
6937 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006938 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 else if (p_verbose > 0)
6940 {
6941 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6942 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943
6944 return did_free;
6945}
6946
6947/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006948 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 */
6950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006951free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006953 dict_T *dd, *dd_next;
6954 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 int did_free = FALSE;
6956
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 */
6960 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006961 {
6962 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006965 /* Free the Dictionary and ordinary items it contains, but don't
6966 * recurse into Lists and Dictionaries, they will be in the list
6967 * of dicts or list of lists. */
6968 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006971 dd = dd_next;
6972 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973
6974 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 * Go through the list of lists and free items without the copyID.
6976 * But don't free a list that has a watcher (used in a for loop), these
6977 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 */
6979 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 {
6981 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6983 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006985 /* Free the List and ordinary items it contains, but don't recurse
6986 * into Lists and Dictionaries, they will be in the list of dicts
6987 * or list of lists. */
6988 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006991 ll = ll_next;
6992 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006993
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 return did_free;
6995}
6996
6997/*
6998 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 * "list_stack" is used to add lists to be marked. Can be NULL.
7000 *
7001 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007004set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005{
7006 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 hashtab_T *cur_ht;
7010 ht_stack_T *ht_stack = NULL;
7011 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007013 cur_ht = ht;
7014 for (;;)
7015 {
7016 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 /* Mark each item in the hashtab. If the item contains a hashtab
7019 * it is added to ht_stack, if it contains a list it is added to
7020 * list_stack. */
7021 todo = (int)cur_ht->ht_used;
7022 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7023 if (!HASHITEM_EMPTY(hi))
7024 {
7025 --todo;
7026 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7027 &ht_stack, list_stack);
7028 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030
7031 if (ht_stack == NULL)
7032 break;
7033
7034 /* take an item from the stack */
7035 cur_ht = ht_stack->ht;
7036 tempitem = ht_stack;
7037 ht_stack = ht_stack->prev;
7038 free(tempitem);
7039 }
7040
7041 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042}
7043
7044/*
7045 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7047 *
7048 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007051set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 listitem_T *li;
7054 int abort = FALSE;
7055 list_T *cur_l;
7056 list_stack_T *list_stack = NULL;
7057 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 cur_l = l;
7060 for (;;)
7061 {
7062 if (!abort)
7063 /* Mark each item in the list. If the item contains a hashtab
7064 * it is added to ht_stack, if it contains a list it is added to
7065 * list_stack. */
7066 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7067 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7068 ht_stack, &list_stack);
7069 if (list_stack == NULL)
7070 break;
7071
7072 /* take an item from the stack */
7073 cur_l = list_stack->list;
7074 tempitem = list_stack;
7075 list_stack = list_stack->prev;
7076 free(tempitem);
7077 }
7078
7079 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080}
7081
7082/*
7083 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 * "list_stack" is used to add lists to be marked. Can be NULL.
7085 * "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_item(
7091 typval_T *tv,
7092 int copyID,
7093 ht_stack_T **ht_stack,
7094 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095{
7096 dict_T *dd;
7097 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007099
Bram Moolenaara03f2332016-02-06 18:09:59 +01007100 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007101 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007102 dd = tv->vval.v_dict;
7103 if (dd != NULL && dd->dv_copyID != copyID)
7104 {
7105 /* Didn't see this dict yet. */
7106 dd->dv_copyID = copyID;
7107 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007108 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007109 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7110 }
7111 else
7112 {
7113 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7114 if (newitem == NULL)
7115 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 else
7117 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 newitem->ht = &dd->dv_hashtab;
7119 newitem->prev = *ht_stack;
7120 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007121 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 }
7124 }
7125 else if (tv->v_type == VAR_LIST)
7126 {
7127 ll = tv->vval.v_list;
7128 if (ll != NULL && ll->lv_copyID != copyID)
7129 {
7130 /* Didn't see this list yet. */
7131 ll->lv_copyID = copyID;
7132 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007133 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007134 abort = set_ref_in_list(ll, copyID, ht_stack);
7135 }
7136 else
7137 {
7138 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007140 if (newitem == NULL)
7141 abort = TRUE;
7142 else
7143 {
7144 newitem->list = ll;
7145 newitem->prev = *list_stack;
7146 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007147 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007149 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007150 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007151 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152}
7153
7154/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155 * Allocate an empty header for a dictionary.
7156 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007157 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007164 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007165 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007166 if (first_dict != NULL)
7167 first_dict->dv_used_prev = d;
7168 d->dv_used_next = first_dict;
7169 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007170 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007171
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007173 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007174 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007175 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007177 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007178 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179}
7180
7181/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007182 * Allocate an empty dict for a return value.
7183 * Returns OK or FAIL.
7184 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007185 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007186rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007187{
7188 dict_T *d = dict_alloc();
7189
7190 if (d == NULL)
7191 return FAIL;
7192
7193 rettv->vval.v_dict = d;
7194 rettv->v_type = VAR_DICT;
7195 ++d->dv_refcount;
7196 return OK;
7197}
7198
7199
7200/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 * Unreference a Dictionary: decrement the reference count and free it when it
7202 * becomes zero.
7203 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007205dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007207 if (d != NULL && --d->dv_refcount <= 0)
7208 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209}
7210
7211/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007212 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 * Ignores the reference count.
7214 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007215 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007216dict_free(
7217 dict_T *d,
7218 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007222 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007224 /* Remove the dict from the list of dicts for garbage collection. */
7225 if (d->dv_used_prev == NULL)
7226 first_dict = d->dv_used_next;
7227 else
7228 d->dv_used_prev->dv_used_next = d->dv_used_next;
7229 if (d->dv_used_next != NULL)
7230 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7231
7232 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007233 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007234 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 if (!HASHITEM_EMPTY(hi))
7238 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007239 /* Remove the item before deleting it, just in case there is
7240 * something recursive causing trouble. */
7241 di = HI2DI(hi);
7242 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007243 if (recurse || (di->di_tv.v_type != VAR_LIST
7244 && di->di_tv.v_type != VAR_DICT))
7245 clear_tv(&di->di_tv);
7246 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 --todo;
7248 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 vim_free(d);
7252}
7253
7254/*
7255 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 * The "key" is copied to the new item.
7257 * Note that the value of the item "di_tv" still needs to be initialized!
7258 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007260 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007261dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262{
Bram Moolenaar33570922005-01-25 22:26:29 +00007263 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007265 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007267 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007269 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007270 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272}
7273
7274/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 * Make a copy of a Dictionary item.
7276 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007278dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279{
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007282 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7283 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284 if (di != NULL)
7285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007287 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288 copy_tv(&org->di_tv, &di->di_tv);
7289 }
7290 return di;
7291}
7292
7293/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294 * Remove item "item" from Dictionary "dict" and free it.
7295 */
7296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007297dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298{
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 if (HASHITEM_EMPTY(hi))
7303 EMSG2(_(e_intern2), "dictitem_remove()");
7304 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 dictitem_free(item);
7307}
7308
7309/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 * Free a dict item. Also clears the value.
7311 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007316 if (item->di_flags & DI_FLAGS_ALLOC)
7317 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318}
7319
7320/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7322 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007323 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 * Returns NULL when out of memory.
7325 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007327dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328{
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dict_T *copy;
7330 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333
7334 if (orig == NULL)
7335 return NULL;
7336
7337 copy = dict_alloc();
7338 if (copy != NULL)
7339 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007340 if (copyID != 0)
7341 {
7342 orig->dv_copyID = copyID;
7343 orig->dv_copydict = copy;
7344 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007345 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007346 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350 --todo;
7351
7352 di = dictitem_alloc(hi->hi_key);
7353 if (di == NULL)
7354 break;
7355 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356 {
7357 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7358 copyID) == FAIL)
7359 {
7360 vim_free(di);
7361 break;
7362 }
7363 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 else
7365 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7366 if (dict_add(copy, di) == FAIL)
7367 {
7368 dictitem_free(di);
7369 break;
7370 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 if (todo > 0)
7376 {
7377 dict_unref(copy);
7378 copy = NULL;
7379 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 }
7381
7382 return copy;
7383}
7384
7385/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007387 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007390dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391{
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393}
7394
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007396 * Add a number or string entry to dictionary "d".
7397 * When "str" is NULL use number "nr", otherwise use "str".
7398 * Returns FAIL when out of memory and when key already exists.
7399 */
7400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007401dict_add_nr_str(
7402 dict_T *d,
7403 char *key,
7404 long nr,
7405 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007406{
7407 dictitem_T *item;
7408
7409 item = dictitem_alloc((char_u *)key);
7410 if (item == NULL)
7411 return FAIL;
7412 item->di_tv.v_lock = 0;
7413 if (str == NULL)
7414 {
7415 item->di_tv.v_type = VAR_NUMBER;
7416 item->di_tv.vval.v_number = nr;
7417 }
7418 else
7419 {
7420 item->di_tv.v_type = VAR_STRING;
7421 item->di_tv.vval.v_string = vim_strsave(str);
7422 }
7423 if (dict_add(d, item) == FAIL)
7424 {
7425 dictitem_free(item);
7426 return FAIL;
7427 }
7428 return OK;
7429}
7430
7431/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007432 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007433 * Returns FAIL when out of memory and when key already exists.
7434 */
7435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007436dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007437{
7438 dictitem_T *item;
7439
7440 item = dictitem_alloc((char_u *)key);
7441 if (item == NULL)
7442 return FAIL;
7443 item->di_tv.v_lock = 0;
7444 item->di_tv.v_type = VAR_LIST;
7445 item->di_tv.vval.v_list = list;
7446 if (dict_add(d, item) == FAIL)
7447 {
7448 dictitem_free(item);
7449 return FAIL;
7450 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007451 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007452 return OK;
7453}
7454
7455/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456 * Get the number of items in a Dictionary.
7457 */
7458 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007459dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461 if (d == NULL)
7462 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007463 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464}
7465
7466/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 * Find item "key[len]" in Dictionary "d".
7468 * If "len" is negative use strlen(key).
7469 * Returns NULL when not found.
7470 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007471 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007472dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474#define AKEYLEN 200
7475 char_u buf[AKEYLEN];
7476 char_u *akey;
7477 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 if (len < 0)
7481 akey = key;
7482 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007483 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007484 tofree = akey = vim_strnsave(key, len);
7485 if (akey == NULL)
7486 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007487 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 else
7489 {
7490 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007491 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 akey = buf;
7493 }
7494
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 vim_free(tofree);
7497 if (HASHITEM_EMPTY(hi))
7498 return NULL;
7499 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500}
7501
7502/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007503 * Get a string item from a dictionary.
7504 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007505 * Returns NULL if the entry doesn't exist or out of memory.
7506 */
7507 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007508get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007509{
7510 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007511 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007512
7513 di = dict_find(d, key, -1);
7514 if (di == NULL)
7515 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007516 s = get_tv_string(&di->di_tv);
7517 if (save && s != NULL)
7518 s = vim_strsave(s);
7519 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007520}
7521
7522/*
7523 * Get a number item from a dictionary.
7524 * Returns 0 if the entry doesn't exist or out of memory.
7525 */
7526 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007527get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007528{
7529 dictitem_T *di;
7530
7531 di = dict_find(d, key, -1);
7532 if (di == NULL)
7533 return 0;
7534 return get_tv_number(&di->di_tv);
7535}
7536
7537/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 * Return an allocated string with the string representation of a Dictionary.
7539 * May return NULL.
7540 */
7541 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543{
7544 garray_T ga;
7545 int first = TRUE;
7546 char_u *tofree;
7547 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 return NULL;
7555 ga_init2(&ga, (int)sizeof(char), 80);
7556 ga_append(&ga, '{');
7557
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007558 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007559 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 --todo;
7564
7565 if (first)
7566 first = FALSE;
7567 else
7568 ga_concat(&ga, (char_u *)", ");
7569
7570 tofree = string_quote(hi->hi_key, FALSE);
7571 if (tofree != NULL)
7572 {
7573 ga_concat(&ga, tofree);
7574 vim_free(tofree);
7575 }
7576 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007577 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 if (s != NULL)
7579 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007581 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007582 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007583 line_breakcheck();
7584
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007587 if (todo > 0)
7588 {
7589 vim_free(ga.ga_data);
7590 return NULL;
7591 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592
7593 ga_append(&ga, '}');
7594 ga_append(&ga, NUL);
7595 return (char_u *)ga.ga_data;
7596}
7597
7598/*
7599 * Allocate a variable for a Dictionary and fill it from "*arg".
7600 * Return OK or FAIL. Returns NOTDONE for {expr}.
7601 */
7602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007603get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604{
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 dict_T *d = NULL;
7606 typval_T tvkey;
7607 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007608 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007611 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612
7613 /*
7614 * First check if it's not a curly-braces thing: {expr}.
7615 * Must do this without evaluating, otherwise a function may be called
7616 * twice. Unfortunately this means we need to call eval1() twice for the
7617 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 if (*start != '}')
7621 {
7622 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7623 return FAIL;
7624 if (*start == '}')
7625 return NOTDONE;
7626 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627
7628 if (evaluate)
7629 {
7630 d = dict_alloc();
7631 if (d == NULL)
7632 return FAIL;
7633 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 tvkey.v_type = VAR_UNKNOWN;
7635 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636
7637 *arg = skipwhite(*arg + 1);
7638 while (**arg != '}' && **arg != NUL)
7639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 goto failret;
7642 if (**arg != ':')
7643 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007644 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 goto failret;
7647 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007648 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007650 key = get_tv_string_buf_chk(&tvkey, buf);
7651 if (key == NULL || *key == NUL)
7652 {
7653 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7654 if (key != NULL)
7655 EMSG(_(e_emptykey));
7656 clear_tv(&tvkey);
7657 goto failret;
7658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660
7661 *arg = skipwhite(*arg + 1);
7662 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7663 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007664 if (evaluate)
7665 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 goto failret;
7667 }
7668 if (evaluate)
7669 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007670 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 if (item != NULL)
7672 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007673 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007674 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 clear_tv(&tv);
7676 goto failret;
7677 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007678 item = dictitem_alloc(key);
7679 clear_tv(&tvkey);
7680 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007683 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 if (dict_add(d, item) == FAIL)
7685 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 }
7687 }
7688
7689 if (**arg == '}')
7690 break;
7691 if (**arg != ',')
7692 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007693 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 goto failret;
7695 }
7696 *arg = skipwhite(*arg + 1);
7697 }
7698
7699 if (**arg != '}')
7700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007701 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702failret:
7703 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007704 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 return FAIL;
7706 }
7707
7708 *arg = skipwhite(*arg + 1);
7709 if (evaluate)
7710 {
7711 rettv->v_type = VAR_DICT;
7712 rettv->vval.v_dict = d;
7713 ++d->dv_refcount;
7714 }
7715
7716 return OK;
7717}
7718
Bram Moolenaar835dc632016-02-07 14:27:38 +01007719#ifdef FEAT_JOB
7720 static void
7721job_free(job_T *job)
7722{
7723 /* TODO: free any handles */
7724
7725 vim_free(job);
7726}
7727
7728 static void
7729job_unref(job_T *job)
7730{
7731 if (job != NULL && --job->jv_refcount <= 0)
7732 job_free(job);
7733}
7734
7735/*
7736 * Allocate a job. Sets the refcount to one.
7737 */
7738 static job_T *
7739job_alloc(void)
7740{
7741 job_T *job;
7742
7743 job = (job_T *)alloc_clear(sizeof(job_T));
7744 if (job != NULL)
7745 {
7746 job->jv_refcount = 1;
7747 }
7748 return job;
7749}
7750
7751#endif
7752
Bram Moolenaar17a13432016-01-24 14:22:10 +01007753 static char *
7754get_var_special_name(int nr)
7755{
7756 switch (nr)
7757 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007758 case VVAL_FALSE: return "v:false";
7759 case VVAL_TRUE: return "v:true";
7760 case VVAL_NONE: return "v:none";
7761 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007762 }
7763 EMSG2(_(e_intern2), "get_var_special_name()");
7764 return "42";
7765}
7766
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 * Return a string with the string representation of a variable.
7769 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007770 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007772 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007773 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774 */
7775 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007776echo_string(
7777 typval_T *tv,
7778 char_u **tofree,
7779 char_u *numbuf,
7780 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007782 static int recurse = 0;
7783 char_u *r = NULL;
7784
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007787 if (!did_echo_string_emsg)
7788 {
7789 /* Only give this message once for a recursive call to avoid
7790 * flooding the user with errors. And stop iterating over lists
7791 * and dicts. */
7792 did_echo_string_emsg = TRUE;
7793 EMSG(_("E724: variable nested too deep for displaying"));
7794 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007796 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007797 }
7798 ++recurse;
7799
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 switch (tv->v_type)
7801 {
7802 case VAR_FUNC:
7803 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007804 r = tv->vval.v_string;
7805 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007806
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007808 if (tv->vval.v_list == NULL)
7809 {
7810 *tofree = NULL;
7811 r = NULL;
7812 }
7813 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7814 {
7815 *tofree = NULL;
7816 r = (char_u *)"[...]";
7817 }
7818 else
7819 {
7820 tv->vval.v_list->lv_copyID = copyID;
7821 *tofree = list2string(tv, copyID);
7822 r = *tofree;
7823 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007825
Bram Moolenaar8c711452005-01-14 21:53:12 +00007826 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007827 if (tv->vval.v_dict == NULL)
7828 {
7829 *tofree = NULL;
7830 r = NULL;
7831 }
7832 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7833 {
7834 *tofree = NULL;
7835 r = (char_u *)"{...}";
7836 }
7837 else
7838 {
7839 tv->vval.v_dict->dv_copyID = copyID;
7840 *tofree = dict2string(tv, copyID);
7841 r = *tofree;
7842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007844
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 case VAR_STRING:
7846 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007847 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007848 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 *tofree = NULL;
7850 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007853 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007854#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007855 *tofree = NULL;
7856 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7857 r = numbuf;
7858 break;
7859#endif
7860
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007861 case VAR_SPECIAL:
7862 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007863 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007864 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007865 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866
Bram Moolenaar8502c702014-06-17 12:51:16 +02007867 if (--recurse == 0)
7868 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007870}
7871
7872/*
7873 * Return a string with the string representation of a variable.
7874 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7875 * "numbuf" is used for a number.
7876 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007877 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 */
7879 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880tv2string(
7881 typval_T *tv,
7882 char_u **tofree,
7883 char_u *numbuf,
7884 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885{
7886 switch (tv->v_type)
7887 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 case VAR_FUNC:
7889 *tofree = string_quote(tv->vval.v_string, TRUE);
7890 return *tofree;
7891 case VAR_STRING:
7892 *tofree = string_quote(tv->vval.v_string, FALSE);
7893 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894#ifdef FEAT_FLOAT
7895 case VAR_FLOAT:
7896 *tofree = NULL;
7897 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7898 return numbuf;
7899#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007901 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007902 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007903 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007904 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007905 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007907 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007908 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007909}
7910
7911/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 * Return string "str" in ' quotes, doubling ' characters.
7913 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007914 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 */
7916 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007917string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918{
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007920 char_u *p, *r, *s;
7921
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 len = (function ? 13 : 3);
7923 if (str != NULL)
7924 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007925 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 for (p = str; *p != NUL; mb_ptr_adv(p))
7927 if (*p == '\'')
7928 ++len;
7929 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 s = r = alloc(len);
7931 if (r != NULL)
7932 {
7933 if (function)
7934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007936 r += 10;
7937 }
7938 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007939 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 if (str != NULL)
7941 for (p = str; *p != NUL; )
7942 {
7943 if (*p == '\'')
7944 *r++ = '\'';
7945 MB_COPY_CHAR(p, r);
7946 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007948 if (function)
7949 *r++ = ')';
7950 *r++ = NUL;
7951 }
7952 return s;
7953}
7954
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007955#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007956/*
7957 * Convert the string "text" to a floating point number.
7958 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7959 * this always uses a decimal point.
7960 * Returns the length of the text that was consumed.
7961 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007963string2float(
7964 char_u *text,
7965 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966{
7967 char *s = (char *)text;
7968 float_T f;
7969
7970 f = strtod(s, &s);
7971 *value = f;
7972 return (int)((char_u *)s - text);
7973}
7974#endif
7975
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 * Get the value of an environment variable.
7978 * "arg" is pointing to the '$'. It is advanced to after the name.
7979 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007980 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 */
7982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007983get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985 char_u *string = NULL;
7986 int len;
7987 int cc;
7988 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007989 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990
7991 ++*arg;
7992 name = *arg;
7993 len = get_env_len(arg);
7994 if (evaluate)
7995 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007996 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007997 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007998
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007999 cc = name[len];
8000 name[len] = NUL;
8001 /* first try vim_getenv(), fast for normal environment vars */
8002 string = vim_getenv(name, &mustfree);
8003 if (string != NULL && *string != NUL)
8004 {
8005 if (!mustfree)
8006 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008008 else
8009 {
8010 if (mustfree)
8011 vim_free(string);
8012
8013 /* next try expanding things like $VIM and ${HOME} */
8014 string = expand_env_save(name - 1);
8015 if (string != NULL && *string == '$')
8016 {
8017 vim_free(string);
8018 string = NULL;
8019 }
8020 }
8021 name[len] = cc;
8022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 rettv->v_type = VAR_STRING;
8024 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026
8027 return OK;
8028}
8029
8030/*
8031 * Array with names and number of arguments of all internal functions
8032 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8033 */
8034static struct fst
8035{
8036 char *f_name; /* function name */
8037 char f_min_argc; /* minimal number of arguments */
8038 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008039 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008040 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041} functions[] =
8042{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008043#ifdef FEAT_FLOAT
8044 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008045 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008047 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008048 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008049 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"append", 2, 2, f_append},
8051 {"argc", 0, 0, f_argc},
8052 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008053 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008054 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008055#ifdef FEAT_FLOAT
8056 {"asin", 1, 1, f_asin}, /* WJMc */
8057#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008058 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008059 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008060 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008061 {"assert_false", 1, 2, f_assert_false},
8062 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063#ifdef FEAT_FLOAT
8064 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008065 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008068 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"bufexists", 1, 1, f_bufexists},
8070 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8071 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8072 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8073 {"buflisted", 1, 1, f_buflisted},
8074 {"bufloaded", 1, 1, f_bufloaded},
8075 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008076 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"bufwinnr", 1, 1, f_bufwinnr},
8078 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008079 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008080 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008081 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"ceil", 1, 1, f_ceil},
8084#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008085#ifdef FEAT_CHANNEL
8086 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008087 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008088 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8089 {"ch_sendraw", 2, 3, f_ch_sendraw},
8090#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008091 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008092 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008094 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008096#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008097 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008098 {"complete_add", 1, 1, f_complete_add},
8099 {"complete_check", 0, 0, f_complete_check},
8100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008107 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008109 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008110 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008111 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008113 {"diff_filler", 1, 1, f_diff_filler},
8114 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008115 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008116 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008118 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"eventhandler", 0, 0, f_eventhandler},
8120 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008121 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008123#ifdef FEAT_FLOAT
8124 {"exp", 1, 1, f_exp},
8125#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008126 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008127 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008128 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8130 {"filereadable", 1, 1, f_filereadable},
8131 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008132 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008133 {"finddir", 1, 3, f_finddir},
8134 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008135#ifdef FEAT_FLOAT
8136 {"float2nr", 1, 1, f_float2nr},
8137 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008139#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008140 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"fnamemodify", 2, 2, f_fnamemodify},
8142 {"foldclosed", 1, 1, f_foldclosed},
8143 {"foldclosedend", 1, 1, f_foldclosedend},
8144 {"foldlevel", 1, 1, f_foldlevel},
8145 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008146 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008149 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008150 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008151 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008152 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"getchar", 0, 1, f_getchar},
8154 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008155 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"getcmdline", 0, 0, f_getcmdline},
8157 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008158 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008159 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008160 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008161 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008162 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008163 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"getfsize", 1, 1, f_getfsize},
8165 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008166 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008167 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008168 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008169 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008170 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008171 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008172 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008173 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008175 {"gettabvar", 2, 3, f_gettabvar},
8176 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"getwinposx", 0, 0, f_getwinposx},
8178 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008179 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008180 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008181 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008182 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008185 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008186 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8188 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8189 {"histadd", 2, 2, f_histadd},
8190 {"histdel", 1, 2, f_histdel},
8191 {"histget", 1, 2, f_histget},
8192 {"histnr", 1, 1, f_histnr},
8193 {"hlID", 1, 1, f_hlID},
8194 {"hlexists", 1, 1, f_hlexists},
8195 {"hostname", 0, 0, f_hostname},
8196 {"iconv", 3, 3, f_iconv},
8197 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008199 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008201 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"inputrestore", 0, 0, f_inputrestore},
8203 {"inputsave", 0, 0, f_inputsave},
8204 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008205 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008206 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008208 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008209 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008210#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +01008211 {"job_start", 1, 2, f_job_start},
8212 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008213 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008214#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008215 {"join", 1, 2, f_join},
Bram Moolenaar595e64e2016-02-07 19:19:53 +01008216 {"jsdecode", 1, 1, f_jsdecode},
8217 {"jsencode", 1, 1, f_jsencode},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008218 {"jsondecode", 1, 1, f_jsondecode},
8219 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008220 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008222 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"libcall", 3, 3, f_libcall},
8224 {"libcallnr", 3, 3, f_libcallnr},
8225 {"line", 1, 1, f_line},
8226 {"line2byte", 1, 1, f_line2byte},
8227 {"lispindent", 1, 1, f_lispindent},
8228 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008230 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008231 {"log10", 1, 1, f_log10},
8232#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008233#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008234 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008235#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008236 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008237 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008238 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008239 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008240 {"matchadd", 2, 5, f_matchadd},
8241 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008242 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008243 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008244 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008245 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008246 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008247 {"max", 1, 1, f_max},
8248 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008249#ifdef vim_mkdir
8250 {"mkdir", 1, 3, f_mkdir},
8251#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008252 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008253#ifdef FEAT_MZSCHEME
8254 {"mzeval", 1, 1, f_mzeval},
8255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008257 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008258 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008259 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008260#ifdef FEAT_PERL
8261 {"perleval", 1, 1, f_perleval},
8262#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008263#ifdef FEAT_FLOAT
8264 {"pow", 2, 2, f_pow},
8265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008267 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008268 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008269#ifdef FEAT_PYTHON3
8270 {"py3eval", 1, 1, f_py3eval},
8271#endif
8272#ifdef FEAT_PYTHON
8273 {"pyeval", 1, 1, f_pyeval},
8274#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008275 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008276 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008277 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008278 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008279 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"remote_expr", 2, 3, f_remote_expr},
8281 {"remote_foreground", 1, 1, f_remote_foreground},
8282 {"remote_peek", 1, 2, f_remote_peek},
8283 {"remote_read", 1, 1, f_remote_read},
8284 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008285 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008287 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008289 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008290#ifdef FEAT_FLOAT
8291 {"round", 1, 1, f_round},
8292#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008293 {"screenattr", 2, 2, f_screenattr},
8294 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008295 {"screencol", 0, 0, f_screencol},
8296 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008297 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008298 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008299 {"searchpair", 3, 7, f_searchpair},
8300 {"searchpairpos", 3, 7, f_searchpairpos},
8301 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"server2client", 2, 2, f_server2client},
8303 {"serverlist", 0, 0, f_serverlist},
8304 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008305 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"setcmdpos", 1, 1, f_setcmdpos},
8307 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008308 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008309 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008310 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008311 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008313 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008314 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008316#ifdef FEAT_CRYPT
8317 {"sha256", 1, 1, f_sha256},
8318#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008319 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008320 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008322#ifdef FEAT_FLOAT
8323 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008324 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008325#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008326 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008327 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008328 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008329 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008330 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008331#ifdef FEAT_FLOAT
8332 {"sqrt", 1, 1, f_sqrt},
8333 {"str2float", 1, 1, f_str2float},
8334#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008335 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008336 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008337 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338#ifdef HAVE_STRFTIME
8339 {"strftime", 1, 2, f_strftime},
8340#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008342 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"strlen", 1, 1, f_strlen},
8344 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008345 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008347 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008348 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"substitute", 4, 4, f_substitute},
8350 {"synID", 3, 3, f_synID},
8351 {"synIDattr", 2, 3, f_synIDattr},
8352 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008353 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008354 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008355 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008356 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008357 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008358 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008359 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008360 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008361 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008362#ifdef FEAT_FLOAT
8363 {"tan", 1, 1, f_tan},
8364 {"tanh", 1, 1, f_tanh},
8365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008367 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"tolower", 1, 1, f_tolower},
8369 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008370 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371#ifdef FEAT_FLOAT
8372 {"trunc", 1, 1, f_trunc},
8373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008375 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008376 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008377 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008378 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"virtcol", 1, 1, f_virtcol},
8380 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008381 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"winbufnr", 1, 1, f_winbufnr},
8383 {"wincol", 0, 0, f_wincol},
8384 {"winheight", 1, 1, f_winheight},
8385 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008386 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008388 {"winrestview", 1, 1, f_winrestview},
8389 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008391 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008392 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008393 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394};
8395
8396#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8397
8398/*
8399 * Function given to ExpandGeneric() to obtain the list of internal
8400 * or user defined function names.
8401 */
8402 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008403get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 static int intidx = -1;
8406 char_u *name;
8407
8408 if (idx == 0)
8409 intidx = -1;
8410 if (intidx < 0)
8411 {
8412 name = get_user_func_name(xp, idx);
8413 if (name != NULL)
8414 return name;
8415 }
8416 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8417 {
8418 STRCPY(IObuff, functions[intidx].f_name);
8419 STRCAT(IObuff, "(");
8420 if (functions[intidx].f_max_argc == 0)
8421 STRCAT(IObuff, ")");
8422 return IObuff;
8423 }
8424
8425 return NULL;
8426}
8427
8428/*
8429 * Function given to ExpandGeneric() to obtain the list of internal or
8430 * user defined variable or function names.
8431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008433get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434{
8435 static int intidx = -1;
8436 char_u *name;
8437
8438 if (idx == 0)
8439 intidx = -1;
8440 if (intidx < 0)
8441 {
8442 name = get_function_name(xp, idx);
8443 if (name != NULL)
8444 return name;
8445 }
8446 return get_user_var_name(xp, ++intidx);
8447}
8448
8449#endif /* FEAT_CMDL_COMPL */
8450
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008451#if defined(EBCDIC) || defined(PROTO)
8452/*
8453 * Compare struct fst by function name.
8454 */
8455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008456compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008457{
8458 struct fst *p1 = (struct fst *)s1;
8459 struct fst *p2 = (struct fst *)s2;
8460
8461 return STRCMP(p1->f_name, p2->f_name);
8462}
8463
8464/*
8465 * Sort the function table by function name.
8466 * The sorting of the table above is ASCII dependant.
8467 * On machines using EBCDIC we have to sort it.
8468 */
8469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008470sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008471{
8472 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8473
8474 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8475}
8476#endif
8477
8478
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479/*
8480 * Find internal function in table above.
8481 * Return index, or -1 if not found
8482 */
8483 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008484find_internal_func(
8485 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 int first = 0;
8488 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8489 int cmp;
8490 int x;
8491
8492 /*
8493 * Find the function name in the table. Binary search.
8494 */
8495 while (first <= last)
8496 {
8497 x = first + ((unsigned)(last - first) >> 1);
8498 cmp = STRCMP(name, functions[x].f_name);
8499 if (cmp < 0)
8500 last = x - 1;
8501 else if (cmp > 0)
8502 first = x + 1;
8503 else
8504 return x;
8505 }
8506 return -1;
8507}
8508
8509/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008510 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8511 * name it contains, otherwise return "name".
8512 */
8513 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008514deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008515{
Bram Moolenaar33570922005-01-25 22:26:29 +00008516 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008517 int cc;
8518
8519 cc = name[*lenp];
8520 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008521 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008525 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 {
8527 *lenp = 0;
8528 return (char_u *)""; /* just in case */
8529 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008530 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008532 }
8533
8534 return name;
8535}
8536
8537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 * Allocate a variable for the result of a function.
8539 * Return OK or FAIL.
8540 */
8541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008542get_func_tv(
8543 char_u *name, /* name of the function */
8544 int len, /* length of "name" */
8545 typval_T *rettv,
8546 char_u **arg, /* argument, pointing to the '(' */
8547 linenr_T firstline, /* first line of range */
8548 linenr_T lastline, /* last line of range */
8549 int *doesrange, /* return: function handled range */
8550 int evaluate,
8551 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 char_u *argp;
8554 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008555 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 int argcount = 0; /* number of arguments found */
8557
8558 /*
8559 * Get the arguments.
8560 */
8561 argp = *arg;
8562 while (argcount < MAX_FUNC_ARGS)
8563 {
8564 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8565 if (*argp == ')' || *argp == ',' || *argp == NUL)
8566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8568 {
8569 ret = FAIL;
8570 break;
8571 }
8572 ++argcount;
8573 if (*argp != ',')
8574 break;
8575 }
8576 if (*argp == ')')
8577 ++argp;
8578 else
8579 ret = FAIL;
8580
8581 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008585 {
8586 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008587 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008589 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
8592 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
8595 *arg = skipwhite(argp);
8596 return ret;
8597}
8598
8599
8600/*
8601 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008602 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008603 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008606call_func(
8607 char_u *funcname, /* name of the function */
8608 int len, /* length of "name" */
8609 typval_T *rettv, /* return value goes here */
8610 int argcount, /* number of "argvars" */
8611 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008612 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008613 linenr_T firstline, /* first line of range */
8614 linenr_T lastline, /* last line of range */
8615 int *doesrange, /* return: function handled range */
8616 int evaluate,
8617 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620#define ERROR_UNKNOWN 0
8621#define ERROR_TOOMANY 1
8622#define ERROR_TOOFEW 2
8623#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008624#define ERROR_DICT 4
8625#define ERROR_NONE 5
8626#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 int error = ERROR_NONE;
8628 int i;
8629 int llen;
8630 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631#define FLEN_FIXED 40
8632 char_u fname_buf[FLEN_FIXED + 1];
8633 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008634 char_u *name;
8635
8636 /* Make a copy of the name, if it comes from a funcref variable it could
8637 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008638 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008639 if (name == NULL)
8640 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642 /*
8643 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8644 * Change <SNR>123_name() to K_SNR 123_name().
8645 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 llen = eval_fname_script(name);
8648 if (llen > 0)
8649 {
8650 fname_buf[0] = K_SPECIAL;
8651 fname_buf[1] = KS_EXTRA;
8652 fname_buf[2] = (int)KE_SNR;
8653 i = 3;
8654 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8655 {
8656 if (current_SID <= 0)
8657 error = ERROR_SCRIPT;
8658 else
8659 {
8660 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8661 i = (int)STRLEN(fname_buf);
8662 }
8663 }
8664 if (i + STRLEN(name + llen) < FLEN_FIXED)
8665 {
8666 STRCPY(fname_buf + i, name + llen);
8667 fname = fname_buf;
8668 }
8669 else
8670 {
8671 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8672 if (fname == NULL)
8673 error = ERROR_OTHER;
8674 else
8675 {
8676 mch_memmove(fname, fname_buf, (size_t)i);
8677 STRCPY(fname + i, name + llen);
8678 }
8679 }
8680 }
8681 else
8682 fname = name;
8683
8684 *doesrange = FALSE;
8685
8686
8687 /* execute the function if no errors detected and executing */
8688 if (evaluate && error == ERROR_NONE)
8689 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008690 char_u *rfname = fname;
8691
8692 /* Ignore "g:" before a function name. */
8693 if (fname[0] == 'g' && fname[1] == ':')
8694 rfname = fname + 2;
8695
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008696 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8697 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 error = ERROR_UNKNOWN;
8699
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008700 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {
8702 /*
8703 * User defined function.
8704 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008705 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008706
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008708 /* Trigger FuncUndefined event, may load the function. */
8709 if (fp == NULL
8710 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008714 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008715 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 }
8717#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008718 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008719 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008720 {
8721 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008722 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008723 }
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 if (fp != NULL)
8726 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008727 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008731 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008733 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008734 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 else
8736 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008737 int did_save_redo = FALSE;
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 /*
8740 * Call the user function.
8741 * Save and restore search patterns, script variables and
8742 * redo buffer.
8743 */
8744 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008745#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008746 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008747#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008748 {
8749 saveRedobuff();
8750 did_save_redo = TRUE;
8751 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008752 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008755 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8756 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8757 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008758 /* Function was unreferenced while being used, free it
8759 * now. */
8760 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008761 if (did_save_redo)
8762 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 restore_search_patterns();
8764 error = ERROR_NONE;
8765 }
8766 }
8767 }
8768 else
8769 {
8770 /*
8771 * Find the function name in the table, call its implementation.
8772 */
8773 i = find_internal_func(fname);
8774 if (i >= 0)
8775 {
8776 if (argcount < functions[i].f_min_argc)
8777 error = ERROR_TOOFEW;
8778 else if (argcount > functions[i].f_max_argc)
8779 error = ERROR_TOOMANY;
8780 else
8781 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008782 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 error = ERROR_NONE;
8785 }
8786 }
8787 }
8788 /*
8789 * The function call (or "FuncUndefined" autocommand sequence) might
8790 * have been aborted by an error, an interrupt, or an explicitly thrown
8791 * exception that has not been caught so far. This situation can be
8792 * tested for by calling aborting(). For an error in an internal
8793 * function or for the "E132" error in call_user_func(), however, the
8794 * throw point at which the "force_abort" flag (temporarily reset by
8795 * emsg()) is normally updated has not been reached yet. We need to
8796 * update that flag first to make aborting() reliable.
8797 */
8798 update_force_abort();
8799 }
8800 if (error == ERROR_NONE)
8801 ret = OK;
8802
8803 /*
8804 * Report an error unless the argument evaluation or function call has been
8805 * cancelled due to an aborting error, an interrupt, or an exception.
8806 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008807 if (!aborting())
8808 {
8809 switch (error)
8810 {
8811 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008812 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 break;
8814 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008815 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008816 break;
8817 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008818 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008819 name);
8820 break;
8821 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008822 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 name);
8824 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008826 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008827 name);
8828 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008829 }
8830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 if (fname != name && fname != fname_buf)
8833 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008834 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
8836 return ret;
8837}
8838
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008839/*
8840 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008841 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008842 */
8843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008844emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008845{
8846 char_u *p;
8847
8848 if (*name == K_SPECIAL)
8849 p = concat_str((char_u *)"<SNR>", name + 3);
8850 else
8851 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008852 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008853 if (p != name)
8854 vim_free(p);
8855}
8856
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008857/*
8858 * Return TRUE for a non-zero Number and a non-empty String.
8859 */
8860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008861non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008862{
8863 return ((argvars[0].v_type == VAR_NUMBER
8864 && argvars[0].vval.v_number != 0)
8865 || (argvars[0].v_type == VAR_STRING
8866 && argvars[0].vval.v_string != NULL
8867 && *argvars[0].vval.v_string != NUL));
8868}
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870/*********************************************
8871 * Implementation of the built-in functions
8872 */
8873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008875static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008876
8877/*
8878 * Get the float value of "argvars[0]" into "f".
8879 * Returns FAIL when the argument is not a Number or Float.
8880 */
8881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008882get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008883{
8884 if (argvars[0].v_type == VAR_FLOAT)
8885 {
8886 *f = argvars[0].vval.v_float;
8887 return OK;
8888 }
8889 if (argvars[0].v_type == VAR_NUMBER)
8890 {
8891 *f = (float_T)argvars[0].vval.v_number;
8892 return OK;
8893 }
8894 EMSG(_("E808: Number or Float required"));
8895 return FAIL;
8896}
8897
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008898/*
8899 * "abs(expr)" function
8900 */
8901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008902f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008903{
8904 if (argvars[0].v_type == VAR_FLOAT)
8905 {
8906 rettv->v_type = VAR_FLOAT;
8907 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8908 }
8909 else
8910 {
8911 varnumber_T n;
8912 int error = FALSE;
8913
8914 n = get_tv_number_chk(&argvars[0], &error);
8915 if (error)
8916 rettv->vval.v_number = -1;
8917 else if (n > 0)
8918 rettv->vval.v_number = n;
8919 else
8920 rettv->vval.v_number = -n;
8921 }
8922}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008923
8924/*
8925 * "acos()" function
8926 */
8927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008928f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008929{
8930 float_T f;
8931
8932 rettv->v_type = VAR_FLOAT;
8933 if (get_float_arg(argvars, &f) == OK)
8934 rettv->vval.v_float = acos(f);
8935 else
8936 rettv->vval.v_float = 0.0;
8937}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008938#endif
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008941 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 */
8943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008944f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008949 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008951 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008952 && !tv_check_lock(l->lv_lock,
8953 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008954 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008956 }
8957 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008958 EMSG(_(e_listreq));
8959}
8960
8961/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008962 * "alloc_fail(id, countdown, repeat)" function
8963 */
8964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008965f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008966{
8967 if (argvars[0].v_type != VAR_NUMBER
8968 || argvars[0].vval.v_number <= 0
8969 || argvars[1].v_type != VAR_NUMBER
8970 || argvars[1].vval.v_number < 0
8971 || argvars[2].v_type != VAR_NUMBER)
8972 EMSG(_(e_invarg));
8973 else
8974 {
8975 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008976 if (alloc_fail_id >= aid_last)
8977 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008978 alloc_fail_countdown = argvars[1].vval.v_number;
8979 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008980 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008981 }
8982}
8983
8984/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008985 * "and(expr, expr)" function
8986 */
8987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008989{
8990 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8991 & get_tv_number_chk(&argvars[1], NULL);
8992}
8993
8994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008995 * "append(lnum, string/list)" function
8996 */
8997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008998f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008999{
9000 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 list_T *l = NULL;
9003 listitem_T *li = NULL;
9004 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009005 long added = 0;
9006
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009007 /* When coming here from Insert mode, sync undo, so that this can be
9008 * undone separately from what was previously inserted. */
9009 if (u_sync_once == 2)
9010 {
9011 u_sync_once = 1; /* notify that u_sync() was called */
9012 u_sync(TRUE);
9013 }
9014
Bram Moolenaar0d660222005-01-07 21:51:51 +00009015 lnum = get_tv_lnum(argvars);
9016 if (lnum >= 0
9017 && lnum <= curbuf->b_ml.ml_line_count
9018 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009019 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009020 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009021 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009022 l = argvars[1].vval.v_list;
9023 if (l == NULL)
9024 return;
9025 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009026 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009027 for (;;)
9028 {
9029 if (l == NULL)
9030 tv = &argvars[1]; /* append a string */
9031 else if (li == NULL)
9032 break; /* end of list */
9033 else
9034 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035 line = get_tv_string_chk(tv);
9036 if (line == NULL) /* type error */
9037 {
9038 rettv->vval.v_number = 1; /* Failed */
9039 break;
9040 }
9041 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042 ++added;
9043 if (l == NULL)
9044 break;
9045 li = li->li_next;
9046 }
9047
9048 appended_lines_mark(lnum, added);
9049 if (curwin->w_cursor.lnum > lnum)
9050 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 else
9053 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054}
9055
9056/*
9057 * "argc()" function
9058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009060f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063}
9064
9065/*
9066 * "argidx()" function
9067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009069f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009075 * "arglistid()" function
9076 */
9077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009078f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009079{
9080 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009081
9082 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009083 wp = find_tabwin(&argvars[0], &argvars[1]);
9084 if (wp != NULL)
9085 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009086}
9087
9088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 * "argv(nr)" function
9090 */
9091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009092f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
9094 int idx;
9095
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009096 if (argvars[0].v_type != VAR_UNKNOWN)
9097 {
9098 idx = get_tv_number_chk(&argvars[0], NULL);
9099 if (idx >= 0 && idx < ARGCOUNT)
9100 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9101 else
9102 rettv->vval.v_string = NULL;
9103 rettv->v_type = VAR_STRING;
9104 }
9105 else if (rettv_list_alloc(rettv) == OK)
9106 for (idx = 0; idx < ARGCOUNT; ++idx)
9107 list_append_string(rettv->vval.v_list,
9108 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009111static void prepare_assert_error(garray_T*gap);
9112static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9113static void assert_error(garray_T *gap);
9114static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009115
9116/*
9117 * Prepare "gap" for an assert error and add the sourcing position.
9118 */
9119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009120prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009121{
9122 char buf[NUMBUFLEN];
9123
9124 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009125 if (sourcing_name != NULL)
9126 {
9127 ga_concat(gap, sourcing_name);
9128 if (sourcing_lnum > 0)
9129 ga_concat(gap, (char_u *)" ");
9130 }
9131 if (sourcing_lnum > 0)
9132 {
9133 sprintf(buf, "line %ld", (long)sourcing_lnum);
9134 ga_concat(gap, (char_u *)buf);
9135 }
9136 if (sourcing_name != NULL || sourcing_lnum > 0)
9137 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009138}
9139
9140/*
9141 * Fill "gap" with information about an assert error.
9142 */
9143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009144fill_assert_error(
9145 garray_T *gap,
9146 typval_T *opt_msg_tv,
9147 char_u *exp_str,
9148 typval_T *exp_tv,
9149 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009150{
9151 char_u numbuf[NUMBUFLEN];
9152 char_u *tofree;
9153
9154 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9155 {
9156 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9157 vim_free(tofree);
9158 }
9159 else
9160 {
9161 ga_concat(gap, (char_u *)"Expected ");
9162 if (exp_str == NULL)
9163 {
9164 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9165 vim_free(tofree);
9166 }
9167 else
9168 ga_concat(gap, exp_str);
9169 ga_concat(gap, (char_u *)" but got ");
9170 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9171 vim_free(tofree);
9172 }
9173}
Bram Moolenaar43345542015-11-29 17:35:35 +01009174
9175/*
9176 * Add an assert error to v:errors.
9177 */
9178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009179assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009180{
9181 struct vimvar *vp = &vimvars[VV_ERRORS];
9182
9183 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9184 /* Make sure v:errors is a list. */
9185 set_vim_var_list(VV_ERRORS, list_alloc());
9186 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9187}
9188
Bram Moolenaar43345542015-11-29 17:35:35 +01009189/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009190 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009191 */
9192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009193f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009194{
9195 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009196
9197 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9198 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009199 prepare_assert_error(&ga);
9200 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9201 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009202 ga_clear(&ga);
9203 }
9204}
9205
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009206/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009207 * "assert_exception(string[, msg])" function
9208 */
9209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009210f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009211{
9212 garray_T ga;
9213 char *error;
9214
9215 error = (char *)get_tv_string_chk(&argvars[0]);
9216 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9217 {
9218 prepare_assert_error(&ga);
9219 ga_concat(&ga, (char_u *)"v:exception is not set");
9220 assert_error(&ga);
9221 ga_clear(&ga);
9222 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009223 else if (error != NULL
9224 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009225 {
9226 prepare_assert_error(&ga);
9227 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9228 &vimvars[VV_EXCEPTION].vv_tv);
9229 assert_error(&ga);
9230 ga_clear(&ga);
9231 }
9232}
9233
9234/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009235 * "assert_fails(cmd [, error])" function
9236 */
9237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009238f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009239{
9240 char_u *cmd = get_tv_string_chk(&argvars[0]);
9241 garray_T ga;
9242
9243 called_emsg = FALSE;
9244 suppress_errthrow = TRUE;
9245 emsg_silent = TRUE;
9246 do_cmdline_cmd(cmd);
9247 if (!called_emsg)
9248 {
9249 prepare_assert_error(&ga);
9250 ga_concat(&ga, (char_u *)"command did not fail: ");
9251 ga_concat(&ga, cmd);
9252 assert_error(&ga);
9253 ga_clear(&ga);
9254 }
9255 else if (argvars[1].v_type != VAR_UNKNOWN)
9256 {
9257 char_u buf[NUMBUFLEN];
9258 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9259
9260 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9261 {
9262 prepare_assert_error(&ga);
9263 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9264 &vimvars[VV_ERRMSG].vv_tv);
9265 assert_error(&ga);
9266 ga_clear(&ga);
9267 }
9268 }
9269
9270 called_emsg = FALSE;
9271 suppress_errthrow = FALSE;
9272 emsg_silent = FALSE;
9273 emsg_on_display = FALSE;
9274 set_vim_var_string(VV_ERRMSG, NULL, 0);
9275}
9276
9277/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009278 * Common for assert_true() and assert_false().
9279 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009281assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009282{
9283 int error = FALSE;
9284 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009285
Bram Moolenaar37127922016-02-06 20:29:28 +01009286 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009287 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009288 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009289 if (argvars[0].v_type != VAR_NUMBER
9290 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9291 || error)
9292 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009293 prepare_assert_error(&ga);
9294 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009295 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009296 NULL, &argvars[0]);
9297 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009298 ga_clear(&ga);
9299 }
9300}
9301
9302/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009303 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009304 */
9305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009306f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009307{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009308 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009309}
9310
9311/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009312 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009313 */
9314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009315f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009316{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009317 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009318}
9319
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009320#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009321/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009322 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009323 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009325f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009326{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009327 float_T f;
9328
9329 rettv->v_type = VAR_FLOAT;
9330 if (get_float_arg(argvars, &f) == OK)
9331 rettv->vval.v_float = asin(f);
9332 else
9333 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009334}
9335
9336/*
9337 * "atan()" function
9338 */
9339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009340f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009341{
9342 float_T f;
9343
9344 rettv->v_type = VAR_FLOAT;
9345 if (get_float_arg(argvars, &f) == OK)
9346 rettv->vval.v_float = atan(f);
9347 else
9348 rettv->vval.v_float = 0.0;
9349}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009350
9351/*
9352 * "atan2()" function
9353 */
9354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009356{
9357 float_T fx, fy;
9358
9359 rettv->v_type = VAR_FLOAT;
9360 if (get_float_arg(argvars, &fx) == OK
9361 && get_float_arg(&argvars[1], &fy) == OK)
9362 rettv->vval.v_float = atan2(fx, fy);
9363 else
9364 rettv->vval.v_float = 0.0;
9365}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009366#endif
9367
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368/*
9369 * "browse(save, title, initdir, default)" function
9370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009372f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373{
9374#ifdef FEAT_BROWSE
9375 int save;
9376 char_u *title;
9377 char_u *initdir;
9378 char_u *defname;
9379 char_u buf[NUMBUFLEN];
9380 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009383 save = get_tv_number_chk(&argvars[0], &error);
9384 title = get_tv_string_chk(&argvars[1]);
9385 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9386 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 if (error || title == NULL || initdir == NULL || defname == NULL)
9389 rettv->vval.v_string = NULL;
9390 else
9391 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009392 do_browse(save ? BROWSE_SAVE : 0,
9393 title, defname, NULL, initdir, NULL, curbuf);
9394#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009396#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009398}
9399
9400/*
9401 * "browsedir(title, initdir)" function
9402 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009404f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009405{
9406#ifdef FEAT_BROWSE
9407 char_u *title;
9408 char_u *initdir;
9409 char_u buf[NUMBUFLEN];
9410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 title = get_tv_string_chk(&argvars[0]);
9412 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 if (title == NULL || initdir == NULL)
9415 rettv->vval.v_string = NULL;
9416 else
9417 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009418 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423}
9424
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009425static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009426
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427/*
9428 * Find a buffer by number or exact name.
9429 */
9430 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009431find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009435 if (avar->v_type == VAR_NUMBER)
9436 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009437 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009439 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009440 if (buf == NULL)
9441 {
9442 /* No full path name match, try a match with a URL or a "nofile"
9443 * buffer, these don't use the full path. */
9444 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9445 if (buf->b_fname != NULL
9446 && (path_with_url(buf->b_fname)
9447#ifdef FEAT_QUICKFIX
9448 || bt_nofile(buf)
9449#endif
9450 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009451 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009452 break;
9453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 }
9455 return buf;
9456}
9457
9458/*
9459 * "bufexists(expr)" function
9460 */
9461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009462f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465}
9466
9467/*
9468 * "buflisted(expr)" function
9469 */
9470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009471f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 buf_T *buf;
9474
9475 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477}
9478
9479/*
9480 * "bufloaded(expr)" function
9481 */
9482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009483f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 buf_T *buf;
9486
9487 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489}
9490
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009491static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009492
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493/*
9494 * Get buffer by number or pattern.
9495 */
9496 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009497get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 int save_magic;
9501 char_u *save_cpo;
9502 buf_T *buf;
9503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 if (tv->v_type == VAR_NUMBER)
9505 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009506 if (tv->v_type != VAR_STRING)
9507 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 if (name == NULL || *name == NUL)
9509 return curbuf;
9510 if (name[0] == '$' && name[1] == NUL)
9511 return lastbuf;
9512
9513 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9514 save_magic = p_magic;
9515 p_magic = TRUE;
9516 save_cpo = p_cpo;
9517 p_cpo = (char_u *)"";
9518
9519 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009520 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
9522 p_magic = save_magic;
9523 p_cpo = save_cpo;
9524
9525 /* If not found, try expanding the name, like done for bufexists(). */
9526 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
9529 return buf;
9530}
9531
9532/*
9533 * "bufname(expr)" function
9534 */
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538 buf_T *buf;
9539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009542 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 --emsg_off;
9549}
9550
9551/*
9552 * "bufnr(expr)" function
9553 */
9554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009555f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
9557 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009558 int error = FALSE;
9559 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009563 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009564 --emsg_off;
9565
9566 /* If the buffer isn't found and the second argument is not zero create a
9567 * new buffer. */
9568 if (buf == NULL
9569 && argvars[1].v_type != VAR_UNKNOWN
9570 && get_tv_number_chk(&argvars[1], &error) != 0
9571 && !error
9572 && (name = get_tv_string_chk(&argvars[0])) != NULL
9573 && !error)
9574 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9575
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
9581
9582/*
9583 * "bufwinnr(nr)" function
9584 */
9585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588#ifdef FEAT_WINDOWS
9589 win_T *wp;
9590 int winnr = 0;
9591#endif
9592 buf_T *buf;
9593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009596 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597#ifdef FEAT_WINDOWS
9598 for (wp = firstwin; wp; wp = wp->w_next)
9599 {
9600 ++winnr;
9601 if (wp->w_buffer == buf)
9602 break;
9603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607#endif
9608 --emsg_off;
9609}
9610
9611/*
9612 * "byte2line(byte)" function
9613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009615f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616{
9617#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619#else
9620 long boff = 0;
9621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 (linenr_T)0, &boff);
9628#endif
9629}
9630
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009633{
9634#ifdef FEAT_MBYTE
9635 char_u *t;
9636#endif
9637 char_u *str;
9638 long idx;
9639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 str = get_tv_string_chk(&argvars[0]);
9641 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009644 return;
9645
9646#ifdef FEAT_MBYTE
9647 t = str;
9648 for ( ; idx > 0; idx--)
9649 {
9650 if (*t == NUL) /* EOL reached */
9651 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009652 if (enc_utf8 && comp)
9653 t += utf_ptr2len(t);
9654 else
9655 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009656 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009657 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009658#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009659 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009661#endif
9662}
9663
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009664/*
9665 * "byteidx()" function
9666 */
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009669{
9670 byteidx(argvars, rettv, FALSE);
9671}
9672
9673/*
9674 * "byteidxcomp()" function
9675 */
9676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009677f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009678{
9679 byteidx(argvars, rettv, TRUE);
9680}
9681
Bram Moolenaardb913952012-06-29 12:54:53 +02009682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009683func_call(
9684 char_u *name,
9685 typval_T *args,
9686 dict_T *selfdict,
9687 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009688{
9689 listitem_T *item;
9690 typval_T argv[MAX_FUNC_ARGS + 1];
9691 int argc = 0;
9692 int dummy;
9693 int r = 0;
9694
9695 for (item = args->vval.v_list->lv_first; item != NULL;
9696 item = item->li_next)
9697 {
9698 if (argc == MAX_FUNC_ARGS)
9699 {
9700 EMSG(_("E699: Too many arguments"));
9701 break;
9702 }
9703 /* Make a copy of each argument. This is needed to be able to set
9704 * v_lock to VAR_FIXED in the copy without changing the original list.
9705 */
9706 copy_tv(&item->li_tv, &argv[argc++]);
9707 }
9708
9709 if (item == NULL)
9710 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9711 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9712 &dummy, TRUE, selfdict);
9713
9714 /* Free the arguments. */
9715 while (argc > 0)
9716 clear_tv(&argv[--argc]);
9717
9718 return r;
9719}
9720
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009721/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009722 * "call(func, arglist)" function
9723 */
9724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009725f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009726{
9727 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009729
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009730 if (argvars[1].v_type != VAR_LIST)
9731 {
9732 EMSG(_(e_listreq));
9733 return;
9734 }
9735 if (argvars[1].vval.v_list == NULL)
9736 return;
9737
9738 if (argvars[0].v_type == VAR_FUNC)
9739 func = argvars[0].vval.v_string;
9740 else
9741 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 if (*func == NUL)
9743 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009744
Bram Moolenaare9a41262005-01-15 22:18:47 +00009745 if (argvars[2].v_type != VAR_UNKNOWN)
9746 {
9747 if (argvars[2].v_type != VAR_DICT)
9748 {
9749 EMSG(_(e_dictreq));
9750 return;
9751 }
9752 selfdict = argvars[2].vval.v_dict;
9753 }
9754
Bram Moolenaardb913952012-06-29 12:54:53 +02009755 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009756}
9757
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009758#ifdef FEAT_FLOAT
9759/*
9760 * "ceil({float})" function
9761 */
9762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009763f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009764{
9765 float_T f;
9766
9767 rettv->v_type = VAR_FLOAT;
9768 if (get_float_arg(argvars, &f) == OK)
9769 rettv->vval.v_float = ceil(f);
9770 else
9771 rettv->vval.v_float = 0.0;
9772}
9773#endif
9774
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009775#ifdef FEAT_CHANNEL
9776/*
9777 * Get the channel index from the handle argument.
9778 * Returns -1 if the handle is invalid or the channel is closed.
9779 */
9780 static int
9781get_channel_arg(typval_T *tv)
9782{
9783 int ch_idx;
9784
9785 if (tv->v_type != VAR_NUMBER)
9786 {
9787 EMSG2(_(e_invarg2), get_tv_string(tv));
9788 return -1;
9789 }
9790 ch_idx = tv->vval.v_number;
9791
9792 if (!channel_is_open(ch_idx))
9793 {
9794 EMSGN(_("E906: not an open channel"), ch_idx);
9795 return -1;
9796 }
9797 return ch_idx;
9798}
9799
9800/*
9801 * "ch_close()" function
9802 */
9803 static void
9804f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9805{
9806 int ch_idx = get_channel_arg(&argvars[0]);
9807
9808 if (ch_idx >= 0)
9809 channel_close(ch_idx);
9810}
9811
9812/*
9813 * Get a callback from "arg". It can be a Funcref or a function name.
9814 * When "arg" is zero return an empty string.
9815 * Return NULL for an invalid argument.
9816 */
9817 static char_u *
9818get_callback(typval_T *arg)
9819{
9820 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9821 return arg->vval.v_string;
9822 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9823 return (char_u *)"";
9824 EMSG(_("E999: Invalid callback argument"));
9825 return NULL;
9826}
9827
9828/*
9829 * "ch_open()" function
9830 */
9831 static void
9832f_ch_open(typval_T *argvars, typval_T *rettv)
9833{
9834 char_u *address;
9835 char_u *mode;
9836 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009837 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009838 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009839 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009840 int waittime = 0;
9841 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009842 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009843 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009844
9845 /* default: fail */
9846 rettv->vval.v_number = -1;
9847
9848 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009849 if (argvars[1].v_type != VAR_UNKNOWN
9850 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009851 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009852 EMSG(_(e_invarg));
9853 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009854 }
9855
9856 /* parse address */
9857 p = vim_strchr(address, ':');
9858 if (p == NULL)
9859 {
9860 EMSG2(_(e_invarg2), address);
9861 return;
9862 }
9863 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009864 port = strtol((char *)p, &rest, 10);
9865 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009866 {
9867 p[-1] = ':';
9868 EMSG2(_(e_invarg2), address);
9869 return;
9870 }
9871
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009872 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009873 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009874 /* parse argdict */
9875 dict_T *dict = argvars[1].vval.v_dict;
9876
9877 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9878 {
9879 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9880 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009881 ch_mode = MODE_RAW;
9882 else if (STRCMP(mode, "js") == 0)
9883 ch_mode = MODE_JS;
9884 else if (STRCMP(mode, "json") == 0)
9885 ch_mode = MODE_JSON;
9886 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009887 {
9888 EMSG2(_(e_invarg2), mode);
9889 return;
9890 }
9891 }
9892 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9893 waittime = get_dict_number(dict, (char_u *)"waittime");
9894 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9895 timeout = get_dict_number(dict, (char_u *)"timeout");
9896 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9897 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9898 }
9899 if (waittime < 0 || timeout < 0)
9900 {
9901 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009902 return;
9903 }
9904
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009905 ch_idx = channel_open((char *)address, port, waittime, NULL);
9906 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009907 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009908 channel_set_json_mode(ch_idx, ch_mode);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009909 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009910 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009911 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009912 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009913 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009914}
9915
9916/*
9917 * common for "sendexpr()" and "sendraw()"
9918 * Returns the channel index if the caller should read the response.
9919 * Otherwise returns -1.
9920 */
9921 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009922send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009923{
9924 int ch_idx;
9925 char_u *callback = NULL;
9926
9927 ch_idx = get_channel_arg(&argvars[0]);
9928 if (ch_idx < 0)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009929 {
9930 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009931 return -1;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009932 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009933
9934 if (argvars[2].v_type != VAR_UNKNOWN)
9935 {
9936 callback = get_callback(&argvars[2]);
9937 if (callback == NULL)
9938 return -1;
9939 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009940 /* Set the callback. An empty callback means no callback and not reading
9941 * the response. */
9942 if (callback != NULL && *callback != NUL)
9943 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009944
9945 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9946 return ch_idx;
9947 return -1;
9948}
9949
9950/*
9951 * "ch_sendexpr()" function
9952 */
9953 static void
9954f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9955{
9956 char_u *text;
9957 typval_T *listtv;
9958 int ch_idx;
9959 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009960 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009961
9962 /* return an empty string by default */
9963 rettv->v_type = VAR_STRING;
9964 rettv->vval.v_string = NULL;
9965
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009966 ch_idx = get_channel_arg(&argvars[0]);
9967 if (ch_idx < 0)
9968 {
9969 EMSG(_(e_invarg));
9970 return;
9971 }
9972
9973 ch_mode = channel_get_mode(ch_idx);
9974 if (ch_mode == MODE_RAW)
9975 {
9976 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
9977 return;
9978 }
9979
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009980 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009981 text = json_encode_nr_expr(id, &argvars[1],
9982 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009983 if (text == NULL)
9984 return;
9985
Bram Moolenaara07fec92016-02-05 21:04:08 +01009986 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009987 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009988 if (ch_idx >= 0)
9989 {
9990 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9991 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009992 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009993
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009994 /* Move the item from the list and then change the type to
9995 * avoid the value being freed. */
9996 *rettv = list->lv_last->li_tv;
9997 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009998 clear_tv(listtv);
9999 }
10000 }
10001}
10002
10003/*
10004 * "ch_sendraw()" function
10005 */
10006 static void
10007f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10008{
10009 char_u buf[NUMBUFLEN];
10010 char_u *text;
10011 int ch_idx;
10012
10013 /* return an empty string by default */
10014 rettv->v_type = VAR_STRING;
10015 rettv->vval.v_string = NULL;
10016
10017 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +010010018 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010019 if (ch_idx >= 0)
10020 rettv->vval.v_string = channel_read_block(ch_idx);
10021}
10022#endif
10023
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010024/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010025 * "changenr()" function
10026 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010028f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010029{
10030 rettv->vval.v_number = curbuf->b_u_seq_cur;
10031}
10032
10033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 * "char2nr(string)" function
10035 */
10036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010037f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038{
10039#ifdef FEAT_MBYTE
10040 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010041 {
10042 int utf8 = 0;
10043
10044 if (argvars[1].v_type != VAR_UNKNOWN)
10045 utf8 = get_tv_number_chk(&argvars[1], NULL);
10046
10047 if (utf8)
10048 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10049 else
10050 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 else
10053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
10056
10057/*
10058 * "cindent(lnum)" function
10059 */
10060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010061f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063#ifdef FEAT_CINDENT
10064 pos_T pos;
10065 linenr_T lnum;
10066
10067 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10070 {
10071 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 curwin->w_cursor = pos;
10074 }
10075 else
10076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010081 * "clearmatches()" function
10082 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010084f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010085{
10086#ifdef FEAT_SEARCH_EXTRA
10087 clear_matches(curwin);
10088#endif
10089}
10090
10091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 * "col(string)" function
10093 */
10094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010095f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097 colnr_T col = 0;
10098 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010099 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010101 fp = var2fpos(&argvars[0], FALSE, &fnum);
10102 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 {
10104 if (fp->col == MAXCOL)
10105 {
10106 /* '> can be MAXCOL, get the length of the line then */
10107 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010108 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 else
10110 col = MAXCOL;
10111 }
10112 else
10113 {
10114 col = fp->col + 1;
10115#ifdef FEAT_VIRTUALEDIT
10116 /* col(".") when the cursor is on the NUL at the end of the line
10117 * because of "coladd" can be seen as an extra column. */
10118 if (virtual_active() && fp == &curwin->w_cursor)
10119 {
10120 char_u *p = ml_get_cursor();
10121
10122 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10123 curwin->w_virtcol - curwin->w_cursor.coladd))
10124 {
10125# ifdef FEAT_MBYTE
10126 int l;
10127
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010128 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 col += l;
10130# else
10131 if (*p != NUL && p[1] == NUL)
10132 ++col;
10133# endif
10134 }
10135 }
10136#endif
10137 }
10138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140}
10141
Bram Moolenaar572cb562005-08-05 21:35:02 +000010142#if defined(FEAT_INS_EXPAND)
10143/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010144 * "complete()" function
10145 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010147f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010148{
10149 int startcol;
10150
10151 if ((State & INSERT) == 0)
10152 {
10153 EMSG(_("E785: complete() can only be used in Insert mode"));
10154 return;
10155 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010156
10157 /* Check for undo allowed here, because if something was already inserted
10158 * the line was already saved for undo and this check isn't done. */
10159 if (!undo_allowed())
10160 return;
10161
Bram Moolenaarade00832006-03-10 21:46:58 +000010162 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10163 {
10164 EMSG(_(e_invarg));
10165 return;
10166 }
10167
10168 startcol = get_tv_number_chk(&argvars[0], NULL);
10169 if (startcol <= 0)
10170 return;
10171
10172 set_completion(startcol - 1, argvars[1].vval.v_list);
10173}
10174
10175/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010176 * "complete_add()" function
10177 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010179f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010180{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010181 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010182}
10183
10184/*
10185 * "complete_check()" function
10186 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010188f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010189{
10190 int saved = RedrawingDisabled;
10191
10192 RedrawingDisabled = 0;
10193 ins_compl_check_keys(0);
10194 rettv->vval.v_number = compl_interrupted;
10195 RedrawingDisabled = saved;
10196}
10197#endif
10198
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199/*
10200 * "confirm(message, buttons[, default [, type]])" function
10201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010203f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204{
10205#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10206 char_u *message;
10207 char_u *buttons = NULL;
10208 char_u buf[NUMBUFLEN];
10209 char_u buf2[NUMBUFLEN];
10210 int def = 1;
10211 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 char_u *typestr;
10213 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 message = get_tv_string_chk(&argvars[0]);
10216 if (message == NULL)
10217 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10221 if (buttons == NULL)
10222 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010223 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010226 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10229 if (typestr == NULL)
10230 error = TRUE;
10231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010233 switch (TOUPPER_ASC(*typestr))
10234 {
10235 case 'E': type = VIM_ERROR; break;
10236 case 'Q': type = VIM_QUESTION; break;
10237 case 'I': type = VIM_INFO; break;
10238 case 'W': type = VIM_WARNING; break;
10239 case 'G': type = VIM_GENERIC; break;
10240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 }
10242 }
10243 }
10244 }
10245
10246 if (buttons == NULL || *buttons == NUL)
10247 buttons = (char_u *)_("&Ok");
10248
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010249 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010251 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252#endif
10253}
10254
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010255/*
10256 * "copy()" function
10257 */
10258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010259f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010260{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010261 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010262}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010264#ifdef FEAT_FLOAT
10265/*
10266 * "cos()" function
10267 */
10268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010269f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010270{
10271 float_T f;
10272
10273 rettv->v_type = VAR_FLOAT;
10274 if (get_float_arg(argvars, &f) == OK)
10275 rettv->vval.v_float = cos(f);
10276 else
10277 rettv->vval.v_float = 0.0;
10278}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010279
10280/*
10281 * "cosh()" function
10282 */
10283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010284f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010285{
10286 float_T f;
10287
10288 rettv->v_type = VAR_FLOAT;
10289 if (get_float_arg(argvars, &f) == OK)
10290 rettv->vval.v_float = cosh(f);
10291 else
10292 rettv->vval.v_float = 0.0;
10293}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010294#endif
10295
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010297 * "count()" function
10298 */
10299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010300f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010301{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010302 long n = 0;
10303 int ic = FALSE;
10304
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010306 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010307 listitem_T *li;
10308 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010310
Bram Moolenaare9a41262005-01-15 22:18:47 +000010311 if ((l = argvars[0].vval.v_list) != NULL)
10312 {
10313 li = l->lv_first;
10314 if (argvars[2].v_type != VAR_UNKNOWN)
10315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 int error = FALSE;
10317
10318 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010319 if (argvars[3].v_type != VAR_UNKNOWN)
10320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 idx = get_tv_number_chk(&argvars[3], &error);
10322 if (!error)
10323 {
10324 li = list_find(l, idx);
10325 if (li == NULL)
10326 EMSGN(_(e_listidx), idx);
10327 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 if (error)
10330 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010331 }
10332
10333 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010334 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010335 ++n;
10336 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010337 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 else if (argvars[0].v_type == VAR_DICT)
10339 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010340 int todo;
10341 dict_T *d;
10342 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010343
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010344 if ((d = argvars[0].vval.v_dict) != NULL)
10345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010346 int error = FALSE;
10347
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 if (argvars[2].v_type != VAR_UNKNOWN)
10349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010350 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351 if (argvars[3].v_type != VAR_UNKNOWN)
10352 EMSG(_(e_invarg));
10353 }
10354
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010355 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010356 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010357 {
10358 if (!HASHITEM_EMPTY(hi))
10359 {
10360 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010361 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010362 ++n;
10363 }
10364 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 }
10366 }
10367 else
10368 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010369 rettv->vval.v_number = n;
10370}
10371
10372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10374 *
10375 * Checks the existence of a cscope connection.
10376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010378f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
10380#ifdef FEAT_CSCOPE
10381 int num = 0;
10382 char_u *dbpath = NULL;
10383 char_u *prepend = NULL;
10384 char_u buf[NUMBUFLEN];
10385
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010386 if (argvars[0].v_type != VAR_UNKNOWN
10387 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010389 num = (int)get_tv_number(&argvars[0]);
10390 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010391 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010392 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
10394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396#endif
10397}
10398
10399/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010400 * "cursor(lnum, col)" function, or
10401 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010403 * Moves the cursor to the specified line and column.
10404 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010407f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010410#ifdef FEAT_VIRTUALEDIT
10411 long coladd = 0;
10412#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010413 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010415 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010416 if (argvars[1].v_type == VAR_UNKNOWN)
10417 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010418 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010419 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010420
Bram Moolenaar493c1782014-05-28 14:34:46 +020010421 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010422 {
10423 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010424 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010425 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010426 line = pos.lnum;
10427 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010428#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010429 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010430#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010431 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010432 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010433 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010434 set_curswant = FALSE;
10435 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010436 }
10437 else
10438 {
10439 line = get_tv_lnum(argvars);
10440 col = get_tv_number_chk(&argvars[1], NULL);
10441#ifdef FEAT_VIRTUALEDIT
10442 if (argvars[2].v_type != VAR_UNKNOWN)
10443 coladd = get_tv_number_chk(&argvars[2], NULL);
10444#endif
10445 }
10446 if (line < 0 || col < 0
10447#ifdef FEAT_VIRTUALEDIT
10448 || coladd < 0
10449#endif
10450 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 if (line > 0)
10453 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 if (col > 0)
10455 curwin->w_cursor.col = col - 1;
10456#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010457 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458#endif
10459
10460 /* Make sure the cursor is in a valid position. */
10461 check_cursor();
10462#ifdef FEAT_MBYTE
10463 /* Correct cursor for multi-byte character. */
10464 if (has_mbyte)
10465 mb_adjust_cursor();
10466#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010467
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010468 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010469 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470}
10471
10472/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010473 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 */
10475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010476f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010478 int noref = 0;
10479
10480 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010482 if (noref < 0 || noref > 1)
10483 EMSG(_(e_invarg));
10484 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010485 {
10486 current_copyID += COPYID_INC;
10487 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489}
10490
10491/*
10492 * "delete()" function
10493 */
10494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010495f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010497 char_u nbuf[NUMBUFLEN];
10498 char_u *name;
10499 char_u *flags;
10500
10501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010503 return;
10504
10505 name = get_tv_string(&argvars[0]);
10506 if (name == NULL || *name == NUL)
10507 {
10508 EMSG(_(e_invarg));
10509 return;
10510 }
10511
10512 if (argvars[1].v_type != VAR_UNKNOWN)
10513 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010515 flags = (char_u *)"";
10516
10517 if (*flags == NUL)
10518 /* delete a file */
10519 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10520 else if (STRCMP(flags, "d") == 0)
10521 /* delete an empty directory */
10522 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10523 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010524 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010525 rettv->vval.v_number = delete_recursive(name);
10526 else
10527 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528}
10529
10530/*
10531 * "did_filetype()" function
10532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010534f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535{
10536#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538#endif
10539}
10540
10541/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010542 * "diff_filler()" function
10543 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010545f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010546{
10547#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010549#endif
10550}
10551
10552/*
10553 * "diff_hlID()" function
10554 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010556f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010557{
10558#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010560 static linenr_T prev_lnum = 0;
10561 static int changedtick = 0;
10562 static int fnum = 0;
10563 static int change_start = 0;
10564 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010565 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010566 int filler_lines;
10567 int col;
10568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 if (lnum < 0) /* ignore type error in {lnum} arg */
10570 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010571 if (lnum != prev_lnum
10572 || changedtick != curbuf->b_changedtick
10573 || fnum != curbuf->b_fnum)
10574 {
10575 /* New line, buffer, change: need to get the values. */
10576 filler_lines = diff_check(curwin, lnum);
10577 if (filler_lines < 0)
10578 {
10579 if (filler_lines == -1)
10580 {
10581 change_start = MAXCOL;
10582 change_end = -1;
10583 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10584 hlID = HLF_ADD; /* added line */
10585 else
10586 hlID = HLF_CHD; /* changed line */
10587 }
10588 else
10589 hlID = HLF_ADD; /* added line */
10590 }
10591 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010592 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010593 prev_lnum = lnum;
10594 changedtick = curbuf->b_changedtick;
10595 fnum = curbuf->b_fnum;
10596 }
10597
10598 if (hlID == HLF_CHD || hlID == HLF_TXD)
10599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010601 if (col >= change_start && col <= change_end)
10602 hlID = HLF_TXD; /* changed text */
10603 else
10604 hlID = HLF_CHD; /* changed line */
10605 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010606 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010607#endif
10608}
10609
10610/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010611 * "disable_char_avail_for_testing({expr})" function
10612 */
10613 static void
10614f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10615{
10616 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10617}
10618
10619/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010620 * "empty({expr})" function
10621 */
10622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010623f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010624{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010625 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010626
10627 switch (argvars[0].v_type)
10628 {
10629 case VAR_STRING:
10630 case VAR_FUNC:
10631 n = argvars[0].vval.v_string == NULL
10632 || *argvars[0].vval.v_string == NUL;
10633 break;
10634 case VAR_NUMBER:
10635 n = argvars[0].vval.v_number == 0;
10636 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010637 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010638#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010639 n = argvars[0].vval.v_float == 0.0;
10640 break;
10641#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010642 case VAR_LIST:
10643 n = argvars[0].vval.v_list == NULL
10644 || argvars[0].vval.v_list->lv_first == NULL;
10645 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010646 case VAR_DICT:
10647 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010648 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010649 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010650 case VAR_SPECIAL:
10651 n = argvars[0].vval.v_number != VVAL_TRUE;
10652 break;
10653
Bram Moolenaar835dc632016-02-07 14:27:38 +010010654 case VAR_JOB:
10655#ifdef FEAT_JOB
10656 n = argvars[0].vval.v_job->jv_status != JOB_STARTED;
10657 break;
10658#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010659 case VAR_UNKNOWN:
10660 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10661 n = TRUE;
10662 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010663 }
10664
10665 rettv->vval.v_number = n;
10666}
10667
10668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 * "escape({string}, {chars})" function
10670 */
10671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010672f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
10674 char_u buf[NUMBUFLEN];
10675
Bram Moolenaar758711c2005-02-02 23:11:38 +000010676 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10677 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
10681/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010682 * "eval()" function
10683 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010685f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010686{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010687 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 s = get_tv_string_chk(&argvars[0]);
10690 if (s != NULL)
10691 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010692
Bram Moolenaar615b9972015-01-14 17:15:05 +010010693 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10695 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010696 if (p != NULL && !aborting())
10697 EMSG2(_(e_invexpr2), p);
10698 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010700 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010702 else if (*s != NUL)
10703 EMSG(_(e_trailing));
10704}
10705
10706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 * "eventhandler()" function
10708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010710f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713}
10714
10715/*
10716 * "executable()" function
10717 */
10718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010719f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010721 char_u *name = get_tv_string(&argvars[0]);
10722
10723 /* Check in $PATH and also check directly if there is a directory name. */
10724 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10725 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010726}
10727
10728/*
10729 * "exepath()" function
10730 */
10731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010732f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010733{
10734 char_u *p = NULL;
10735
Bram Moolenaarb5971142015-03-21 17:32:19 +010010736 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010737 rettv->v_type = VAR_STRING;
10738 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739}
10740
10741/*
10742 * "exists()" function
10743 */
10744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010745f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746{
10747 char_u *p;
10748 char_u *name;
10749 int n = FALSE;
10750 int len = 0;
10751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 if (*p == '$') /* environment variable */
10754 {
10755 /* first try "normal" environment variables (fast) */
10756 if (mch_getenv(p + 1) != NULL)
10757 n = TRUE;
10758 else
10759 {
10760 /* try expanding things like $VIM and ${HOME} */
10761 p = expand_env_save(p);
10762 if (p != NULL && *p != '$')
10763 n = TRUE;
10764 vim_free(p);
10765 }
10766 }
10767 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010770 if (*skipwhite(p) != NUL)
10771 n = FALSE; /* trailing garbage */
10772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 else if (*p == '*') /* internal or user defined function */
10774 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010775 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 }
10777 else if (*p == ':')
10778 {
10779 n = cmd_exists(p + 1);
10780 }
10781 else if (*p == '#')
10782 {
10783#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010784 if (p[1] == '#')
10785 n = autocmd_supported(p + 2);
10786 else
10787 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788#endif
10789 }
10790 else /* internal variable */
10791 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010792 char_u *tofree;
10793 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010795 /* get_name_len() takes care of expanding curly braces */
10796 name = p;
10797 len = get_name_len(&p, &tofree, TRUE, FALSE);
10798 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010800 if (tofree != NULL)
10801 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010802 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010803 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010805 /* handle d.key, l[idx], f(expr) */
10806 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10807 if (n)
10808 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 }
10810 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010811 if (*p != NUL)
10812 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010814 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 }
10816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818}
10819
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010820#ifdef FEAT_FLOAT
10821/*
10822 * "exp()" function
10823 */
10824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010825f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010826{
10827 float_T f;
10828
10829 rettv->v_type = VAR_FLOAT;
10830 if (get_float_arg(argvars, &f) == OK)
10831 rettv->vval.v_float = exp(f);
10832 else
10833 rettv->vval.v_float = 0.0;
10834}
10835#endif
10836
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837/*
10838 * "expand()" function
10839 */
10840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010841f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843 char_u *s;
10844 int len;
10845 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010846 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010848 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010849 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010852 if (argvars[1].v_type != VAR_UNKNOWN
10853 && argvars[2].v_type != VAR_UNKNOWN
10854 && get_tv_number_chk(&argvars[2], &error)
10855 && !error)
10856 {
10857 rettv->v_type = VAR_LIST;
10858 rettv->vval.v_list = NULL;
10859 }
10860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 if (*s == '%' || *s == '#' || *s == '<')
10863 {
10864 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010865 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010867 if (rettv->v_type == VAR_LIST)
10868 {
10869 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10870 list_append_string(rettv->vval.v_list, result, -1);
10871 else
10872 vim_free(result);
10873 }
10874 else
10875 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 }
10877 else
10878 {
10879 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010880 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 if (argvars[1].v_type != VAR_UNKNOWN
10882 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010883 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 if (!error)
10885 {
10886 ExpandInit(&xpc);
10887 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010888 if (p_wic)
10889 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010890 if (rettv->v_type == VAR_STRING)
10891 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10892 options, WILD_ALL);
10893 else if (rettv_list_alloc(rettv) != FAIL)
10894 {
10895 int i;
10896
10897 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10898 for (i = 0; i < xpc.xp_numfiles; i++)
10899 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10900 ExpandCleanup(&xpc);
10901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 }
10903 else
10904 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906}
10907
10908/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010909 * Go over all entries in "d2" and add them to "d1".
10910 * When "action" is "error" then a duplicate key is an error.
10911 * When "action" is "force" then a duplicate key is overwritten.
10912 * Otherwise duplicate keys are ignored ("action" is "keep").
10913 */
10914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010915dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010916{
10917 dictitem_T *di1;
10918 hashitem_T *hi2;
10919 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010920 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010921
10922 todo = (int)d2->dv_hashtab.ht_used;
10923 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10924 {
10925 if (!HASHITEM_EMPTY(hi2))
10926 {
10927 --todo;
10928 di1 = dict_find(d1, hi2->hi_key, -1);
10929 if (d1->dv_scope != 0)
10930 {
10931 /* Disallow replacing a builtin function in l: and g:.
10932 * Check the key to be valid when adding to any
10933 * scope. */
10934 if (d1->dv_scope == VAR_DEF_SCOPE
10935 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10936 && var_check_func_name(hi2->hi_key,
10937 di1 == NULL))
10938 break;
10939 if (!valid_varname(hi2->hi_key))
10940 break;
10941 }
10942 if (di1 == NULL)
10943 {
10944 di1 = dictitem_copy(HI2DI(hi2));
10945 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10946 dictitem_free(di1);
10947 }
10948 else if (*action == 'e')
10949 {
10950 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10951 break;
10952 }
10953 else if (*action == 'f' && HI2DI(hi2) != di1)
10954 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010955 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10956 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010957 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010958 clear_tv(&di1->di_tv);
10959 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10960 }
10961 }
10962 }
10963}
10964
10965/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010966 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010968 */
10969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010970f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010971{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010972 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010973
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010975 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 list_T *l1, *l2;
10977 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010980
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981 l1 = argvars[0].vval.v_list;
10982 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010983 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010984 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010985 {
10986 if (argvars[2].v_type != VAR_UNKNOWN)
10987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 before = get_tv_number_chk(&argvars[2], &error);
10989 if (error)
10990 return; /* type error; errmsg already given */
10991
Bram Moolenaar758711c2005-02-02 23:11:38 +000010992 if (before == l1->lv_len)
10993 item = NULL;
10994 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010996 item = list_find(l1, before);
10997 if (item == NULL)
10998 {
10999 EMSGN(_(e_listidx), before);
11000 return;
11001 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011002 }
11003 }
11004 else
11005 item = NULL;
11006 list_extend(l1, l2, item);
11007
Bram Moolenaare9a41262005-01-15 22:18:47 +000011008 copy_tv(&argvars[0], rettv);
11009 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011010 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011011 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11012 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011013 dict_T *d1, *d2;
11014 char_u *action;
11015 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016
11017 d1 = argvars[0].vval.v_dict;
11018 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011019 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011020 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011021 {
11022 /* Check the third argument. */
11023 if (argvars[2].v_type != VAR_UNKNOWN)
11024 {
11025 static char *(av[]) = {"keep", "force", "error"};
11026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 action = get_tv_string_chk(&argvars[2]);
11028 if (action == NULL)
11029 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011030 for (i = 0; i < 3; ++i)
11031 if (STRCMP(action, av[i]) == 0)
11032 break;
11033 if (i == 3)
11034 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011035 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011036 return;
11037 }
11038 }
11039 else
11040 action = (char_u *)"force";
11041
Bram Moolenaara9922d62013-05-30 13:01:18 +020011042 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044 copy_tv(&argvars[0], rettv);
11045 }
11046 }
11047 else
11048 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011049}
11050
11051/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011052 * "feedkeys()" function
11053 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011055f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011056{
11057 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011058 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011059 char_u *keys, *flags;
11060 char_u nbuf[NUMBUFLEN];
11061 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011062 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011063 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011064
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011065 /* This is not allowed in the sandbox. If the commands would still be
11066 * executed in the sandbox it would be OK, but it probably happens later,
11067 * when "sandbox" is no longer set. */
11068 if (check_secure())
11069 return;
11070
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011071 keys = get_tv_string(&argvars[0]);
11072 if (*keys != NUL)
11073 {
11074 if (argvars[1].v_type != VAR_UNKNOWN)
11075 {
11076 flags = get_tv_string_buf(&argvars[1], nbuf);
11077 for ( ; *flags != NUL; ++flags)
11078 {
11079 switch (*flags)
11080 {
11081 case 'n': remap = FALSE; break;
11082 case 'm': remap = TRUE; break;
11083 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011084 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011085 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011086 }
11087 }
11088 }
11089
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011090 /* Need to escape K_SPECIAL and CSI before putting the string in the
11091 * typeahead buffer. */
11092 keys_esc = vim_strsave_escape_csi(keys);
11093 if (keys_esc != NULL)
11094 {
11095 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011096 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011097 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011098 if (vgetc_busy)
11099 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011100 if (execute)
11101 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011102 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011103 }
11104}
11105
11106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 * "filereadable()" function
11108 */
11109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011110f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011112 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 char_u *p;
11114 int n;
11115
Bram Moolenaarc236c162008-07-13 17:41:49 +000011116#ifndef O_NONBLOCK
11117# define O_NONBLOCK 0
11118#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011120 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11121 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 {
11123 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011124 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 }
11126 else
11127 n = FALSE;
11128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130}
11131
11132/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011133 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 * rights to write into.
11135 */
11136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011137f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011139 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140}
11141
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011142 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011143findfilendir(
11144 typval_T *argvars UNUSED,
11145 typval_T *rettv,
11146 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011147{
11148#ifdef FEAT_SEARCHPATH
11149 char_u *fname;
11150 char_u *fresult = NULL;
11151 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11152 char_u *p;
11153 char_u pathbuf[NUMBUFLEN];
11154 int count = 1;
11155 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011156 int error = FALSE;
11157#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011158
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011159 rettv->vval.v_string = NULL;
11160 rettv->v_type = VAR_STRING;
11161
11162#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011164
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011165 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11168 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011169 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170 else
11171 {
11172 if (*p != NUL)
11173 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011176 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011178 }
11179
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011180 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11181 error = TRUE;
11182
11183 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 do
11186 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011187 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011188 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 fresult = find_file_in_path_option(first ? fname : NULL,
11190 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011191 0, first, path,
11192 find_what,
11193 curbuf->b_ffname,
11194 find_what == FINDFILE_DIR
11195 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011197
11198 if (fresult != NULL && rettv->v_type == VAR_LIST)
11199 list_append_string(rettv->vval.v_list, fresult, -1);
11200
11201 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011203
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011204 if (rettv->v_type == VAR_STRING)
11205 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011206#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011207}
11208
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011209static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11210static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011211
11212/*
11213 * Implementation of map() and filter().
11214 */
11215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011216filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011217{
11218 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011219 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 listitem_T *li, *nli;
11221 list_T *l = NULL;
11222 dictitem_T *di;
11223 hashtab_T *ht;
11224 hashitem_T *hi;
11225 dict_T *d = NULL;
11226 typval_T save_val;
11227 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011229 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011230 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011231 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011232 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011233 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011234 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011235
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011237 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011238 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011239 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011240 return;
11241 }
11242 else if (argvars[0].v_type == VAR_DICT)
11243 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011244 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011245 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246 return;
11247 }
11248 else
11249 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011250 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011251 return;
11252 }
11253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 expr = get_tv_string_buf_chk(&argvars[1], buf);
11255 /* On type errors, the preceding call has already displayed an error
11256 * message. Avoid a misleading error message for an empty string that
11257 * was not passed as argument. */
11258 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011260 prepare_vimvar(VV_VAL, &save_val);
11261 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011262
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011263 /* We reset "did_emsg" to be able to detect whether an error
11264 * occurred during evaluation of the expression. */
11265 save_did_emsg = did_emsg;
11266 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011267
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011268 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 vimvars[VV_KEY].vv_type = VAR_STRING;
11272
11273 ht = &d->dv_hashtab;
11274 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011275 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 if (!HASHITEM_EMPTY(hi))
11279 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011280 int r;
11281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 --todo;
11283 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011284 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011285 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11286 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 break;
11288 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011289 r = filter_map_one(&di->di_tv, expr, map, &rem);
11290 clear_tv(&vimvars[VV_KEY].vv_tv);
11291 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 break;
11293 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011294 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011295 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11296 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011297 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011299 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 }
11301 }
11302 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011303 }
11304 else
11305 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011306 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 for (li = l->lv_first; li != NULL; li = nli)
11309 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011310 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011311 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011313 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011314 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011315 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011316 break;
11317 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011319 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011320 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011321 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011322
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011323 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011325
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011326 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011327 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011328
11329 copy_tv(&argvars[0], rettv);
11330}
11331
11332 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011333filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011334{
Bram Moolenaar33570922005-01-25 22:26:29 +000011335 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011336 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011337 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011338
Bram Moolenaar33570922005-01-25 22:26:29 +000011339 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011340 s = expr;
11341 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011342 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011343 if (*s != NUL) /* check for trailing chars after expr */
11344 {
11345 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011346 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011347 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011348 }
11349 if (map)
11350 {
11351 /* map(): replace the list item value */
11352 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011353 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011354 *tv = rettv;
11355 }
11356 else
11357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358 int error = FALSE;
11359
Bram Moolenaare9a41262005-01-15 22:18:47 +000011360 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 /* On type error, nothing has been removed; return FAIL to stop the
11364 * loop. The error message was given by get_tv_number_chk(). */
11365 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011366 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011367 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011368 retval = OK;
11369theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011371 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372}
11373
11374/*
11375 * "filter()" function
11376 */
11377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011378f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011379{
11380 filter_map(argvars, rettv, FALSE);
11381}
11382
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011383/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011384 * "finddir({fname}[, {path}[, {count}]])" function
11385 */
11386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011387f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011389 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011390}
11391
11392/*
11393 * "findfile({fname}[, {path}[, {count}]])" function
11394 */
11395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011396f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011397{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011398 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011399}
11400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011401#ifdef FEAT_FLOAT
11402/*
11403 * "float2nr({float})" function
11404 */
11405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011406f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011407{
11408 float_T f;
11409
11410 if (get_float_arg(argvars, &f) == OK)
11411 {
11412 if (f < -0x7fffffff)
11413 rettv->vval.v_number = -0x7fffffff;
11414 else if (f > 0x7fffffff)
11415 rettv->vval.v_number = 0x7fffffff;
11416 else
11417 rettv->vval.v_number = (varnumber_T)f;
11418 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011419}
11420
11421/*
11422 * "floor({float})" function
11423 */
11424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011425f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011426{
11427 float_T f;
11428
11429 rettv->v_type = VAR_FLOAT;
11430 if (get_float_arg(argvars, &f) == OK)
11431 rettv->vval.v_float = floor(f);
11432 else
11433 rettv->vval.v_float = 0.0;
11434}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011435
11436/*
11437 * "fmod()" function
11438 */
11439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011440f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011441{
11442 float_T fx, fy;
11443
11444 rettv->v_type = VAR_FLOAT;
11445 if (get_float_arg(argvars, &fx) == OK
11446 && get_float_arg(&argvars[1], &fy) == OK)
11447 rettv->vval.v_float = fmod(fx, fy);
11448 else
11449 rettv->vval.v_float = 0.0;
11450}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011451#endif
11452
Bram Moolenaar0d660222005-01-07 21:51:51 +000011453/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011454 * "fnameescape({string})" function
11455 */
11456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011457f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011458{
11459 rettv->vval.v_string = vim_strsave_fnameescape(
11460 get_tv_string(&argvars[0]), FALSE);
11461 rettv->v_type = VAR_STRING;
11462}
11463
11464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 * "fnamemodify({fname}, {mods})" function
11466 */
11467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011468f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469{
11470 char_u *fname;
11471 char_u *mods;
11472 int usedlen = 0;
11473 int len;
11474 char_u *fbuf = NULL;
11475 char_u buf[NUMBUFLEN];
11476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 fname = get_tv_string_chk(&argvars[0]);
11478 mods = get_tv_string_buf_chk(&argvars[1], buf);
11479 if (fname == NULL || mods == NULL)
11480 fname = NULL;
11481 else
11482 {
11483 len = (int)STRLEN(fname);
11484 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 vim_free(fbuf);
11493}
11494
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011495static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496
11497/*
11498 * "foldclosed()" function
11499 */
11500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011501foldclosed_both(
11502 typval_T *argvars UNUSED,
11503 typval_T *rettv,
11504 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505{
11506#ifdef FEAT_FOLDING
11507 linenr_T lnum;
11508 linenr_T first, last;
11509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11512 {
11513 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11514 {
11515 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 return;
11520 }
11521 }
11522#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524}
11525
11526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527 * "foldclosed()" function
11528 */
11529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011530f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531{
11532 foldclosed_both(argvars, rettv, FALSE);
11533}
11534
11535/*
11536 * "foldclosedend()" function
11537 */
11538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011539f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540{
11541 foldclosed_both(argvars, rettv, TRUE);
11542}
11543
11544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 * "foldlevel()" function
11546 */
11547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011548f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
11550#ifdef FEAT_FOLDING
11551 linenr_T lnum;
11552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557}
11558
11559/*
11560 * "foldtext()" function
11561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011563f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564{
11565#ifdef FEAT_FOLDING
11566 linenr_T lnum;
11567 char_u *s;
11568 char_u *r;
11569 int len;
11570 char *txt;
11571#endif
11572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 rettv->v_type = VAR_STRING;
11574 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11577 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11578 <= curbuf->b_ml.ml_line_count
11579 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 {
11581 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011582 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11583 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 {
11585 if (!linewhite(lnum))
11586 break;
11587 ++lnum;
11588 }
11589
11590 /* Find interesting text in this line. */
11591 s = skipwhite(ml_get(lnum));
11592 /* skip C comment-start */
11593 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011596 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011598 {
11599 s = skipwhite(ml_get(lnum + 1));
11600 if (*s == '*')
11601 s = skipwhite(s + 1);
11602 }
11603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 txt = _("+-%s%3ld lines: ");
11605 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011606 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 + 20 /* for %3ld */
11608 + STRLEN(s))); /* concatenated */
11609 if (r != NULL)
11610 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011611 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11612 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11613 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 len = (int)STRLEN(r);
11615 STRCAT(r, s);
11616 /* remove 'foldmarker' and 'commentstring' */
11617 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 }
11620 }
11621#endif
11622}
11623
11624/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011625 * "foldtextresult(lnum)" function
11626 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011628f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011629{
11630#ifdef FEAT_FOLDING
11631 linenr_T lnum;
11632 char_u *text;
11633 char_u buf[51];
11634 foldinfo_T foldinfo;
11635 int fold_count;
11636#endif
11637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 rettv->v_type = VAR_STRING;
11639 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011640#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011642 /* treat illegal types and illegal string values for {lnum} the same */
11643 if (lnum < 0)
11644 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011645 fold_count = foldedCount(curwin, lnum, &foldinfo);
11646 if (fold_count > 0)
11647 {
11648 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11649 &foldinfo, buf);
11650 if (text == buf)
11651 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011653 }
11654#endif
11655}
11656
11657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 * "foreground()" function
11659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011661f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663#ifdef FEAT_GUI
11664 if (gui.in_use)
11665 gui_mch_set_foreground();
11666#else
11667# ifdef WIN32
11668 win32_set_foreground();
11669# endif
11670#endif
11671}
11672
11673/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011674 * "function()" function
11675 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011677f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011678{
11679 char_u *s;
11680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011682 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011683 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011684 /* Don't check an autoload name for existence here. */
11685 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011686 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011687 else
11688 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011689 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011690 {
11691 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011692 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011693
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011694 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11695 * also be called from another script. Using trans_function_name()
11696 * would also work, but some plugins depend on the name being
11697 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011698 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011699 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011700 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011701 if (rettv->vval.v_string != NULL)
11702 {
11703 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011704 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011705 }
11706 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011707 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011708 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011710 }
11711}
11712
11713/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011714 * "garbagecollect()" function
11715 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011717f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011718{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011719 /* This is postponed until we are back at the toplevel, because we may be
11720 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11721 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011722
11723 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11724 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011725}
11726
11727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011728 * "get()" function
11729 */
11730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011731f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011732{
Bram Moolenaar33570922005-01-25 22:26:29 +000011733 listitem_T *li;
11734 list_T *l;
11735 dictitem_T *di;
11736 dict_T *d;
11737 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011738
Bram Moolenaare9a41262005-01-15 22:18:47 +000011739 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011740 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011743 int error = FALSE;
11744
11745 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11746 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011747 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011748 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011749 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011750 else if (argvars[0].v_type == VAR_DICT)
11751 {
11752 if ((d = argvars[0].vval.v_dict) != NULL)
11753 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011754 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 if (di != NULL)
11756 tv = &di->di_tv;
11757 }
11758 }
11759 else
11760 EMSG2(_(e_listdictarg), "get()");
11761
11762 if (tv == NULL)
11763 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011764 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011765 copy_tv(&argvars[2], rettv);
11766 }
11767 else
11768 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011769}
11770
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011771static 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 +000011772
11773/*
11774 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011775 * Return a range (from start to end) of lines in rettv from the specified
11776 * buffer.
11777 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011778 */
11779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011780get_buffer_lines(
11781 buf_T *buf,
11782 linenr_T start,
11783 linenr_T end,
11784 int retlist,
11785 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011786{
11787 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011788
Bram Moolenaar959a1432013-12-14 12:17:38 +010011789 rettv->v_type = VAR_STRING;
11790 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011791 if (retlist && rettv_list_alloc(rettv) == FAIL)
11792 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011793
11794 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11795 return;
11796
11797 if (!retlist)
11798 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011799 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11800 p = ml_get_buf(buf, start, FALSE);
11801 else
11802 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011803 rettv->vval.v_string = vim_strsave(p);
11804 }
11805 else
11806 {
11807 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011808 return;
11809
11810 if (start < 1)
11811 start = 1;
11812 if (end > buf->b_ml.ml_line_count)
11813 end = buf->b_ml.ml_line_count;
11814 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011815 if (list_append_string(rettv->vval.v_list,
11816 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011817 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011818 }
11819}
11820
11821/*
11822 * "getbufline()" function
11823 */
11824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011825f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011826{
11827 linenr_T lnum;
11828 linenr_T end;
11829 buf_T *buf;
11830
11831 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11832 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011833 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011834 --emsg_off;
11835
Bram Moolenaar661b1822005-07-28 22:36:45 +000011836 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011837 if (argvars[2].v_type == VAR_UNKNOWN)
11838 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011839 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011840 end = get_tv_lnum_buf(&argvars[2], buf);
11841
Bram Moolenaar342337a2005-07-21 21:11:17 +000011842 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011843}
11844
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845/*
11846 * "getbufvar()" function
11847 */
11848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011849f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850{
11851 buf_T *buf;
11852 buf_T *save_curbuf;
11853 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011854 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011855 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011857 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11858 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011859 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011860 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011861
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011862 rettv->v_type = VAR_STRING;
11863 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011864
11865 if (buf != NULL && varname != NULL)
11866 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011867 /* set curbuf to be our buf, temporarily */
11868 save_curbuf = curbuf;
11869 curbuf = buf;
11870
Bram Moolenaar0d660222005-01-07 21:51:51 +000011871 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011872 {
11873 if (get_option_tv(&varname, rettv, TRUE) == OK)
11874 done = TRUE;
11875 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011876 else if (STRCMP(varname, "changedtick") == 0)
11877 {
11878 rettv->v_type = VAR_NUMBER;
11879 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011880 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011881 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011882 else
11883 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011884 /* Look up the variable. */
11885 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11886 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11887 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011889 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011890 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011891 done = TRUE;
11892 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011893 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011894
11895 /* restore previous notion of curbuf */
11896 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897 }
11898
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011899 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11900 /* use the default value */
11901 copy_tv(&argvars[2], rettv);
11902
Bram Moolenaar0d660222005-01-07 21:51:51 +000011903 --emsg_off;
11904}
11905
11906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 * "getchar()" function
11908 */
11909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011910f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911{
11912 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011915 /* Position the cursor. Needed after a message that ends in a space. */
11916 windgoto(msg_row, msg_col);
11917
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 ++no_mapping;
11919 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011920 for (;;)
11921 {
11922 if (argvars[0].v_type == VAR_UNKNOWN)
11923 /* getchar(): blocking wait. */
11924 n = safe_vgetc();
11925 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11926 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011927 n = vpeekc_any();
11928 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011929 /* illegal argument or getchar(0) and no char avail: return zero */
11930 n = 0;
11931 else
11932 /* getchar(0) and char avail: return char */
11933 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011934
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011935 if (n == K_IGNORE)
11936 continue;
11937 break;
11938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 --no_mapping;
11940 --allow_keys;
11941
Bram Moolenaar219b8702006-11-01 14:32:36 +000011942 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11943 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11944 vimvars[VV_MOUSE_COL].vv_nr = 0;
11945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 if (IS_SPECIAL(n) || mod_mask != 0)
11948 {
11949 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11950 int i = 0;
11951
11952 /* Turn a special key into three bytes, plus modifier. */
11953 if (mod_mask != 0)
11954 {
11955 temp[i++] = K_SPECIAL;
11956 temp[i++] = KS_MODIFIER;
11957 temp[i++] = mod_mask;
11958 }
11959 if (IS_SPECIAL(n))
11960 {
11961 temp[i++] = K_SPECIAL;
11962 temp[i++] = K_SECOND(n);
11963 temp[i++] = K_THIRD(n);
11964 }
11965#ifdef FEAT_MBYTE
11966 else if (has_mbyte)
11967 i += (*mb_char2bytes)(n, temp + i);
11968#endif
11969 else
11970 temp[i++] = n;
11971 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972 rettv->v_type = VAR_STRING;
11973 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011974
11975#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011976 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011977 {
11978 int row = mouse_row;
11979 int col = mouse_col;
11980 win_T *win;
11981 linenr_T lnum;
11982# ifdef FEAT_WINDOWS
11983 win_T *wp;
11984# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011985 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011986
11987 if (row >= 0 && col >= 0)
11988 {
11989 /* Find the window at the mouse coordinates and compute the
11990 * text position. */
11991 win = mouse_find_win(&row, &col);
11992 (void)mouse_comp_pos(win, &row, &col, &lnum);
11993# ifdef FEAT_WINDOWS
11994 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011995 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011996# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011997 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011998 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11999 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12000 }
12001 }
12002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 }
12004}
12005
12006/*
12007 * "getcharmod()" function
12008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012010f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013}
12014
12015/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012016 * "getcharsearch()" function
12017 */
12018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012019f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012020{
12021 if (rettv_dict_alloc(rettv) != FAIL)
12022 {
12023 dict_T *dict = rettv->vval.v_dict;
12024
12025 dict_add_nr_str(dict, "char", 0L, last_csearch());
12026 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12027 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12028 }
12029}
12030
12031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 * "getcmdline()" function
12033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012035f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->v_type = VAR_STRING;
12038 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039}
12040
12041/*
12042 * "getcmdpos()" function
12043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012045f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048}
12049
12050/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012051 * "getcmdtype()" function
12052 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012054f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012055{
12056 rettv->v_type = VAR_STRING;
12057 rettv->vval.v_string = alloc(2);
12058 if (rettv->vval.v_string != NULL)
12059 {
12060 rettv->vval.v_string[0] = get_cmdline_type();
12061 rettv->vval.v_string[1] = NUL;
12062 }
12063}
12064
12065/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012066 * "getcmdwintype()" function
12067 */
12068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012069f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012070{
12071 rettv->v_type = VAR_STRING;
12072 rettv->vval.v_string = NULL;
12073#ifdef FEAT_CMDWIN
12074 rettv->vval.v_string = alloc(2);
12075 if (rettv->vval.v_string != NULL)
12076 {
12077 rettv->vval.v_string[0] = cmdwin_type;
12078 rettv->vval.v_string[1] = NUL;
12079 }
12080#endif
12081}
12082
12083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 * "getcwd()" function
12085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012087f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012089 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012090 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012093 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012094
12095 wp = find_tabwin(&argvars[0], &argvars[1]);
12096 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012098 if (wp->w_localdir != NULL)
12099 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12100 else if(globaldir != NULL)
12101 rettv->vval.v_string = vim_strsave(globaldir);
12102 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012103 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012104 cwd = alloc(MAXPATHL);
12105 if (cwd != NULL)
12106 {
12107 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12108 rettv->vval.v_string = vim_strsave(cwd);
12109 vim_free(cwd);
12110 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012111 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012112#ifdef BACKSLASH_IN_FILENAME
12113 if (rettv->vval.v_string != NULL)
12114 slash_adjust(rettv->vval.v_string);
12115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 }
12117}
12118
12119/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012120 * "getfontname()" function
12121 */
12122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012123f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->v_type = VAR_STRING;
12126 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012127#ifdef FEAT_GUI
12128 if (gui.in_use)
12129 {
12130 GuiFont font;
12131 char_u *name = NULL;
12132
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012133 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012134 {
12135 /* Get the "Normal" font. Either the name saved by
12136 * hl_set_font_name() or from the font ID. */
12137 font = gui.norm_font;
12138 name = hl_get_font_name();
12139 }
12140 else
12141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012143 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12144 return;
12145 font = gui_mch_get_font(name, FALSE);
12146 if (font == NOFONT)
12147 return; /* Invalid font name, return empty string. */
12148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012150 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012151 gui_mch_free_font(font);
12152 }
12153#endif
12154}
12155
12156/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012157 * "getfperm({fname})" function
12158 */
12159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012160f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012161{
12162 char_u *fname;
12163 struct stat st;
12164 char_u *perm = NULL;
12165 char_u flags[] = "rwx";
12166 int i;
12167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012171 if (mch_stat((char *)fname, &st) >= 0)
12172 {
12173 perm = vim_strsave((char_u *)"---------");
12174 if (perm != NULL)
12175 {
12176 for (i = 0; i < 9; i++)
12177 {
12178 if (st.st_mode & (1 << (8 - i)))
12179 perm[i] = flags[i % 3];
12180 }
12181 }
12182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012184}
12185
12186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 * "getfsize({fname})" function
12188 */
12189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012190f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
12192 char_u *fname;
12193 struct stat st;
12194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198
12199 if (mch_stat((char *)fname, &st) >= 0)
12200 {
12201 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012206
12207 /* non-perfect check for overflow */
12208 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12209 rettv->vval.v_number = -2;
12210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 }
12212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214}
12215
12216/*
12217 * "getftime({fname})" function
12218 */
12219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012220f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221{
12222 char_u *fname;
12223 struct stat st;
12224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226
12227 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231}
12232
12233/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012234 * "getftype({fname})" function
12235 */
12236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012237f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012238{
12239 char_u *fname;
12240 struct stat st;
12241 char_u *type = NULL;
12242 char *t;
12243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012247 if (mch_lstat((char *)fname, &st) >= 0)
12248 {
12249#ifdef S_ISREG
12250 if (S_ISREG(st.st_mode))
12251 t = "file";
12252 else if (S_ISDIR(st.st_mode))
12253 t = "dir";
12254# ifdef S_ISLNK
12255 else if (S_ISLNK(st.st_mode))
12256 t = "link";
12257# endif
12258# ifdef S_ISBLK
12259 else if (S_ISBLK(st.st_mode))
12260 t = "bdev";
12261# endif
12262# ifdef S_ISCHR
12263 else if (S_ISCHR(st.st_mode))
12264 t = "cdev";
12265# endif
12266# ifdef S_ISFIFO
12267 else if (S_ISFIFO(st.st_mode))
12268 t = "fifo";
12269# endif
12270# ifdef S_ISSOCK
12271 else if (S_ISSOCK(st.st_mode))
12272 t = "fifo";
12273# endif
12274 else
12275 t = "other";
12276#else
12277# ifdef S_IFMT
12278 switch (st.st_mode & S_IFMT)
12279 {
12280 case S_IFREG: t = "file"; break;
12281 case S_IFDIR: t = "dir"; break;
12282# ifdef S_IFLNK
12283 case S_IFLNK: t = "link"; break;
12284# endif
12285# ifdef S_IFBLK
12286 case S_IFBLK: t = "bdev"; break;
12287# endif
12288# ifdef S_IFCHR
12289 case S_IFCHR: t = "cdev"; break;
12290# endif
12291# ifdef S_IFIFO
12292 case S_IFIFO: t = "fifo"; break;
12293# endif
12294# ifdef S_IFSOCK
12295 case S_IFSOCK: t = "socket"; break;
12296# endif
12297 default: t = "other";
12298 }
12299# else
12300 if (mch_isdir(fname))
12301 t = "dir";
12302 else
12303 t = "file";
12304# endif
12305#endif
12306 type = vim_strsave((char_u *)t);
12307 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012309}
12310
12311/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012312 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313 */
12314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012315f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012316{
12317 linenr_T lnum;
12318 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012319 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012320
12321 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012322 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012323 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012324 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012325 retlist = FALSE;
12326 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012327 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012328 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012329 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012330 retlist = TRUE;
12331 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012332
Bram Moolenaar342337a2005-07-21 21:11:17 +000012333 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012334}
12335
12336/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012337 * "getmatches()" function
12338 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012340f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012341{
12342#ifdef FEAT_SEARCH_EXTRA
12343 dict_T *dict;
12344 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012345 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012346
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012347 if (rettv_list_alloc(rettv) == OK)
12348 {
12349 while (cur != NULL)
12350 {
12351 dict = dict_alloc();
12352 if (dict == NULL)
12353 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012354 if (cur->match.regprog == NULL)
12355 {
12356 /* match added with matchaddpos() */
12357 for (i = 0; i < MAXPOSMATCH; ++i)
12358 {
12359 llpos_T *llpos;
12360 char buf[6];
12361 list_T *l;
12362
12363 llpos = &cur->pos.pos[i];
12364 if (llpos->lnum == 0)
12365 break;
12366 l = list_alloc();
12367 if (l == NULL)
12368 break;
12369 list_append_number(l, (varnumber_T)llpos->lnum);
12370 if (llpos->col > 0)
12371 {
12372 list_append_number(l, (varnumber_T)llpos->col);
12373 list_append_number(l, (varnumber_T)llpos->len);
12374 }
12375 sprintf(buf, "pos%d", i + 1);
12376 dict_add_list(dict, buf, l);
12377 }
12378 }
12379 else
12380 {
12381 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12382 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012383 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012384 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12385 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012386# ifdef FEAT_CONCEAL
12387 if (cur->conceal_char)
12388 {
12389 char_u buf[MB_MAXBYTES + 1];
12390
12391 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12392 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12393 }
12394# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012395 list_append_dict(rettv->vval.v_list, dict);
12396 cur = cur->next;
12397 }
12398 }
12399#endif
12400}
12401
12402/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012403 * "getpid()" function
12404 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012406f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012407{
12408 rettv->vval.v_number = mch_get_pid();
12409}
12410
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012411static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012412
12413/*
12414 * "getcurpos()" function
12415 */
12416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012417f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012418{
12419 getpos_both(argvars, rettv, TRUE);
12420}
12421
Bram Moolenaar18081e32008-02-20 19:11:07 +000012422/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012423 * "getpos(string)" function
12424 */
12425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012427{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012428 getpos_both(argvars, rettv, FALSE);
12429}
12430
12431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012432getpos_both(
12433 typval_T *argvars,
12434 typval_T *rettv,
12435 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012436{
Bram Moolenaara5525202006-03-02 22:52:09 +000012437 pos_T *fp;
12438 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012439 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012440
12441 if (rettv_list_alloc(rettv) == OK)
12442 {
12443 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012444 if (getcurpos)
12445 fp = &curwin->w_cursor;
12446 else
12447 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012448 if (fnum != -1)
12449 list_append_number(l, (varnumber_T)fnum);
12450 else
12451 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012452 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12453 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012454 list_append_number(l, (fp != NULL)
12455 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012456 : (varnumber_T)0);
12457 list_append_number(l,
12458#ifdef FEAT_VIRTUALEDIT
12459 (fp != NULL) ? (varnumber_T)fp->coladd :
12460#endif
12461 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012462 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012463 {
12464 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012465 list_append_number(l, curwin->w_curswant == MAXCOL ?
12466 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012467 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012468 }
12469 else
12470 rettv->vval.v_number = FALSE;
12471}
12472
12473/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012474 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012475 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012477f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012478{
12479#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012480 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012481#endif
12482
Bram Moolenaar2641f772005-03-25 21:58:17 +000012483#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012484 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012485 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012486 wp = NULL;
12487 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12488 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012489 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012490 if (wp == NULL)
12491 return;
12492 }
12493
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012494 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012495 }
12496#endif
12497}
12498
12499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 * "getreg()" function
12501 */
12502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012503f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
12505 char_u *strregname;
12506 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012507 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012508 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012511 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 strregname = get_tv_string_chk(&argvars[0]);
12514 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012515 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012518 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12519 return_list = get_tv_number_chk(&argvars[2], &error);
12520 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012523 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012524
12525 if (error)
12526 return;
12527
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 regname = (strregname == NULL ? '"' : *strregname);
12529 if (regname == 0)
12530 regname = '"';
12531
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012532 if (return_list)
12533 {
12534 rettv->v_type = VAR_LIST;
12535 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12536 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012537 if (rettv->vval.v_list != NULL)
12538 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012539 }
12540 else
12541 {
12542 rettv->v_type = VAR_STRING;
12543 rettv->vval.v_string = get_reg_contents(regname,
12544 arg2 ? GREG_EXPR_SRC : 0);
12545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546}
12547
12548/*
12549 * "getregtype()" function
12550 */
12551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012552f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553{
12554 char_u *strregname;
12555 int regname;
12556 char_u buf[NUMBUFLEN + 2];
12557 long reglen = 0;
12558
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012559 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012560 {
12561 strregname = get_tv_string_chk(&argvars[0]);
12562 if (strregname == NULL) /* type error; errmsg already given */
12563 {
12564 rettv->v_type = VAR_STRING;
12565 rettv->vval.v_string = NULL;
12566 return;
12567 }
12568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 else
12570 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012571 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572
12573 regname = (strregname == NULL ? '"' : *strregname);
12574 if (regname == 0)
12575 regname = '"';
12576
12577 buf[0] = NUL;
12578 buf[1] = NUL;
12579 switch (get_reg_type(regname, &reglen))
12580 {
12581 case MLINE: buf[0] = 'V'; break;
12582 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 case MBLOCK:
12584 buf[0] = Ctrl_V;
12585 sprintf((char *)buf + 1, "%ld", reglen + 1);
12586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012588 rettv->v_type = VAR_STRING;
12589 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590}
12591
12592/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012593 * "gettabvar()" function
12594 */
12595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012596f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012597{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012598 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012599 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012600 dictitem_T *v;
12601 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012602 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012603
12604 rettv->v_type = VAR_STRING;
12605 rettv->vval.v_string = NULL;
12606
12607 varname = get_tv_string_chk(&argvars[1]);
12608 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12609 if (tp != NULL && varname != NULL)
12610 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012611 /* Set tp to be our tabpage, temporarily. Also set the window to the
12612 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012613 if (switch_win(&oldcurwin, &oldtabpage,
12614 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012615 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012616 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012617 /* look up the variable */
12618 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12619 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12620 if (v != NULL)
12621 {
12622 copy_tv(&v->di_tv, rettv);
12623 done = TRUE;
12624 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012625 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012626
12627 /* restore previous notion of curwin */
12628 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012629 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012630
12631 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012632 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012633}
12634
12635/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012636 * "gettabwinvar()" function
12637 */
12638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012639f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012640{
12641 getwinvar(argvars, rettv, 1);
12642}
12643
12644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 * "getwinposx()" function
12646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012648f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651#ifdef FEAT_GUI
12652 if (gui.in_use)
12653 {
12654 int x, y;
12655
12656 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 }
12659#endif
12660}
12661
12662/*
12663 * "getwinposy()" function
12664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012666f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669#ifdef FEAT_GUI
12670 if (gui.in_use)
12671 {
12672 int x, y;
12673
12674 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 }
12677#endif
12678}
12679
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012680/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012681 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012682 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012683 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012684find_win_by_nr(
12685 typval_T *vp,
12686 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012687{
12688#ifdef FEAT_WINDOWS
12689 win_T *wp;
12690#endif
12691 int nr;
12692
12693 nr = get_tv_number_chk(vp, NULL);
12694
12695#ifdef FEAT_WINDOWS
12696 if (nr < 0)
12697 return NULL;
12698 if (nr == 0)
12699 return curwin;
12700
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012701 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12702 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012703 if (--nr <= 0)
12704 break;
12705 return wp;
12706#else
12707 if (nr == 0 || nr == 1)
12708 return curwin;
12709 return NULL;
12710#endif
12711}
12712
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012714 * Find window specified by "wvp" in tabpage "tvp".
12715 */
12716 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012717find_tabwin(
12718 typval_T *wvp, /* VAR_UNKNOWN for current window */
12719 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012720{
12721 win_T *wp = NULL;
12722 tabpage_T *tp = NULL;
12723 long n;
12724
12725 if (wvp->v_type != VAR_UNKNOWN)
12726 {
12727 if (tvp->v_type != VAR_UNKNOWN)
12728 {
12729 n = get_tv_number(tvp);
12730 if (n >= 0)
12731 tp = find_tabpage(n);
12732 }
12733 else
12734 tp = curtab;
12735
12736 if (tp != NULL)
12737 wp = find_win_by_nr(wvp, tp);
12738 }
12739 else
12740 wp = curwin;
12741
12742 return wp;
12743}
12744
12745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 * "getwinvar()" function
12747 */
12748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012749f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012751 getwinvar(argvars, rettv, 0);
12752}
12753
12754/*
12755 * getwinvar() and gettabwinvar()
12756 */
12757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012758getwinvar(
12759 typval_T *argvars,
12760 typval_T *rettv,
12761 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012762{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012763 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012765 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012766 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012767 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012768#ifdef FEAT_WINDOWS
12769 win_T *oldcurwin;
12770 tabpage_T *oldtabpage;
12771 int need_switch_win;
12772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012774#ifdef FEAT_WINDOWS
12775 if (off == 1)
12776 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12777 else
12778 tp = curtab;
12779#endif
12780 win = find_win_by_nr(&argvars[off], tp);
12781 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012784 rettv->v_type = VAR_STRING;
12785 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786
12787 if (win != NULL && varname != NULL)
12788 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012789#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012790 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012791 * otherwise the window is not valid. Only do this when needed,
12792 * autocommands get blocked. */
12793 need_switch_win = !(tp == curtab && win == curwin);
12794 if (!need_switch_win
12795 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12796#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012797 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012798 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012799 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012800 if (get_option_tv(&varname, rettv, 1) == OK)
12801 done = TRUE;
12802 }
12803 else
12804 {
12805 /* Look up the variable. */
12806 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12807 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12808 varname, FALSE);
12809 if (v != NULL)
12810 {
12811 copy_tv(&v->di_tv, rettv);
12812 done = TRUE;
12813 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012816
Bram Moolenaarba117c22015-09-29 16:53:22 +020012817#ifdef FEAT_WINDOWS
12818 if (need_switch_win)
12819 /* restore previous notion of curwin */
12820 restore_win(oldcurwin, oldtabpage, TRUE);
12821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 }
12823
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012824 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12825 /* use the default return value */
12826 copy_tv(&argvars[off + 2], rettv);
12827
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 --emsg_off;
12829}
12830
12831/*
12832 * "glob()" function
12833 */
12834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012835f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012837 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012839 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012841 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012842 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012844 if (argvars[1].v_type != VAR_UNKNOWN)
12845 {
12846 if (get_tv_number_chk(&argvars[1], &error))
12847 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012848 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012849 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012850 if (get_tv_number_chk(&argvars[2], &error))
12851 {
12852 rettv->v_type = VAR_LIST;
12853 rettv->vval.v_list = NULL;
12854 }
12855 if (argvars[3].v_type != VAR_UNKNOWN
12856 && get_tv_number_chk(&argvars[3], &error))
12857 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012858 }
12859 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012860 if (!error)
12861 {
12862 ExpandInit(&xpc);
12863 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012864 if (p_wic)
12865 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012866 if (rettv->v_type == VAR_STRING)
12867 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012868 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012869 else if (rettv_list_alloc(rettv) != FAIL)
12870 {
12871 int i;
12872
12873 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12874 NULL, options, WILD_ALL_KEEP);
12875 for (i = 0; i < xpc.xp_numfiles; i++)
12876 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12877
12878 ExpandCleanup(&xpc);
12879 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012880 }
12881 else
12882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883}
12884
12885/*
12886 * "globpath()" function
12887 */
12888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012889f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012891 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012893 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012894 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012895 garray_T ga;
12896 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012898 /* When the optional second argument is non-zero, don't remove matches
12899 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012901 if (argvars[2].v_type != VAR_UNKNOWN)
12902 {
12903 if (get_tv_number_chk(&argvars[2], &error))
12904 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012905 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012906 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012907 if (get_tv_number_chk(&argvars[3], &error))
12908 {
12909 rettv->v_type = VAR_LIST;
12910 rettv->vval.v_list = NULL;
12911 }
12912 if (argvars[4].v_type != VAR_UNKNOWN
12913 && get_tv_number_chk(&argvars[4], &error))
12914 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012915 }
12916 }
12917 if (file != NULL && !error)
12918 {
12919 ga_init2(&ga, (int)sizeof(char_u *), 10);
12920 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12921 if (rettv->v_type == VAR_STRING)
12922 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12923 else if (rettv_list_alloc(rettv) != FAIL)
12924 for (i = 0; i < ga.ga_len; ++i)
12925 list_append_string(rettv->vval.v_list,
12926 ((char_u **)(ga.ga_data))[i], -1);
12927 ga_clear_strings(&ga);
12928 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012929 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931}
12932
12933/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012934 * "glob2regpat()" function
12935 */
12936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012937f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012938{
12939 char_u *pat = get_tv_string_chk(&argvars[0]);
12940
12941 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012942 rettv->vval.v_string = (pat == NULL)
12943 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012944}
12945
12946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 * "has()" function
12948 */
12949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012950f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951{
12952 int i;
12953 char_u *name;
12954 int n = FALSE;
12955 static char *(has_list[]) =
12956 {
12957#ifdef AMIGA
12958 "amiga",
12959# ifdef FEAT_ARP
12960 "arp",
12961# endif
12962#endif
12963#ifdef __BEOS__
12964 "beos",
12965#endif
12966#ifdef MSDOS
12967# ifdef DJGPP
12968 "dos32",
12969# else
12970 "dos16",
12971# endif
12972#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012973#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 "mac",
12975#endif
12976#if defined(MACOS_X_UNIX)
12977 "macunix",
12978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979#ifdef __QNX__
12980 "qnx",
12981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982#ifdef UNIX
12983 "unix",
12984#endif
12985#ifdef VMS
12986 "vms",
12987#endif
12988#ifdef WIN16
12989 "win16",
12990#endif
12991#ifdef WIN32
12992 "win32",
12993#endif
12994#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12995 "win32unix",
12996#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012997#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 "win64",
12999#endif
13000#ifdef EBCDIC
13001 "ebcdic",
13002#endif
13003#ifndef CASE_INSENSITIVE_FILENAME
13004 "fname_case",
13005#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013006#ifdef HAVE_ACL
13007 "acl",
13008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009#ifdef FEAT_ARABIC
13010 "arabic",
13011#endif
13012#ifdef FEAT_AUTOCMD
13013 "autocmd",
13014#endif
13015#ifdef FEAT_BEVAL
13016 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013017# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13018 "balloon_multiline",
13019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020#endif
13021#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13022 "builtin_terms",
13023# ifdef ALL_BUILTIN_TCAPS
13024 "all_builtin_terms",
13025# endif
13026#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013027#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13028 || defined(FEAT_GUI_W32) \
13029 || defined(FEAT_GUI_MOTIF))
13030 "browsefilter",
13031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032#ifdef FEAT_BYTEOFF
13033 "byte_offset",
13034#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013035#ifdef FEAT_CHANNEL
13036 "channel",
13037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038#ifdef FEAT_CINDENT
13039 "cindent",
13040#endif
13041#ifdef FEAT_CLIENTSERVER
13042 "clientserver",
13043#endif
13044#ifdef FEAT_CLIPBOARD
13045 "clipboard",
13046#endif
13047#ifdef FEAT_CMDL_COMPL
13048 "cmdline_compl",
13049#endif
13050#ifdef FEAT_CMDHIST
13051 "cmdline_hist",
13052#endif
13053#ifdef FEAT_COMMENTS
13054 "comments",
13055#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013056#ifdef FEAT_CONCEAL
13057 "conceal",
13058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059#ifdef FEAT_CRYPT
13060 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013061 "crypt-blowfish",
13062 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063#endif
13064#ifdef FEAT_CSCOPE
13065 "cscope",
13066#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013067#ifdef FEAT_CURSORBIND
13068 "cursorbind",
13069#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013070#ifdef CURSOR_SHAPE
13071 "cursorshape",
13072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073#ifdef DEBUG
13074 "debug",
13075#endif
13076#ifdef FEAT_CON_DIALOG
13077 "dialog_con",
13078#endif
13079#ifdef FEAT_GUI_DIALOG
13080 "dialog_gui",
13081#endif
13082#ifdef FEAT_DIFF
13083 "diff",
13084#endif
13085#ifdef FEAT_DIGRAPHS
13086 "digraphs",
13087#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013088#ifdef FEAT_DIRECTX
13089 "directx",
13090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091#ifdef FEAT_DND
13092 "dnd",
13093#endif
13094#ifdef FEAT_EMACS_TAGS
13095 "emacs_tags",
13096#endif
13097 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013098 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099#ifdef FEAT_SEARCH_EXTRA
13100 "extra_search",
13101#endif
13102#ifdef FEAT_FKMAP
13103 "farsi",
13104#endif
13105#ifdef FEAT_SEARCHPATH
13106 "file_in_path",
13107#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013108#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013109 "filterpipe",
13110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#ifdef FEAT_FIND_ID
13112 "find_in_path",
13113#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013114#ifdef FEAT_FLOAT
13115 "float",
13116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117#ifdef FEAT_FOLDING
13118 "folding",
13119#endif
13120#ifdef FEAT_FOOTER
13121 "footer",
13122#endif
13123#if !defined(USE_SYSTEM) && defined(UNIX)
13124 "fork",
13125#endif
13126#ifdef FEAT_GETTEXT
13127 "gettext",
13128#endif
13129#ifdef FEAT_GUI
13130 "gui",
13131#endif
13132#ifdef FEAT_GUI_ATHENA
13133# ifdef FEAT_GUI_NEXTAW
13134 "gui_neXtaw",
13135# else
13136 "gui_athena",
13137# endif
13138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139#ifdef FEAT_GUI_GTK
13140 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013143#ifdef FEAT_GUI_GNOME
13144 "gui_gnome",
13145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146#ifdef FEAT_GUI_MAC
13147 "gui_mac",
13148#endif
13149#ifdef FEAT_GUI_MOTIF
13150 "gui_motif",
13151#endif
13152#ifdef FEAT_GUI_PHOTON
13153 "gui_photon",
13154#endif
13155#ifdef FEAT_GUI_W16
13156 "gui_win16",
13157#endif
13158#ifdef FEAT_GUI_W32
13159 "gui_win32",
13160#endif
13161#ifdef FEAT_HANGULIN
13162 "hangul_input",
13163#endif
13164#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13165 "iconv",
13166#endif
13167#ifdef FEAT_INS_EXPAND
13168 "insert_expand",
13169#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013170#ifdef FEAT_JOB
13171 "job",
13172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173#ifdef FEAT_JUMPLIST
13174 "jumplist",
13175#endif
13176#ifdef FEAT_KEYMAP
13177 "keymap",
13178#endif
13179#ifdef FEAT_LANGMAP
13180 "langmap",
13181#endif
13182#ifdef FEAT_LIBCALL
13183 "libcall",
13184#endif
13185#ifdef FEAT_LINEBREAK
13186 "linebreak",
13187#endif
13188#ifdef FEAT_LISP
13189 "lispindent",
13190#endif
13191#ifdef FEAT_LISTCMDS
13192 "listcmds",
13193#endif
13194#ifdef FEAT_LOCALMAP
13195 "localmap",
13196#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013197#ifdef FEAT_LUA
13198# ifndef DYNAMIC_LUA
13199 "lua",
13200# endif
13201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202#ifdef FEAT_MENU
13203 "menu",
13204#endif
13205#ifdef FEAT_SESSION
13206 "mksession",
13207#endif
13208#ifdef FEAT_MODIFY_FNAME
13209 "modify_fname",
13210#endif
13211#ifdef FEAT_MOUSE
13212 "mouse",
13213#endif
13214#ifdef FEAT_MOUSESHAPE
13215 "mouseshape",
13216#endif
13217#if defined(UNIX) || defined(VMS)
13218# ifdef FEAT_MOUSE_DEC
13219 "mouse_dec",
13220# endif
13221# ifdef FEAT_MOUSE_GPM
13222 "mouse_gpm",
13223# endif
13224# ifdef FEAT_MOUSE_JSB
13225 "mouse_jsbterm",
13226# endif
13227# ifdef FEAT_MOUSE_NET
13228 "mouse_netterm",
13229# endif
13230# ifdef FEAT_MOUSE_PTERM
13231 "mouse_pterm",
13232# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013233# ifdef FEAT_MOUSE_SGR
13234 "mouse_sgr",
13235# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013236# ifdef FEAT_SYSMOUSE
13237 "mouse_sysmouse",
13238# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013239# ifdef FEAT_MOUSE_URXVT
13240 "mouse_urxvt",
13241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242# ifdef FEAT_MOUSE_XTERM
13243 "mouse_xterm",
13244# endif
13245#endif
13246#ifdef FEAT_MBYTE
13247 "multi_byte",
13248#endif
13249#ifdef FEAT_MBYTE_IME
13250 "multi_byte_ime",
13251#endif
13252#ifdef FEAT_MULTI_LANG
13253 "multi_lang",
13254#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013255#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013256#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013257 "mzscheme",
13258#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef FEAT_OLE
13261 "ole",
13262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263#ifdef FEAT_PATH_EXTRA
13264 "path_extra",
13265#endif
13266#ifdef FEAT_PERL
13267#ifndef DYNAMIC_PERL
13268 "perl",
13269#endif
13270#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013271#ifdef FEAT_PERSISTENT_UNDO
13272 "persistent_undo",
13273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274#ifdef FEAT_PYTHON
13275#ifndef DYNAMIC_PYTHON
13276 "python",
13277#endif
13278#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013279#ifdef FEAT_PYTHON3
13280#ifndef DYNAMIC_PYTHON3
13281 "python3",
13282#endif
13283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284#ifdef FEAT_POSTSCRIPT
13285 "postscript",
13286#endif
13287#ifdef FEAT_PRINTER
13288 "printer",
13289#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013290#ifdef FEAT_PROFILE
13291 "profile",
13292#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013293#ifdef FEAT_RELTIME
13294 "reltime",
13295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296#ifdef FEAT_QUICKFIX
13297 "quickfix",
13298#endif
13299#ifdef FEAT_RIGHTLEFT
13300 "rightleft",
13301#endif
13302#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13303 "ruby",
13304#endif
13305#ifdef FEAT_SCROLLBIND
13306 "scrollbind",
13307#endif
13308#ifdef FEAT_CMDL_INFO
13309 "showcmd",
13310 "cmdline_info",
13311#endif
13312#ifdef FEAT_SIGNS
13313 "signs",
13314#endif
13315#ifdef FEAT_SMARTINDENT
13316 "smartindent",
13317#endif
13318#ifdef FEAT_SNIFF
13319 "sniff",
13320#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013321#ifdef STARTUPTIME
13322 "startuptime",
13323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#ifdef FEAT_STL_OPT
13325 "statusline",
13326#endif
13327#ifdef FEAT_SUN_WORKSHOP
13328 "sun_workshop",
13329#endif
13330#ifdef FEAT_NETBEANS_INTG
13331 "netbeans_intg",
13332#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013333#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013334 "spell",
13335#endif
13336#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 "syntax",
13338#endif
13339#if defined(USE_SYSTEM) || !defined(UNIX)
13340 "system",
13341#endif
13342#ifdef FEAT_TAG_BINS
13343 "tag_binary",
13344#endif
13345#ifdef FEAT_TAG_OLDSTATIC
13346 "tag_old_static",
13347#endif
13348#ifdef FEAT_TAG_ANYWHITE
13349 "tag_any_white",
13350#endif
13351#ifdef FEAT_TCL
13352# ifndef DYNAMIC_TCL
13353 "tcl",
13354# endif
13355#endif
13356#ifdef TERMINFO
13357 "terminfo",
13358#endif
13359#ifdef FEAT_TERMRESPONSE
13360 "termresponse",
13361#endif
13362#ifdef FEAT_TEXTOBJ
13363 "textobjects",
13364#endif
13365#ifdef HAVE_TGETENT
13366 "tgetent",
13367#endif
13368#ifdef FEAT_TITLE
13369 "title",
13370#endif
13371#ifdef FEAT_TOOLBAR
13372 "toolbar",
13373#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013374#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13375 "unnamedplus",
13376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377#ifdef FEAT_USR_CMDS
13378 "user-commands", /* was accidentally included in 5.4 */
13379 "user_commands",
13380#endif
13381#ifdef FEAT_VIMINFO
13382 "viminfo",
13383#endif
13384#ifdef FEAT_VERTSPLIT
13385 "vertsplit",
13386#endif
13387#ifdef FEAT_VIRTUALEDIT
13388 "virtualedit",
13389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#ifdef FEAT_VISUALEXTRA
13392 "visualextra",
13393#endif
13394#ifdef FEAT_VREPLACE
13395 "vreplace",
13396#endif
13397#ifdef FEAT_WILDIGN
13398 "wildignore",
13399#endif
13400#ifdef FEAT_WILDMENU
13401 "wildmenu",
13402#endif
13403#ifdef FEAT_WINDOWS
13404 "windows",
13405#endif
13406#ifdef FEAT_WAK
13407 "winaltkeys",
13408#endif
13409#ifdef FEAT_WRITEBACKUP
13410 "writebackup",
13411#endif
13412#ifdef FEAT_XIM
13413 "xim",
13414#endif
13415#ifdef FEAT_XFONTSET
13416 "xfontset",
13417#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013418#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013419 "xpm",
13420 "xpm_w32", /* for backward compatibility */
13421#else
13422# if defined(HAVE_XPM)
13423 "xpm",
13424# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426#ifdef USE_XSMP
13427 "xsmp",
13428#endif
13429#ifdef USE_XSMP_INTERACT
13430 "xsmp_interact",
13431#endif
13432#ifdef FEAT_XCLIPBOARD
13433 "xterm_clipboard",
13434#endif
13435#ifdef FEAT_XTERM_SAVE
13436 "xterm_save",
13437#endif
13438#if defined(UNIX) && defined(FEAT_X11)
13439 "X11",
13440#endif
13441 NULL
13442 };
13443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 for (i = 0; has_list[i] != NULL; ++i)
13446 if (STRICMP(name, has_list[i]) == 0)
13447 {
13448 n = TRUE;
13449 break;
13450 }
13451
13452 if (n == FALSE)
13453 {
13454 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013455 {
13456 if (name[5] == '-'
13457 && STRLEN(name) > 11
13458 && vim_isdigit(name[6])
13459 && vim_isdigit(name[8])
13460 && vim_isdigit(name[10]))
13461 {
13462 int major = atoi((char *)name + 6);
13463 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013464
13465 /* Expect "patch-9.9.01234". */
13466 n = (major < VIM_VERSION_MAJOR
13467 || (major == VIM_VERSION_MAJOR
13468 && (minor < VIM_VERSION_MINOR
13469 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013470 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013471 }
13472 else
13473 n = has_patch(atoi((char *)name + 5));
13474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 else if (STRICMP(name, "vim_starting") == 0)
13476 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013477#ifdef FEAT_MBYTE
13478 else if (STRICMP(name, "multi_byte_encoding") == 0)
13479 n = has_mbyte;
13480#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013481#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13482 else if (STRICMP(name, "balloon_multiline") == 0)
13483 n = multiline_balloon_available();
13484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485#ifdef DYNAMIC_TCL
13486 else if (STRICMP(name, "tcl") == 0)
13487 n = tcl_enabled(FALSE);
13488#endif
13489#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13490 else if (STRICMP(name, "iconv") == 0)
13491 n = iconv_enabled(FALSE);
13492#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013493#ifdef DYNAMIC_LUA
13494 else if (STRICMP(name, "lua") == 0)
13495 n = lua_enabled(FALSE);
13496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013497#ifdef DYNAMIC_MZSCHEME
13498 else if (STRICMP(name, "mzscheme") == 0)
13499 n = mzscheme_enabled(FALSE);
13500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501#ifdef DYNAMIC_RUBY
13502 else if (STRICMP(name, "ruby") == 0)
13503 n = ruby_enabled(FALSE);
13504#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013505#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506#ifdef DYNAMIC_PYTHON
13507 else if (STRICMP(name, "python") == 0)
13508 n = python_enabled(FALSE);
13509#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013510#endif
13511#ifdef FEAT_PYTHON3
13512#ifdef DYNAMIC_PYTHON3
13513 else if (STRICMP(name, "python3") == 0)
13514 n = python3_enabled(FALSE);
13515#endif
13516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517#ifdef DYNAMIC_PERL
13518 else if (STRICMP(name, "perl") == 0)
13519 n = perl_enabled(FALSE);
13520#endif
13521#ifdef FEAT_GUI
13522 else if (STRICMP(name, "gui_running") == 0)
13523 n = (gui.in_use || gui.starting);
13524# ifdef FEAT_GUI_W32
13525 else if (STRICMP(name, "gui_win32s") == 0)
13526 n = gui_is_win32s();
13527# endif
13528# ifdef FEAT_BROWSE
13529 else if (STRICMP(name, "browse") == 0)
13530 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13531# endif
13532#endif
13533#ifdef FEAT_SYN_HL
13534 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013535 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536#endif
13537#if defined(WIN3264)
13538 else if (STRICMP(name, "win95") == 0)
13539 n = mch_windows95();
13540#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013541#ifdef FEAT_NETBEANS_INTG
13542 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013543 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 }
13546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548}
13549
13550/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013551 * "has_key()" function
13552 */
13553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013554f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013555{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013556 if (argvars[0].v_type != VAR_DICT)
13557 {
13558 EMSG(_(e_dictreq));
13559 return;
13560 }
13561 if (argvars[0].vval.v_dict == NULL)
13562 return;
13563
13564 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013565 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013566}
13567
13568/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013569 * "haslocaldir()" function
13570 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013572f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013573{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013574 win_T *wp = NULL;
13575
13576 wp = find_tabwin(&argvars[0], &argvars[1]);
13577 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013578}
13579
13580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 * "hasmapto()" function
13582 */
13583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013584f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585{
13586 char_u *name;
13587 char_u *mode;
13588 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013589 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013592 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 mode = (char_u *)"nvo";
13594 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013597 if (argvars[2].v_type != VAR_UNKNOWN)
13598 abbr = get_tv_number(&argvars[2]);
13599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600
Bram Moolenaar2c932302006-03-18 21:42:09 +000013601 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605}
13606
13607/*
13608 * "histadd()" function
13609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013611f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612{
13613#ifdef FEAT_CMDHIST
13614 int histype;
13615 char_u *str;
13616 char_u buf[NUMBUFLEN];
13617#endif
13618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 if (check_restricted() || check_secure())
13621 return;
13622#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13624 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 if (histype >= 0)
13626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 if (*str != NUL)
13629 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013630 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 return;
13634 }
13635 }
13636#endif
13637}
13638
13639/*
13640 * "histdel()" function
13641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013643f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
13645#ifdef FEAT_CMDHIST
13646 int n;
13647 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013648 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13651 if (str == NULL)
13652 n = 0;
13653 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013656 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 else
13661 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 get_tv_string_buf(&argvars[1], buf));
13664 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665#endif
13666}
13667
13668/*
13669 * "histget()" function
13670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013672f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673{
13674#ifdef FEAT_CMDHIST
13675 int type;
13676 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013677 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013679 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13680 if (str == NULL)
13681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 {
13684 type = get_histtype(str);
13685 if (argvars[1].v_type == VAR_UNKNOWN)
13686 idx = get_history_idx(type);
13687 else
13688 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13689 /* -1 on type error */
13690 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013695 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696}
13697
13698/*
13699 * "histnr()" function
13700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013702f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703{
13704 int i;
13705
13706#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 char_u *history = get_tv_string_chk(&argvars[0]);
13708
13709 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 if (i >= HIST_CMD && i < HIST_COUNT)
13711 i = get_history_idx(i);
13712 else
13713#endif
13714 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716}
13717
13718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 * "highlightID(name)" function
13720 */
13721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013722f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725}
13726
13727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013728 * "highlight_exists()" function
13729 */
13730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013731f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013732{
13733 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13734}
13735
13736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 * "hostname()" function
13738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013740f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741{
13742 char_u hostname[256];
13743
13744 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745 rettv->v_type = VAR_STRING;
13746 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747}
13748
13749/*
13750 * iconv() function
13751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013753f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754{
13755#ifdef FEAT_MBYTE
13756 char_u buf1[NUMBUFLEN];
13757 char_u buf2[NUMBUFLEN];
13758 char_u *from, *to, *str;
13759 vimconv_T vimconv;
13760#endif
13761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 rettv->v_type = VAR_STRING;
13763 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764
13765#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 str = get_tv_string(&argvars[0]);
13767 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13768 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 vimconv.vc_type = CONV_NONE;
13770 convert_setup(&vimconv, from, to);
13771
13772 /* If the encodings are equal, no conversion needed. */
13773 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777
13778 convert_setup(&vimconv, NULL, NULL);
13779 vim_free(from);
13780 vim_free(to);
13781#endif
13782}
13783
13784/*
13785 * "indent()" function
13786 */
13787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013788f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789{
13790 linenr_T lnum;
13791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013792 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797}
13798
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013799/*
13800 * "index()" function
13801 */
13802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013803f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013804{
Bram Moolenaar33570922005-01-25 22:26:29 +000013805 list_T *l;
13806 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013807 long idx = 0;
13808 int ic = FALSE;
13809
13810 rettv->vval.v_number = -1;
13811 if (argvars[0].v_type != VAR_LIST)
13812 {
13813 EMSG(_(e_listreq));
13814 return;
13815 }
13816 l = argvars[0].vval.v_list;
13817 if (l != NULL)
13818 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013819 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 int error = FALSE;
13823
Bram Moolenaar758711c2005-02-02 23:11:38 +000013824 /* Start at specified item. Use the cached index that list_find()
13825 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013827 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013828 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 ic = get_tv_number_chk(&argvars[3], &error);
13830 if (error)
13831 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013832 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013833
Bram Moolenaar758711c2005-02-02 23:11:38 +000013834 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013835 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013836 {
13837 rettv->vval.v_number = idx;
13838 break;
13839 }
13840 }
13841}
13842
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843static int inputsecret_flag = 0;
13844
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013845static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013846
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013848 * This function is used by f_input() and f_inputdialog() functions. The third
13849 * argument to f_input() specifies the type of completion to use at the
13850 * prompt. The third argument to f_inputdialog() specifies the value to return
13851 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 */
13853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013854get_user_input(
13855 typval_T *argvars,
13856 typval_T *rettv,
13857 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 char_u *p = NULL;
13861 int c;
13862 char_u buf[NUMBUFLEN];
13863 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013865 int xp_type = EXPAND_NOTHING;
13866 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870
13871#ifdef NO_CONSOLE_INPUT
13872 /* While starting up, there is no place to enter text. */
13873 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875#endif
13876
13877 cmd_silent = FALSE; /* Want to see the prompt. */
13878 if (prompt != NULL)
13879 {
13880 /* Only the part of the message after the last NL is considered as
13881 * prompt for the command line */
13882 p = vim_strrchr(prompt, '\n');
13883 if (p == NULL)
13884 p = prompt;
13885 else
13886 {
13887 ++p;
13888 c = *p;
13889 *p = NUL;
13890 msg_start();
13891 msg_clr_eos();
13892 msg_puts_attr(prompt, echo_attr);
13893 msg_didout = FALSE;
13894 msg_starthere();
13895 *p = c;
13896 }
13897 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013899 if (argvars[1].v_type != VAR_UNKNOWN)
13900 {
13901 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13902 if (defstr != NULL)
13903 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013905 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013906 {
13907 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013908 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013909 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013910
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013911 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013912 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013913
Bram Moolenaar4463f292005-09-25 22:20:24 +000013914 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13915 if (xp_name == NULL)
13916 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013918 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013919
Bram Moolenaar4463f292005-09-25 22:20:24 +000013920 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13921 &xp_arg) == FAIL)
13922 return;
13923 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013924 }
13925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013926 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013927 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013928 int save_ex_normal_busy = ex_normal_busy;
13929 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013930 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013931 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13932 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013933 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013934 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013935 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013936 && argvars[1].v_type != VAR_UNKNOWN
13937 && argvars[2].v_type != VAR_UNKNOWN)
13938 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13939 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013940
13941 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013943 /* since the user typed this, no need to wait for return */
13944 need_wait_return = FALSE;
13945 msg_didout = FALSE;
13946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 cmd_silent = cmd_silent_save;
13948}
13949
13950/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013951 * "input()" function
13952 * Also handles inputsecret() when inputsecret is set.
13953 */
13954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013955f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013956{
13957 get_user_input(argvars, rettv, FALSE);
13958}
13959
13960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 * "inputdialog()" function
13962 */
13963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013964f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965{
13966#if defined(FEAT_GUI_TEXTDIALOG)
13967 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13968 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13969 {
13970 char_u *message;
13971 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013974 message = get_tv_string_chk(&argvars[0]);
13975 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013976 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013977 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 else
13979 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980 if (message != NULL && defstr != NULL
13981 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013982 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013983 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 else
13985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 if (message != NULL && defstr != NULL
13987 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013988 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 rettv->vval.v_string = vim_strsave(
13990 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013994 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 }
13996 else
13997#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013998 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999}
14000
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014001/*
14002 * "inputlist()" function
14003 */
14004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014005f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014006{
14007 listitem_T *li;
14008 int selected;
14009 int mouse_used;
14010
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014011#ifdef NO_CONSOLE_INPUT
14012 /* While starting up, there is no place to enter text. */
14013 if (no_console_input())
14014 return;
14015#endif
14016 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14017 {
14018 EMSG2(_(e_listarg), "inputlist()");
14019 return;
14020 }
14021
14022 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014023 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014024 lines_left = Rows; /* avoid more prompt */
14025 msg_scroll = TRUE;
14026 msg_clr_eos();
14027
14028 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14029 {
14030 msg_puts(get_tv_string(&li->li_tv));
14031 msg_putchar('\n');
14032 }
14033
14034 /* Ask for choice. */
14035 selected = prompt_for_number(&mouse_used);
14036 if (mouse_used)
14037 selected -= lines_left;
14038
14039 rettv->vval.v_number = selected;
14040}
14041
14042
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14044
14045/*
14046 * "inputrestore()" function
14047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014049f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050{
14051 if (ga_userinput.ga_len > 0)
14052 {
14053 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14055 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014056 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 }
14058 else if (p_verbose > 1)
14059 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014060 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 }
14063}
14064
14065/*
14066 * "inputsave()" function
14067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014069f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014071 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 if (ga_grow(&ga_userinput, 1) == OK)
14073 {
14074 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14075 + ga_userinput.ga_len);
14076 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014077 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 }
14079 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014080 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081}
14082
14083/*
14084 * "inputsecret()" function
14085 */
14086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014087f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088{
14089 ++cmdline_star;
14090 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014091 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 --cmdline_star;
14093 --inputsecret_flag;
14094}
14095
14096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014097 * "insert()" function
14098 */
14099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014100f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014101{
14102 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 listitem_T *item;
14104 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014106
14107 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014109 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014110 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014111 {
14112 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 before = get_tv_number_chk(&argvars[2], &error);
14114 if (error)
14115 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014116
Bram Moolenaar758711c2005-02-02 23:11:38 +000014117 if (before == l->lv_len)
14118 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014119 else
14120 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014121 item = list_find(l, before);
14122 if (item == NULL)
14123 {
14124 EMSGN(_(e_listidx), before);
14125 l = NULL;
14126 }
14127 }
14128 if (l != NULL)
14129 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014130 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014131 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014132 }
14133 }
14134}
14135
14136/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014137 * "invert(expr)" function
14138 */
14139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014140f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014141{
14142 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14143}
14144
14145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 * "isdirectory()" function
14147 */
14148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014149f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152}
14153
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014154/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014155 * "islocked()" function
14156 */
14157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014158f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014159{
14160 lval_T lv;
14161 char_u *end;
14162 dictitem_T *di;
14163
14164 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014165 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14166 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014167 if (end != NULL && lv.ll_name != NULL)
14168 {
14169 if (*end != NUL)
14170 EMSG(_(e_trailing));
14171 else
14172 {
14173 if (lv.ll_tv == NULL)
14174 {
14175 if (check_changedtick(lv.ll_name))
14176 rettv->vval.v_number = 1; /* always locked */
14177 else
14178 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014179 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014180 if (di != NULL)
14181 {
14182 /* Consider a variable locked when:
14183 * 1. the variable itself is locked
14184 * 2. the value of the variable is locked.
14185 * 3. the List or Dict value is locked.
14186 */
14187 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14188 || tv_islocked(&di->di_tv));
14189 }
14190 }
14191 }
14192 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014193 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014194 else if (lv.ll_newkey != NULL)
14195 EMSG2(_(e_dictkey), lv.ll_newkey);
14196 else if (lv.ll_list != NULL)
14197 /* List item. */
14198 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14199 else
14200 /* Dictionary item. */
14201 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14202 }
14203 }
14204
14205 clear_lval(&lv);
14206}
14207
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014208static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014209
14210/*
14211 * Turn a dict into a list:
14212 * "what" == 0: list of keys
14213 * "what" == 1: list of values
14214 * "what" == 2: list of items
14215 */
14216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014217dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014218{
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 list_T *l2;
14220 dictitem_T *di;
14221 hashitem_T *hi;
14222 listitem_T *li;
14223 listitem_T *li2;
14224 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014225 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014226
Bram Moolenaar8c711452005-01-14 21:53:12 +000014227 if (argvars[0].v_type != VAR_DICT)
14228 {
14229 EMSG(_(e_dictreq));
14230 return;
14231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014232 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014233 return;
14234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014235 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014237
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014238 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014241 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014243 --todo;
14244 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014245
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014246 li = listitem_alloc();
14247 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014248 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014249 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014250
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014251 if (what == 0)
14252 {
14253 /* keys() */
14254 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014255 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014256 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14257 }
14258 else if (what == 1)
14259 {
14260 /* values() */
14261 copy_tv(&di->di_tv, &li->li_tv);
14262 }
14263 else
14264 {
14265 /* items() */
14266 l2 = list_alloc();
14267 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014268 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014269 li->li_tv.vval.v_list = l2;
14270 if (l2 == NULL)
14271 break;
14272 ++l2->lv_refcount;
14273
14274 li2 = listitem_alloc();
14275 if (li2 == NULL)
14276 break;
14277 list_append(l2, li2);
14278 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014279 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014280 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14281
14282 li2 = listitem_alloc();
14283 if (li2 == NULL)
14284 break;
14285 list_append(l2, li2);
14286 copy_tv(&di->di_tv, &li2->li_tv);
14287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014288 }
14289 }
14290}
14291
14292/*
14293 * "items(dict)" function
14294 */
14295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014296f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014297{
14298 dict_list(argvars, rettv, 2);
14299}
14300
Bram Moolenaar835dc632016-02-07 14:27:38 +010014301#ifdef FEAT_JOB
14302/*
14303 * "job_start()" function
14304 */
14305 static void
14306f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14307{
14308 job_T *job;
14309 char_u *cmd = NULL;
14310#if defined(UNIX)
14311# define USE_ARGV
14312 char **argv = NULL;
14313 int argc = 0;
14314#else
14315 garray_T ga;
14316#endif
14317
14318 rettv->v_type = VAR_JOB;
14319 job = job_alloc();
14320 rettv->vval.v_job = job;
14321 if (job == NULL)
14322 return;
14323
14324 rettv->vval.v_job->jv_status = JOB_FAILED;
14325#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014326 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014327#endif
14328
14329 if (argvars[0].v_type == VAR_STRING)
14330 {
14331 /* Command is a string. */
14332 cmd = argvars[0].vval.v_string;
14333#ifdef USE_ARGV
14334 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14335 return;
14336 argv[argc] = NULL;
14337#endif
14338 }
14339 else if (argvars[0].v_type != VAR_LIST
14340 || argvars[0].vval.v_list == NULL
14341 || argvars[0].vval.v_list->lv_len < 1)
14342 {
14343 EMSG(_(e_invarg));
14344 return;
14345 }
14346 else
14347 {
14348 list_T *l = argvars[0].vval.v_list;
14349 listitem_T *li;
14350 char_u *s;
14351
14352#ifdef USE_ARGV
14353 /* Pass argv[] to mch_call_shell(). */
14354 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14355 if (argv == NULL)
14356 return;
14357#endif
14358 for (li = l->lv_first; li != NULL; li = li->li_next)
14359 {
14360 s = get_tv_string_chk(&li->li_tv);
14361 if (s == NULL)
14362 goto theend;
14363#ifdef USE_ARGV
14364 argv[argc++] = (char *)s;
14365#else
14366 if (li != l->lv_first)
14367 {
14368 s = vim_strsave_shellescape(s, FALSE, TRUE);
14369 if (s == NULL)
14370 goto theend;
14371 }
14372 ga_concat(&ga, s);
14373 vim_free(s);
14374 if (li->li_next != NULL)
14375 ga_append(&ga, ' ');
14376#endif
14377 }
14378#ifdef USE_ARGV
14379 argv[argc] = NULL;
14380#else
14381 cmd = ga.ga_data;
14382#endif
14383 }
14384#ifdef USE_ARGV
14385 mch_start_job(argv, job);
14386#else
14387 mch_start_job(cmd, job);
14388#endif
14389
14390theend:
14391#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014392 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014393#else
14394 vim_free(ga.ga_data);
14395#endif
14396}
14397
14398/*
14399 * "job_status()" function
14400 */
14401 static void
14402f_job_status(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14403{
14404 char *result;
14405
14406 if (argvars[0].v_type != VAR_JOB)
14407 EMSG(_(e_invarg));
14408 else
14409 {
14410 job_T *job = argvars[0].vval.v_job;
14411
14412 if (job->jv_status == JOB_ENDED)
14413 /* No need to check, dead is dead. */
14414 result = "dead";
14415 else if (job->jv_status == JOB_FAILED)
14416 result = "fail";
14417 else
14418 result = mch_job_status(job);
14419 rettv->v_type = VAR_STRING;
14420 rettv->vval.v_string = vim_strsave((char_u *)result);
14421 }
14422}
14423
14424/*
14425 * "job_stop()" function
14426 */
14427 static void
14428f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14429{
14430 if (argvars[0].v_type != VAR_JOB)
14431 EMSG(_(e_invarg));
14432 else
14433 {
14434 char_u *arg;
14435
14436 if (argvars[1].v_type == VAR_UNKNOWN)
14437 arg = (char_u *)"";
14438 else
14439 {
14440 arg = get_tv_string_chk(&argvars[1]);
14441 if (arg == NULL)
14442 {
14443 EMSG(_(e_invarg));
14444 return;
14445 }
14446 }
14447 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14448 rettv->vval.v_number = 0;
14449 else
14450 rettv->vval.v_number = 1;
14451 }
14452}
14453#endif
14454
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014456 * "join()" function
14457 */
14458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014459f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014460{
14461 garray_T ga;
14462 char_u *sep;
14463
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014464 if (argvars[0].v_type != VAR_LIST)
14465 {
14466 EMSG(_(e_listreq));
14467 return;
14468 }
14469 if (argvars[0].vval.v_list == NULL)
14470 return;
14471 if (argvars[1].v_type == VAR_UNKNOWN)
14472 sep = (char_u *)" ";
14473 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014474 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014475
14476 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014477
14478 if (sep != NULL)
14479 {
14480 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014481 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014482 ga_append(&ga, NUL);
14483 rettv->vval.v_string = (char_u *)ga.ga_data;
14484 }
14485 else
14486 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014487}
14488
14489/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014490 * "jsdecode()" function
14491 */
14492 static void
14493f_jsdecode(typval_T *argvars, typval_T *rettv)
14494{
14495 js_read_T reader;
14496
14497 reader.js_buf = get_tv_string(&argvars[0]);
14498 reader.js_fill = NULL;
14499 reader.js_used = 0;
14500 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14501 EMSG(_(e_invarg));
14502}
14503
14504/*
14505 * "jsencode()" function
14506 */
14507 static void
14508f_jsencode(typval_T *argvars, typval_T *rettv)
14509{
14510 rettv->v_type = VAR_STRING;
14511 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14512}
14513
14514/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014515 * "jsondecode()" function
14516 */
14517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014518f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014519{
14520 js_read_T reader;
14521
14522 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014523 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014524 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014525 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014526 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014527}
14528
14529/*
14530 * "jsonencode()" function
14531 */
14532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014533f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014534{
14535 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014536 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014537}
14538
14539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014540 * "keys()" function
14541 */
14542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014543f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014544{
14545 dict_list(argvars, rettv, 0);
14546}
14547
14548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 * "last_buffer_nr()" function.
14550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014552f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553{
14554 int n = 0;
14555 buf_T *buf;
14556
14557 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14558 if (n < buf->b_fnum)
14559 n = buf->b_fnum;
14560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014562}
14563
14564/*
14565 * "len()" function
14566 */
14567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014568f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014569{
14570 switch (argvars[0].v_type)
14571 {
14572 case VAR_STRING:
14573 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014574 rettv->vval.v_number = (varnumber_T)STRLEN(
14575 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014576 break;
14577 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014579 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014580 case VAR_DICT:
14581 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14582 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014583 case VAR_UNKNOWN:
14584 case VAR_SPECIAL:
14585 case VAR_FLOAT:
14586 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014587 case VAR_JOB:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014588 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014589 break;
14590 }
14591}
14592
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014593static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014594
14595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014596libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014597{
14598#ifdef FEAT_LIBCALL
14599 char_u *string_in;
14600 char_u **string_result;
14601 int nr_result;
14602#endif
14603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014605 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014607
14608 if (check_restricted() || check_secure())
14609 return;
14610
14611#ifdef FEAT_LIBCALL
14612 /* The first two args must be strings, otherwise its meaningless */
14613 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14614 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014615 string_in = NULL;
14616 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014617 string_in = argvars[2].vval.v_string;
14618 if (type == VAR_NUMBER)
14619 string_result = NULL;
14620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014622 if (mch_libcall(argvars[0].vval.v_string,
14623 argvars[1].vval.v_string,
14624 string_in,
14625 argvars[2].vval.v_number,
14626 string_result,
14627 &nr_result) == OK
14628 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014629 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014630 }
14631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632}
14633
14634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635 * "libcall()" function
14636 */
14637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014638f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639{
14640 libcall_common(argvars, rettv, VAR_STRING);
14641}
14642
14643/*
14644 * "libcallnr()" function
14645 */
14646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014647f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648{
14649 libcall_common(argvars, rettv, VAR_NUMBER);
14650}
14651
14652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 * "line(string)" function
14654 */
14655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014656f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657{
14658 linenr_T lnum = 0;
14659 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014660 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014662 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 if (fp != NULL)
14664 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666}
14667
14668/*
14669 * "line2byte(lnum)" function
14670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014672f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673{
14674#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014675 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676#else
14677 linenr_T lnum;
14678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14684 if (rettv->vval.v_number >= 0)
14685 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686#endif
14687}
14688
14689/*
14690 * "lispindent(lnum)" function
14691 */
14692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014693f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694{
14695#ifdef FEAT_LISP
14696 pos_T pos;
14697 linenr_T lnum;
14698
14699 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014700 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14702 {
14703 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014704 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705 curwin->w_cursor = pos;
14706 }
14707 else
14708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710}
14711
14712/*
14713 * "localtime()" function
14714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014716f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719}
14720
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014721static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722
14723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014724get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725{
14726 char_u *keys;
14727 char_u *which;
14728 char_u buf[NUMBUFLEN];
14729 char_u *keys_buf = NULL;
14730 char_u *rhs;
14731 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014732 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014733 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014734 mapblock_T *mp;
14735 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736
14737 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738 rettv->v_type = VAR_STRING;
14739 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 if (*keys == NUL)
14743 return;
14744
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014745 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014747 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014748 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014749 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014750 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014751 if (argvars[3].v_type != VAR_UNKNOWN)
14752 get_dict = get_tv_number(&argvars[3]);
14753 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 else
14756 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014757 if (which == NULL)
14758 return;
14759
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 mode = get_map_mode(&which, 0);
14761
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014762 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014763 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014765
14766 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014768 /* Return a string. */
14769 if (rhs != NULL)
14770 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771
Bram Moolenaarbd743252010-10-20 21:23:33 +020014772 }
14773 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14774 {
14775 /* Return a dictionary. */
14776 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14777 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14778 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779
Bram Moolenaarbd743252010-10-20 21:23:33 +020014780 dict_add_nr_str(dict, "lhs", 0L, lhs);
14781 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14782 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14783 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14784 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14785 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14786 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014787 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014788 dict_add_nr_str(dict, "mode", 0L, mapmode);
14789
14790 vim_free(lhs);
14791 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 }
14793}
14794
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014795#ifdef FEAT_FLOAT
14796/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014797 * "log()" function
14798 */
14799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014800f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014801{
14802 float_T f;
14803
14804 rettv->v_type = VAR_FLOAT;
14805 if (get_float_arg(argvars, &f) == OK)
14806 rettv->vval.v_float = log(f);
14807 else
14808 rettv->vval.v_float = 0.0;
14809}
14810
14811/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014812 * "log10()" function
14813 */
14814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014815f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014816{
14817 float_T f;
14818
14819 rettv->v_type = VAR_FLOAT;
14820 if (get_float_arg(argvars, &f) == OK)
14821 rettv->vval.v_float = log10(f);
14822 else
14823 rettv->vval.v_float = 0.0;
14824}
14825#endif
14826
Bram Moolenaar1dced572012-04-05 16:54:08 +020014827#ifdef FEAT_LUA
14828/*
14829 * "luaeval()" function
14830 */
14831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014832f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014833{
14834 char_u *str;
14835 char_u buf[NUMBUFLEN];
14836
14837 str = get_tv_string_buf(&argvars[0], buf);
14838 do_luaeval(str, argvars + 1, rettv);
14839}
14840#endif
14841
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014843 * "map()" function
14844 */
14845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014846f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014847{
14848 filter_map(argvars, rettv, TRUE);
14849}
14850
14851/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014852 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 */
14854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014855f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014857 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858}
14859
14860/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014861 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 */
14863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014864f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867}
14868
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014869static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870
14871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014872find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014874 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014875 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014876 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877 char_u *pat;
14878 regmatch_T regmatch;
14879 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014880 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881 char_u *save_cpo;
14882 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014883 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014884 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014885 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014886 list_T *l = NULL;
14887 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014888 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014889 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890
14891 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14892 save_cpo = p_cpo;
14893 p_cpo = (char_u *)"";
14894
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014895 rettv->vval.v_number = -1;
14896 if (type == 3)
14897 {
14898 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014899 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014900 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014901 }
14902 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014904 rettv->v_type = VAR_STRING;
14905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014908 if (argvars[0].v_type == VAR_LIST)
14909 {
14910 if ((l = argvars[0].vval.v_list) == NULL)
14911 goto theend;
14912 li = l->lv_first;
14913 }
14914 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014915 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014916 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014917 len = (long)STRLEN(str);
14918 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014920 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14921 if (pat == NULL)
14922 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014923
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014926 int error = FALSE;
14927
14928 start = get_tv_number_chk(&argvars[2], &error);
14929 if (error)
14930 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014931 if (l != NULL)
14932 {
14933 li = list_find(l, start);
14934 if (li == NULL)
14935 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014936 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014937 }
14938 else
14939 {
14940 if (start < 0)
14941 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014942 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014943 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014944 /* When "count" argument is there ignore matches before "start",
14945 * otherwise skip part of the string. Differs when pattern is "^"
14946 * or "\<". */
14947 if (argvars[3].v_type != VAR_UNKNOWN)
14948 startcol = start;
14949 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014950 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014951 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014952 len -= start;
14953 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014954 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014955
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014956 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014957 nth = get_tv_number_chk(&argvars[3], &error);
14958 if (error)
14959 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 }
14961
14962 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14963 if (regmatch.regprog != NULL)
14964 {
14965 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014966
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014967 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014968 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014969 if (l != NULL)
14970 {
14971 if (li == NULL)
14972 {
14973 match = FALSE;
14974 break;
14975 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014976 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014977 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014978 if (str == NULL)
14979 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014980 }
14981
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014982 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014983
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014984 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014986 if (l == NULL && !match)
14987 break;
14988
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014989 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014990 if (l != NULL)
14991 {
14992 li = li->li_next;
14993 ++idx;
14994 }
14995 else
14996 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014997#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014998 startcol = (colnr_T)(regmatch.startp[0]
14999 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015000#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015001 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015002#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015003 if (startcol > (colnr_T)len
15004 || str + startcol <= regmatch.startp[0])
15005 {
15006 match = FALSE;
15007 break;
15008 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015009 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015010 }
15011
15012 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015014 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015015 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015016 int i;
15017
15018 /* return list with matched string and submatches */
15019 for (i = 0; i < NSUBEXP; ++i)
15020 {
15021 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015022 {
15023 if (list_append_string(rettv->vval.v_list,
15024 (char_u *)"", 0) == FAIL)
15025 break;
15026 }
15027 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015028 regmatch.startp[i],
15029 (int)(regmatch.endp[i] - regmatch.startp[i]))
15030 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015031 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015032 }
15033 }
15034 else if (type == 2)
15035 {
15036 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015037 if (l != NULL)
15038 copy_tv(&li->li_tv, rettv);
15039 else
15040 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015042 }
15043 else if (l != NULL)
15044 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 else
15046 {
15047 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015048 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 (varnumber_T)(regmatch.startp[0] - str);
15050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015053 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 }
15055 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015056 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 }
15058
15059theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015060 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 p_cpo = save_cpo;
15062}
15063
15064/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065 * "match()" function
15066 */
15067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015068f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069{
15070 find_some_match(argvars, rettv, 1);
15071}
15072
15073/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015074 * "matchadd()" function
15075 */
15076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015077f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015078{
15079#ifdef FEAT_SEARCH_EXTRA
15080 char_u buf[NUMBUFLEN];
15081 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15082 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15083 int prio = 10; /* default priority */
15084 int id = -1;
15085 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015086 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015087
15088 rettv->vval.v_number = -1;
15089
15090 if (grp == NULL || pat == NULL)
15091 return;
15092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015093 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015094 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015095 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015096 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015097 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015098 if (argvars[4].v_type != VAR_UNKNOWN)
15099 {
15100 if (argvars[4].v_type != VAR_DICT)
15101 {
15102 EMSG(_(e_dictreq));
15103 return;
15104 }
15105 if (dict_find(argvars[4].vval.v_dict,
15106 (char_u *)"conceal", -1) != NULL)
15107 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15108 (char_u *)"conceal", FALSE);
15109 }
15110 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015111 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015112 if (error == TRUE)
15113 return;
15114 if (id >= 1 && id <= 3)
15115 {
15116 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15117 return;
15118 }
15119
Bram Moolenaar6561d522015-07-21 15:48:27 +020015120 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15121 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015122#endif
15123}
15124
15125/*
15126 * "matchaddpos()" function
15127 */
15128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015129f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015130{
15131#ifdef FEAT_SEARCH_EXTRA
15132 char_u buf[NUMBUFLEN];
15133 char_u *group;
15134 int prio = 10;
15135 int id = -1;
15136 int error = FALSE;
15137 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015138 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015139
15140 rettv->vval.v_number = -1;
15141
15142 group = get_tv_string_buf_chk(&argvars[0], buf);
15143 if (group == NULL)
15144 return;
15145
15146 if (argvars[1].v_type != VAR_LIST)
15147 {
15148 EMSG2(_(e_listarg), "matchaddpos()");
15149 return;
15150 }
15151 l = argvars[1].vval.v_list;
15152 if (l == NULL)
15153 return;
15154
15155 if (argvars[2].v_type != VAR_UNKNOWN)
15156 {
15157 prio = get_tv_number_chk(&argvars[2], &error);
15158 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015159 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015160 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015161 if (argvars[4].v_type != VAR_UNKNOWN)
15162 {
15163 if (argvars[4].v_type != VAR_DICT)
15164 {
15165 EMSG(_(e_dictreq));
15166 return;
15167 }
15168 if (dict_find(argvars[4].vval.v_dict,
15169 (char_u *)"conceal", -1) != NULL)
15170 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15171 (char_u *)"conceal", FALSE);
15172 }
15173 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015174 }
15175 if (error == TRUE)
15176 return;
15177
15178 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15179 if (id == 1 || id == 2)
15180 {
15181 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15182 return;
15183 }
15184
Bram Moolenaar6561d522015-07-21 15:48:27 +020015185 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15186 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015187#endif
15188}
15189
15190/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015191 * "matcharg()" function
15192 */
15193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015194f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015195{
15196 if (rettv_list_alloc(rettv) == OK)
15197 {
15198#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015199 int id = get_tv_number(&argvars[0]);
15200 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015201
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015202 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015203 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015204 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15205 {
15206 list_append_string(rettv->vval.v_list,
15207 syn_id2name(m->hlg_id), -1);
15208 list_append_string(rettv->vval.v_list, m->pattern, -1);
15209 }
15210 else
15211 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015212 list_append_string(rettv->vval.v_list, NULL, -1);
15213 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015214 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015215 }
15216#endif
15217 }
15218}
15219
15220/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015221 * "matchdelete()" function
15222 */
15223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015224f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015225{
15226#ifdef FEAT_SEARCH_EXTRA
15227 rettv->vval.v_number = match_delete(curwin,
15228 (int)get_tv_number(&argvars[0]), TRUE);
15229#endif
15230}
15231
15232/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233 * "matchend()" function
15234 */
15235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015236f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237{
15238 find_some_match(argvars, rettv, 0);
15239}
15240
15241/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015242 * "matchlist()" function
15243 */
15244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015245f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015246{
15247 find_some_match(argvars, rettv, 3);
15248}
15249
15250/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251 * "matchstr()" function
15252 */
15253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015254f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015255{
15256 find_some_match(argvars, rettv, 2);
15257}
15258
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015259static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015260
15261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015262max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015263{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015264 long n = 0;
15265 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015267
15268 if (argvars[0].v_type == VAR_LIST)
15269 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015270 list_T *l;
15271 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015272
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015273 l = argvars[0].vval.v_list;
15274 if (l != NULL)
15275 {
15276 li = l->lv_first;
15277 if (li != NULL)
15278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015279 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015280 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015281 {
15282 li = li->li_next;
15283 if (li == NULL)
15284 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015286 if (domax ? i > n : i < n)
15287 n = i;
15288 }
15289 }
15290 }
15291 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015292 else if (argvars[0].v_type == VAR_DICT)
15293 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015294 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015295 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015296 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015297 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015298
15299 d = argvars[0].vval.v_dict;
15300 if (d != NULL)
15301 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015302 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015303 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015305 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015307 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015308 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015309 if (first)
15310 {
15311 n = i;
15312 first = FALSE;
15313 }
15314 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015315 n = i;
15316 }
15317 }
15318 }
15319 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015320 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015321 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015322 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015323}
15324
15325/*
15326 * "max()" function
15327 */
15328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015329f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015330{
15331 max_min(argvars, rettv, TRUE);
15332}
15333
15334/*
15335 * "min()" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015339{
15340 max_min(argvars, rettv, FALSE);
15341}
15342
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015343static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015344
15345/*
15346 * Create the directory in which "dir" is located, and higher levels when
15347 * needed.
15348 */
15349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015350mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015351{
15352 char_u *p;
15353 char_u *updir;
15354 int r = FAIL;
15355
15356 /* Get end of directory name in "dir".
15357 * We're done when it's "/" or "c:/". */
15358 p = gettail_sep(dir);
15359 if (p <= get_past_head(dir))
15360 return OK;
15361
15362 /* If the directory exists we're done. Otherwise: create it.*/
15363 updir = vim_strnsave(dir, (int)(p - dir));
15364 if (updir == NULL)
15365 return FAIL;
15366 if (mch_isdir(updir))
15367 r = OK;
15368 else if (mkdir_recurse(updir, prot) == OK)
15369 r = vim_mkdir_emsg(updir, prot);
15370 vim_free(updir);
15371 return r;
15372}
15373
15374#ifdef vim_mkdir
15375/*
15376 * "mkdir()" function
15377 */
15378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015379f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015380{
15381 char_u *dir;
15382 char_u buf[NUMBUFLEN];
15383 int prot = 0755;
15384
15385 rettv->vval.v_number = FAIL;
15386 if (check_restricted() || check_secure())
15387 return;
15388
15389 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015390 if (*dir == NUL)
15391 rettv->vval.v_number = FAIL;
15392 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015393 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015394 if (*gettail(dir) == NUL)
15395 /* remove trailing slashes */
15396 *gettail_sep(dir) = NUL;
15397
15398 if (argvars[1].v_type != VAR_UNKNOWN)
15399 {
15400 if (argvars[2].v_type != VAR_UNKNOWN)
15401 prot = get_tv_number_chk(&argvars[2], NULL);
15402 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15403 mkdir_recurse(dir, prot);
15404 }
15405 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015406 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015407}
15408#endif
15409
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 * "mode()" function
15412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015414f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015416 char_u buf[3];
15417
15418 buf[1] = NUL;
15419 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 if (VIsual_active)
15422 {
15423 if (VIsual_select)
15424 buf[0] = VIsual_mode + 's' - 'v';
15425 else
15426 buf[0] = VIsual_mode;
15427 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015428 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015429 || State == CONFIRM)
15430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015432 if (State == ASKMORE)
15433 buf[1] = 'm';
15434 else if (State == CONFIRM)
15435 buf[1] = '?';
15436 }
15437 else if (State == EXTERNCMD)
15438 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 else if (State & INSERT)
15440 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015441#ifdef FEAT_VREPLACE
15442 if (State & VREPLACE_FLAG)
15443 {
15444 buf[0] = 'R';
15445 buf[1] = 'v';
15446 }
15447 else
15448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 if (State & REPLACE_FLAG)
15450 buf[0] = 'R';
15451 else
15452 buf[0] = 'i';
15453 }
15454 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015457 if (exmode_active)
15458 buf[1] = 'v';
15459 }
15460 else if (exmode_active)
15461 {
15462 buf[0] = 'c';
15463 buf[1] = 'e';
15464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015468 if (finish_op)
15469 buf[1] = 'o';
15470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015472 /* Clear out the minor mode when the argument is not a non-zero number or
15473 * non-empty string. */
15474 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015475 buf[1] = NUL;
15476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477 rettv->vval.v_string = vim_strsave(buf);
15478 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479}
15480
Bram Moolenaar429fa852013-04-15 12:27:36 +020015481#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015482/*
15483 * "mzeval()" function
15484 */
15485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015486f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015487{
15488 char_u *str;
15489 char_u buf[NUMBUFLEN];
15490
15491 str = get_tv_string_buf(&argvars[0], buf);
15492 do_mzeval(str, rettv);
15493}
Bram Moolenaar75676462013-01-30 14:55:42 +010015494
15495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015496mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015497{
15498 typval_T argvars[3];
15499
15500 argvars[0].v_type = VAR_STRING;
15501 argvars[0].vval.v_string = name;
15502 copy_tv(args, &argvars[1]);
15503 argvars[2].v_type = VAR_UNKNOWN;
15504 f_call(argvars, rettv);
15505 clear_tv(&argvars[1]);
15506}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015507#endif
15508
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015510 * "nextnonblank()" function
15511 */
15512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015513f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015514{
15515 linenr_T lnum;
15516
15517 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015519 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015520 {
15521 lnum = 0;
15522 break;
15523 }
15524 if (*skipwhite(ml_get(lnum)) != NUL)
15525 break;
15526 }
15527 rettv->vval.v_number = lnum;
15528}
15529
15530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 * "nr2char()" function
15532 */
15533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015534f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535{
15536 char_u buf[NUMBUFLEN];
15537
15538#ifdef FEAT_MBYTE
15539 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015540 {
15541 int utf8 = 0;
15542
15543 if (argvars[1].v_type != VAR_UNKNOWN)
15544 utf8 = get_tv_number_chk(&argvars[1], NULL);
15545 if (utf8)
15546 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15547 else
15548 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 else
15551#endif
15552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 buf[1] = NUL;
15555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->v_type = VAR_STRING;
15557 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015558}
15559
15560/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015561 * "or(expr, expr)" function
15562 */
15563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015564f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015565{
15566 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15567 | get_tv_number_chk(&argvars[1], NULL);
15568}
15569
15570/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015571 * "pathshorten()" function
15572 */
15573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015574f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015575{
15576 char_u *p;
15577
15578 rettv->v_type = VAR_STRING;
15579 p = get_tv_string_chk(&argvars[0]);
15580 if (p == NULL)
15581 rettv->vval.v_string = NULL;
15582 else
15583 {
15584 p = vim_strsave(p);
15585 rettv->vval.v_string = p;
15586 if (p != NULL)
15587 shorten_dir(p);
15588 }
15589}
15590
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015591#ifdef FEAT_PERL
15592/*
15593 * "perleval()" function
15594 */
15595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015596f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015597{
15598 char_u *str;
15599 char_u buf[NUMBUFLEN];
15600
15601 str = get_tv_string_buf(&argvars[0], buf);
15602 do_perleval(str, rettv);
15603}
15604#endif
15605
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015606#ifdef FEAT_FLOAT
15607/*
15608 * "pow()" function
15609 */
15610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015611f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015612{
15613 float_T fx, fy;
15614
15615 rettv->v_type = VAR_FLOAT;
15616 if (get_float_arg(argvars, &fx) == OK
15617 && get_float_arg(&argvars[1], &fy) == OK)
15618 rettv->vval.v_float = pow(fx, fy);
15619 else
15620 rettv->vval.v_float = 0.0;
15621}
15622#endif
15623
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015625 * "prevnonblank()" function
15626 */
15627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015628f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015629{
15630 linenr_T lnum;
15631
15632 lnum = get_tv_lnum(argvars);
15633 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15634 lnum = 0;
15635 else
15636 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15637 --lnum;
15638 rettv->vval.v_number = lnum;
15639}
15640
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015641/* This dummy va_list is here because:
15642 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15643 * - locally in the function results in a "used before set" warning
15644 * - using va_start() to initialize it gives "function with fixed args" error */
15645static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015646
Bram Moolenaar8c711452005-01-14 21:53:12 +000015647/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015648 * "printf()" function
15649 */
15650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015651f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015652{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015653 char_u buf[NUMBUFLEN];
15654 int len;
15655 char_u *s;
15656 int saved_did_emsg = did_emsg;
15657 char *fmt;
15658
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015659 rettv->v_type = VAR_STRING;
15660 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015661
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015662 /* Get the required length, allocate the buffer and do it for real. */
15663 did_emsg = FALSE;
15664 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15665 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15666 if (!did_emsg)
15667 {
15668 s = alloc(len + 1);
15669 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015670 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015671 rettv->vval.v_string = s;
15672 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015673 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015674 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015675 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015676}
15677
15678/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015679 * "pumvisible()" function
15680 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015682f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015683{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015684#ifdef FEAT_INS_EXPAND
15685 if (pum_visible())
15686 rettv->vval.v_number = 1;
15687#endif
15688}
15689
Bram Moolenaardb913952012-06-29 12:54:53 +020015690#ifdef FEAT_PYTHON3
15691/*
15692 * "py3eval()" function
15693 */
15694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015695f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015696{
15697 char_u *str;
15698 char_u buf[NUMBUFLEN];
15699
15700 str = get_tv_string_buf(&argvars[0], buf);
15701 do_py3eval(str, rettv);
15702}
15703#endif
15704
15705#ifdef FEAT_PYTHON
15706/*
15707 * "pyeval()" function
15708 */
15709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015710f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015711{
15712 char_u *str;
15713 char_u buf[NUMBUFLEN];
15714
15715 str = get_tv_string_buf(&argvars[0], buf);
15716 do_pyeval(str, rettv);
15717}
15718#endif
15719
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015720/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015721 * "range()" function
15722 */
15723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015724f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015725{
15726 long start;
15727 long end;
15728 long stride = 1;
15729 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015730 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015732 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015733 if (argvars[1].v_type == VAR_UNKNOWN)
15734 {
15735 end = start - 1;
15736 start = 0;
15737 }
15738 else
15739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015740 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015741 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015743 }
15744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015745 if (error)
15746 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015747 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015748 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015749 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015750 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015751 else
15752 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015753 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015754 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015755 if (list_append_number(rettv->vval.v_list,
15756 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015757 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015758 }
15759}
15760
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015761/*
15762 * "readfile()" function
15763 */
15764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015765f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015766{
15767 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015768 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015769 char_u *fname;
15770 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015771 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15772 int io_size = sizeof(buf);
15773 int readlen; /* size of last fread() */
15774 char_u *prev = NULL; /* previously read bytes, if any */
15775 long prevlen = 0; /* length of data in prev */
15776 long prevsize = 0; /* size of prev buffer */
15777 long maxline = MAXLNUM;
15778 long cnt = 0;
15779 char_u *p; /* position in buf */
15780 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015781
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015782 if (argvars[1].v_type != VAR_UNKNOWN)
15783 {
15784 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15785 binary = TRUE;
15786 if (argvars[2].v_type != VAR_UNKNOWN)
15787 maxline = get_tv_number(&argvars[2]);
15788 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015789
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015790 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015791 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015792
15793 /* Always open the file in binary mode, library functions have a mind of
15794 * their own about CR-LF conversion. */
15795 fname = get_tv_string(&argvars[0]);
15796 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15797 {
15798 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15799 return;
15800 }
15801
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015802 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015803 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015804 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015805
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015806 /* This for loop processes what was read, but is also entered at end
15807 * of file so that either:
15808 * - an incomplete line gets written
15809 * - a "binary" file gets an empty line at the end if it ends in a
15810 * newline. */
15811 for (p = buf, start = buf;
15812 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15813 ++p)
15814 {
15815 if (*p == '\n' || readlen <= 0)
15816 {
15817 listitem_T *li;
15818 char_u *s = NULL;
15819 long_u len = p - start;
15820
15821 /* Finished a line. Remove CRs before NL. */
15822 if (readlen > 0 && !binary)
15823 {
15824 while (len > 0 && start[len - 1] == '\r')
15825 --len;
15826 /* removal may cross back to the "prev" string */
15827 if (len == 0)
15828 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15829 --prevlen;
15830 }
15831 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015832 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015833 else
15834 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015835 /* Change "prev" buffer to be the right size. This way
15836 * the bytes are only copied once, and very long lines are
15837 * allocated only once. */
15838 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015839 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015840 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015841 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015842 prev = NULL; /* the list will own the string */
15843 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015844 }
15845 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015846 if (s == NULL)
15847 {
15848 do_outofmem_msg((long_u) prevlen + len + 1);
15849 failed = TRUE;
15850 break;
15851 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015852
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015853 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015854 {
15855 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015856 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015857 break;
15858 }
15859 li->li_tv.v_type = VAR_STRING;
15860 li->li_tv.v_lock = 0;
15861 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015862 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015863
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015864 start = p + 1; /* step over newline */
15865 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015866 break;
15867 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015868 else if (*p == NUL)
15869 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015870#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015871 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15872 * when finding the BF and check the previous two bytes. */
15873 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015874 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015875 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15876 * + 1, these may be in the "prev" string. */
15877 char_u back1 = p >= buf + 1 ? p[-1]
15878 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15879 char_u back2 = p >= buf + 2 ? p[-2]
15880 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15881 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015882
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015883 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015884 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015885 char_u *dest = p - 2;
15886
15887 /* Usually a BOM is at the beginning of a file, and so at
15888 * the beginning of a line; then we can just step over it.
15889 */
15890 if (start == dest)
15891 start = p + 1;
15892 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015893 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015894 /* have to shuffle buf to close gap */
15895 int adjust_prevlen = 0;
15896
15897 if (dest < buf)
15898 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015899 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015900 dest = buf;
15901 }
15902 if (readlen > p - buf + 1)
15903 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15904 readlen -= 3 - adjust_prevlen;
15905 prevlen -= adjust_prevlen;
15906 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015907 }
15908 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015909 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015910#endif
15911 } /* for */
15912
15913 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15914 break;
15915 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015916 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015917 /* There's part of a line in buf, store it in "prev". */
15918 if (p - start + prevlen >= prevsize)
15919 {
15920 /* need bigger "prev" buffer */
15921 char_u *newprev;
15922
15923 /* A common use case is ordinary text files and "prev" gets a
15924 * fragment of a line, so the first allocation is made
15925 * small, to avoid repeatedly 'allocing' large and
15926 * 'reallocing' small. */
15927 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015928 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015929 else
15930 {
15931 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015932 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015933 prevsize = grow50pc > growmin ? grow50pc : growmin;
15934 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015935 newprev = prev == NULL ? alloc(prevsize)
15936 : vim_realloc(prev, prevsize);
15937 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015938 {
15939 do_outofmem_msg((long_u)prevsize);
15940 failed = TRUE;
15941 break;
15942 }
15943 prev = newprev;
15944 }
15945 /* Add the line part to end of "prev". */
15946 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015947 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015948 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015949 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015950
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015951 /*
15952 * For a negative line count use only the lines at the end of the file,
15953 * free the rest.
15954 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015955 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015956 while (cnt > -maxline)
15957 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015958 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015959 --cnt;
15960 }
15961
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015962 if (failed)
15963 {
15964 list_free(rettv->vval.v_list, TRUE);
15965 /* readfile doc says an empty list is returned on error */
15966 rettv->vval.v_list = list_alloc();
15967 }
15968
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015969 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015970 fclose(fd);
15971}
15972
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015973#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015974static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015975
15976/*
15977 * Convert a List to proftime_T.
15978 * Return FAIL when there is something wrong.
15979 */
15980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015981list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015982{
15983 long n1, n2;
15984 int error = FALSE;
15985
15986 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15987 || arg->vval.v_list->lv_len != 2)
15988 return FAIL;
15989 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15990 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15991# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015992 tm->HighPart = n1;
15993 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015994# else
15995 tm->tv_sec = n1;
15996 tm->tv_usec = n2;
15997# endif
15998 return error ? FAIL : OK;
15999}
16000#endif /* FEAT_RELTIME */
16001
16002/*
16003 * "reltime()" function
16004 */
16005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016006f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016007{
16008#ifdef FEAT_RELTIME
16009 proftime_T res;
16010 proftime_T start;
16011
16012 if (argvars[0].v_type == VAR_UNKNOWN)
16013 {
16014 /* No arguments: get current time. */
16015 profile_start(&res);
16016 }
16017 else if (argvars[1].v_type == VAR_UNKNOWN)
16018 {
16019 if (list2proftime(&argvars[0], &res) == FAIL)
16020 return;
16021 profile_end(&res);
16022 }
16023 else
16024 {
16025 /* Two arguments: compute the difference. */
16026 if (list2proftime(&argvars[0], &start) == FAIL
16027 || list2proftime(&argvars[1], &res) == FAIL)
16028 return;
16029 profile_sub(&res, &start);
16030 }
16031
16032 if (rettv_list_alloc(rettv) == OK)
16033 {
16034 long n1, n2;
16035
16036# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016037 n1 = res.HighPart;
16038 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016039# else
16040 n1 = res.tv_sec;
16041 n2 = res.tv_usec;
16042# endif
16043 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16044 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16045 }
16046#endif
16047}
16048
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016049#ifdef FEAT_FLOAT
16050/*
16051 * "reltimefloat()" function
16052 */
16053 static void
16054f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16055{
16056# ifdef FEAT_RELTIME
16057 proftime_T tm;
16058# endif
16059
16060 rettv->v_type = VAR_FLOAT;
16061 rettv->vval.v_float = 0;
16062# ifdef FEAT_RELTIME
16063 if (list2proftime(&argvars[0], &tm) == OK)
16064 rettv->vval.v_float = profile_float(&tm);
16065# endif
16066}
16067#endif
16068
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016069/*
16070 * "reltimestr()" function
16071 */
16072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016073f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016074{
16075#ifdef FEAT_RELTIME
16076 proftime_T tm;
16077#endif
16078
16079 rettv->v_type = VAR_STRING;
16080 rettv->vval.v_string = NULL;
16081#ifdef FEAT_RELTIME
16082 if (list2proftime(&argvars[0], &tm) == OK)
16083 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16084#endif
16085}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016086
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016088static void make_connection(void);
16089static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090
16091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016092make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093{
16094 if (X_DISPLAY == NULL
16095# ifdef FEAT_GUI
16096 && !gui.in_use
16097# endif
16098 )
16099 {
16100 x_force_connect = TRUE;
16101 setup_term_clip();
16102 x_force_connect = FALSE;
16103 }
16104}
16105
16106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016107check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016108{
16109 make_connection();
16110 if (X_DISPLAY == NULL)
16111 {
16112 EMSG(_("E240: No connection to Vim server"));
16113 return FAIL;
16114 }
16115 return OK;
16116}
16117#endif
16118
16119#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016120static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121
16122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016123remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016124{
16125 char_u *server_name;
16126 char_u *keys;
16127 char_u *r = NULL;
16128 char_u buf[NUMBUFLEN];
16129# ifdef WIN32
16130 HWND w;
16131# else
16132 Window w;
16133# endif
16134
16135 if (check_restricted() || check_secure())
16136 return;
16137
16138# ifdef FEAT_X11
16139 if (check_connection() == FAIL)
16140 return;
16141# endif
16142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016143 server_name = get_tv_string_chk(&argvars[0]);
16144 if (server_name == NULL)
16145 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146 keys = get_tv_string_buf(&argvars[1], buf);
16147# ifdef WIN32
16148 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16149# else
16150 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16151 < 0)
16152# endif
16153 {
16154 if (r != NULL)
16155 EMSG(r); /* sending worked but evaluation failed */
16156 else
16157 EMSG2(_("E241: Unable to send to %s"), server_name);
16158 return;
16159 }
16160
16161 rettv->vval.v_string = r;
16162
16163 if (argvars[2].v_type != VAR_UNKNOWN)
16164 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016165 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016166 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016167 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016169 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016170 v.di_tv.v_type = VAR_STRING;
16171 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016172 idvar = get_tv_string_chk(&argvars[2]);
16173 if (idvar != NULL)
16174 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016175 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016176 }
16177}
16178#endif
16179
16180/*
16181 * "remote_expr()" function
16182 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016184f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185{
16186 rettv->v_type = VAR_STRING;
16187 rettv->vval.v_string = NULL;
16188#ifdef FEAT_CLIENTSERVER
16189 remote_common(argvars, rettv, TRUE);
16190#endif
16191}
16192
16193/*
16194 * "remote_foreground()" function
16195 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016197f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199#ifdef FEAT_CLIENTSERVER
16200# ifdef WIN32
16201 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016202 {
16203 char_u *server_name = get_tv_string_chk(&argvars[0]);
16204
16205 if (server_name != NULL)
16206 serverForeground(server_name);
16207 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208# else
16209 /* Send a foreground() expression to the server. */
16210 argvars[1].v_type = VAR_STRING;
16211 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16212 argvars[2].v_type = VAR_UNKNOWN;
16213 remote_common(argvars, rettv, TRUE);
16214 vim_free(argvars[1].vval.v_string);
16215# endif
16216#endif
16217}
16218
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016220f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221{
16222#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016223 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224 char_u *s = NULL;
16225# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016226 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016228 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229
16230 if (check_restricted() || check_secure())
16231 {
16232 rettv->vval.v_number = -1;
16233 return;
16234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016235 serverid = get_tv_string_chk(&argvars[0]);
16236 if (serverid == NULL)
16237 {
16238 rettv->vval.v_number = -1;
16239 return; /* type error; errmsg already given */
16240 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016241# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016242 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016243 if (n == 0)
16244 rettv->vval.v_number = -1;
16245 else
16246 {
16247 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16248 rettv->vval.v_number = (s != NULL);
16249 }
16250# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 if (check_connection() == FAIL)
16252 return;
16253
16254 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256# endif
16257
16258 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016260 char_u *retvar;
16261
Bram Moolenaar33570922005-01-25 22:26:29 +000016262 v.di_tv.v_type = VAR_STRING;
16263 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 retvar = get_tv_string_chk(&argvars[1]);
16265 if (retvar != NULL)
16266 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016268 }
16269#else
16270 rettv->vval.v_number = -1;
16271#endif
16272}
16273
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016275f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016276{
16277 char_u *r = NULL;
16278
16279#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016280 char_u *serverid = get_tv_string_chk(&argvars[0]);
16281
16282 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016283 {
16284# ifdef WIN32
16285 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016286 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016288 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289 if (n != 0)
16290 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16291 if (r == NULL)
16292# else
16293 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016294 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295# endif
16296 EMSG(_("E277: Unable to read a server reply"));
16297 }
16298#endif
16299 rettv->v_type = VAR_STRING;
16300 rettv->vval.v_string = r;
16301}
16302
16303/*
16304 * "remote_send()" function
16305 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016307f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308{
16309 rettv->v_type = VAR_STRING;
16310 rettv->vval.v_string = NULL;
16311#ifdef FEAT_CLIENTSERVER
16312 remote_common(argvars, rettv, FALSE);
16313#endif
16314}
16315
16316/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016317 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016318 */
16319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016320f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016321{
Bram Moolenaar33570922005-01-25 22:26:29 +000016322 list_T *l;
16323 listitem_T *item, *item2;
16324 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016325 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016326 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016327 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016328 dict_T *d;
16329 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016330 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016331
Bram Moolenaar8c711452005-01-14 21:53:12 +000016332 if (argvars[0].v_type == VAR_DICT)
16333 {
16334 if (argvars[2].v_type != VAR_UNKNOWN)
16335 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016336 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016337 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016339 key = get_tv_string_chk(&argvars[1]);
16340 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016342 di = dict_find(d, key, -1);
16343 if (di == NULL)
16344 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016345 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16346 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016347 {
16348 *rettv = di->di_tv;
16349 init_tv(&di->di_tv);
16350 dictitem_remove(d, di);
16351 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016353 }
16354 }
16355 else if (argvars[0].v_type != VAR_LIST)
16356 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016357 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016358 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016360 int error = FALSE;
16361
16362 idx = get_tv_number_chk(&argvars[1], &error);
16363 if (error)
16364 ; /* type error: do nothing, errmsg already given */
16365 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016366 EMSGN(_(e_listidx), idx);
16367 else
16368 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016369 if (argvars[2].v_type == VAR_UNKNOWN)
16370 {
16371 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016372 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016373 *rettv = item->li_tv;
16374 vim_free(item);
16375 }
16376 else
16377 {
16378 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016379 end = get_tv_number_chk(&argvars[2], &error);
16380 if (error)
16381 ; /* type error: do nothing */
16382 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016383 EMSGN(_(e_listidx), end);
16384 else
16385 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016386 int cnt = 0;
16387
16388 for (li = item; li != NULL; li = li->li_next)
16389 {
16390 ++cnt;
16391 if (li == item2)
16392 break;
16393 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016394 if (li == NULL) /* didn't find "item2" after "item" */
16395 EMSG(_(e_invrange));
16396 else
16397 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016398 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016399 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016400 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016401 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016402 l->lv_first = item;
16403 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016404 item->li_prev = NULL;
16405 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016406 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016407 }
16408 }
16409 }
16410 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016411 }
16412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413}
16414
16415/*
16416 * "rename({from}, {to})" function
16417 */
16418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016419f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420{
16421 char_u buf[NUMBUFLEN];
16422
16423 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016426 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16427 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428}
16429
16430/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016431 * "repeat()" function
16432 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016434f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016435{
16436 char_u *p;
16437 int n;
16438 int slen;
16439 int len;
16440 char_u *r;
16441 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016442
16443 n = get_tv_number(&argvars[1]);
16444 if (argvars[0].v_type == VAR_LIST)
16445 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016446 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016447 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016448 if (list_extend(rettv->vval.v_list,
16449 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016450 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016451 }
16452 else
16453 {
16454 p = get_tv_string(&argvars[0]);
16455 rettv->v_type = VAR_STRING;
16456 rettv->vval.v_string = NULL;
16457
16458 slen = (int)STRLEN(p);
16459 len = slen * n;
16460 if (len <= 0)
16461 return;
16462
16463 r = alloc(len + 1);
16464 if (r != NULL)
16465 {
16466 for (i = 0; i < n; i++)
16467 mch_memmove(r + i * slen, p, (size_t)slen);
16468 r[len] = NUL;
16469 }
16470
16471 rettv->vval.v_string = r;
16472 }
16473}
16474
16475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476 * "resolve()" function
16477 */
16478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016479f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480{
16481 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016482#ifdef HAVE_READLINK
16483 char_u *buf = NULL;
16484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016486 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487#ifdef FEAT_SHORTCUT
16488 {
16489 char_u *v = NULL;
16490
16491 v = mch_resolve_shortcut(p);
16492 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016493 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016495 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 }
16497#else
16498# ifdef HAVE_READLINK
16499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 char_u *cpy;
16501 int len;
16502 char_u *remain = NULL;
16503 char_u *q;
16504 int is_relative_to_current = FALSE;
16505 int has_trailing_pathsep = FALSE;
16506 int limit = 100;
16507
16508 p = vim_strsave(p);
16509
16510 if (p[0] == '.' && (vim_ispathsep(p[1])
16511 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16512 is_relative_to_current = TRUE;
16513
16514 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016515 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016518 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520
16521 q = getnextcomp(p);
16522 if (*q != NUL)
16523 {
16524 /* Separate the first path component in "p", and keep the
16525 * remainder (beginning with the path separator). */
16526 remain = vim_strsave(q - 1);
16527 q[-1] = NUL;
16528 }
16529
Bram Moolenaard9462e32011-04-11 21:35:11 +020016530 buf = alloc(MAXPATHL + 1);
16531 if (buf == NULL)
16532 goto fail;
16533
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534 for (;;)
16535 {
16536 for (;;)
16537 {
16538 len = readlink((char *)p, (char *)buf, MAXPATHL);
16539 if (len <= 0)
16540 break;
16541 buf[len] = NUL;
16542
16543 if (limit-- == 0)
16544 {
16545 vim_free(p);
16546 vim_free(remain);
16547 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 goto fail;
16550 }
16551
16552 /* Ensure that the result will have a trailing path separator
16553 * if the argument has one. */
16554 if (remain == NULL && has_trailing_pathsep)
16555 add_pathsep(buf);
16556
16557 /* Separate the first path component in the link value and
16558 * concatenate the remainders. */
16559 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16560 if (*q != NUL)
16561 {
16562 if (remain == NULL)
16563 remain = vim_strsave(q - 1);
16564 else
16565 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016566 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 if (cpy != NULL)
16568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 vim_free(remain);
16570 remain = cpy;
16571 }
16572 }
16573 q[-1] = NUL;
16574 }
16575
16576 q = gettail(p);
16577 if (q > p && *q == NUL)
16578 {
16579 /* Ignore trailing path separator. */
16580 q[-1] = NUL;
16581 q = gettail(p);
16582 }
16583 if (q > p && !mch_isFullName(buf))
16584 {
16585 /* symlink is relative to directory of argument */
16586 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16587 if (cpy != NULL)
16588 {
16589 STRCPY(cpy, p);
16590 STRCPY(gettail(cpy), buf);
16591 vim_free(p);
16592 p = cpy;
16593 }
16594 }
16595 else
16596 {
16597 vim_free(p);
16598 p = vim_strsave(buf);
16599 }
16600 }
16601
16602 if (remain == NULL)
16603 break;
16604
16605 /* Append the first path component of "remain" to "p". */
16606 q = getnextcomp(remain + 1);
16607 len = q - remain - (*q != NUL);
16608 cpy = vim_strnsave(p, STRLEN(p) + len);
16609 if (cpy != NULL)
16610 {
16611 STRNCAT(cpy, remain, len);
16612 vim_free(p);
16613 p = cpy;
16614 }
16615 /* Shorten "remain". */
16616 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016617 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 else
16619 {
16620 vim_free(remain);
16621 remain = NULL;
16622 }
16623 }
16624
16625 /* If the result is a relative path name, make it explicitly relative to
16626 * the current directory if and only if the argument had this form. */
16627 if (!vim_ispathsep(*p))
16628 {
16629 if (is_relative_to_current
16630 && *p != NUL
16631 && !(p[0] == '.'
16632 && (p[1] == NUL
16633 || vim_ispathsep(p[1])
16634 || (p[1] == '.'
16635 && (p[2] == NUL
16636 || vim_ispathsep(p[2]))))))
16637 {
16638 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016639 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 if (cpy != NULL)
16641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 vim_free(p);
16643 p = cpy;
16644 }
16645 }
16646 else if (!is_relative_to_current)
16647 {
16648 /* Strip leading "./". */
16649 q = p;
16650 while (q[0] == '.' && vim_ispathsep(q[1]))
16651 q += 2;
16652 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016653 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654 }
16655 }
16656
16657 /* Ensure that the result will have no trailing path separator
16658 * if the argument had none. But keep "/" or "//". */
16659 if (!has_trailing_pathsep)
16660 {
16661 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016662 if (after_pathsep(p, q))
16663 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 }
16665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 }
16668# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016669 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670# endif
16671#endif
16672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674
16675#ifdef HAVE_READLINK
16676fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016677 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016679 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680}
16681
16682/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 */
16685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016686f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687{
Bram Moolenaar33570922005-01-25 22:26:29 +000016688 list_T *l;
16689 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691 if (argvars[0].v_type != VAR_LIST)
16692 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016693 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016694 && !tv_check_lock(l->lv_lock,
16695 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016696 {
16697 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016698 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016699 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016700 while (li != NULL)
16701 {
16702 ni = li->li_prev;
16703 list_append(l, li);
16704 li = ni;
16705 }
16706 rettv->vval.v_list = l;
16707 rettv->v_type = VAR_LIST;
16708 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016709 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711}
16712
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016713#define SP_NOMOVE 0x01 /* don't move cursor */
16714#define SP_REPEAT 0x02 /* repeat to find outer pair */
16715#define SP_RETCOUNT 0x04 /* return matchcount */
16716#define SP_SETPCMARK 0x08 /* set previous context mark */
16717#define SP_START 0x10 /* accept match at start position */
16718#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16719#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016720#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016721
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016722static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016723
16724/*
16725 * Get flags for a search function.
16726 * Possibly sets "p_ws".
16727 * Returns BACKWARD, FORWARD or zero (for an error).
16728 */
16729 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016730get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016731{
16732 int dir = FORWARD;
16733 char_u *flags;
16734 char_u nbuf[NUMBUFLEN];
16735 int mask;
16736
16737 if (varp->v_type != VAR_UNKNOWN)
16738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016739 flags = get_tv_string_buf_chk(varp, nbuf);
16740 if (flags == NULL)
16741 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016742 while (*flags != NUL)
16743 {
16744 switch (*flags)
16745 {
16746 case 'b': dir = BACKWARD; break;
16747 case 'w': p_ws = TRUE; break;
16748 case 'W': p_ws = FALSE; break;
16749 default: mask = 0;
16750 if (flagsp != NULL)
16751 switch (*flags)
16752 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016753 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016754 case 'e': mask = SP_END; break;
16755 case 'm': mask = SP_RETCOUNT; break;
16756 case 'n': mask = SP_NOMOVE; break;
16757 case 'p': mask = SP_SUBPAT; break;
16758 case 'r': mask = SP_REPEAT; break;
16759 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016760 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016761 }
16762 if (mask == 0)
16763 {
16764 EMSG2(_(e_invarg2), flags);
16765 dir = 0;
16766 }
16767 else
16768 *flagsp |= mask;
16769 }
16770 if (dir == 0)
16771 break;
16772 ++flags;
16773 }
16774 }
16775 return dir;
16776}
16777
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016779 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016782search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016784 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 char_u *pat;
16786 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016787 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 int save_p_ws = p_ws;
16789 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016790 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016791 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016792 proftime_T tm;
16793#ifdef FEAT_RELTIME
16794 long time_limit = 0;
16795#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016796 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016797 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016800 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016801 if (dir == 0)
16802 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016803 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016804 if (flags & SP_START)
16805 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016806 if (flags & SP_END)
16807 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016808 if (flags & SP_COLUMN)
16809 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016810
Bram Moolenaar76929292008-01-06 19:07:36 +000016811 /* Optional arguments: line number to stop searching and timeout. */
16812 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016813 {
16814 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16815 if (lnum_stop < 0)
16816 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016817#ifdef FEAT_RELTIME
16818 if (argvars[3].v_type != VAR_UNKNOWN)
16819 {
16820 time_limit = get_tv_number_chk(&argvars[3], NULL);
16821 if (time_limit < 0)
16822 goto theend;
16823 }
16824#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016825 }
16826
Bram Moolenaar76929292008-01-06 19:07:36 +000016827#ifdef FEAT_RELTIME
16828 /* Set the time limit, if there is one. */
16829 profile_setlimit(time_limit, &tm);
16830#endif
16831
Bram Moolenaar231334e2005-07-25 20:46:57 +000016832 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016833 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016834 * Check to make sure only those flags are set.
16835 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16836 * flags cannot be set. Check for that condition also.
16837 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016838 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016839 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016842 goto theend;
16843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016845 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016846 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016847 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016848 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016850 if (flags & SP_SUBPAT)
16851 retval = subpatnum;
16852 else
16853 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016854 if (flags & SP_SETPCMARK)
16855 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016857 if (match_pos != NULL)
16858 {
16859 /* Store the match cursor position */
16860 match_pos->lnum = pos.lnum;
16861 match_pos->col = pos.col + 1;
16862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 /* "/$" will put the cursor after the end of the line, may need to
16864 * correct that here */
16865 check_cursor();
16866 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016867
16868 /* If 'n' flag is used: restore cursor position. */
16869 if (flags & SP_NOMOVE)
16870 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016871 else
16872 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016873theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016875
16876 return retval;
16877}
16878
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016879#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016880
16881/*
16882 * round() is not in C90, use ceil() or floor() instead.
16883 */
16884 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016885vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016886{
16887 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16888}
16889
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016890/*
16891 * "round({float})" function
16892 */
16893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016894f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016895{
16896 float_T f;
16897
16898 rettv->v_type = VAR_FLOAT;
16899 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016900 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016901 else
16902 rettv->vval.v_float = 0.0;
16903}
16904#endif
16905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016906/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016907 * "screenattr()" function
16908 */
16909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016910f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016911{
16912 int row;
16913 int col;
16914 int c;
16915
16916 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16917 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16918 if (row < 0 || row >= screen_Rows
16919 || col < 0 || col >= screen_Columns)
16920 c = -1;
16921 else
16922 c = ScreenAttrs[LineOffset[row] + col];
16923 rettv->vval.v_number = c;
16924}
16925
16926/*
16927 * "screenchar()" function
16928 */
16929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016930f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016931{
16932 int row;
16933 int col;
16934 int off;
16935 int c;
16936
16937 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16938 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16939 if (row < 0 || row >= screen_Rows
16940 || col < 0 || col >= screen_Columns)
16941 c = -1;
16942 else
16943 {
16944 off = LineOffset[row] + col;
16945#ifdef FEAT_MBYTE
16946 if (enc_utf8 && ScreenLinesUC[off] != 0)
16947 c = ScreenLinesUC[off];
16948 else
16949#endif
16950 c = ScreenLines[off];
16951 }
16952 rettv->vval.v_number = c;
16953}
16954
16955/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016956 * "screencol()" function
16957 *
16958 * First column is 1 to be consistent with virtcol().
16959 */
16960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016961f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016962{
16963 rettv->vval.v_number = screen_screencol() + 1;
16964}
16965
16966/*
16967 * "screenrow()" function
16968 */
16969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016970f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016971{
16972 rettv->vval.v_number = screen_screenrow() + 1;
16973}
16974
16975/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016976 * "search()" function
16977 */
16978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016979f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016980{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016981 int flags = 0;
16982
16983 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984}
16985
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016987 * "searchdecl()" function
16988 */
16989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016990f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016991{
16992 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016993 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016994 int error = FALSE;
16995 char_u *name;
16996
16997 rettv->vval.v_number = 1; /* default: FAIL */
16998
16999 name = get_tv_string_chk(&argvars[0]);
17000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017001 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017002 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017003 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17004 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17005 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017006 if (!error && name != NULL)
17007 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017008 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017009}
17010
17011/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017012 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017015searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016{
17017 char_u *spat, *mpat, *epat;
17018 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 int dir;
17021 int flags = 0;
17022 char_u nbuf1[NUMBUFLEN];
17023 char_u nbuf2[NUMBUFLEN];
17024 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017025 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017026 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017027 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017030 spat = get_tv_string_chk(&argvars[0]);
17031 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17032 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17033 if (spat == NULL || mpat == NULL || epat == NULL)
17034 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 /* Handle the optional fourth argument: flags */
17037 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017038 if (dir == 0)
17039 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017040
17041 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017042 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17043 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017044 if ((flags & (SP_END | SP_SUBPAT)) != 0
17045 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017046 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017047 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017048 goto theend;
17049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017051 /* Using 'r' implies 'W', otherwise it doesn't work. */
17052 if (flags & SP_REPEAT)
17053 p_ws = FALSE;
17054
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017055 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017056 if (argvars[3].v_type == VAR_UNKNOWN
17057 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 skip = (char_u *)"";
17059 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017061 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017062 if (argvars[5].v_type != VAR_UNKNOWN)
17063 {
17064 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17065 if (lnum_stop < 0)
17066 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017067#ifdef FEAT_RELTIME
17068 if (argvars[6].v_type != VAR_UNKNOWN)
17069 {
17070 time_limit = get_tv_number_chk(&argvars[6], NULL);
17071 if (time_limit < 0)
17072 goto theend;
17073 }
17074#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017075 }
17076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077 if (skip == NULL)
17078 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017080 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017081 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017082
17083theend:
17084 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017085
17086 return retval;
17087}
17088
17089/*
17090 * "searchpair()" function
17091 */
17092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017093f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017094{
17095 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17096}
17097
17098/*
17099 * "searchpairpos()" function
17100 */
17101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017102f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017103{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017104 pos_T match_pos;
17105 int lnum = 0;
17106 int col = 0;
17107
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017108 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017109 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017110
17111 if (searchpair_cmn(argvars, &match_pos) > 0)
17112 {
17113 lnum = match_pos.lnum;
17114 col = match_pos.col;
17115 }
17116
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017117 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17118 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017119}
17120
17121/*
17122 * Search for a start/middle/end thing.
17123 * Used by searchpair(), see its documentation for the details.
17124 * Returns 0 or -1 for no match,
17125 */
17126 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017127do_searchpair(
17128 char_u *spat, /* start pattern */
17129 char_u *mpat, /* middle pattern */
17130 char_u *epat, /* end pattern */
17131 int dir, /* BACKWARD or FORWARD */
17132 char_u *skip, /* skip expression */
17133 int flags, /* SP_SETPCMARK and other SP_ values */
17134 pos_T *match_pos,
17135 linenr_T lnum_stop, /* stop at this line if not zero */
17136 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017137{
17138 char_u *save_cpo;
17139 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17140 long retval = 0;
17141 pos_T pos;
17142 pos_T firstpos;
17143 pos_T foundpos;
17144 pos_T save_cursor;
17145 pos_T save_pos;
17146 int n;
17147 int r;
17148 int nest = 1;
17149 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017150 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017151 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017152
17153 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17154 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017155 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017156
Bram Moolenaar76929292008-01-06 19:07:36 +000017157#ifdef FEAT_RELTIME
17158 /* Set the time limit, if there is one. */
17159 profile_setlimit(time_limit, &tm);
17160#endif
17161
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017162 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17163 * start/middle/end (pat3, for the top pair). */
17164 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17165 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17166 if (pat2 == NULL || pat3 == NULL)
17167 goto theend;
17168 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17169 if (*mpat == NUL)
17170 STRCPY(pat3, pat2);
17171 else
17172 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17173 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017174 if (flags & SP_START)
17175 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017176
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 save_cursor = curwin->w_cursor;
17178 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017179 clearpos(&firstpos);
17180 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 pat = pat3;
17182 for (;;)
17183 {
17184 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017185 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17187 /* didn't find it or found the first match again: FAIL */
17188 break;
17189
17190 if (firstpos.lnum == 0)
17191 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017192 if (equalpos(pos, foundpos))
17193 {
17194 /* Found the same position again. Can happen with a pattern that
17195 * has "\zs" at the end and searching backwards. Advance one
17196 * character and try again. */
17197 if (dir == BACKWARD)
17198 decl(&pos);
17199 else
17200 incl(&pos);
17201 }
17202 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017204 /* clear the start flag to avoid getting stuck here */
17205 options &= ~SEARCH_START;
17206
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 /* If the skip pattern matches, ignore this match. */
17208 if (*skip != NUL)
17209 {
17210 save_pos = curwin->w_cursor;
17211 curwin->w_cursor = pos;
17212 r = eval_to_bool(skip, &err, NULL, FALSE);
17213 curwin->w_cursor = save_pos;
17214 if (err)
17215 {
17216 /* Evaluating {skip} caused an error, break here. */
17217 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017218 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 break;
17220 }
17221 if (r)
17222 continue;
17223 }
17224
17225 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17226 {
17227 /* Found end when searching backwards or start when searching
17228 * forward: nested pair. */
17229 ++nest;
17230 pat = pat2; /* nested, don't search for middle */
17231 }
17232 else
17233 {
17234 /* Found end when searching forward or start when searching
17235 * backward: end of (nested) pair; or found middle in outer pair. */
17236 if (--nest == 1)
17237 pat = pat3; /* outer level, search for middle */
17238 }
17239
17240 if (nest == 0)
17241 {
17242 /* Found the match: return matchcount or line number. */
17243 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017244 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017246 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017247 if (flags & SP_SETPCMARK)
17248 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 curwin->w_cursor = pos;
17250 if (!(flags & SP_REPEAT))
17251 break;
17252 nest = 1; /* search for next unmatched */
17253 }
17254 }
17255
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017256 if (match_pos != NULL)
17257 {
17258 /* Store the match cursor position */
17259 match_pos->lnum = curwin->w_cursor.lnum;
17260 match_pos->col = curwin->w_cursor.col + 1;
17261 }
17262
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017264 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 curwin->w_cursor = save_cursor;
17266
17267theend:
17268 vim_free(pat2);
17269 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017270 if (p_cpo == empty_option)
17271 p_cpo = save_cpo;
17272 else
17273 /* Darn, evaluating the {skip} expression changed the value. */
17274 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017275
17276 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277}
17278
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017279/*
17280 * "searchpos()" function
17281 */
17282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017283f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017284{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017285 pos_T match_pos;
17286 int lnum = 0;
17287 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017288 int n;
17289 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017290
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017291 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017292 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017293
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017294 n = search_cmn(argvars, &match_pos, &flags);
17295 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017296 {
17297 lnum = match_pos.lnum;
17298 col = match_pos.col;
17299 }
17300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017301 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17302 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017303 if (flags & SP_SUBPAT)
17304 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017305}
17306
Bram Moolenaar0d660222005-01-07 21:51:51 +000017307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017308f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310#ifdef FEAT_CLIENTSERVER
17311 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017312 char_u *server = get_tv_string_chk(&argvars[0]);
17313 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314
Bram Moolenaar0d660222005-01-07 21:51:51 +000017315 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017316 if (server == NULL || reply == NULL)
17317 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318 if (check_restricted() || check_secure())
17319 return;
17320# ifdef FEAT_X11
17321 if (check_connection() == FAIL)
17322 return;
17323# endif
17324
17325 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017327 EMSG(_("E258: Unable to send to client"));
17328 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330 rettv->vval.v_number = 0;
17331#else
17332 rettv->vval.v_number = -1;
17333#endif
17334}
17335
Bram Moolenaar0d660222005-01-07 21:51:51 +000017336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017337f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017338{
17339 char_u *r = NULL;
17340
17341#ifdef FEAT_CLIENTSERVER
17342# ifdef WIN32
17343 r = serverGetVimNames();
17344# else
17345 make_connection();
17346 if (X_DISPLAY != NULL)
17347 r = serverGetVimNames(X_DISPLAY);
17348# endif
17349#endif
17350 rettv->v_type = VAR_STRING;
17351 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352}
17353
17354/*
17355 * "setbufvar()" function
17356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017358f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359{
17360 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017363 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 char_u nbuf[NUMBUFLEN];
17365
17366 if (check_restricted() || check_secure())
17367 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017368 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17369 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017370 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 varp = &argvars[2];
17372
17373 if (buf != NULL && varname != NULL && varp != NULL)
17374 {
17375 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377
17378 if (*varname == '&')
17379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017380 long numval;
17381 char_u *strval;
17382 int error = FALSE;
17383
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 numval = get_tv_number_chk(varp, &error);
17386 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017387 if (!error && strval != NULL)
17388 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 }
17390 else
17391 {
17392 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17393 if (bufvarname != NULL)
17394 {
17395 STRCPY(bufvarname, "b:");
17396 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017397 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 vim_free(bufvarname);
17399 }
17400 }
17401
17402 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405}
17406
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017408f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017409{
17410 dict_T *d;
17411 dictitem_T *di;
17412 char_u *csearch;
17413
17414 if (argvars[0].v_type != VAR_DICT)
17415 {
17416 EMSG(_(e_dictreq));
17417 return;
17418 }
17419
17420 if ((d = argvars[0].vval.v_dict) != NULL)
17421 {
17422 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17423 if (csearch != NULL)
17424 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017425#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017426 if (enc_utf8)
17427 {
17428 int pcc[MAX_MCO];
17429 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017430
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017431 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17432 }
17433 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017434#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017435 set_last_csearch(PTR2CHAR(csearch),
17436 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017437 }
17438
17439 di = dict_find(d, (char_u *)"forward", -1);
17440 if (di != NULL)
17441 set_csearch_direction(get_tv_number(&di->di_tv)
17442 ? FORWARD : BACKWARD);
17443
17444 di = dict_find(d, (char_u *)"until", -1);
17445 if (di != NULL)
17446 set_csearch_until(!!get_tv_number(&di->di_tv));
17447 }
17448}
17449
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450/*
17451 * "setcmdpos()" function
17452 */
17453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017454f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017456 int pos = (int)get_tv_number(&argvars[0]) - 1;
17457
17458 if (pos >= 0)
17459 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460}
17461
17462/*
17463 * "setline()" function
17464 */
17465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017466f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467{
17468 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017469 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017470 list_T *l = NULL;
17471 listitem_T *li = NULL;
17472 long added = 0;
17473 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017475 lnum = get_tv_lnum(&argvars[0]);
17476 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017478 l = argvars[1].vval.v_list;
17479 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017481 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017482 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017483
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017484 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017485 for (;;)
17486 {
17487 if (l != NULL)
17488 {
17489 /* list argument, get next string */
17490 if (li == NULL)
17491 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017492 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017493 li = li->li_next;
17494 }
17495
17496 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017497 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017498 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017499
17500 /* When coming here from Insert mode, sync undo, so that this can be
17501 * undone separately from what was previously inserted. */
17502 if (u_sync_once == 2)
17503 {
17504 u_sync_once = 1; /* notify that u_sync() was called */
17505 u_sync(TRUE);
17506 }
17507
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017508 if (lnum <= curbuf->b_ml.ml_line_count)
17509 {
17510 /* existing line, replace it */
17511 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17512 {
17513 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017514 if (lnum == curwin->w_cursor.lnum)
17515 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017516 rettv->vval.v_number = 0; /* OK */
17517 }
17518 }
17519 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17520 {
17521 /* lnum is one past the last line, append the line */
17522 ++added;
17523 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17524 rettv->vval.v_number = 0; /* OK */
17525 }
17526
17527 if (l == NULL) /* only one string argument */
17528 break;
17529 ++lnum;
17530 }
17531
17532 if (added > 0)
17533 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534}
17535
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017536static 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 +000017537
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017539 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017540 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017542set_qf_ll_list(
17543 win_T *wp UNUSED,
17544 typval_T *list_arg UNUSED,
17545 typval_T *action_arg UNUSED,
17546 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017547{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017548#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017549 char_u *act;
17550 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017551#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017552
Bram Moolenaar2641f772005-03-25 21:58:17 +000017553 rettv->vval.v_number = -1;
17554
17555#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017556 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017557 EMSG(_(e_listreq));
17558 else
17559 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017560 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017561
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017562 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017563 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017564 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017565 if (act == NULL)
17566 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017567 if (*act == 'a' || *act == 'r')
17568 action = *act;
17569 }
17570
Bram Moolenaar81484f42012-12-05 15:16:47 +010017571 if (l != NULL && set_errorlist(wp, l, action,
17572 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017573 rettv->vval.v_number = 0;
17574 }
17575#endif
17576}
17577
17578/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017579 * "setloclist()" function
17580 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017582f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017583{
17584 win_T *win;
17585
17586 rettv->vval.v_number = -1;
17587
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017588 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017589 if (win != NULL)
17590 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17591}
17592
17593/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017594 * "setmatches()" function
17595 */
17596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017597f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017598{
17599#ifdef FEAT_SEARCH_EXTRA
17600 list_T *l;
17601 listitem_T *li;
17602 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017603 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017604
17605 rettv->vval.v_number = -1;
17606 if (argvars[0].v_type != VAR_LIST)
17607 {
17608 EMSG(_(e_listreq));
17609 return;
17610 }
17611 if ((l = argvars[0].vval.v_list) != NULL)
17612 {
17613
17614 /* To some extent make sure that we are dealing with a list from
17615 * "getmatches()". */
17616 li = l->lv_first;
17617 while (li != NULL)
17618 {
17619 if (li->li_tv.v_type != VAR_DICT
17620 || (d = li->li_tv.vval.v_dict) == NULL)
17621 {
17622 EMSG(_(e_invarg));
17623 return;
17624 }
17625 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017626 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17627 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017628 && dict_find(d, (char_u *)"priority", -1) != NULL
17629 && dict_find(d, (char_u *)"id", -1) != NULL))
17630 {
17631 EMSG(_(e_invarg));
17632 return;
17633 }
17634 li = li->li_next;
17635 }
17636
17637 clear_matches(curwin);
17638 li = l->lv_first;
17639 while (li != NULL)
17640 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017641 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017642 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017643 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017644 char_u *group;
17645 int priority;
17646 int id;
17647 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017648
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017649 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017650 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17651 {
17652 if (s == NULL)
17653 {
17654 s = list_alloc();
17655 if (s == NULL)
17656 return;
17657 }
17658
17659 /* match from matchaddpos() */
17660 for (i = 1; i < 9; i++)
17661 {
17662 sprintf((char *)buf, (char *)"pos%d", i);
17663 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17664 {
17665 if (di->di_tv.v_type != VAR_LIST)
17666 return;
17667
17668 list_append_tv(s, &di->di_tv);
17669 s->lv_refcount++;
17670 }
17671 else
17672 break;
17673 }
17674 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017675
17676 group = get_dict_string(d, (char_u *)"group", FALSE);
17677 priority = (int)get_dict_number(d, (char_u *)"priority");
17678 id = (int)get_dict_number(d, (char_u *)"id");
17679 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17680 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17681 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017682 if (i == 0)
17683 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017684 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017685 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017686 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017687 }
17688 else
17689 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017690 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017691 list_unref(s);
17692 s = NULL;
17693 }
17694
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017695 li = li->li_next;
17696 }
17697 rettv->vval.v_number = 0;
17698 }
17699#endif
17700}
17701
17702/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017703 * "setpos()" function
17704 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017706f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017707{
17708 pos_T pos;
17709 int fnum;
17710 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017711 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017712
Bram Moolenaar08250432008-02-13 11:42:46 +000017713 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017714 name = get_tv_string_chk(argvars);
17715 if (name != NULL)
17716 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017717 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017718 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017719 if (--pos.col < 0)
17720 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017721 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017722 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017723 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017724 if (fnum == curbuf->b_fnum)
17725 {
17726 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017727 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017728 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017729 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017730 curwin->w_set_curswant = FALSE;
17731 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017732 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017733 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017734 }
17735 else
17736 EMSG(_(e_invarg));
17737 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017738 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17739 {
17740 /* set mark */
17741 if (setmark_pos(name[1], &pos, fnum) == OK)
17742 rettv->vval.v_number = 0;
17743 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017744 else
17745 EMSG(_(e_invarg));
17746 }
17747 }
17748}
17749
17750/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017751 * "setqflist()" function
17752 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017754f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017755{
17756 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17757}
17758
17759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 * "setreg()" function
17761 */
17762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017763f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764{
17765 int regname;
17766 char_u *strregname;
17767 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 int append;
17770 char_u yank_type;
17771 long block_len;
17772
17773 block_len = -1;
17774 yank_type = MAUTO;
17775 append = FALSE;
17776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017777 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017778 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017780 if (strregname == NULL)
17781 return; /* type error; errmsg already given */
17782 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 if (regname == 0 || regname == '@')
17784 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017786 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017788 stropt = get_tv_string_chk(&argvars[2]);
17789 if (stropt == NULL)
17790 return; /* type error */
17791 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 switch (*stropt)
17793 {
17794 case 'a': case 'A': /* append */
17795 append = TRUE;
17796 break;
17797 case 'v': case 'c': /* character-wise selection */
17798 yank_type = MCHAR;
17799 break;
17800 case 'V': case 'l': /* line-wise selection */
17801 yank_type = MLINE;
17802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 case 'b': case Ctrl_V: /* block-wise selection */
17804 yank_type = MBLOCK;
17805 if (VIM_ISDIGIT(stropt[1]))
17806 {
17807 ++stropt;
17808 block_len = getdigits(&stropt) - 1;
17809 --stropt;
17810 }
17811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 }
17813 }
17814
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017815 if (argvars[1].v_type == VAR_LIST)
17816 {
17817 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017818 char_u **allocval;
17819 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017820 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017821 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017822 int len = argvars[1].vval.v_list->lv_len;
17823 listitem_T *li;
17824
Bram Moolenaar7d647822014-04-05 21:28:56 +020017825 /* First half: use for pointers to result lines; second half: use for
17826 * pointers to allocated copies. */
17827 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017828 if (lstval == NULL)
17829 return;
17830 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017831 allocval = lstval + len + 2;
17832 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017833
17834 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17835 li = li->li_next)
17836 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017837 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017838 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017839 goto free_lstval;
17840 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017841 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017842 /* Need to make a copy, next get_tv_string_buf_chk() will
17843 * overwrite the string. */
17844 strval = vim_strsave(buf);
17845 if (strval == NULL)
17846 goto free_lstval;
17847 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017848 }
17849 *curval++ = strval;
17850 }
17851 *curval++ = NULL;
17852
17853 write_reg_contents_lst(regname, lstval, -1,
17854 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017855free_lstval:
17856 while (curallocval > allocval)
17857 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017858 vim_free(lstval);
17859 }
17860 else
17861 {
17862 strval = get_tv_string_chk(&argvars[1]);
17863 if (strval == NULL)
17864 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017865 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017868 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869}
17870
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017871/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017872 * "settabvar()" function
17873 */
17874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017875f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017876{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017877#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017878 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017879 tabpage_T *tp;
17880#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017881 char_u *varname, *tabvarname;
17882 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017883
17884 rettv->vval.v_number = 0;
17885
17886 if (check_restricted() || check_secure())
17887 return;
17888
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017889#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017890 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017891#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017892 varname = get_tv_string_chk(&argvars[1]);
17893 varp = &argvars[2];
17894
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017895 if (varname != NULL && varp != NULL
17896#ifdef FEAT_WINDOWS
17897 && tp != NULL
17898#endif
17899 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017900 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017901#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017902 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017903 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017904#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017905
17906 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17907 if (tabvarname != NULL)
17908 {
17909 STRCPY(tabvarname, "t:");
17910 STRCPY(tabvarname + 2, varname);
17911 set_var(tabvarname, varp, TRUE);
17912 vim_free(tabvarname);
17913 }
17914
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017915#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017916 /* Restore current tabpage */
17917 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017918 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017919#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017920 }
17921}
17922
17923/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017924 * "settabwinvar()" function
17925 */
17926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017927f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017928{
17929 setwinvar(argvars, rettv, 1);
17930}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931
17932/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017933 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017936f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017938 setwinvar(argvars, rettv, 0);
17939}
17940
17941/*
17942 * "setwinvar()" and "settabwinvar()" functions
17943 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017944
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017946setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017947{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 win_T *win;
17949#ifdef FEAT_WINDOWS
17950 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017951 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017952 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953#endif
17954 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017957 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958
17959 if (check_restricted() || check_secure())
17960 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017961
17962#ifdef FEAT_WINDOWS
17963 if (off == 1)
17964 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17965 else
17966 tp = curtab;
17967#endif
17968 win = find_win_by_nr(&argvars[off], tp);
17969 varname = get_tv_string_chk(&argvars[off + 1]);
17970 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971
17972 if (win != NULL && varname != NULL && varp != NULL)
17973 {
17974#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017975 need_switch_win = !(tp == curtab && win == curwin);
17976 if (!need_switch_win
17977 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017980 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017982 long numval;
17983 char_u *strval;
17984 int error = FALSE;
17985
17986 ++varname;
17987 numval = get_tv_number_chk(varp, &error);
17988 strval = get_tv_string_buf_chk(varp, nbuf);
17989 if (!error && strval != NULL)
17990 set_option_value(varname, numval, strval, OPT_LOCAL);
17991 }
17992 else
17993 {
17994 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17995 if (winvarname != NULL)
17996 {
17997 STRCPY(winvarname, "w:");
17998 STRCPY(winvarname + 2, varname);
17999 set_var(winvarname, varp, TRUE);
18000 vim_free(winvarname);
18001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 }
18003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018005 if (need_switch_win)
18006 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007#endif
18008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009}
18010
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018011#ifdef FEAT_CRYPT
18012/*
18013 * "sha256({string})" function
18014 */
18015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018016f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018017{
18018 char_u *p;
18019
18020 p = get_tv_string(&argvars[0]);
18021 rettv->vval.v_string = vim_strsave(
18022 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18023 rettv->v_type = VAR_STRING;
18024}
18025#endif /* FEAT_CRYPT */
18026
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018028 * "shellescape({string})" function
18029 */
18030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018031f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018032{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018033 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018034 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018035 rettv->v_type = VAR_STRING;
18036}
18037
18038/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018039 * shiftwidth() function
18040 */
18041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018042f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018043{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018044 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018045}
18046
18047/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018048 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 */
18050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018051f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018053 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054
Bram Moolenaar0d660222005-01-07 21:51:51 +000018055 p = get_tv_string(&argvars[0]);
18056 rettv->vval.v_string = vim_strsave(p);
18057 simplify_filename(rettv->vval.v_string); /* simplify in place */
18058 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059}
18060
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018061#ifdef FEAT_FLOAT
18062/*
18063 * "sin()" function
18064 */
18065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018066f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018067{
18068 float_T f;
18069
18070 rettv->v_type = VAR_FLOAT;
18071 if (get_float_arg(argvars, &f) == OK)
18072 rettv->vval.v_float = sin(f);
18073 else
18074 rettv->vval.v_float = 0.0;
18075}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018076
18077/*
18078 * "sinh()" function
18079 */
18080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018081f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018082{
18083 float_T f;
18084
18085 rettv->v_type = VAR_FLOAT;
18086 if (get_float_arg(argvars, &f) == OK)
18087 rettv->vval.v_float = sinh(f);
18088 else
18089 rettv->vval.v_float = 0.0;
18090}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018091#endif
18092
Bram Moolenaar0d660222005-01-07 21:51:51 +000018093static int
18094#ifdef __BORLANDC__
18095 _RTLENTRYF
18096#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018097 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018098static int
18099#ifdef __BORLANDC__
18100 _RTLENTRYF
18101#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018102 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018103
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018104/* struct used in the array that's given to qsort() */
18105typedef struct
18106{
18107 listitem_T *item;
18108 int idx;
18109} sortItem_T;
18110
Bram Moolenaar0d660222005-01-07 21:51:51 +000018111static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018112static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018113static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018114#ifdef FEAT_FLOAT
18115static int item_compare_float;
18116#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018117static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018118static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018119static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018120static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018121static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018122#define ITEM_COMPARE_FAIL 999
18123
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018125 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018127 static int
18128#ifdef __BORLANDC__
18129_RTLENTRYF
18130#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018131item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018133 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018134 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018135 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018136 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018137 int res;
18138 char_u numbuf1[NUMBUFLEN];
18139 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018141 si1 = (sortItem_T *)s1;
18142 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018143 tv1 = &si1->item->li_tv;
18144 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018145
18146 if (item_compare_numbers)
18147 {
18148 long v1 = get_tv_number(tv1);
18149 long v2 = get_tv_number(tv2);
18150
18151 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18152 }
18153
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018154#ifdef FEAT_FLOAT
18155 if (item_compare_float)
18156 {
18157 float_T v1 = get_tv_float(tv1);
18158 float_T v2 = get_tv_float(tv2);
18159
18160 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18161 }
18162#endif
18163
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018164 /* tv2string() puts quotes around a string and allocates memory. Don't do
18165 * that for string variables. Use a single quote when comparing with a
18166 * non-string to do what the docs promise. */
18167 if (tv1->v_type == VAR_STRING)
18168 {
18169 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18170 p1 = (char_u *)"'";
18171 else
18172 p1 = tv1->vval.v_string;
18173 }
18174 else
18175 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18176 if (tv2->v_type == VAR_STRING)
18177 {
18178 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18179 p2 = (char_u *)"'";
18180 else
18181 p2 = tv2->vval.v_string;
18182 }
18183 else
18184 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018185 if (p1 == NULL)
18186 p1 = (char_u *)"";
18187 if (p2 == NULL)
18188 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018189 if (!item_compare_numeric)
18190 {
18191 if (item_compare_ic)
18192 res = STRICMP(p1, p2);
18193 else
18194 res = STRCMP(p1, p2);
18195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018197 {
18198 double n1, n2;
18199 n1 = strtod((char *)p1, (char **)&p1);
18200 n2 = strtod((char *)p2, (char **)&p2);
18201 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18202 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018203
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018204 /* When the result would be zero, compare the item indexes. Makes the
18205 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018206 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018207 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018208
Bram Moolenaar0d660222005-01-07 21:51:51 +000018209 vim_free(tofree1);
18210 vim_free(tofree2);
18211 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212}
18213
18214 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018215#ifdef __BORLANDC__
18216_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018218item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018219{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018220 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018222 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018223 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018226 /* shortcut after failure in previous call; compare all items equal */
18227 if (item_compare_func_err)
18228 return 0;
18229
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018230 si1 = (sortItem_T *)s1;
18231 si2 = (sortItem_T *)s2;
18232
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018233 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018234 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018235 copy_tv(&si1->item->li_tv, &argv[0]);
18236 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237
18238 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018239 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018240 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18241 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 clear_tv(&argv[0]);
18243 clear_tv(&argv[1]);
18244
18245 if (res == FAIL)
18246 res = ITEM_COMPARE_FAIL;
18247 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018248 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18249 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018250 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018252
18253 /* When the result would be zero, compare the pointers themselves. Makes
18254 * the sort stable. */
18255 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018256 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018257
Bram Moolenaar0d660222005-01-07 21:51:51 +000018258 return res;
18259}
18260
18261/*
18262 * "sort({list})" function
18263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018265do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266{
Bram Moolenaar33570922005-01-25 22:26:29 +000018267 list_T *l;
18268 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018269 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018270 long len;
18271 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018274 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 else
18276 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018277 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018278 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018279 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18280 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 return;
18282 rettv->vval.v_list = l;
18283 rettv->v_type = VAR_LIST;
18284 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285
Bram Moolenaar0d660222005-01-07 21:51:51 +000018286 len = list_len(l);
18287 if (len <= 1)
18288 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018291 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018292 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018293#ifdef FEAT_FLOAT
18294 item_compare_float = FALSE;
18295#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018296 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018297 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018298 if (argvars[1].v_type != VAR_UNKNOWN)
18299 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018300 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018301 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018302 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018303 else
18304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018305 int error = FALSE;
18306
18307 i = get_tv_number_chk(&argvars[1], &error);
18308 if (error)
18309 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018310 if (i == 1)
18311 item_compare_ic = TRUE;
18312 else
18313 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018314 if (item_compare_func != NULL)
18315 {
18316 if (STRCMP(item_compare_func, "n") == 0)
18317 {
18318 item_compare_func = NULL;
18319 item_compare_numeric = TRUE;
18320 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018321 else if (STRCMP(item_compare_func, "N") == 0)
18322 {
18323 item_compare_func = NULL;
18324 item_compare_numbers = TRUE;
18325 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018326#ifdef FEAT_FLOAT
18327 else if (STRCMP(item_compare_func, "f") == 0)
18328 {
18329 item_compare_func = NULL;
18330 item_compare_float = TRUE;
18331 }
18332#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018333 else if (STRCMP(item_compare_func, "i") == 0)
18334 {
18335 item_compare_func = NULL;
18336 item_compare_ic = TRUE;
18337 }
18338 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018339 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018340
18341 if (argvars[2].v_type != VAR_UNKNOWN)
18342 {
18343 /* optional third argument: {dict} */
18344 if (argvars[2].v_type != VAR_DICT)
18345 {
18346 EMSG(_(e_dictreq));
18347 return;
18348 }
18349 item_compare_selfdict = argvars[2].vval.v_dict;
18350 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352
Bram Moolenaar0d660222005-01-07 21:51:51 +000018353 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018354 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018355 if (ptrs == NULL)
18356 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357
Bram Moolenaar327aa022014-03-25 18:24:23 +010018358 i = 0;
18359 if (sort)
18360 {
18361 /* sort(): ptrs will be the list to sort */
18362 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018363 {
18364 ptrs[i].item = li;
18365 ptrs[i].idx = i;
18366 ++i;
18367 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018368
18369 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018370 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018371 /* test the compare function */
18372 if (item_compare_func != NULL
18373 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018374 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018375 EMSG(_("E702: Sort compare function failed"));
18376 else
18377 {
18378 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018379 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018380 item_compare_func == NULL ? item_compare : item_compare2);
18381
18382 if (!item_compare_func_err)
18383 {
18384 /* Clear the List and append the items in sorted order. */
18385 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18386 l->lv_len = 0;
18387 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018388 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018389 }
18390 }
18391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018393 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018394 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018395
18396 /* f_uniq(): ptrs will be a stack of items to remove */
18397 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018398 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018399 item_compare_func_ptr = item_compare_func
18400 ? item_compare2 : item_compare;
18401
18402 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18403 li = li->li_next)
18404 {
18405 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18406 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018407 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018408 if (item_compare_func_err)
18409 {
18410 EMSG(_("E882: Uniq compare function failed"));
18411 break;
18412 }
18413 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018415 if (!item_compare_func_err)
18416 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018417 while (--i >= 0)
18418 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018419 li = ptrs[i].item->li_next;
18420 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018421 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018422 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018423 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018424 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018425 list_fix_watch(l, li);
18426 listitem_free(li);
18427 l->lv_len--;
18428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018429 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018430 }
18431
18432 vim_free(ptrs);
18433 }
18434}
18435
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018436/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018437 * "sort({list})" function
18438 */
18439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018440f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018441{
18442 do_sort_uniq(argvars, rettv, TRUE);
18443}
18444
18445/*
18446 * "uniq({list})" function
18447 */
18448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018449f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018450{
18451 do_sort_uniq(argvars, rettv, FALSE);
18452}
18453
18454/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018455 * "soundfold({word})" function
18456 */
18457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018458f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018459{
18460 char_u *s;
18461
18462 rettv->v_type = VAR_STRING;
18463 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018464#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018465 rettv->vval.v_string = eval_soundfold(s);
18466#else
18467 rettv->vval.v_string = vim_strsave(s);
18468#endif
18469}
18470
18471/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018472 * "spellbadword()" function
18473 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018475f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018476{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018477 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018478 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018479 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018480
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018481 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018482 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018483
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018484#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018485 if (argvars[0].v_type == VAR_UNKNOWN)
18486 {
18487 /* Find the start and length of the badly spelled word. */
18488 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18489 if (len != 0)
18490 word = ml_get_cursor();
18491 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018492 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018493 {
18494 char_u *str = get_tv_string_chk(&argvars[0]);
18495 int capcol = -1;
18496
18497 if (str != NULL)
18498 {
18499 /* Check the argument for spelling. */
18500 while (*str != NUL)
18501 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018502 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018503 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018504 {
18505 word = str;
18506 break;
18507 }
18508 str += len;
18509 }
18510 }
18511 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018512#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018513
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018514 list_append_string(rettv->vval.v_list, word, len);
18515 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018516 attr == HLF_SPB ? "bad" :
18517 attr == HLF_SPR ? "rare" :
18518 attr == HLF_SPL ? "local" :
18519 attr == HLF_SPC ? "caps" :
18520 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018521}
18522
18523/*
18524 * "spellsuggest()" function
18525 */
18526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018527f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018528{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018529#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018530 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018531 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018532 int maxcount;
18533 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018534 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018535 listitem_T *li;
18536 int need_capital = FALSE;
18537#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018538
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018539 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018540 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018541
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018542#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018543 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018544 {
18545 str = get_tv_string(&argvars[0]);
18546 if (argvars[1].v_type != VAR_UNKNOWN)
18547 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018548 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018549 if (maxcount <= 0)
18550 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018551 if (argvars[2].v_type != VAR_UNKNOWN)
18552 {
18553 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18554 if (typeerr)
18555 return;
18556 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018557 }
18558 else
18559 maxcount = 25;
18560
Bram Moolenaar4770d092006-01-12 23:22:24 +000018561 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018562
18563 for (i = 0; i < ga.ga_len; ++i)
18564 {
18565 str = ((char_u **)ga.ga_data)[i];
18566
18567 li = listitem_alloc();
18568 if (li == NULL)
18569 vim_free(str);
18570 else
18571 {
18572 li->li_tv.v_type = VAR_STRING;
18573 li->li_tv.v_lock = 0;
18574 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018575 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018576 }
18577 }
18578 ga_clear(&ga);
18579 }
18580#endif
18581}
18582
Bram Moolenaar0d660222005-01-07 21:51:51 +000018583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018584f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585{
18586 char_u *str;
18587 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018588 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018589 regmatch_T regmatch;
18590 char_u patbuf[NUMBUFLEN];
18591 char_u *save_cpo;
18592 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018593 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018594 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018595 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018596
18597 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18598 save_cpo = p_cpo;
18599 p_cpo = (char_u *)"";
18600
18601 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018602 if (argvars[1].v_type != VAR_UNKNOWN)
18603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18605 if (pat == NULL)
18606 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018607 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018608 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018609 }
18610 if (pat == NULL || *pat == NUL)
18611 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018612
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018613 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018615 if (typeerr)
18616 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617
Bram Moolenaar0d660222005-01-07 21:51:51 +000018618 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18619 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018621 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018622 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018623 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018624 if (*str == NUL)
18625 match = FALSE; /* empty item at the end */
18626 else
18627 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018628 if (match)
18629 end = regmatch.startp[0];
18630 else
18631 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018632 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18633 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018635 if (list_append_string(rettv->vval.v_list, str,
18636 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018638 }
18639 if (!match)
18640 break;
18641 /* Advance to just after the match. */
18642 if (regmatch.endp[0] > str)
18643 col = 0;
18644 else
18645 {
18646 /* Don't get stuck at the same match. */
18647#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018648 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018649#else
18650 col = 1;
18651#endif
18652 }
18653 str = regmatch.endp[0];
18654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655
Bram Moolenaar473de612013-06-08 18:19:48 +020018656 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658
Bram Moolenaar0d660222005-01-07 21:51:51 +000018659 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660}
18661
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018662#ifdef FEAT_FLOAT
18663/*
18664 * "sqrt()" function
18665 */
18666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018667f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018668{
18669 float_T f;
18670
18671 rettv->v_type = VAR_FLOAT;
18672 if (get_float_arg(argvars, &f) == OK)
18673 rettv->vval.v_float = sqrt(f);
18674 else
18675 rettv->vval.v_float = 0.0;
18676}
18677
18678/*
18679 * "str2float()" function
18680 */
18681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018682f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018683{
18684 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18685
18686 if (*p == '+')
18687 p = skipwhite(p + 1);
18688 (void)string2float(p, &rettv->vval.v_float);
18689 rettv->v_type = VAR_FLOAT;
18690}
18691#endif
18692
Bram Moolenaar2c932302006-03-18 21:42:09 +000018693/*
18694 * "str2nr()" function
18695 */
18696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018697f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018698{
18699 int base = 10;
18700 char_u *p;
18701 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018702 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018703
18704 if (argvars[1].v_type != VAR_UNKNOWN)
18705 {
18706 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018707 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018708 {
18709 EMSG(_(e_invarg));
18710 return;
18711 }
18712 }
18713
18714 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018715 if (*p == '+')
18716 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018717 switch (base)
18718 {
18719 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18720 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18721 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18722 default: what = 0;
18723 }
18724 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018725 rettv->vval.v_number = n;
18726}
18727
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728#ifdef HAVE_STRFTIME
18729/*
18730 * "strftime({format}[, {time}])" function
18731 */
18732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018733f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
18735 char_u result_buf[256];
18736 struct tm *curtime;
18737 time_t seconds;
18738 char_u *p;
18739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018743 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 seconds = time(NULL);
18745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018746 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 curtime = localtime(&seconds);
18748 /* MSVC returns NULL for an invalid value of seconds. */
18749 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 else
18752 {
18753# ifdef FEAT_MBYTE
18754 vimconv_T conv;
18755 char_u *enc;
18756
18757 conv.vc_type = CONV_NONE;
18758 enc = enc_locale();
18759 convert_setup(&conv, p_enc, enc);
18760 if (conv.vc_type != CONV_NONE)
18761 p = string_convert(&conv, p, NULL);
18762# endif
18763 if (p != NULL)
18764 (void)strftime((char *)result_buf, sizeof(result_buf),
18765 (char *)p, curtime);
18766 else
18767 result_buf[0] = NUL;
18768
18769# ifdef FEAT_MBYTE
18770 if (conv.vc_type != CONV_NONE)
18771 vim_free(p);
18772 convert_setup(&conv, enc, p_enc);
18773 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 else
18776# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018777 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778
18779# ifdef FEAT_MBYTE
18780 /* Release conversion descriptors */
18781 convert_setup(&conv, NULL, NULL);
18782 vim_free(enc);
18783# endif
18784 }
18785}
18786#endif
18787
18788/*
18789 * "stridx()" function
18790 */
18791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018792f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793{
18794 char_u buf[NUMBUFLEN];
18795 char_u *needle;
18796 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018797 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018801 needle = get_tv_string_chk(&argvars[1]);
18802 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018803 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018804 if (needle == NULL || haystack == NULL)
18805 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806
Bram Moolenaar33570922005-01-25 22:26:29 +000018807 if (argvars[2].v_type != VAR_UNKNOWN)
18808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018809 int error = FALSE;
18810
18811 start_idx = get_tv_number_chk(&argvars[2], &error);
18812 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018813 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018814 if (start_idx >= 0)
18815 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 }
18817
18818 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18819 if (pos != NULL)
18820 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821}
18822
18823/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018824 * "string()" function
18825 */
18826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018827f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018828{
18829 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018830 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018832 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018833 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018834 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018835 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018836 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837}
18838
18839/*
18840 * "strlen()" function
18841 */
18842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018843f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018845 rettv->vval.v_number = (varnumber_T)(STRLEN(
18846 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847}
18848
18849/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018850 * "strchars()" function
18851 */
18852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018853f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018854{
18855 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018856 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018857#ifdef FEAT_MBYTE
18858 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018859 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018860#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018861
18862 if (argvars[1].v_type != VAR_UNKNOWN)
18863 skipcc = get_tv_number_chk(&argvars[1], NULL);
18864 if (skipcc < 0 || skipcc > 1)
18865 EMSG(_(e_invarg));
18866 else
18867 {
18868#ifdef FEAT_MBYTE
18869 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18870 while (*s != NUL)
18871 {
18872 func_mb_ptr2char_adv(&s);
18873 ++len;
18874 }
18875 rettv->vval.v_number = len;
18876#else
18877 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18878#endif
18879 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018880}
18881
18882/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018883 * "strdisplaywidth()" function
18884 */
18885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018886f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018887{
18888 char_u *s = get_tv_string(&argvars[0]);
18889 int col = 0;
18890
18891 if (argvars[1].v_type != VAR_UNKNOWN)
18892 col = get_tv_number(&argvars[1]);
18893
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018894 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018895}
18896
18897/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018898 * "strwidth()" function
18899 */
18900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018901f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018902{
18903 char_u *s = get_tv_string(&argvars[0]);
18904
18905 rettv->vval.v_number = (varnumber_T)(
18906#ifdef FEAT_MBYTE
18907 mb_string2cells(s, -1)
18908#else
18909 STRLEN(s)
18910#endif
18911 );
18912}
18913
18914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 * "strpart()" function
18916 */
18917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018918f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919{
18920 char_u *p;
18921 int n;
18922 int len;
18923 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018924 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018926 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 slen = (int)STRLEN(p);
18928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018929 n = get_tv_number_chk(&argvars[1], &error);
18930 if (error)
18931 len = 0;
18932 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018933 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 else
18935 len = slen - n; /* default len: all bytes that are available. */
18936
18937 /*
18938 * Only return the overlap between the specified part and the actual
18939 * string.
18940 */
18941 if (n < 0)
18942 {
18943 len += n;
18944 n = 0;
18945 }
18946 else if (n > slen)
18947 n = slen;
18948 if (len < 0)
18949 len = 0;
18950 else if (n + len > slen)
18951 len = slen - n;
18952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018953 rettv->v_type = VAR_STRING;
18954 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955}
18956
18957/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 * "strridx()" function
18959 */
18960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018961f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018962{
18963 char_u buf[NUMBUFLEN];
18964 char_u *needle;
18965 char_u *haystack;
18966 char_u *rest;
18967 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018968 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018970 needle = get_tv_string_chk(&argvars[1]);
18971 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018972
18973 rettv->vval.v_number = -1;
18974 if (needle == NULL || haystack == NULL)
18975 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018976
18977 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018978 if (argvars[2].v_type != VAR_UNKNOWN)
18979 {
18980 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018981 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018982 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018983 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018984 }
18985 else
18986 end_idx = haystack_len;
18987
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018989 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018990 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018991 lastmatch = haystack + end_idx;
18992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018994 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018995 for (rest = haystack; *rest != '\0'; ++rest)
18996 {
18997 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018998 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 break;
19000 lastmatch = rest;
19001 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019003
19004 if (lastmatch == NULL)
19005 rettv->vval.v_number = -1;
19006 else
19007 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19008}
19009
19010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 * "strtrans()" function
19012 */
19013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019014f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019016 rettv->v_type = VAR_STRING;
19017 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018}
19019
19020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021 * "submatch()" function
19022 */
19023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019024f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019025{
Bram Moolenaar41571762014-04-02 19:00:58 +020019026 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019027 int no;
19028 int retList = 0;
19029
19030 no = (int)get_tv_number_chk(&argvars[0], &error);
19031 if (error)
19032 return;
19033 error = FALSE;
19034 if (argvars[1].v_type != VAR_UNKNOWN)
19035 retList = get_tv_number_chk(&argvars[1], &error);
19036 if (error)
19037 return;
19038
19039 if (retList == 0)
19040 {
19041 rettv->v_type = VAR_STRING;
19042 rettv->vval.v_string = reg_submatch(no);
19043 }
19044 else
19045 {
19046 rettv->v_type = VAR_LIST;
19047 rettv->vval.v_list = reg_submatch_list(no);
19048 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019049}
19050
19051/*
19052 * "substitute()" function
19053 */
19054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019055f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056{
19057 char_u patbuf[NUMBUFLEN];
19058 char_u subbuf[NUMBUFLEN];
19059 char_u flagsbuf[NUMBUFLEN];
19060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019061 char_u *str = get_tv_string_chk(&argvars[0]);
19062 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19063 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19064 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19065
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019067 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19068 rettv->vval.v_string = NULL;
19069 else
19070 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019071}
19072
19073/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019074 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019077f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078{
19079 int id = 0;
19080#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019081 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 long col;
19083 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019084 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019086 lnum = get_tv_lnum(argvars); /* -1 on type error */
19087 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19088 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019090 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019091 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019092 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093#endif
19094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096}
19097
19098/*
19099 * "synIDattr(id, what [, mode])" function
19100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019102f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103{
19104 char_u *p = NULL;
19105#ifdef FEAT_SYN_HL
19106 int id;
19107 char_u *what;
19108 char_u *mode;
19109 char_u modebuf[NUMBUFLEN];
19110 int modec;
19111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019112 id = get_tv_number(&argvars[0]);
19113 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019114 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019116 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019118 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 modec = 0; /* replace invalid with current */
19120 }
19121 else
19122 {
19123#ifdef FEAT_GUI
19124 if (gui.in_use)
19125 modec = 'g';
19126 else
19127#endif
19128 if (t_colors > 1)
19129 modec = 'c';
19130 else
19131 modec = 't';
19132 }
19133
19134
19135 switch (TOLOWER_ASC(what[0]))
19136 {
19137 case 'b':
19138 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19139 p = highlight_color(id, what, modec);
19140 else /* bold */
19141 p = highlight_has_attr(id, HL_BOLD, modec);
19142 break;
19143
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019144 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 p = highlight_color(id, what, modec);
19146 break;
19147
19148 case 'i':
19149 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19150 p = highlight_has_attr(id, HL_INVERSE, modec);
19151 else /* italic */
19152 p = highlight_has_attr(id, HL_ITALIC, modec);
19153 break;
19154
19155 case 'n': /* name */
19156 p = get_highlight_name(NULL, id - 1);
19157 break;
19158
19159 case 'r': /* reverse */
19160 p = highlight_has_attr(id, HL_INVERSE, modec);
19161 break;
19162
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019163 case 's':
19164 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19165 p = highlight_color(id, what, modec);
19166 else /* standout */
19167 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 break;
19169
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019170 case 'u':
19171 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19172 /* underline */
19173 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19174 else
19175 /* undercurl */
19176 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 break;
19178 }
19179
19180 if (p != NULL)
19181 p = vim_strsave(p);
19182#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 rettv->v_type = VAR_STRING;
19184 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185}
19186
19187/*
19188 * "synIDtrans(id)" function
19189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019191f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192{
19193 int id;
19194
19195#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019196 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197
19198 if (id > 0)
19199 id = syn_get_final_id(id);
19200 else
19201#endif
19202 id = 0;
19203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205}
19206
19207/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019208 * "synconcealed(lnum, col)" function
19209 */
19210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019211f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019212{
19213#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19214 long lnum;
19215 long col;
19216 int syntax_flags = 0;
19217 int cchar;
19218 int matchid = 0;
19219 char_u str[NUMBUFLEN];
19220#endif
19221
19222 rettv->v_type = VAR_LIST;
19223 rettv->vval.v_list = NULL;
19224
19225#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19226 lnum = get_tv_lnum(argvars); /* -1 on type error */
19227 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19228
19229 vim_memset(str, NUL, sizeof(str));
19230
19231 if (rettv_list_alloc(rettv) != FAIL)
19232 {
19233 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19234 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19235 && curwin->w_p_cole > 0)
19236 {
19237 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19238 syntax_flags = get_syntax_info(&matchid);
19239
19240 /* get the conceal character */
19241 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19242 {
19243 cchar = syn_get_sub_char();
19244 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19245 cchar = lcs_conceal;
19246 if (cchar != NUL)
19247 {
19248# ifdef FEAT_MBYTE
19249 if (has_mbyte)
19250 (*mb_char2bytes)(cchar, str);
19251 else
19252# endif
19253 str[0] = cchar;
19254 }
19255 }
19256 }
19257
19258 list_append_number(rettv->vval.v_list,
19259 (syntax_flags & HL_CONCEAL) != 0);
19260 /* -1 to auto-determine strlen */
19261 list_append_string(rettv->vval.v_list, str, -1);
19262 list_append_number(rettv->vval.v_list, matchid);
19263 }
19264#endif
19265}
19266
19267/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019268 * "synstack(lnum, col)" function
19269 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019271f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019272{
19273#ifdef FEAT_SYN_HL
19274 long lnum;
19275 long col;
19276 int i;
19277 int id;
19278#endif
19279
19280 rettv->v_type = VAR_LIST;
19281 rettv->vval.v_list = NULL;
19282
19283#ifdef FEAT_SYN_HL
19284 lnum = get_tv_lnum(argvars); /* -1 on type error */
19285 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19286
19287 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019288 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019289 && rettv_list_alloc(rettv) != FAIL)
19290 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019291 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019292 for (i = 0; ; ++i)
19293 {
19294 id = syn_get_stack_item(i);
19295 if (id < 0)
19296 break;
19297 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19298 break;
19299 }
19300 }
19301#endif
19302}
19303
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019305get_cmd_output_as_rettv(
19306 typval_T *argvars,
19307 typval_T *rettv,
19308 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019310 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019312 char_u *infile = NULL;
19313 char_u buf[NUMBUFLEN];
19314 int err = FALSE;
19315 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019316 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019317 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 rettv->v_type = VAR_STRING;
19320 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019321 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019322 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019323
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019324 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019325 {
19326 /*
19327 * Write the string to a temp file, to be used for input of the shell
19328 * command.
19329 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019330 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019331 {
19332 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019333 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019334 }
19335
19336 fd = mch_fopen((char *)infile, WRITEBIN);
19337 if (fd == NULL)
19338 {
19339 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019340 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019341 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019342 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019343 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019344 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19345 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019346 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019347 else
19348 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019349 size_t len;
19350
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019351 p = get_tv_string_buf_chk(&argvars[1], buf);
19352 if (p == NULL)
19353 {
19354 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019355 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019356 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019357 len = STRLEN(p);
19358 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019359 err = TRUE;
19360 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019361 if (fclose(fd) != 0)
19362 err = TRUE;
19363 if (err)
19364 {
19365 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019366 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019367 }
19368 }
19369
Bram Moolenaar52a72462014-08-29 15:53:52 +020019370 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19371 * echoes typeahead, that messes up the display. */
19372 if (!msg_silent)
19373 flags += SHELL_COOKED;
19374
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019375 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019377 int len;
19378 listitem_T *li;
19379 char_u *s = NULL;
19380 char_u *start;
19381 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019382 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383
Bram Moolenaar52a72462014-08-29 15:53:52 +020019384 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019385 if (res == NULL)
19386 goto errret;
19387
19388 list = list_alloc();
19389 if (list == NULL)
19390 goto errret;
19391
19392 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019394 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019395 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019396 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019397 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019398
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019399 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019400 if (s == NULL)
19401 goto errret;
19402
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019403 for (p = s; start < end; ++p, ++start)
19404 *p = *start == NUL ? NL : *start;
19405 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019406
19407 li = listitem_alloc();
19408 if (li == NULL)
19409 {
19410 vim_free(s);
19411 goto errret;
19412 }
19413 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019414 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019415 li->li_tv.vval.v_string = s;
19416 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019418
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019419 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019420 rettv->v_type = VAR_LIST;
19421 rettv->vval.v_list = list;
19422 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019424 else
19425 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019426 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019427#ifdef USE_CR
19428 /* translate <CR> into <NL> */
19429 if (res != NULL)
19430 {
19431 char_u *s;
19432
19433 for (s = res; *s; ++s)
19434 {
19435 if (*s == CAR)
19436 *s = NL;
19437 }
19438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439#else
19440# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019441 /* translate <CR><NL> into <NL> */
19442 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019444 char_u *s, *d;
19445
19446 d = res;
19447 for (s = res; *s; ++s)
19448 {
19449 if (s[0] == CAR && s[1] == NL)
19450 ++s;
19451 *d++ = *s;
19452 }
19453 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455# endif
19456#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019457 rettv->vval.v_string = res;
19458 res = NULL;
19459 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019460
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019461errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019462 if (infile != NULL)
19463 {
19464 mch_remove(infile);
19465 vim_free(infile);
19466 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019467 if (res != NULL)
19468 vim_free(res);
19469 if (list != NULL)
19470 list_free(list, TRUE);
19471}
19472
19473/*
19474 * "system()" function
19475 */
19476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019477f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019478{
19479 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19480}
19481
19482/*
19483 * "systemlist()" function
19484 */
19485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019486f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019487{
19488 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489}
19490
19491/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019492 * "tabpagebuflist()" function
19493 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019495f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019496{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019497#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019498 tabpage_T *tp;
19499 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019500
19501 if (argvars[0].v_type == VAR_UNKNOWN)
19502 wp = firstwin;
19503 else
19504 {
19505 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19506 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019507 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019508 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019509 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019510 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019511 for (; wp != NULL; wp = wp->w_next)
19512 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019513 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019514 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019515 }
19516#endif
19517}
19518
19519
19520/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019521 * "tabpagenr()" function
19522 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019524f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019525{
19526 int nr = 1;
19527#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019528 char_u *arg;
19529
19530 if (argvars[0].v_type != VAR_UNKNOWN)
19531 {
19532 arg = get_tv_string_chk(&argvars[0]);
19533 nr = 0;
19534 if (arg != NULL)
19535 {
19536 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019537 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019538 else
19539 EMSG2(_(e_invexpr2), arg);
19540 }
19541 }
19542 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019543 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019544#endif
19545 rettv->vval.v_number = nr;
19546}
19547
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019548
19549#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019550static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019551
19552/*
19553 * Common code for tabpagewinnr() and winnr().
19554 */
19555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019556get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019557{
19558 win_T *twin;
19559 int nr = 1;
19560 win_T *wp;
19561 char_u *arg;
19562
19563 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19564 if (argvar->v_type != VAR_UNKNOWN)
19565 {
19566 arg = get_tv_string_chk(argvar);
19567 if (arg == NULL)
19568 nr = 0; /* type error; errmsg already given */
19569 else if (STRCMP(arg, "$") == 0)
19570 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19571 else if (STRCMP(arg, "#") == 0)
19572 {
19573 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19574 if (twin == NULL)
19575 nr = 0;
19576 }
19577 else
19578 {
19579 EMSG2(_(e_invexpr2), arg);
19580 nr = 0;
19581 }
19582 }
19583
19584 if (nr > 0)
19585 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19586 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019587 {
19588 if (wp == NULL)
19589 {
19590 /* didn't find it in this tabpage */
19591 nr = 0;
19592 break;
19593 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019594 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019595 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019596 return nr;
19597}
19598#endif
19599
19600/*
19601 * "tabpagewinnr()" function
19602 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019604f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019605{
19606 int nr = 1;
19607#ifdef FEAT_WINDOWS
19608 tabpage_T *tp;
19609
19610 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19611 if (tp == NULL)
19612 nr = 0;
19613 else
19614 nr = get_winnr(tp, &argvars[1]);
19615#endif
19616 rettv->vval.v_number = nr;
19617}
19618
19619
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019620/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019621 * "tagfiles()" function
19622 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019624f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019625{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019626 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019627 tagname_T tn;
19628 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019629
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019630 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019631 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019632 fname = alloc(MAXPATHL);
19633 if (fname == NULL)
19634 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019635
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019636 for (first = TRUE; ; first = FALSE)
19637 if (get_tagfname(&tn, first, fname) == FAIL
19638 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019639 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019640 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019641 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019642}
19643
19644/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019645 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019646 */
19647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019648f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019649{
19650 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019651
19652 tag_pattern = get_tv_string(&argvars[0]);
19653
19654 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019655 if (*tag_pattern == NUL)
19656 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019658 if (rettv_list_alloc(rettv) == OK)
19659 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019660}
19661
19662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 * "tempname()" function
19664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019666f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667{
19668 static int x = 'A';
19669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019670 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019671 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672
19673 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19674 * names. Skip 'I' and 'O', they are used for shell redirection. */
19675 do
19676 {
19677 if (x == 'Z')
19678 x = '0';
19679 else if (x == '9')
19680 x = 'A';
19681 else
19682 {
19683#ifdef EBCDIC
19684 if (x == 'I')
19685 x = 'J';
19686 else if (x == 'R')
19687 x = 'S';
19688 else
19689#endif
19690 ++x;
19691 }
19692 } while (x == 'I' || x == 'O');
19693}
19694
19695/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019696 * "test(list)" function: Just checking the walls...
19697 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019699f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019700{
19701 /* Used for unit testing. Change the code below to your liking. */
19702#if 0
19703 listitem_T *li;
19704 list_T *l;
19705 char_u *bad, *good;
19706
19707 if (argvars[0].v_type != VAR_LIST)
19708 return;
19709 l = argvars[0].vval.v_list;
19710 if (l == NULL)
19711 return;
19712 li = l->lv_first;
19713 if (li == NULL)
19714 return;
19715 bad = get_tv_string(&li->li_tv);
19716 li = li->li_next;
19717 if (li == NULL)
19718 return;
19719 good = get_tv_string(&li->li_tv);
19720 rettv->vval.v_number = test_edit_score(bad, good);
19721#endif
19722}
19723
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019724#ifdef FEAT_FLOAT
19725/*
19726 * "tan()" function
19727 */
19728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019729f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019730{
19731 float_T f;
19732
19733 rettv->v_type = VAR_FLOAT;
19734 if (get_float_arg(argvars, &f) == OK)
19735 rettv->vval.v_float = tan(f);
19736 else
19737 rettv->vval.v_float = 0.0;
19738}
19739
19740/*
19741 * "tanh()" function
19742 */
19743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019744f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019745{
19746 float_T f;
19747
19748 rettv->v_type = VAR_FLOAT;
19749 if (get_float_arg(argvars, &f) == OK)
19750 rettv->vval.v_float = tanh(f);
19751 else
19752 rettv->vval.v_float = 0.0;
19753}
19754#endif
19755
Bram Moolenaard52d9742005-08-21 22:20:28 +000019756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 * "tolower(string)" function
19758 */
19759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019760f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761{
19762 char_u *p;
19763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764 p = vim_strsave(get_tv_string(&argvars[0]));
19765 rettv->v_type = VAR_STRING;
19766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767
19768 if (p != NULL)
19769 while (*p != NUL)
19770 {
19771#ifdef FEAT_MBYTE
19772 int l;
19773
19774 if (enc_utf8)
19775 {
19776 int c, lc;
19777
19778 c = utf_ptr2char(p);
19779 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019780 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 /* TODO: reallocate string when byte count changes. */
19782 if (utf_char2len(lc) == l)
19783 utf_char2bytes(lc, p);
19784 p += l;
19785 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019786 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 p += l; /* skip multi-byte character */
19788 else
19789#endif
19790 {
19791 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19792 ++p;
19793 }
19794 }
19795}
19796
19797/*
19798 * "toupper(string)" function
19799 */
19800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019801f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019804 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805}
19806
19807/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019808 * "tr(string, fromstr, tostr)" function
19809 */
19810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019811f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019812{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019813 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019814 char_u *fromstr;
19815 char_u *tostr;
19816 char_u *p;
19817#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019818 int inlen;
19819 int fromlen;
19820 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019821 int idx;
19822 char_u *cpstr;
19823 int cplen;
19824 int first = TRUE;
19825#endif
19826 char_u buf[NUMBUFLEN];
19827 char_u buf2[NUMBUFLEN];
19828 garray_T ga;
19829
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019830 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019831 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19832 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019833
19834 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 rettv->v_type = VAR_STRING;
19836 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019837 if (fromstr == NULL || tostr == NULL)
19838 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019839 ga_init2(&ga, (int)sizeof(char), 80);
19840
19841#ifdef FEAT_MBYTE
19842 if (!has_mbyte)
19843#endif
19844 /* not multi-byte: fromstr and tostr must be the same length */
19845 if (STRLEN(fromstr) != STRLEN(tostr))
19846 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019847#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019848error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019849#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019850 EMSG2(_(e_invarg2), fromstr);
19851 ga_clear(&ga);
19852 return;
19853 }
19854
19855 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019856 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019857 {
19858#ifdef FEAT_MBYTE
19859 if (has_mbyte)
19860 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019861 inlen = (*mb_ptr2len)(in_str);
19862 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019863 cplen = inlen;
19864 idx = 0;
19865 for (p = fromstr; *p != NUL; p += fromlen)
19866 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019867 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019868 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019869 {
19870 for (p = tostr; *p != NUL; p += tolen)
19871 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019872 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019873 if (idx-- == 0)
19874 {
19875 cplen = tolen;
19876 cpstr = p;
19877 break;
19878 }
19879 }
19880 if (*p == NUL) /* tostr is shorter than fromstr */
19881 goto error;
19882 break;
19883 }
19884 ++idx;
19885 }
19886
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019887 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019888 {
19889 /* Check that fromstr and tostr have the same number of
19890 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019891 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019892 first = FALSE;
19893 for (p = tostr; *p != NUL; p += tolen)
19894 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019895 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019896 --idx;
19897 }
19898 if (idx != 0)
19899 goto error;
19900 }
19901
Bram Moolenaarcde88542015-08-11 19:14:00 +020019902 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019903 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019904 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019905
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019906 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019907 }
19908 else
19909#endif
19910 {
19911 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019912 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019913 if (p != NULL)
19914 ga_append(&ga, tostr[p - fromstr]);
19915 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019916 ga_append(&ga, *in_str);
19917 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019918 }
19919 }
19920
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019921 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019922 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019923 ga_append(&ga, NUL);
19924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019926}
19927
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019928#ifdef FEAT_FLOAT
19929/*
19930 * "trunc({float})" function
19931 */
19932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019933f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019934{
19935 float_T f;
19936
19937 rettv->v_type = VAR_FLOAT;
19938 if (get_float_arg(argvars, &f) == OK)
19939 /* trunc() is not in C90, use floor() or ceil() instead. */
19940 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19941 else
19942 rettv->vval.v_float = 0.0;
19943}
19944#endif
19945
Bram Moolenaar8299df92004-07-10 09:47:34 +000019946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 * "type(expr)" function
19948 */
19949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019950f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010019952 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019953
19954 switch (argvars[0].v_type)
19955 {
19956 case VAR_NUMBER: n = 0; break;
19957 case VAR_STRING: n = 1; break;
19958 case VAR_FUNC: n = 2; break;
19959 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019960 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019961 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019962 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019963 if (argvars[0].vval.v_number == VVAL_FALSE
19964 || argvars[0].vval.v_number == VVAL_TRUE)
19965 n = 6;
19966 else
19967 n = 7;
19968 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010019969 case VAR_JOB: n = 8; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010019970 case VAR_UNKNOWN:
19971 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19972 n = -1;
19973 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019974 }
19975 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976}
19977
19978/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019979 * "undofile(name)" function
19980 */
19981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019982f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019983{
19984 rettv->v_type = VAR_STRING;
19985#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019986 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019987 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019988
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019989 if (*fname == NUL)
19990 {
19991 /* If there is no file name there will be no undo file. */
19992 rettv->vval.v_string = NULL;
19993 }
19994 else
19995 {
19996 char_u *ffname = FullName_save(fname, FALSE);
19997
19998 if (ffname != NULL)
19999 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20000 vim_free(ffname);
20001 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020002 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020003#else
20004 rettv->vval.v_string = NULL;
20005#endif
20006}
20007
20008/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020009 * "undotree()" function
20010 */
20011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020012f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020013{
20014 if (rettv_dict_alloc(rettv) == OK)
20015 {
20016 dict_T *dict = rettv->vval.v_dict;
20017 list_T *list;
20018
Bram Moolenaar730cde92010-06-27 05:18:54 +020020019 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020020 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020021 dict_add_nr_str(dict, "save_last",
20022 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020023 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20024 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020025 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020026
20027 list = list_alloc();
20028 if (list != NULL)
20029 {
20030 u_eval_tree(curbuf->b_u_oldhead, list);
20031 dict_add_list(dict, "entries", list);
20032 }
20033 }
20034}
20035
20036/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020037 * "values(dict)" function
20038 */
20039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020040f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020041{
20042 dict_list(argvars, rettv, 1);
20043}
20044
20045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 * "virtcol(string)" function
20047 */
20048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020049f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050{
20051 colnr_T vcol = 0;
20052 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020053 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020055 fp = var2fpos(&argvars[0], FALSE, &fnum);
20056 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20057 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 {
20059 getvvcol(curwin, fp, NULL, NULL, &vcol);
20060 ++vcol;
20061 }
20062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064}
20065
20066/*
20067 * "visualmode()" function
20068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020070f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 char_u str[2];
20073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020074 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 str[0] = curbuf->b_visual_mode_eval;
20076 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078
20079 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020080 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082}
20083
20084/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020085 * "wildmenumode()" function
20086 */
20087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020088f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020089{
20090#ifdef FEAT_WILDMENU
20091 if (wild_menu_showing)
20092 rettv->vval.v_number = 1;
20093#endif
20094}
20095
20096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097 * "winbufnr(nr)" function
20098 */
20099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020100f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101{
20102 win_T *wp;
20103
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020104 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109}
20110
20111/*
20112 * "wincol()" function
20113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020115f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116{
20117 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119}
20120
20121/*
20122 * "winheight(nr)" function
20123 */
20124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020125f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126{
20127 win_T *wp;
20128
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020129 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134}
20135
20136/*
20137 * "winline()" function
20138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020140f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141{
20142 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144}
20145
20146/*
20147 * "winnr()" function
20148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020150f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151{
20152 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020153
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020155 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020157 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158}
20159
20160/*
20161 * "winrestcmd()" function
20162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020164f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165{
20166#ifdef FEAT_WINDOWS
20167 win_T *wp;
20168 int winnr = 1;
20169 garray_T ga;
20170 char_u buf[50];
20171
20172 ga_init2(&ga, (int)sizeof(char), 70);
20173 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20174 {
20175 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20176 ga_concat(&ga, buf);
20177# ifdef FEAT_VERTSPLIT
20178 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20179 ga_concat(&ga, buf);
20180# endif
20181 ++winnr;
20182 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020183 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020185 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020187 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020189 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190}
20191
20192/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020193 * "winrestview()" function
20194 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020196f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020197{
20198 dict_T *dict;
20199
20200 if (argvars[0].v_type != VAR_DICT
20201 || (dict = argvars[0].vval.v_dict) == NULL)
20202 EMSG(_(e_invarg));
20203 else
20204 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020205 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20206 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20207 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20208 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020209#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020210 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20211 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020212#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020213 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20214 {
20215 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20216 curwin->w_set_curswant = FALSE;
20217 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020218
Bram Moolenaar82c25852014-05-28 16:47:16 +020020219 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20220 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020221#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020222 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20223 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020224#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020225 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20226 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20227 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20228 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020229
20230 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020231 win_new_height(curwin, curwin->w_height);
20232# ifdef FEAT_VERTSPLIT
20233 win_new_width(curwin, W_WIDTH(curwin));
20234# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020235 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020236
Bram Moolenaarb851a962014-10-31 15:45:52 +010020237 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020238 curwin->w_topline = 1;
20239 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20240 curwin->w_topline = curbuf->b_ml.ml_line_count;
20241#ifdef FEAT_DIFF
20242 check_topfill(curwin, TRUE);
20243#endif
20244 }
20245}
20246
20247/*
20248 * "winsaveview()" function
20249 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020252{
20253 dict_T *dict;
20254
Bram Moolenaara800b422010-06-27 01:15:55 +020020255 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020256 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020257 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020258
20259 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20260 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20261#ifdef FEAT_VIRTUALEDIT
20262 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20263#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020264 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020265 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20266
20267 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20268#ifdef FEAT_DIFF
20269 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20270#endif
20271 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20272 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20273}
20274
20275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 * "winwidth(nr)" function
20277 */
20278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020279f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280{
20281 win_T *wp;
20282
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020283 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 else
20287#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020288 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291#endif
20292}
20293
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020295 * "wordcount()" function
20296 */
20297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020298f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020299{
20300 if (rettv_dict_alloc(rettv) == FAIL)
20301 return;
20302 cursor_pos_info(rettv->vval.v_dict);
20303}
20304
20305/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020306 * Write list of strings to file
20307 */
20308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020309write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020310{
20311 listitem_T *li;
20312 int c;
20313 int ret = OK;
20314 char_u *s;
20315
20316 for (li = list->lv_first; li != NULL; li = li->li_next)
20317 {
20318 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20319 {
20320 if (*s == '\n')
20321 c = putc(NUL, fd);
20322 else
20323 c = putc(*s, fd);
20324 if (c == EOF)
20325 {
20326 ret = FAIL;
20327 break;
20328 }
20329 }
20330 if (!binary || li->li_next != NULL)
20331 if (putc('\n', fd) == EOF)
20332 {
20333 ret = FAIL;
20334 break;
20335 }
20336 if (ret == FAIL)
20337 {
20338 EMSG(_(e_write));
20339 break;
20340 }
20341 }
20342 return ret;
20343}
20344
20345/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020346 * "writefile()" function
20347 */
20348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020349f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020350{
20351 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020352 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020353 char_u *fname;
20354 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020355 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020356
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020357 if (check_restricted() || check_secure())
20358 return;
20359
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020360 if (argvars[0].v_type != VAR_LIST)
20361 {
20362 EMSG2(_(e_listarg), "writefile()");
20363 return;
20364 }
20365 if (argvars[0].vval.v_list == NULL)
20366 return;
20367
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020368 if (argvars[2].v_type != VAR_UNKNOWN)
20369 {
20370 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20371 binary = TRUE;
20372 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20373 append = TRUE;
20374 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020375
20376 /* Always open the file in binary mode, library functions have a mind of
20377 * their own about CR-LF conversion. */
20378 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020379 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20380 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020381 {
20382 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20383 ret = -1;
20384 }
20385 else
20386 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020387 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20388 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020389 fclose(fd);
20390 }
20391
20392 rettv->vval.v_number = ret;
20393}
20394
20395/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020396 * "xor(expr, expr)" function
20397 */
20398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020399f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020400{
20401 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20402 ^ get_tv_number_chk(&argvars[1], NULL);
20403}
20404
20405
20406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020408 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 */
20410 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020411var2fpos(
20412 typval_T *varp,
20413 int dollar_lnum, /* TRUE when $ is last line */
20414 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020416 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020418 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419
Bram Moolenaara5525202006-03-02 22:52:09 +000020420 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020421 if (varp->v_type == VAR_LIST)
20422 {
20423 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020424 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020425 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020426 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020427
20428 l = varp->vval.v_list;
20429 if (l == NULL)
20430 return NULL;
20431
20432 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020433 pos.lnum = list_find_nr(l, 0L, &error);
20434 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020435 return NULL; /* invalid line number */
20436
20437 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020438 pos.col = list_find_nr(l, 1L, &error);
20439 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020440 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020441 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020442
20443 /* We accept "$" for the column number: last column. */
20444 li = list_find(l, 1L);
20445 if (li != NULL && li->li_tv.v_type == VAR_STRING
20446 && li->li_tv.vval.v_string != NULL
20447 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20448 pos.col = len + 1;
20449
Bram Moolenaara5525202006-03-02 22:52:09 +000020450 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020451 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020452 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020453 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020454
Bram Moolenaara5525202006-03-02 22:52:09 +000020455#ifdef FEAT_VIRTUALEDIT
20456 /* Get the virtual offset. Defaults to zero. */
20457 pos.coladd = list_find_nr(l, 2L, &error);
20458 if (error)
20459 pos.coladd = 0;
20460#endif
20461
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020462 return &pos;
20463 }
20464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020465 name = get_tv_string_chk(varp);
20466 if (name == NULL)
20467 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020468 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020470 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20471 {
20472 if (VIsual_active)
20473 return &VIsual;
20474 return &curwin->w_cursor;
20475 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020476 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020478 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20480 return NULL;
20481 return pp;
20482 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020483
20484#ifdef FEAT_VIRTUALEDIT
20485 pos.coladd = 0;
20486#endif
20487
Bram Moolenaar477933c2007-07-17 14:32:23 +000020488 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020489 {
20490 pos.col = 0;
20491 if (name[1] == '0') /* "w0": first visible line */
20492 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020493 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020494 pos.lnum = curwin->w_topline;
20495 return &pos;
20496 }
20497 else if (name[1] == '$') /* "w$": last visible line */
20498 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020499 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020500 pos.lnum = curwin->w_botline - 1;
20501 return &pos;
20502 }
20503 }
20504 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020506 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 {
20508 pos.lnum = curbuf->b_ml.ml_line_count;
20509 pos.col = 0;
20510 }
20511 else
20512 {
20513 pos.lnum = curwin->w_cursor.lnum;
20514 pos.col = (colnr_T)STRLEN(ml_get_curline());
20515 }
20516 return &pos;
20517 }
20518 return NULL;
20519}
20520
20521/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020522 * Convert list in "arg" into a position and optional file number.
20523 * When "fnump" is NULL there is no file number, only 3 items.
20524 * Note that the column is passed on as-is, the caller may want to decrement
20525 * it to use 1 for the first column.
20526 * Return FAIL when conversion is not possible, doesn't check the position for
20527 * validity.
20528 */
20529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020530list2fpos(
20531 typval_T *arg,
20532 pos_T *posp,
20533 int *fnump,
20534 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020535{
20536 list_T *l = arg->vval.v_list;
20537 long i = 0;
20538 long n;
20539
Bram Moolenaar493c1782014-05-28 14:34:46 +020020540 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20541 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020542 if (arg->v_type != VAR_LIST
20543 || l == NULL
20544 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020545 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020546 return FAIL;
20547
20548 if (fnump != NULL)
20549 {
20550 n = list_find_nr(l, i++, NULL); /* fnum */
20551 if (n < 0)
20552 return FAIL;
20553 if (n == 0)
20554 n = curbuf->b_fnum; /* current buffer */
20555 *fnump = n;
20556 }
20557
20558 n = list_find_nr(l, i++, NULL); /* lnum */
20559 if (n < 0)
20560 return FAIL;
20561 posp->lnum = n;
20562
20563 n = list_find_nr(l, i++, NULL); /* col */
20564 if (n < 0)
20565 return FAIL;
20566 posp->col = n;
20567
20568#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020569 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020570 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020571 posp->coladd = 0;
20572 else
20573 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020574#endif
20575
Bram Moolenaar493c1782014-05-28 14:34:46 +020020576 if (curswantp != NULL)
20577 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20578
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020579 return OK;
20580}
20581
20582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 * Get the length of an environment variable name.
20584 * Advance "arg" to the first character after the name.
20585 * Return 0 for error.
20586 */
20587 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020588get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589{
20590 char_u *p;
20591 int len;
20592
20593 for (p = *arg; vim_isIDc(*p); ++p)
20594 ;
20595 if (p == *arg) /* no name found */
20596 return 0;
20597
20598 len = (int)(p - *arg);
20599 *arg = p;
20600 return len;
20601}
20602
20603/*
20604 * Get the length of the name of a function or internal variable.
20605 * "arg" is advanced to the first non-white character after the name.
20606 * Return 0 if something is wrong.
20607 */
20608 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020609get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610{
20611 char_u *p;
20612 int len;
20613
20614 /* Find the end of the name. */
20615 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020616 {
20617 if (*p == ':')
20618 {
20619 /* "s:" is start of "s:var", but "n:" is not and can be used in
20620 * slice "[n:]". Also "xx:" is not a namespace. */
20621 len = (int)(p - *arg);
20622 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20623 || len > 1)
20624 break;
20625 }
20626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 if (p == *arg) /* no name found */
20628 return 0;
20629
20630 len = (int)(p - *arg);
20631 *arg = skipwhite(p);
20632
20633 return len;
20634}
20635
20636/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020637 * Get the length of the name of a variable or function.
20638 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020640 * Return -1 if curly braces expansion failed.
20641 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 * If the name contains 'magic' {}'s, expand them and return the
20643 * expanded name in an allocated string via 'alias' - caller must free.
20644 */
20645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020646get_name_len(
20647 char_u **arg,
20648 char_u **alias,
20649 int evaluate,
20650 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651{
20652 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 char_u *p;
20654 char_u *expr_start;
20655 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656
20657 *alias = NULL; /* default to no alias */
20658
20659 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20660 && (*arg)[2] == (int)KE_SNR)
20661 {
20662 /* hard coded <SNR>, already translated */
20663 *arg += 3;
20664 return get_id_len(arg) + 3;
20665 }
20666 len = eval_fname_script(*arg);
20667 if (len > 0)
20668 {
20669 /* literal "<SID>", "s:" or "<SNR>" */
20670 *arg += len;
20671 }
20672
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020676 p = find_name_end(*arg, &expr_start, &expr_end,
20677 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020678 if (expr_start != NULL)
20679 {
20680 char_u *temp_string;
20681
20682 if (!evaluate)
20683 {
20684 len += (int)(p - *arg);
20685 *arg = skipwhite(p);
20686 return len;
20687 }
20688
20689 /*
20690 * Include any <SID> etc in the expanded string:
20691 * Thus the -len here.
20692 */
20693 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20694 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020695 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 *alias = temp_string;
20697 *arg = skipwhite(p);
20698 return (int)STRLEN(temp_string);
20699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700
20701 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020702 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020703 EMSG2(_(e_invexpr2), *arg);
20704
20705 return len;
20706}
20707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020708/*
20709 * Find the end of a variable or function name, taking care of magic braces.
20710 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20711 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020712 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020713 * Return a pointer to just after the name. Equal to "arg" if there is no
20714 * valid name.
20715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020717find_name_end(
20718 char_u *arg,
20719 char_u **expr_start,
20720 char_u **expr_end,
20721 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020723 int mb_nest = 0;
20724 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020726 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020728 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020730 *expr_start = NULL;
20731 *expr_end = NULL;
20732 }
20733
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020734 /* Quick check for valid starting character. */
20735 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20736 return arg;
20737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020738 for (p = arg; *p != NUL
20739 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020740 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020741 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020743 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020744 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020745 if (*p == '\'')
20746 {
20747 /* skip over 'string' to avoid counting [ and ] inside it. */
20748 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20749 ;
20750 if (*p == NUL)
20751 break;
20752 }
20753 else if (*p == '"')
20754 {
20755 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20756 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20757 if (*p == '\\' && p[1] != NUL)
20758 ++p;
20759 if (*p == NUL)
20760 break;
20761 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020762 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20763 {
20764 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020765 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020766 len = (int)(p - arg);
20767 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020768 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020769 break;
20770 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020772 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020774 if (*p == '[')
20775 ++br_nest;
20776 else if (*p == ']')
20777 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020780 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020782 if (*p == '{')
20783 {
20784 mb_nest++;
20785 if (expr_start != NULL && *expr_start == NULL)
20786 *expr_start = p;
20787 }
20788 else if (*p == '}')
20789 {
20790 mb_nest--;
20791 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20792 *expr_end = p;
20793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 }
20796
20797 return p;
20798}
20799
20800/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020801 * Expands out the 'magic' {}'s in a variable/function name.
20802 * Note that this can call itself recursively, to deal with
20803 * constructs like foo{bar}{baz}{bam}
20804 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20805 * "in_start" ^
20806 * "expr_start" ^
20807 * "expr_end" ^
20808 * "in_end" ^
20809 *
20810 * Returns a new allocated string, which the caller must free.
20811 * Returns NULL for failure.
20812 */
20813 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020814make_expanded_name(
20815 char_u *in_start,
20816 char_u *expr_start,
20817 char_u *expr_end,
20818 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020819{
20820 char_u c1;
20821 char_u *retval = NULL;
20822 char_u *temp_result;
20823 char_u *nextcmd = NULL;
20824
20825 if (expr_end == NULL || in_end == NULL)
20826 return NULL;
20827 *expr_start = NUL;
20828 *expr_end = NUL;
20829 c1 = *in_end;
20830 *in_end = NUL;
20831
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020832 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020833 if (temp_result != NULL && nextcmd == NULL)
20834 {
20835 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20836 + (in_end - expr_end) + 1));
20837 if (retval != NULL)
20838 {
20839 STRCPY(retval, in_start);
20840 STRCAT(retval, temp_result);
20841 STRCAT(retval, expr_end + 1);
20842 }
20843 }
20844 vim_free(temp_result);
20845
20846 *in_end = c1; /* put char back for error messages */
20847 *expr_start = '{';
20848 *expr_end = '}';
20849
20850 if (retval != NULL)
20851 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020852 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020853 if (expr_start != NULL)
20854 {
20855 /* Further expansion! */
20856 temp_result = make_expanded_name(retval, expr_start,
20857 expr_end, temp_result);
20858 vim_free(retval);
20859 retval = temp_result;
20860 }
20861 }
20862
20863 return retval;
20864}
20865
20866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020868 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 */
20870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020871eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020873 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20874}
20875
20876/*
20877 * Return TRUE if character "c" can be used as the first character in a
20878 * variable or function name (excluding '{' and '}').
20879 */
20880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020881eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020882{
20883 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884}
20885
20886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 * Set number v: variable to "val".
20888 */
20889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020890set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020892 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893}
20894
20895/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020896 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 */
20898 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020899get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020901 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902}
20903
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020904/*
20905 * Get string v: variable value. Uses a static buffer, can only be used once.
20906 */
20907 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020908get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020909{
20910 return get_tv_string(&vimvars[idx].vv_tv);
20911}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020912
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020914 * Get List v: variable value. Caller must take care of reference count when
20915 * needed.
20916 */
20917 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020918get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020919{
20920 return vimvars[idx].vv_list;
20921}
20922
20923/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020924 * Set v:char to character "c".
20925 */
20926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020927set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020928{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020929 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020930
20931#ifdef FEAT_MBYTE
20932 if (has_mbyte)
20933 buf[(*mb_char2bytes)(c, buf)] = NUL;
20934 else
20935#endif
20936 {
20937 buf[0] = c;
20938 buf[1] = NUL;
20939 }
20940 set_vim_var_string(VV_CHAR, buf, -1);
20941}
20942
20943/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020944 * Set v:count to "count" and v:count1 to "count1".
20945 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 */
20947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020948set_vcount(
20949 long count,
20950 long count1,
20951 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020953 if (set_prevcount)
20954 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020955 vimvars[VV_COUNT].vv_nr = count;
20956 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957}
20958
20959/*
20960 * Set string v: variable to a copy of "val".
20961 */
20962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020963set_vim_var_string(
20964 int idx,
20965 char_u *val,
20966 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967{
Bram Moolenaara542c682016-01-31 16:28:04 +010020968 clear_tv(&vimvars[idx].vv_di.di_tv);
20969 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020971 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020973 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020975 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976}
20977
20978/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020979 * Set List v: variable to "val".
20980 */
20981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020982set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020983{
Bram Moolenaara542c682016-01-31 16:28:04 +010020984 clear_tv(&vimvars[idx].vv_di.di_tv);
20985 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020986 vimvars[idx].vv_list = val;
20987 if (val != NULL)
20988 ++val->lv_refcount;
20989}
20990
20991/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020992 * Set Dictionary v: variable to "val".
20993 */
20994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020995set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020996{
20997 int todo;
20998 hashitem_T *hi;
20999
Bram Moolenaara542c682016-01-31 16:28:04 +010021000 clear_tv(&vimvars[idx].vv_di.di_tv);
21001 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021002 vimvars[idx].vv_dict = val;
21003 if (val != NULL)
21004 {
21005 ++val->dv_refcount;
21006
21007 /* Set readonly */
21008 todo = (int)val->dv_hashtab.ht_used;
21009 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21010 {
21011 if (HASHITEM_EMPTY(hi))
21012 continue;
21013 --todo;
21014 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21015 }
21016 }
21017}
21018
21019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 * Set v:register if needed.
21021 */
21022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021023set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024{
21025 char_u regname;
21026
21027 if (c == 0 || c == ' ')
21028 regname = '"';
21029 else
21030 regname = c;
21031 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021032 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 set_vim_var_string(VV_REG, &regname, 1);
21034}
21035
21036/*
21037 * Get or set v:exception. If "oldval" == NULL, return the current value.
21038 * Otherwise, restore the value to "oldval" and return NULL.
21039 * Must always be called in pairs to save and restore v:exception! Does not
21040 * take care of memory allocations.
21041 */
21042 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021043v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044{
21045 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021046 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047
Bram Moolenaare9a41262005-01-15 22:18:47 +000021048 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 return NULL;
21050}
21051
21052/*
21053 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21054 * Otherwise, restore the value to "oldval" and return NULL.
21055 * Must always be called in pairs to save and restore v:throwpoint! Does not
21056 * take care of memory allocations.
21057 */
21058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021059v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060{
21061 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021062 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063
Bram Moolenaare9a41262005-01-15 22:18:47 +000021064 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 return NULL;
21066}
21067
21068#if defined(FEAT_AUTOCMD) || defined(PROTO)
21069/*
21070 * Set v:cmdarg.
21071 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21072 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21073 * Must always be called in pairs!
21074 */
21075 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021076set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077{
21078 char_u *oldval;
21079 char_u *newval;
21080 unsigned len;
21081
Bram Moolenaare9a41262005-01-15 22:18:47 +000021082 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021083 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021085 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021086 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021087 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 }
21089
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021090 if (eap->force_bin == FORCE_BIN)
21091 len = 6;
21092 else if (eap->force_bin == FORCE_NOBIN)
21093 len = 8;
21094 else
21095 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021096
21097 if (eap->read_edit)
21098 len += 7;
21099
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021100 if (eap->force_ff != 0)
21101 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21102# ifdef FEAT_MBYTE
21103 if (eap->force_enc != 0)
21104 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021105 if (eap->bad_char != 0)
21106 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021107# endif
21108
21109 newval = alloc(len + 1);
21110 if (newval == NULL)
21111 return NULL;
21112
21113 if (eap->force_bin == FORCE_BIN)
21114 sprintf((char *)newval, " ++bin");
21115 else if (eap->force_bin == FORCE_NOBIN)
21116 sprintf((char *)newval, " ++nobin");
21117 else
21118 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021119
21120 if (eap->read_edit)
21121 STRCAT(newval, " ++edit");
21122
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021123 if (eap->force_ff != 0)
21124 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21125 eap->cmd + eap->force_ff);
21126# ifdef FEAT_MBYTE
21127 if (eap->force_enc != 0)
21128 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21129 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021130 if (eap->bad_char == BAD_KEEP)
21131 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21132 else if (eap->bad_char == BAD_DROP)
21133 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21134 else if (eap->bad_char != 0)
21135 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021136# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021137 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021138 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139}
21140#endif
21141
21142/*
21143 * Get the value of internal variable "name".
21144 * Return OK or FAIL.
21145 */
21146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147get_var_tv(
21148 char_u *name,
21149 int len, /* length of "name" */
21150 typval_T *rettv, /* NULL when only checking existence */
21151 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21152 int verbose, /* may give error message */
21153 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154{
21155 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021156 typval_T *tv = NULL;
21157 typval_T atv;
21158 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160
21161 /* truncate the name, so that we can use strcmp() */
21162 cc = name[len];
21163 name[len] = NUL;
21164
21165 /*
21166 * Check for "b:changedtick".
21167 */
21168 if (STRCMP(name, "b:changedtick") == 0)
21169 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021170 atv.v_type = VAR_NUMBER;
21171 atv.vval.v_number = curbuf->b_changedtick;
21172 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 }
21174
21175 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 * Check for user-defined variables.
21177 */
21178 else
21179 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021180 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021182 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021183 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021184 if (dip != NULL)
21185 *dip = v;
21186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 }
21188
Bram Moolenaare9a41262005-01-15 22:18:47 +000021189 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021191 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021192 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 ret = FAIL;
21194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021196 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197
21198 name[len] = cc;
21199
21200 return ret;
21201}
21202
21203/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021204 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21205 * Also handle function call with Funcref variable: func(expr)
21206 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21207 */
21208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021209handle_subscript(
21210 char_u **arg,
21211 typval_T *rettv,
21212 int evaluate, /* do more than finding the end */
21213 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021214{
21215 int ret = OK;
21216 dict_T *selfdict = NULL;
21217 char_u *s;
21218 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021219 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021220
21221 while (ret == OK
21222 && (**arg == '['
21223 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021224 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021225 && !vim_iswhite(*(*arg - 1)))
21226 {
21227 if (**arg == '(')
21228 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021229 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021230 if (evaluate)
21231 {
21232 functv = *rettv;
21233 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021234
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021235 /* Invoke the function. Recursive! */
21236 s = functv.vval.v_string;
21237 }
21238 else
21239 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021240 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21242 &len, evaluate, selfdict);
21243
21244 /* Clear the funcref afterwards, so that deleting it while
21245 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021246 if (evaluate)
21247 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021248
21249 /* Stop the expression evaluation when immediately aborting on
21250 * error, or when an interrupt occurred or an exception was thrown
21251 * but not caught. */
21252 if (aborting())
21253 {
21254 if (ret == OK)
21255 clear_tv(rettv);
21256 ret = FAIL;
21257 }
21258 dict_unref(selfdict);
21259 selfdict = NULL;
21260 }
21261 else /* **arg == '[' || **arg == '.' */
21262 {
21263 dict_unref(selfdict);
21264 if (rettv->v_type == VAR_DICT)
21265 {
21266 selfdict = rettv->vval.v_dict;
21267 if (selfdict != NULL)
21268 ++selfdict->dv_refcount;
21269 }
21270 else
21271 selfdict = NULL;
21272 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21273 {
21274 clear_tv(rettv);
21275 ret = FAIL;
21276 }
21277 }
21278 }
21279 dict_unref(selfdict);
21280 return ret;
21281}
21282
21283/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021284 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021285 * value).
21286 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021287 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021288alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289{
Bram Moolenaar33570922005-01-25 22:26:29 +000021290 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021291}
21292
21293/*
21294 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 * The string "s" must have been allocated, it is consumed.
21296 * Return NULL for out of memory, the variable otherwise.
21297 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021299alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300{
Bram Moolenaar33570922005-01-25 22:26:29 +000021301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021303 rettv = alloc_tv();
21304 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021306 rettv->v_type = VAR_STRING;
21307 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 }
21309 else
21310 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021311 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312}
21313
21314/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021315 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021318free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319{
21320 if (varp != NULL)
21321 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021322 switch (varp->v_type)
21323 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021324 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021325 func_unref(varp->vval.v_string);
21326 /*FALLTHROUGH*/
21327 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021328 vim_free(varp->vval.v_string);
21329 break;
21330 case VAR_LIST:
21331 list_unref(varp->vval.v_list);
21332 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021333 case VAR_DICT:
21334 dict_unref(varp->vval.v_dict);
21335 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021336 case VAR_JOB:
21337#ifdef FEAT_JOB
21338 job_unref(varp->vval.v_job);
21339 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021340#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021341 case VAR_NUMBER:
21342 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021343 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021344 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021345 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 vim_free(varp);
21348 }
21349}
21350
21351/*
21352 * Free the memory for a variable value and set the value to NULL or 0.
21353 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021355clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356{
21357 if (varp != NULL)
21358 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021359 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021361 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021362 func_unref(varp->vval.v_string);
21363 /*FALLTHROUGH*/
21364 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021365 vim_free(varp->vval.v_string);
21366 varp->vval.v_string = NULL;
21367 break;
21368 case VAR_LIST:
21369 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021370 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021371 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021372 case VAR_DICT:
21373 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021374 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021375 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021376 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021377 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021378 varp->vval.v_number = 0;
21379 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021380 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021381#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021382 varp->vval.v_float = 0.0;
21383 break;
21384#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021385 case VAR_JOB:
21386#ifdef FEAT_JOB
21387 job_unref(varp->vval.v_job);
21388 varp->vval.v_job = NULL;
21389#endif
21390 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021391 case VAR_UNKNOWN:
21392 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021394 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 }
21396}
21397
21398/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021399 * Set the value of a variable to NULL without freeing items.
21400 */
21401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021402init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403{
21404 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021405 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406}
21407
21408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 * Get the number value of a variable.
21410 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021411 * For incompatible types, return 0.
21412 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21413 * caller of incompatible types: it sets *denote to TRUE if "denote"
21414 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 */
21416 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021417get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021419 int error = FALSE;
21420
21421 return get_tv_number_chk(varp, &error); /* return 0L on error */
21422}
21423
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021424 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021425get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021426{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021427 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021429 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021431 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021432 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021433 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021434#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021435 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021436 break;
21437#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021438 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021439 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021440 break;
21441 case VAR_STRING:
21442 if (varp->vval.v_string != NULL)
21443 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021444 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021445 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021446 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021447 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021448 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021449 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021450 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021451 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021452 case VAR_SPECIAL:
21453 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21454 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021455 case VAR_JOB:
21456#ifdef FEAT_JOB
21457 EMSG(_("E910: Using a Job as a Number"));
21458 break;
21459#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021460 case VAR_UNKNOWN:
21461 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021464 if (denote == NULL) /* useful for values that must be unsigned */
21465 n = -1;
21466 else
21467 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021468 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469}
21470
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021471#ifdef FEAT_FLOAT
21472 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021473get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021474{
21475 switch (varp->v_type)
21476 {
21477 case VAR_NUMBER:
21478 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021479 case VAR_FLOAT:
21480 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021481 case VAR_FUNC:
21482 EMSG(_("E891: Using a Funcref as a Float"));
21483 break;
21484 case VAR_STRING:
21485 EMSG(_("E892: Using a String as a Float"));
21486 break;
21487 case VAR_LIST:
21488 EMSG(_("E893: Using a List as a Float"));
21489 break;
21490 case VAR_DICT:
21491 EMSG(_("E894: Using a Dictionary as a Float"));
21492 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021493 case VAR_SPECIAL:
21494 EMSG(_("E907: Using a special value as a Float"));
21495 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021496 case VAR_JOB:
21497# ifdef FEAT_JOB
21498 EMSG(_("E911: Using a Job as a Float"));
21499 break;
21500# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021501 case VAR_UNKNOWN:
21502 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021503 break;
21504 }
21505 return 0;
21506}
21507#endif
21508
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021510 * Get the lnum from the first argument.
21511 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021512 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 */
21514 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021515get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516{
Bram Moolenaar33570922005-01-25 22:26:29 +000021517 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 linenr_T lnum;
21519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021520 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 if (lnum == 0) /* no valid number, try using line() */
21522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021523 rettv.v_type = VAR_NUMBER;
21524 f_line(argvars, &rettv);
21525 lnum = rettv.vval.v_number;
21526 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 }
21528 return lnum;
21529}
21530
21531/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021532 * Get the lnum from the first argument.
21533 * Also accepts "$", then "buf" is used.
21534 * Returns 0 on error.
21535 */
21536 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021537get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021538{
21539 if (argvars[0].v_type == VAR_STRING
21540 && argvars[0].vval.v_string != NULL
21541 && argvars[0].vval.v_string[0] == '$'
21542 && buf != NULL)
21543 return buf->b_ml.ml_line_count;
21544 return get_tv_number_chk(&argvars[0], NULL);
21545}
21546
21547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 * Get the string value of a variable.
21549 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021550 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21551 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 * If the String variable has never been set, return an empty string.
21553 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021554 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21555 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 */
21557 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021558get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559{
21560 static char_u mybuf[NUMBUFLEN];
21561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021562 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563}
21564
21565 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021566get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021568 char_u *res = get_tv_string_buf_chk(varp, buf);
21569
21570 return res != NULL ? res : (char_u *)"";
21571}
21572
Bram Moolenaar7d647822014-04-05 21:28:56 +020021573/*
21574 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21575 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021577get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021578{
21579 static char_u mybuf[NUMBUFLEN];
21580
21581 return get_tv_string_buf_chk(varp, mybuf);
21582}
21583
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021584 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021585get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021586{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021587 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021589 case VAR_NUMBER:
21590 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21591 return buf;
21592 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021593 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021594 break;
21595 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021596 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021597 break;
21598 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021599 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021600 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021601 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021602#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021603 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021604 break;
21605#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606 case VAR_STRING:
21607 if (varp->vval.v_string != NULL)
21608 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021609 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021610 case VAR_SPECIAL:
21611 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21612 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021613 case VAR_JOB:
21614#ifdef FEAT_JOB
21615 {
21616 job_T *job = varp->vval.v_job;
21617 char *status = job->jv_status == JOB_FAILED ? "fail"
21618 : job->jv_status == JOB_ENDED ? "dead"
21619 : "run";
21620# ifdef UNIX
21621 vim_snprintf((char *)buf, NUMBUFLEN,
21622 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021623# elif defined(WIN32)
21624 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar6119e612016-02-11 12:48:36 +010021625 "process %ld %s", (long)job->jv_pi.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021626 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021627# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021628 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021629 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21630# endif
21631 return buf;
21632 }
21633#endif
21634 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021635 case VAR_UNKNOWN:
21636 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021639 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640}
21641
21642/*
21643 * Find variable "name" in the list of variables.
21644 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021645 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021646 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021647 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021649 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021650find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654
Bram Moolenaara7043832005-01-21 11:56:39 +000021655 ht = find_var_ht(name, &varname);
21656 if (htp != NULL)
21657 *htp = ht;
21658 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021660 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661}
21662
21663/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021664 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021665 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021668find_var_in_ht(
21669 hashtab_T *ht,
21670 int htname,
21671 char_u *varname,
21672 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021673{
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 hashitem_T *hi;
21675
21676 if (*varname == NUL)
21677 {
21678 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021679 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021681 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021682 case 'g': return &globvars_var;
21683 case 'v': return &vimvars_var;
21684 case 'b': return &curbuf->b_bufvar;
21685 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021686#ifdef FEAT_WINDOWS
21687 case 't': return &curtab->tp_winvar;
21688#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021689 case 'l': return current_funccal == NULL
21690 ? NULL : &current_funccal->l_vars_var;
21691 case 'a': return current_funccal == NULL
21692 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021693 }
21694 return NULL;
21695 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021696
21697 hi = hash_find(ht, varname);
21698 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021699 {
21700 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021701 * worked find the variable again. Don't auto-load a script if it was
21702 * loaded already, otherwise it would be loaded every time when
21703 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021704 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021705 {
21706 /* Note: script_autoload() may make "hi" invalid. It must either
21707 * be obtained again or not used. */
21708 if (!script_autoload(varname, FALSE) || aborting())
21709 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021711 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021712 if (HASHITEM_EMPTY(hi))
21713 return NULL;
21714 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021716}
21717
21718/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021720 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021721 * Set "varname" to the start of name without ':'.
21722 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021723 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021724find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021726 hashitem_T *hi;
21727
Bram Moolenaar73627d02015-08-11 15:46:09 +020021728 if (name[0] == NUL)
21729 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 if (name[1] != ':')
21731 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021732 /* The name must not start with a colon or #. */
21733 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 return NULL;
21735 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021736
21737 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021738 hi = hash_find(&compat_hashtab, name);
21739 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021740 return &compat_hashtab;
21741
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021744 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 }
21746 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021747 if (*name == 'g') /* global variable */
21748 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021749 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21750 */
21751 if (vim_strchr(name + 2, ':') != NULL
21752 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021753 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021755 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021757 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021758#ifdef FEAT_WINDOWS
21759 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021760 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021761#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 if (*name == 'v') /* v: variable */
21763 return &vimvarht;
21764 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021765 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021766 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021767 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 if (*name == 's' /* script variable */
21769 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21770 return &SCRIPT_VARS(current_SID);
21771 return NULL;
21772}
21773
21774/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021775 * Get function call environment based on bactrace debug level
21776 */
21777 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021778get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021779{
21780 int i;
21781 funccall_T *funccal;
21782 funccall_T *temp_funccal;
21783
21784 funccal = current_funccal;
21785 if (debug_backtrace_level > 0)
21786 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021787 for (i = 0; i < debug_backtrace_level; i++)
21788 {
21789 temp_funccal = funccal->caller;
21790 if (temp_funccal)
21791 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021792 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021793 /* backtrace level overflow. reset to max */
21794 debug_backtrace_level = i;
21795 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021796 }
21797 return funccal;
21798}
21799
21800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021802 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 * Returns NULL when it doesn't exist.
21804 */
21805 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021806get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807{
Bram Moolenaar33570922005-01-25 22:26:29 +000021808 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021810 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 if (v == NULL)
21812 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021813 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814}
21815
21816/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021817 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 * sourcing this script and when executing functions defined in the script.
21819 */
21820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021821new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822{
Bram Moolenaara7043832005-01-21 11:56:39 +000021823 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 hashtab_T *ht;
21825 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021826
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21828 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021829 /* Re-allocating ga_data means that an ht_array pointing to
21830 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021831 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021832 for (i = 1; i <= ga_scripts.ga_len; ++i)
21833 {
21834 ht = &SCRIPT_VARS(i);
21835 if (ht->ht_mask == HT_INIT_SIZE - 1)
21836 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021837 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021838 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021839 }
21840
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 while (ga_scripts.ga_len < id)
21842 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021843 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021844 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021845 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 }
21848 }
21849}
21850
21851/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021852 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21853 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 */
21855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021856init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857{
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021859 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021860 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021861 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021862 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 dict_var->di_tv.vval.v_dict = dict;
21864 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021865 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21867 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868}
21869
21870/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021871 * Unreference a dictionary initialized by init_var_dict().
21872 */
21873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021874unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021875{
21876 /* Now the dict needs to be freed if no one else is using it, go back to
21877 * normal reference counting. */
21878 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21879 dict_unref(dict);
21880}
21881
21882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021884 * Frees all allocated variables and the value they contain.
21885 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 */
21887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021888vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021889{
21890 vars_clear_ext(ht, TRUE);
21891}
21892
21893/*
21894 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21895 */
21896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021897vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898{
Bram Moolenaara7043832005-01-21 11:56:39 +000021899 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 hashitem_T *hi;
21901 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021904 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021905 for (hi = ht->ht_array; todo > 0; ++hi)
21906 {
21907 if (!HASHITEM_EMPTY(hi))
21908 {
21909 --todo;
21910
Bram Moolenaar33570922005-01-25 22:26:29 +000021911 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021912 * ht_array might change then. hash_clear() takes care of it
21913 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 v = HI2DI(hi);
21915 if (free_val)
21916 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021917 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021919 }
21920 }
21921 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021922 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923}
21924
Bram Moolenaara7043832005-01-21 11:56:39 +000021925/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 * Delete a variable from hashtab "ht" at item "hi".
21927 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021930delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931{
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021933
21934 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021935 clear_tv(&di->di_tv);
21936 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937}
21938
21939/*
21940 * List the value of one internal variable.
21941 */
21942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021943list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021945 char_u *tofree;
21946 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021947 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021949 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021950 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021951 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021952 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953}
21954
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021956list_one_var_a(
21957 char_u *prefix,
21958 char_u *name,
21959 int type,
21960 char_u *string,
21961 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962{
Bram Moolenaar31859182007-08-14 20:41:13 +000021963 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21964 msg_start();
21965 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 if (name != NULL) /* "a:" vars don't have a name stored */
21967 msg_puts(name);
21968 msg_putchar(' ');
21969 msg_advance(22);
21970 if (type == VAR_NUMBER)
21971 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021972 else if (type == VAR_FUNC)
21973 msg_putchar('*');
21974 else if (type == VAR_LIST)
21975 {
21976 msg_putchar('[');
21977 if (*string == '[')
21978 ++string;
21979 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021980 else if (type == VAR_DICT)
21981 {
21982 msg_putchar('{');
21983 if (*string == '{')
21984 ++string;
21985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 else
21987 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021988
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021990
21991 if (type == VAR_FUNC)
21992 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021993 if (*first)
21994 {
21995 msg_clr_eos();
21996 *first = FALSE;
21997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998}
21999
22000/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022001 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 * If the variable already exists, the value is updated.
22003 * Otherwise the variable is created.
22004 */
22005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022006set_var(
22007 char_u *name,
22008 typval_T *tv,
22009 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010{
Bram Moolenaar33570922005-01-25 22:26:29 +000022011 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022013 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022015 ht = find_var_ht(name, &varname);
22016 if (ht == NULL || *varname == NUL)
22017 {
22018 EMSG2(_(e_illvar), name);
22019 return;
22020 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022021 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022022
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022023 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22024 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022025
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022028 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022029 if (var_check_ro(v->di_flags, name, FALSE)
22030 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 return;
22032 if (v->di_tv.v_type != tv->v_type
22033 && !((v->di_tv.v_type == VAR_STRING
22034 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022035 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022036 || tv->v_type == VAR_NUMBER))
22037#ifdef FEAT_FLOAT
22038 && !((v->di_tv.v_type == VAR_NUMBER
22039 || v->di_tv.v_type == VAR_FLOAT)
22040 && (tv->v_type == VAR_NUMBER
22041 || tv->v_type == VAR_FLOAT))
22042#endif
22043 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022044 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022045 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022046 return;
22047 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022048
22049 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022050 * Handle setting internal v: variables separately where needed to
22051 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022052 */
22053 if (ht == &vimvarht)
22054 {
22055 if (v->di_tv.v_type == VAR_STRING)
22056 {
22057 vim_free(v->di_tv.vval.v_string);
22058 if (copy || tv->v_type != VAR_STRING)
22059 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22060 else
22061 {
22062 /* Take over the string to avoid an extra alloc/free. */
22063 v->di_tv.vval.v_string = tv->vval.v_string;
22064 tv->vval.v_string = NULL;
22065 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022066 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022067 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022068 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022069 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022070 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022071 if (STRCMP(varname, "searchforward") == 0)
22072 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022073#ifdef FEAT_SEARCH_EXTRA
22074 else if (STRCMP(varname, "hlsearch") == 0)
22075 {
22076 no_hlsearch = !v->di_tv.vval.v_number;
22077 redraw_all_later(SOME_VALID);
22078 }
22079#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022080 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022081 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022082 else if (v->di_tv.v_type != tv->v_type)
22083 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022084 }
22085
22086 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 }
22088 else /* add a new variable */
22089 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022090 /* Can't add "v:" variable. */
22091 if (ht == &vimvarht)
22092 {
22093 EMSG2(_(e_illvar), name);
22094 return;
22095 }
22096
Bram Moolenaar92124a32005-06-17 22:03:40 +000022097 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022098 if (!valid_varname(varname))
22099 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022100
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022101 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22102 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022103 if (v == NULL)
22104 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022106 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022108 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 return;
22110 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022111 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022113
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022114 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022115 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022116 else
22117 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022119 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022120 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122}
22123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022124/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022125 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022126 * Also give an error message.
22127 */
22128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022129var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022130{
22131 if (flags & DI_FLAGS_RO)
22132 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022133 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022134 return TRUE;
22135 }
22136 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22137 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022138 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022139 return TRUE;
22140 }
22141 return FALSE;
22142}
22143
22144/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022145 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22146 * Also give an error message.
22147 */
22148 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022149var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022150{
22151 if (flags & DI_FLAGS_FIX)
22152 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022153 EMSG2(_("E795: Cannot delete variable %s"),
22154 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022155 return TRUE;
22156 }
22157 return FALSE;
22158}
22159
22160/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022161 * Check if a funcref is assigned to a valid variable name.
22162 * Return TRUE and give an error if not.
22163 */
22164 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022165var_check_func_name(
22166 char_u *name, /* points to start of variable name */
22167 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022168{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022169 /* Allow for w: b: s: and t:. */
22170 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022171 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22172 ? name[2] : name[0]))
22173 {
22174 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22175 name);
22176 return TRUE;
22177 }
22178 /* Don't allow hiding a function. When "v" is not NULL we might be
22179 * assigning another function to the same var, the type is checked
22180 * below. */
22181 if (new_var && function_exists(name))
22182 {
22183 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22184 name);
22185 return TRUE;
22186 }
22187 return FALSE;
22188}
22189
22190/*
22191 * Check if a variable name is valid.
22192 * Return FALSE and give an error if not.
22193 */
22194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022195valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022196{
22197 char_u *p;
22198
22199 for (p = varname; *p != NUL; ++p)
22200 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22201 && *p != AUTOLOAD_CHAR)
22202 {
22203 EMSG2(_(e_illvar), varname);
22204 return FALSE;
22205 }
22206 return TRUE;
22207}
22208
22209/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022210 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022211 * Also give an error message, using "name" or _("name") when use_gettext is
22212 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022213 */
22214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022216{
22217 if (lock & VAR_LOCKED)
22218 {
22219 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022220 name == NULL ? (char_u *)_("Unknown")
22221 : use_gettext ? (char_u *)_(name)
22222 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022223 return TRUE;
22224 }
22225 if (lock & VAR_FIXED)
22226 {
22227 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022228 name == NULL ? (char_u *)_("Unknown")
22229 : use_gettext ? (char_u *)_(name)
22230 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022231 return TRUE;
22232 }
22233 return FALSE;
22234}
22235
22236/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022237 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022238 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022239 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022240 * It is OK for "from" and "to" to point to the same item. This is used to
22241 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022242 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022244copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022246 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022247 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022248 switch (from->v_type)
22249 {
22250 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022251 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022252 to->vval.v_number = from->vval.v_number;
22253 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022254 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022255#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022256 to->vval.v_float = from->vval.v_float;
22257 break;
22258#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022259 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022260#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022261 to->vval.v_job = from->vval.v_job;
22262 ++to->vval.v_job->jv_refcount;
22263 break;
22264#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022265 case VAR_STRING:
22266 case VAR_FUNC:
22267 if (from->vval.v_string == NULL)
22268 to->vval.v_string = NULL;
22269 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022270 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022271 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022272 if (from->v_type == VAR_FUNC)
22273 func_ref(to->vval.v_string);
22274 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022275 break;
22276 case VAR_LIST:
22277 if (from->vval.v_list == NULL)
22278 to->vval.v_list = NULL;
22279 else
22280 {
22281 to->vval.v_list = from->vval.v_list;
22282 ++to->vval.v_list->lv_refcount;
22283 }
22284 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022285 case VAR_DICT:
22286 if (from->vval.v_dict == NULL)
22287 to->vval.v_dict = NULL;
22288 else
22289 {
22290 to->vval.v_dict = from->vval.v_dict;
22291 ++to->vval.v_dict->dv_refcount;
22292 }
22293 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022294 case VAR_UNKNOWN:
22295 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022296 break;
22297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298}
22299
22300/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022301 * Make a copy of an item.
22302 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022303 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22304 * reference to an already copied list/dict can be used.
22305 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022306 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022308item_copy(
22309 typval_T *from,
22310 typval_T *to,
22311 int deep,
22312 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022313{
22314 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022315 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022316
Bram Moolenaar33570922005-01-25 22:26:29 +000022317 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022318 {
22319 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022320 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022321 }
22322 ++recurse;
22323
22324 switch (from->v_type)
22325 {
22326 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022327 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022328 case VAR_STRING:
22329 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022330 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022331 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022332 copy_tv(from, to);
22333 break;
22334 case VAR_LIST:
22335 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022336 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022337 if (from->vval.v_list == NULL)
22338 to->vval.v_list = NULL;
22339 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22340 {
22341 /* use the copy made earlier */
22342 to->vval.v_list = from->vval.v_list->lv_copylist;
22343 ++to->vval.v_list->lv_refcount;
22344 }
22345 else
22346 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22347 if (to->vval.v_list == NULL)
22348 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022349 break;
22350 case VAR_DICT:
22351 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022352 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022353 if (from->vval.v_dict == NULL)
22354 to->vval.v_dict = NULL;
22355 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22356 {
22357 /* use the copy made earlier */
22358 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22359 ++to->vval.v_dict->dv_refcount;
22360 }
22361 else
22362 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22363 if (to->vval.v_dict == NULL)
22364 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022365 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022366 case VAR_UNKNOWN:
22367 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022368 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022369 }
22370 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022371 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022372}
22373
22374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 * ":echo expr1 ..." print each argument separated with a space, add a
22376 * newline at the end.
22377 * ":echon expr1 ..." print each argument plain.
22378 */
22379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022380ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381{
22382 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 char_u *p;
22386 int needclr = TRUE;
22387 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022388 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389
22390 if (eap->skip)
22391 ++emsg_skip;
22392 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22393 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022394 /* If eval1() causes an error message the text from the command may
22395 * still need to be cleared. E.g., "echo 22,44". */
22396 need_clr_eos = needclr;
22397
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022399 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 {
22401 /*
22402 * Report the invalid expression unless the expression evaluation
22403 * has been cancelled due to an aborting error, an interrupt, or an
22404 * exception.
22405 */
22406 if (!aborting())
22407 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022408 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 break;
22410 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022411 need_clr_eos = FALSE;
22412
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 if (!eap->skip)
22414 {
22415 if (atstart)
22416 {
22417 atstart = FALSE;
22418 /* Call msg_start() after eval1(), evaluating the expression
22419 * may cause a message to appear. */
22420 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022421 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022422 /* Mark the saved text as finishing the line, so that what
22423 * follows is displayed on a new line when scrolling back
22424 * at the more prompt. */
22425 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 }
22429 else if (eap->cmdidx == CMD_echo)
22430 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022431 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022432 if (p != NULL)
22433 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022435 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022437 if (*p != TAB && needclr)
22438 {
22439 /* remove any text still there from the command */
22440 msg_clr_eos();
22441 needclr = FALSE;
22442 }
22443 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 }
22445 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022446 {
22447#ifdef FEAT_MBYTE
22448 if (has_mbyte)
22449 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022450 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022451
22452 (void)msg_outtrans_len_attr(p, i, echo_attr);
22453 p += i - 1;
22454 }
22455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022457 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022460 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022462 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 arg = skipwhite(arg);
22464 }
22465 eap->nextcmd = check_nextcmd(arg);
22466
22467 if (eap->skip)
22468 --emsg_skip;
22469 else
22470 {
22471 /* remove text that may still be there from the command */
22472 if (needclr)
22473 msg_clr_eos();
22474 if (eap->cmdidx == CMD_echo)
22475 msg_end();
22476 }
22477}
22478
22479/*
22480 * ":echohl {name}".
22481 */
22482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022483ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484{
22485 int id;
22486
22487 id = syn_name2id(eap->arg);
22488 if (id == 0)
22489 echo_attr = 0;
22490 else
22491 echo_attr = syn_id2attr(id);
22492}
22493
22494/*
22495 * ":execute expr1 ..." execute the result of an expression.
22496 * ":echomsg expr1 ..." Print a message
22497 * ":echoerr expr1 ..." Print an error
22498 * Each gets spaces around each argument and a newline at the end for
22499 * echo commands
22500 */
22501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022502ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503{
22504 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022505 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 int ret = OK;
22507 char_u *p;
22508 garray_T ga;
22509 int len;
22510 int save_did_emsg;
22511
22512 ga_init2(&ga, 1, 80);
22513
22514 if (eap->skip)
22515 ++emsg_skip;
22516 while (*arg != NUL && *arg != '|' && *arg != '\n')
22517 {
22518 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022519 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 {
22521 /*
22522 * Report the invalid expression unless the expression evaluation
22523 * has been cancelled due to an aborting error, an interrupt, or an
22524 * exception.
22525 */
22526 if (!aborting())
22527 EMSG2(_(e_invexpr2), p);
22528 ret = FAIL;
22529 break;
22530 }
22531
22532 if (!eap->skip)
22533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022534 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 len = (int)STRLEN(p);
22536 if (ga_grow(&ga, len + 2) == FAIL)
22537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 ret = FAIL;
22540 break;
22541 }
22542 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 ga.ga_len += len;
22546 }
22547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022548 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 arg = skipwhite(arg);
22550 }
22551
22552 if (ret != FAIL && ga.ga_data != NULL)
22553 {
22554 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022557 out_flush();
22558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 else if (eap->cmdidx == CMD_echoerr)
22560 {
22561 /* We don't want to abort following commands, restore did_emsg. */
22562 save_did_emsg = did_emsg;
22563 EMSG((char_u *)ga.ga_data);
22564 if (!force_abort)
22565 did_emsg = save_did_emsg;
22566 }
22567 else if (eap->cmdidx == CMD_execute)
22568 do_cmdline((char_u *)ga.ga_data,
22569 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22570 }
22571
22572 ga_clear(&ga);
22573
22574 if (eap->skip)
22575 --emsg_skip;
22576
22577 eap->nextcmd = check_nextcmd(arg);
22578}
22579
22580/*
22581 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22582 * "arg" points to the "&" or '+' when called, to "option" when returning.
22583 * Returns NULL when no option name found. Otherwise pointer to the char
22584 * after the option name.
22585 */
22586 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022587find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588{
22589 char_u *p = *arg;
22590
22591 ++p;
22592 if (*p == 'g' && p[1] == ':')
22593 {
22594 *opt_flags = OPT_GLOBAL;
22595 p += 2;
22596 }
22597 else if (*p == 'l' && p[1] == ':')
22598 {
22599 *opt_flags = OPT_LOCAL;
22600 p += 2;
22601 }
22602 else
22603 *opt_flags = 0;
22604
22605 if (!ASCII_ISALPHA(*p))
22606 return NULL;
22607 *arg = p;
22608
22609 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22610 p += 4; /* termcap option */
22611 else
22612 while (ASCII_ISALPHA(*p))
22613 ++p;
22614 return p;
22615}
22616
22617/*
22618 * ":function"
22619 */
22620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022621ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622{
22623 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022624 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 int j;
22626 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022628 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 char_u *name = NULL;
22630 char_u *p;
22631 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022632 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 garray_T newargs;
22634 garray_T newlines;
22635 int varargs = FALSE;
22636 int mustend = FALSE;
22637 int flags = 0;
22638 ufunc_T *fp;
22639 int indent;
22640 int nesting;
22641 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 dictitem_T *v;
22643 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022644 static int func_nr = 0; /* number for nameless function */
22645 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022646 hashtab_T *ht;
22647 int todo;
22648 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022649 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650
22651 /*
22652 * ":function" without argument: list functions.
22653 */
22654 if (ends_excmd(*eap->arg))
22655 {
22656 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022657 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022658 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022659 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022660 {
22661 if (!HASHITEM_EMPTY(hi))
22662 {
22663 --todo;
22664 fp = HI2UF(hi);
22665 if (!isdigit(*fp->uf_name))
22666 list_func_head(fp, FALSE);
22667 }
22668 }
22669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 eap->nextcmd = check_nextcmd(eap->arg);
22671 return;
22672 }
22673
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022675 * ":function /pat": list functions matching pattern.
22676 */
22677 if (*eap->arg == '/')
22678 {
22679 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22680 if (!eap->skip)
22681 {
22682 regmatch_T regmatch;
22683
22684 c = *p;
22685 *p = NUL;
22686 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22687 *p = c;
22688 if (regmatch.regprog != NULL)
22689 {
22690 regmatch.rm_ic = p_ic;
22691
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022692 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022693 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22694 {
22695 if (!HASHITEM_EMPTY(hi))
22696 {
22697 --todo;
22698 fp = HI2UF(hi);
22699 if (!isdigit(*fp->uf_name)
22700 && vim_regexec(&regmatch, fp->uf_name, 0))
22701 list_func_head(fp, FALSE);
22702 }
22703 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022704 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022705 }
22706 }
22707 if (*p == '/')
22708 ++p;
22709 eap->nextcmd = check_nextcmd(p);
22710 return;
22711 }
22712
22713 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022714 * Get the function name. There are these situations:
22715 * func normal function name
22716 * "name" == func, "fudi.fd_dict" == NULL
22717 * dict.func new dictionary entry
22718 * "name" == NULL, "fudi.fd_dict" set,
22719 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22720 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022721 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022722 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22723 * dict.func existing dict entry that's not a Funcref
22724 * "name" == NULL, "fudi.fd_dict" set,
22725 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022726 * s:func script-local function name
22727 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022730 name = trans_function_name(&p, eap->skip, 0, &fudi);
22731 paren = (vim_strchr(p, '(') != NULL);
22732 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 {
22734 /*
22735 * Return on an invalid expression in braces, unless the expression
22736 * evaluation has been cancelled due to an aborting error, an
22737 * interrupt, or an exception.
22738 */
22739 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022741 if (!eap->skip && fudi.fd_newkey != NULL)
22742 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022743 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 else
22747 eap->skip = TRUE;
22748 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022749
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 /* An error in a function call during evaluation of an expression in magic
22751 * braces should not cause the function not to be defined. */
22752 saved_did_emsg = did_emsg;
22753 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754
22755 /*
22756 * ":function func" with only function name: list function.
22757 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022758 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 {
22760 if (!ends_excmd(*skipwhite(p)))
22761 {
22762 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022763 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 }
22765 eap->nextcmd = check_nextcmd(p);
22766 if (eap->nextcmd != NULL)
22767 *p = NUL;
22768 if (!eap->skip && !got_int)
22769 {
22770 fp = find_func(name);
22771 if (fp != NULL)
22772 {
22773 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022774 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022776 if (FUNCLINE(fp, j) == NULL)
22777 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 msg_putchar('\n');
22779 msg_outnum((long)(j + 1));
22780 if (j < 9)
22781 msg_putchar(' ');
22782 if (j < 99)
22783 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022784 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 out_flush(); /* show a line at a time */
22786 ui_breakcheck();
22787 }
22788 if (!got_int)
22789 {
22790 msg_putchar('\n');
22791 msg_puts((char_u *)" endfunction");
22792 }
22793 }
22794 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022795 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022797 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 }
22799
22800 /*
22801 * ":function name(arg1, arg2)" Define function.
22802 */
22803 p = skipwhite(p);
22804 if (*p != '(')
22805 {
22806 if (!eap->skip)
22807 {
22808 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022809 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 }
22811 /* attempt to continue by skipping some text */
22812 if (vim_strchr(p, '(') != NULL)
22813 p = vim_strchr(p, '(');
22814 }
22815 p = skipwhite(p + 1);
22816
22817 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22818 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22819
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022820 if (!eap->skip)
22821 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022822 /* Check the name of the function. Unless it's a dictionary function
22823 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022824 if (name != NULL)
22825 arg = name;
22826 else
22827 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022828 if (arg != NULL && (fudi.fd_di == NULL
22829 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022830 {
22831 if (*arg == K_SPECIAL)
22832 j = 3;
22833 else
22834 j = 0;
22835 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22836 : eval_isnamec(arg[j])))
22837 ++j;
22838 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022839 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022840 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022841 /* Disallow using the g: dict. */
22842 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22843 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022844 }
22845
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 /*
22847 * Isolate the arguments: "arg1, arg2, ...)"
22848 */
22849 while (*p != ')')
22850 {
22851 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22852 {
22853 varargs = TRUE;
22854 p += 3;
22855 mustend = TRUE;
22856 }
22857 else
22858 {
22859 arg = p;
22860 while (ASCII_ISALNUM(*p) || *p == '_')
22861 ++p;
22862 if (arg == p || isdigit(*arg)
22863 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22864 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22865 {
22866 if (!eap->skip)
22867 EMSG2(_("E125: Illegal argument: %s"), arg);
22868 break;
22869 }
22870 if (ga_grow(&newargs, 1) == FAIL)
22871 goto erret;
22872 c = *p;
22873 *p = NUL;
22874 arg = vim_strsave(arg);
22875 if (arg == NULL)
22876 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022877
22878 /* Check for duplicate argument name. */
22879 for (i = 0; i < newargs.ga_len; ++i)
22880 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22881 {
22882 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022883 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022884 goto erret;
22885 }
22886
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22888 *p = c;
22889 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 if (*p == ',')
22891 ++p;
22892 else
22893 mustend = TRUE;
22894 }
22895 p = skipwhite(p);
22896 if (mustend && *p != ')')
22897 {
22898 if (!eap->skip)
22899 EMSG2(_(e_invarg2), eap->arg);
22900 break;
22901 }
22902 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022903 if (*p != ')')
22904 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 ++p; /* skip the ')' */
22906
Bram Moolenaare9a41262005-01-15 22:18:47 +000022907 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 for (;;)
22909 {
22910 p = skipwhite(p);
22911 if (STRNCMP(p, "range", 5) == 0)
22912 {
22913 flags |= FC_RANGE;
22914 p += 5;
22915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022916 else if (STRNCMP(p, "dict", 4) == 0)
22917 {
22918 flags |= FC_DICT;
22919 p += 4;
22920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 else if (STRNCMP(p, "abort", 5) == 0)
22922 {
22923 flags |= FC_ABORT;
22924 p += 5;
22925 }
22926 else
22927 break;
22928 }
22929
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022930 /* When there is a line break use what follows for the function body.
22931 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22932 if (*p == '\n')
22933 line_arg = p + 1;
22934 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 EMSG(_(e_trailing));
22936
22937 /*
22938 * Read the body of the function, until ":endfunction" is found.
22939 */
22940 if (KeyTyped)
22941 {
22942 /* Check if the function already exists, don't let the user type the
22943 * whole function before telling him it doesn't work! For a script we
22944 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022945 if (!eap->skip && !eap->forceit)
22946 {
22947 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22948 EMSG(_(e_funcdict));
22949 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022950 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022953 if (!eap->skip && did_emsg)
22954 goto erret;
22955
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 msg_putchar('\n'); /* don't overwrite the function name */
22957 cmdline_row = msg_row;
22958 }
22959
22960 indent = 2;
22961 nesting = 0;
22962 for (;;)
22963 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022964 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022965 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022966 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022967 saved_wait_return = FALSE;
22968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022970 sourcing_lnum_off = sourcing_lnum;
22971
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022972 if (line_arg != NULL)
22973 {
22974 /* Use eap->arg, split up in parts by line breaks. */
22975 theline = line_arg;
22976 p = vim_strchr(theline, '\n');
22977 if (p == NULL)
22978 line_arg += STRLEN(line_arg);
22979 else
22980 {
22981 *p = NUL;
22982 line_arg = p + 1;
22983 }
22984 }
22985 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 theline = getcmdline(':', 0L, indent);
22987 else
22988 theline = eap->getline(':', eap->cookie, indent);
22989 if (KeyTyped)
22990 lines_left = Rows - 1;
22991 if (theline == NULL)
22992 {
22993 EMSG(_("E126: Missing :endfunction"));
22994 goto erret;
22995 }
22996
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022997 /* Detect line continuation: sourcing_lnum increased more than one. */
22998 if (sourcing_lnum > sourcing_lnum_off + 1)
22999 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23000 else
23001 sourcing_lnum_off = 0;
23002
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 if (skip_until != NULL)
23004 {
23005 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23006 * don't check for ":endfunc". */
23007 if (STRCMP(theline, skip_until) == 0)
23008 {
23009 vim_free(skip_until);
23010 skip_until = NULL;
23011 }
23012 }
23013 else
23014 {
23015 /* skip ':' and blanks*/
23016 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23017 ;
23018
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023019 /* Check for "endfunction". */
23020 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023022 if (line_arg == NULL)
23023 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 break;
23025 }
23026
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023027 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 * at "end". */
23029 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23030 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023031 else if (STRNCMP(p, "if", 2) == 0
23032 || STRNCMP(p, "wh", 2) == 0
23033 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 || STRNCMP(p, "try", 3) == 0)
23035 indent += 2;
23036
23037 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023038 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023040 if (*p == '!')
23041 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023043 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23044 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023046 ++nesting;
23047 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 }
23049 }
23050
23051 /* Check for ":append" or ":insert". */
23052 p = skip_range(p, NULL);
23053 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23054 || (p[0] == 'i'
23055 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23056 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23057 skip_until = vim_strsave((char_u *)".");
23058
23059 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23060 arg = skipwhite(skiptowhite(p));
23061 if (arg[0] == '<' && arg[1] =='<'
23062 && ((p[0] == 'p' && p[1] == 'y'
23063 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23064 || (p[0] == 'p' && p[1] == 'e'
23065 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23066 || (p[0] == 't' && p[1] == 'c'
23067 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023068 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23069 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23071 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023072 || (p[0] == 'm' && p[1] == 'z'
23073 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 ))
23075 {
23076 /* ":python <<" continues until a dot, like ":append" */
23077 p = skipwhite(arg + 2);
23078 if (*p == NUL)
23079 skip_until = vim_strsave((char_u *)".");
23080 else
23081 skip_until = vim_strsave(p);
23082 }
23083 }
23084
23085 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023086 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023087 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023088 if (line_arg == NULL)
23089 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023091 }
23092
23093 /* Copy the line to newly allocated memory. get_one_sourceline()
23094 * allocates 250 bytes per line, this saves 80% on average. The cost
23095 * is an extra alloc/free. */
23096 p = vim_strsave(theline);
23097 if (p != NULL)
23098 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023099 if (line_arg == NULL)
23100 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023101 theline = p;
23102 }
23103
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023104 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23105
23106 /* Add NULL lines for continuation lines, so that the line count is
23107 * equal to the index in the growarray. */
23108 while (sourcing_lnum_off-- > 0)
23109 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023110
23111 /* Check for end of eap->arg. */
23112 if (line_arg != NULL && *line_arg == NUL)
23113 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 }
23115
23116 /* Don't define the function when skipping commands or when an error was
23117 * detected. */
23118 if (eap->skip || did_emsg)
23119 goto erret;
23120
23121 /*
23122 * If there are no errors, add the function
23123 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023124 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023125 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023126 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023127 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023128 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023129 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023130 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023131 goto erret;
23132 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023133
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023134 fp = find_func(name);
23135 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023137 if (!eap->forceit)
23138 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023139 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023140 goto erret;
23141 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023142 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023143 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023144 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023145 name);
23146 goto erret;
23147 }
23148 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023149 ga_clear_strings(&(fp->uf_args));
23150 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023151 vim_free(name);
23152 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 }
23155 else
23156 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023157 char numbuf[20];
23158
23159 fp = NULL;
23160 if (fudi.fd_newkey == NULL && !eap->forceit)
23161 {
23162 EMSG(_(e_funcdict));
23163 goto erret;
23164 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023165 if (fudi.fd_di == NULL)
23166 {
23167 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023168 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023169 goto erret;
23170 }
23171 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023172 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023173 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023174
23175 /* Give the function a sequential number. Can only be used with a
23176 * Funcref! */
23177 vim_free(name);
23178 sprintf(numbuf, "%d", ++func_nr);
23179 name = vim_strsave((char_u *)numbuf);
23180 if (name == NULL)
23181 goto erret;
23182 }
23183
23184 if (fp == NULL)
23185 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023186 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023187 {
23188 int slen, plen;
23189 char_u *scriptname;
23190
23191 /* Check that the autoload name matches the script name. */
23192 j = FAIL;
23193 if (sourcing_name != NULL)
23194 {
23195 scriptname = autoload_name(name);
23196 if (scriptname != NULL)
23197 {
23198 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023199 plen = (int)STRLEN(p);
23200 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023201 if (slen > plen && fnamecmp(p,
23202 sourcing_name + slen - plen) == 0)
23203 j = OK;
23204 vim_free(scriptname);
23205 }
23206 }
23207 if (j == FAIL)
23208 {
23209 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23210 goto erret;
23211 }
23212 }
23213
23214 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 if (fp == NULL)
23216 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023217
23218 if (fudi.fd_dict != NULL)
23219 {
23220 if (fudi.fd_di == NULL)
23221 {
23222 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023223 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023224 if (fudi.fd_di == NULL)
23225 {
23226 vim_free(fp);
23227 goto erret;
23228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023229 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23230 {
23231 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023232 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023233 goto erret;
23234 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023235 }
23236 else
23237 /* overwrite existing dict entry */
23238 clear_tv(&fudi.fd_di->di_tv);
23239 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023240 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023241 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023242 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023243
23244 /* behave like "dict" was used */
23245 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023246 }
23247
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023249 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023250 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23251 {
23252 vim_free(fp);
23253 goto erret;
23254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023256 fp->uf_args = newargs;
23257 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023258#ifdef FEAT_PROFILE
23259 fp->uf_tml_count = NULL;
23260 fp->uf_tml_total = NULL;
23261 fp->uf_tml_self = NULL;
23262 fp->uf_profiling = FALSE;
23263 if (prof_def_func())
23264 func_do_profile(fp);
23265#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023266 fp->uf_varargs = varargs;
23267 fp->uf_flags = flags;
23268 fp->uf_calls = 0;
23269 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023270 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271
23272erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 ga_clear_strings(&newargs);
23274 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023275ret_free:
23276 vim_free(skip_until);
23277 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023280 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281}
23282
23283/*
23284 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023285 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023287 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023288 * TFN_INT: internal function name OK
23289 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023290 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 * Advances "pp" to just after the function name (if no error).
23292 */
23293 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023294trans_function_name(
23295 char_u **pp,
23296 int skip, /* only find the end, don't evaluate */
23297 int flags,
23298 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023300 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 char_u *start;
23302 char_u *end;
23303 int lead;
23304 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023306 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023307
23308 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023309 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023311
23312 /* Check for hard coded <SNR>: already translated function ID (from a user
23313 * command). */
23314 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23315 && (*pp)[2] == (int)KE_SNR)
23316 {
23317 *pp += 3;
23318 len = get_id_len(pp) + 3;
23319 return vim_strnsave(start, len);
23320 }
23321
23322 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23323 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023325 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023327
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023328 /* Note that TFN_ flags use the same values as GLV_ flags. */
23329 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023330 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023331 if (end == start)
23332 {
23333 if (!skip)
23334 EMSG(_("E129: Function name required"));
23335 goto theend;
23336 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023337 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023338 {
23339 /*
23340 * Report an invalid expression in braces, unless the expression
23341 * evaluation has been cancelled due to an aborting error, an
23342 * interrupt, or an exception.
23343 */
23344 if (!aborting())
23345 {
23346 if (end != NULL)
23347 EMSG2(_(e_invarg2), start);
23348 }
23349 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023350 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023351 goto theend;
23352 }
23353
23354 if (lv.ll_tv != NULL)
23355 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023356 if (fdp != NULL)
23357 {
23358 fdp->fd_dict = lv.ll_dict;
23359 fdp->fd_newkey = lv.ll_newkey;
23360 lv.ll_newkey = NULL;
23361 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023362 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023363 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23364 {
23365 name = vim_strsave(lv.ll_tv->vval.v_string);
23366 *pp = end;
23367 }
23368 else
23369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023370 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23371 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023372 EMSG(_(e_funcref));
23373 else
23374 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023375 name = NULL;
23376 }
23377 goto theend;
23378 }
23379
23380 if (lv.ll_name == NULL)
23381 {
23382 /* Error found, but continue after the function name. */
23383 *pp = end;
23384 goto theend;
23385 }
23386
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023387 /* Check if the name is a Funcref. If so, use the value. */
23388 if (lv.ll_exp_name != NULL)
23389 {
23390 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023391 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023392 if (name == lv.ll_exp_name)
23393 name = NULL;
23394 }
23395 else
23396 {
23397 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023398 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023399 if (name == *pp)
23400 name = NULL;
23401 }
23402 if (name != NULL)
23403 {
23404 name = vim_strsave(name);
23405 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023406 if (STRNCMP(name, "<SNR>", 5) == 0)
23407 {
23408 /* Change "<SNR>" to the byte sequence. */
23409 name[0] = K_SPECIAL;
23410 name[1] = KS_EXTRA;
23411 name[2] = (int)KE_SNR;
23412 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23413 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023414 goto theend;
23415 }
23416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023417 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023418 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023419 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023420 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23421 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23422 {
23423 /* When there was "s:" already or the name expanded to get a
23424 * leading "s:" then remove it. */
23425 lv.ll_name += 2;
23426 len -= 2;
23427 lead = 2;
23428 }
23429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023430 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023431 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023432 /* skip over "s:" and "g:" */
23433 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023434 lv.ll_name += 2;
23435 len = (int)(end - lv.ll_name);
23436 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023437
23438 /*
23439 * Copy the function name to allocated memory.
23440 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23441 * Accept <SNR>123_name() outside a script.
23442 */
23443 if (skip)
23444 lead = 0; /* do nothing */
23445 else if (lead > 0)
23446 {
23447 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023448 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23449 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023450 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023451 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023452 if (current_SID <= 0)
23453 {
23454 EMSG(_(e_usingsid));
23455 goto theend;
23456 }
23457 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23458 lead += (int)STRLEN(sid_buf);
23459 }
23460 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023461 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023462 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023463 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023464 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023465 goto theend;
23466 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023467 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023468 {
23469 char_u *cp = vim_strchr(lv.ll_name, ':');
23470
23471 if (cp != NULL && cp < end)
23472 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023473 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023474 goto theend;
23475 }
23476 }
23477
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023478 name = alloc((unsigned)(len + lead + 1));
23479 if (name != NULL)
23480 {
23481 if (lead > 0)
23482 {
23483 name[0] = K_SPECIAL;
23484 name[1] = KS_EXTRA;
23485 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023486 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023487 STRCPY(name + 3, sid_buf);
23488 }
23489 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023490 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023491 }
23492 *pp = end;
23493
23494theend:
23495 clear_lval(&lv);
23496 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497}
23498
23499/*
23500 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23501 * Return 2 if "p" starts with "s:".
23502 * Return 0 otherwise.
23503 */
23504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023505eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023507 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23508 * the standard library function. */
23509 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23510 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 return 5;
23512 if (p[0] == 's' && p[1] == ':')
23513 return 2;
23514 return 0;
23515}
23516
23517/*
23518 * Return TRUE if "p" starts with "<SID>" or "s:".
23519 * Only works if eval_fname_script() returned non-zero for "p"!
23520 */
23521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023522eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523{
23524 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23525}
23526
23527/*
23528 * List the head of the function: "name(arg1, arg2)".
23529 */
23530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023531list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532{
23533 int j;
23534
23535 msg_start();
23536 if (indent)
23537 MSG_PUTS(" ");
23538 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023539 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 {
23541 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 }
23544 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023545 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 {
23549 if (j)
23550 MSG_PUTS(", ");
23551 msg_puts(FUNCARG(fp, j));
23552 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023553 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 {
23555 if (j)
23556 MSG_PUTS(", ");
23557 MSG_PUTS("...");
23558 }
23559 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023560 if (fp->uf_flags & FC_ABORT)
23561 MSG_PUTS(" abort");
23562 if (fp->uf_flags & FC_RANGE)
23563 MSG_PUTS(" range");
23564 if (fp->uf_flags & FC_DICT)
23565 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023566 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023567 if (p_verbose > 0)
23568 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569}
23570
23571/*
23572 * Find a function by name, return pointer to it in ufuncs.
23573 * Return NULL for unknown function.
23574 */
23575 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023576find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023578 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023580 hi = hash_find(&func_hashtab, name);
23581 if (!HASHITEM_EMPTY(hi))
23582 return HI2UF(hi);
23583 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584}
23585
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023586#if defined(EXITFREE) || defined(PROTO)
23587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023588free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023589{
23590 hashitem_T *hi;
23591
23592 /* Need to start all over every time, because func_free() may change the
23593 * hash table. */
23594 while (func_hashtab.ht_used > 0)
23595 for (hi = func_hashtab.ht_array; ; ++hi)
23596 if (!HASHITEM_EMPTY(hi))
23597 {
23598 func_free(HI2UF(hi));
23599 break;
23600 }
23601}
23602#endif
23603
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023605translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023606{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023607 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023608 return find_internal_func(name) >= 0;
23609 return find_func(name) != NULL;
23610}
23611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023612/*
23613 * Return TRUE if a function "name" exists.
23614 */
23615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023616function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023617{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023618 char_u *nm = name;
23619 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023620 int n = FALSE;
23621
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023622 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23623 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023624 nm = skipwhite(nm);
23625
23626 /* Only accept "funcname", "funcname ", "funcname (..." and
23627 * "funcname(...", not "funcname!...". */
23628 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023629 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023630 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023631 return n;
23632}
23633
Bram Moolenaara1544c02013-05-30 12:35:52 +020023634 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023635get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023636{
23637 char_u *nm = name;
23638 char_u *p;
23639
23640 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23641
23642 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023643 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023644 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023645
Bram Moolenaara1544c02013-05-30 12:35:52 +020023646 vim_free(p);
23647 return NULL;
23648}
23649
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023650/*
23651 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023652 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23653 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023654 */
23655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023656builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023657{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023658 char_u *p;
23659
23660 if (!ASCII_ISLOWER(name[0]))
23661 return FALSE;
23662 p = vim_strchr(name, AUTOLOAD_CHAR);
23663 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023664}
23665
Bram Moolenaar05159a02005-02-26 23:04:13 +000023666#if defined(FEAT_PROFILE) || defined(PROTO)
23667/*
23668 * Start profiling function "fp".
23669 */
23670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023671func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023672{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023673 int len = fp->uf_lines.ga_len;
23674
23675 if (len == 0)
23676 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023677 fp->uf_tm_count = 0;
23678 profile_zero(&fp->uf_tm_self);
23679 profile_zero(&fp->uf_tm_total);
23680 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023681 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023682 if (fp->uf_tml_total == NULL)
23683 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023684 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023685 if (fp->uf_tml_self == NULL)
23686 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023687 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023688 fp->uf_tml_idx = -1;
23689 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23690 || fp->uf_tml_self == NULL)
23691 return; /* out of memory */
23692
23693 fp->uf_profiling = TRUE;
23694}
23695
23696/*
23697 * Dump the profiling results for all functions in file "fd".
23698 */
23699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023700func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023701{
23702 hashitem_T *hi;
23703 int todo;
23704 ufunc_T *fp;
23705 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023706 ufunc_T **sorttab;
23707 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023708
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023709 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023710 if (todo == 0)
23711 return; /* nothing to dump */
23712
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023713 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023714
Bram Moolenaar05159a02005-02-26 23:04:13 +000023715 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23716 {
23717 if (!HASHITEM_EMPTY(hi))
23718 {
23719 --todo;
23720 fp = HI2UF(hi);
23721 if (fp->uf_profiling)
23722 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023723 if (sorttab != NULL)
23724 sorttab[st_len++] = fp;
23725
Bram Moolenaar05159a02005-02-26 23:04:13 +000023726 if (fp->uf_name[0] == K_SPECIAL)
23727 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23728 else
23729 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23730 if (fp->uf_tm_count == 1)
23731 fprintf(fd, "Called 1 time\n");
23732 else
23733 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23734 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23735 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23736 fprintf(fd, "\n");
23737 fprintf(fd, "count total (s) self (s)\n");
23738
23739 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23740 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023741 if (FUNCLINE(fp, i) == NULL)
23742 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023743 prof_func_line(fd, fp->uf_tml_count[i],
23744 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023745 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23746 }
23747 fprintf(fd, "\n");
23748 }
23749 }
23750 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023751
23752 if (sorttab != NULL && st_len > 0)
23753 {
23754 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23755 prof_total_cmp);
23756 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23757 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23758 prof_self_cmp);
23759 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23760 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023761
23762 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023763}
Bram Moolenaar73830342005-02-28 22:48:19 +000023764
23765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023766prof_sort_list(
23767 FILE *fd,
23768 ufunc_T **sorttab,
23769 int st_len,
23770 char *title,
23771 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023772{
23773 int i;
23774 ufunc_T *fp;
23775
23776 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23777 fprintf(fd, "count total (s) self (s) function\n");
23778 for (i = 0; i < 20 && i < st_len; ++i)
23779 {
23780 fp = sorttab[i];
23781 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23782 prefer_self);
23783 if (fp->uf_name[0] == K_SPECIAL)
23784 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23785 else
23786 fprintf(fd, " %s()\n", fp->uf_name);
23787 }
23788 fprintf(fd, "\n");
23789}
23790
23791/*
23792 * Print the count and times for one function or function line.
23793 */
23794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023795prof_func_line(
23796 FILE *fd,
23797 int count,
23798 proftime_T *total,
23799 proftime_T *self,
23800 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023801{
23802 if (count > 0)
23803 {
23804 fprintf(fd, "%5d ", count);
23805 if (prefer_self && profile_equal(total, self))
23806 fprintf(fd, " ");
23807 else
23808 fprintf(fd, "%s ", profile_msg(total));
23809 if (!prefer_self && profile_equal(total, self))
23810 fprintf(fd, " ");
23811 else
23812 fprintf(fd, "%s ", profile_msg(self));
23813 }
23814 else
23815 fprintf(fd, " ");
23816}
23817
23818/*
23819 * Compare function for total time sorting.
23820 */
23821 static int
23822#ifdef __BORLANDC__
23823_RTLENTRYF
23824#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023825prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023826{
23827 ufunc_T *p1, *p2;
23828
23829 p1 = *(ufunc_T **)s1;
23830 p2 = *(ufunc_T **)s2;
23831 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23832}
23833
23834/*
23835 * Compare function for self time sorting.
23836 */
23837 static int
23838#ifdef __BORLANDC__
23839_RTLENTRYF
23840#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023841prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023842{
23843 ufunc_T *p1, *p2;
23844
23845 p1 = *(ufunc_T **)s1;
23846 p2 = *(ufunc_T **)s2;
23847 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23848}
23849
Bram Moolenaar05159a02005-02-26 23:04:13 +000023850#endif
23851
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023852/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023853 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023854 * Return TRUE if a package was loaded.
23855 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023857script_autoload(
23858 char_u *name,
23859 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023860{
23861 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023862 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023863 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023864 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023865
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023866 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023867 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023868 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023869 return FALSE;
23870
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023871 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023872
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023873 /* Find the name in the list of previously loaded package names. Skip
23874 * "autoload/", it's always the same. */
23875 for (i = 0; i < ga_loaded.ga_len; ++i)
23876 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23877 break;
23878 if (!reload && i < ga_loaded.ga_len)
23879 ret = FALSE; /* was loaded already */
23880 else
23881 {
23882 /* Remember the name if it wasn't loaded already. */
23883 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23884 {
23885 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23886 tofree = NULL;
23887 }
23888
23889 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023890 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023891 ret = TRUE;
23892 }
23893
23894 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023895 return ret;
23896}
23897
23898/*
23899 * Return the autoload script name for a function or variable name.
23900 * Returns NULL when out of memory.
23901 */
23902 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023903autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023904{
23905 char_u *p;
23906 char_u *scriptname;
23907
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023908 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023909 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23910 if (scriptname == NULL)
23911 return FALSE;
23912 STRCPY(scriptname, "autoload/");
23913 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023914 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023915 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023916 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023917 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023918 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023919}
23920
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23922
23923/*
23924 * Function given to ExpandGeneric() to obtain the list of user defined
23925 * function names.
23926 */
23927 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023928get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023930 static long_u done;
23931 static hashitem_T *hi;
23932 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933
23934 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023936 done = 0;
23937 hi = func_hashtab.ht_array;
23938 }
23939 if (done < func_hashtab.ht_used)
23940 {
23941 if (done++ > 0)
23942 ++hi;
23943 while (HASHITEM_EMPTY(hi))
23944 ++hi;
23945 fp = HI2UF(hi);
23946
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023947 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023948 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023949
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023950 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23951 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952
23953 cat_func_name(IObuff, fp);
23954 if (xp->xp_context != EXPAND_USER_FUNC)
23955 {
23956 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023957 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 STRCAT(IObuff, ")");
23959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 return IObuff;
23961 }
23962 return NULL;
23963}
23964
23965#endif /* FEAT_CMDL_COMPL */
23966
23967/*
23968 * Copy the function name of "fp" to buffer "buf".
23969 * "buf" must be able to hold the function name plus three bytes.
23970 * Takes care of script-local function names.
23971 */
23972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023973cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023975 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 {
23977 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023978 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 }
23980 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023981 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982}
23983
23984/*
23985 * ":delfunction {name}"
23986 */
23987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023988ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023990 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 char_u *p;
23992 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023993 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994
23995 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023996 name = trans_function_name(&p, eap->skip, 0, &fudi);
23997 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023999 {
24000 if (fudi.fd_dict != NULL && !eap->skip)
24001 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 if (!ends_excmd(*skipwhite(p)))
24005 {
24006 vim_free(name);
24007 EMSG(_(e_trailing));
24008 return;
24009 }
24010 eap->nextcmd = check_nextcmd(p);
24011 if (eap->nextcmd != NULL)
24012 *p = NUL;
24013
24014 if (!eap->skip)
24015 fp = find_func(name);
24016 vim_free(name);
24017
24018 if (!eap->skip)
24019 {
24020 if (fp == NULL)
24021 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024022 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 return;
24024 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024025 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 {
24027 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24028 return;
24029 }
24030
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024031 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024033 /* Delete the dict item that refers to the function, it will
24034 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024035 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024037 else
24038 func_free(fp);
24039 }
24040}
24041
24042/*
24043 * Free a function and remove it from the list of functions.
24044 */
24045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024046func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024047{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024048 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024049
24050 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024051 ga_clear_strings(&(fp->uf_args));
24052 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024053#ifdef FEAT_PROFILE
24054 vim_free(fp->uf_tml_count);
24055 vim_free(fp->uf_tml_total);
24056 vim_free(fp->uf_tml_self);
24057#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024059 /* remove the function from the function hashtable */
24060 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24061 if (HASHITEM_EMPTY(hi))
24062 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024063 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024064 hash_remove(&func_hashtab, hi);
24065
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024066 vim_free(fp);
24067}
24068
24069/*
24070 * Unreference a Function: decrement the reference count and free it when it
24071 * becomes zero. Only for numbered functions.
24072 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024074func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024075{
24076 ufunc_T *fp;
24077
24078 if (name != NULL && isdigit(*name))
24079 {
24080 fp = find_func(name);
24081 if (fp == NULL)
24082 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024083 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024084 {
24085 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024086 * when "uf_calls" becomes zero. */
24087 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024088 func_free(fp);
24089 }
24090 }
24091}
24092
24093/*
24094 * Count a reference to a Function.
24095 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024097func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024098{
24099 ufunc_T *fp;
24100
24101 if (name != NULL && isdigit(*name))
24102 {
24103 fp = find_func(name);
24104 if (fp == NULL)
24105 EMSG2(_(e_intern2), "func_ref()");
24106 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024107 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 }
24109}
24110
24111/*
24112 * Call a user function.
24113 */
24114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024115call_user_func(
24116 ufunc_T *fp, /* pointer to function */
24117 int argcount, /* nr of args */
24118 typval_T *argvars, /* arguments */
24119 typval_T *rettv, /* return value */
24120 linenr_T firstline, /* first line of range */
24121 linenr_T lastline, /* last line of range */
24122 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123{
Bram Moolenaar33570922005-01-25 22:26:29 +000024124 char_u *save_sourcing_name;
24125 linenr_T save_sourcing_lnum;
24126 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024127 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024128 int save_did_emsg;
24129 static int depth = 0;
24130 dictitem_T *v;
24131 int fixvar_idx = 0; /* index in fixvar[] */
24132 int i;
24133 int ai;
24134 char_u numbuf[NUMBUFLEN];
24135 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024136 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024137#ifdef FEAT_PROFILE
24138 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024139 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141
24142 /* If depth of calling is getting too high, don't execute the function */
24143 if (depth >= p_mfd)
24144 {
24145 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024146 rettv->v_type = VAR_NUMBER;
24147 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 return;
24149 }
24150 ++depth;
24151
24152 line_breakcheck(); /* check for CTRL-C hit */
24153
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024154 fc = (funccall_T *)alloc(sizeof(funccall_T));
24155 fc->caller = current_funccal;
24156 current_funccal = fc;
24157 fc->func = fp;
24158 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024159 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024160 fc->linenr = 0;
24161 fc->returned = FALSE;
24162 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024164 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24165 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166
Bram Moolenaar33570922005-01-25 22:26:29 +000024167 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024168 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024169 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24170 * each argument variable and saves a lot of time.
24171 */
24172 /*
24173 * Init l: variables.
24174 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024175 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024176 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024177 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024178 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24179 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024180 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024181 name = v->di_key;
24182 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024183 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024184 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024185 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024186 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024187 v->di_tv.vval.v_dict = selfdict;
24188 ++selfdict->dv_refcount;
24189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024190
Bram Moolenaar33570922005-01-25 22:26:29 +000024191 /*
24192 * Init a: variables.
24193 * Set a:0 to "argcount".
24194 * Set a:000 to a list with room for the "..." arguments.
24195 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024196 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024197 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024198 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024199 /* Use "name" to avoid a warning from some compiler that checks the
24200 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024201 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024202 name = v->di_key;
24203 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024204 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024205 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024206 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024207 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024208 v->di_tv.vval.v_list = &fc->l_varlist;
24209 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24210 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24211 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024212
24213 /*
24214 * Set a:firstline to "firstline" and a:lastline to "lastline".
24215 * Set a:name to named arguments.
24216 * Set a:N to the "..." arguments.
24217 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024218 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024219 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024220 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024221 (varnumber_T)lastline);
24222 for (i = 0; i < argcount; ++i)
24223 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024224 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024225 if (ai < 0)
24226 /* named argument a:name */
24227 name = FUNCARG(fp, i);
24228 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024229 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024230 /* "..." argument a:1, a:2, etc. */
24231 sprintf((char *)numbuf, "%d", ai + 1);
24232 name = numbuf;
24233 }
24234 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24235 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024236 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024237 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24238 }
24239 else
24240 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024241 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24242 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024243 if (v == NULL)
24244 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024245 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024246 }
24247 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024248 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024249
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024250 /* Note: the values are copied directly to avoid alloc/free.
24251 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024252 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024253 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024254
24255 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24256 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024257 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24258 fc->l_listitems[ai].li_tv = argvars[i];
24259 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024260 }
24261 }
24262
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 /* Don't redraw while executing the function. */
24264 ++RedrawingDisabled;
24265 save_sourcing_name = sourcing_name;
24266 save_sourcing_lnum = sourcing_lnum;
24267 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024268 /* need space for function name + ("function " + 3) or "[number]" */
24269 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24270 + STRLEN(fp->uf_name) + 20;
24271 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 if (sourcing_name != NULL)
24273 {
24274 if (save_sourcing_name != NULL
24275 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024276 sprintf((char *)sourcing_name, "%s[%d]..",
24277 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 else
24279 STRCPY(sourcing_name, "function ");
24280 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24281
24282 if (p_verbose >= 12)
24283 {
24284 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024285 verbose_enter_scroll();
24286
Bram Moolenaar555b2802005-05-19 21:08:39 +000024287 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 if (p_verbose >= 14)
24289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024291 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024292 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024293 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294
24295 msg_puts((char_u *)"(");
24296 for (i = 0; i < argcount; ++i)
24297 {
24298 if (i > 0)
24299 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024300 if (argvars[i].v_type == VAR_NUMBER)
24301 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 else
24303 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024304 /* Do not want errors such as E724 here. */
24305 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024306 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024307 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024308 if (s != NULL)
24309 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024310 if (vim_strsize(s) > MSG_BUF_CLEN)
24311 {
24312 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24313 s = buf;
24314 }
24315 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024316 vim_free(tofree);
24317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 }
24319 }
24320 msg_puts((char_u *)")");
24321 }
24322 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024323
24324 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 --no_wait_return;
24326 }
24327 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024328#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024329 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024330 {
24331 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24332 func_do_profile(fp);
24333 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024334 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024335 {
24336 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024337 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024338 profile_zero(&fp->uf_tm_children);
24339 }
24340 script_prof_save(&wait_start);
24341 }
24342#endif
24343
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024345 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 save_did_emsg = did_emsg;
24347 did_emsg = FALSE;
24348
24349 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024350 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24352
24353 --RedrawingDisabled;
24354
24355 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024356 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024358 clear_tv(rettv);
24359 rettv->v_type = VAR_NUMBER;
24360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 }
24362
Bram Moolenaar05159a02005-02-26 23:04:13 +000024363#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024364 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024365 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024366 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024367 profile_end(&call_start);
24368 profile_sub_wait(&wait_start, &call_start);
24369 profile_add(&fp->uf_tm_total, &call_start);
24370 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024371 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024372 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024373 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24374 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024375 }
24376 }
24377#endif
24378
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 /* when being verbose, mention the return value */
24380 if (p_verbose >= 12)
24381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024383 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024386 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024387 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024388 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024389 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024390 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024392 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024393 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024394 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024395 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024396
Bram Moolenaar555b2802005-05-19 21:08:39 +000024397 /* The value may be very long. Skip the middle part, so that we
24398 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024399 * truncate it at the end. Don't want errors such as E724 here. */
24400 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024401 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024402 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024403 if (s != NULL)
24404 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024405 if (vim_strsize(s) > MSG_BUF_CLEN)
24406 {
24407 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24408 s = buf;
24409 }
24410 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024411 vim_free(tofree);
24412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 }
24414 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024415
24416 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 --no_wait_return;
24418 }
24419
24420 vim_free(sourcing_name);
24421 sourcing_name = save_sourcing_name;
24422 sourcing_lnum = save_sourcing_lnum;
24423 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024424#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024425 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024426 script_prof_restore(&wait_start);
24427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428
24429 if (p_verbose >= 12 && sourcing_name != NULL)
24430 {
24431 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024432 verbose_enter_scroll();
24433
Bram Moolenaar555b2802005-05-19 21:08:39 +000024434 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024436
24437 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438 --no_wait_return;
24439 }
24440
24441 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024442 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024444
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024445 /* If the a:000 list and the l: and a: dicts are not referenced we can
24446 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024447 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24448 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24449 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24450 {
24451 free_funccal(fc, FALSE);
24452 }
24453 else
24454 {
24455 hashitem_T *hi;
24456 listitem_T *li;
24457 int todo;
24458
24459 /* "fc" is still in use. This can happen when returning "a:000" or
24460 * assigning "l:" to a global variable.
24461 * Link "fc" in the list for garbage collection later. */
24462 fc->caller = previous_funccal;
24463 previous_funccal = fc;
24464
24465 /* Make a copy of the a: variables, since we didn't do that above. */
24466 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24467 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24468 {
24469 if (!HASHITEM_EMPTY(hi))
24470 {
24471 --todo;
24472 v = HI2DI(hi);
24473 copy_tv(&v->di_tv, &v->di_tv);
24474 }
24475 }
24476
24477 /* Make a copy of the a:000 items, since we didn't do that above. */
24478 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24479 copy_tv(&li->li_tv, &li->li_tv);
24480 }
24481}
24482
24483/*
24484 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024485 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024486 */
24487 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024488can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024489{
24490 return (fc->l_varlist.lv_copyID != copyID
24491 && fc->l_vars.dv_copyID != copyID
24492 && fc->l_avars.dv_copyID != copyID);
24493}
24494
24495/*
24496 * Free "fc" and what it contains.
24497 */
24498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024499free_funccal(
24500 funccall_T *fc,
24501 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024502{
24503 listitem_T *li;
24504
24505 /* The a: variables typevals may not have been allocated, only free the
24506 * allocated variables. */
24507 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24508
24509 /* free all l: variables */
24510 vars_clear(&fc->l_vars.dv_hashtab);
24511
24512 /* Free the a:000 variables if they were allocated. */
24513 if (free_val)
24514 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24515 clear_tv(&li->li_tv);
24516
24517 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518}
24519
24520/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024521 * Add a number variable "name" to dict "dp" with value "nr".
24522 */
24523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024524add_nr_var(
24525 dict_T *dp,
24526 dictitem_T *v,
24527 char *name,
24528 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024529{
24530 STRCPY(v->di_key, name);
24531 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24532 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24533 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024534 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024535 v->di_tv.vval.v_number = nr;
24536}
24537
24538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 * ":return [expr]"
24540 */
24541 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024542ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543{
24544 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024545 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 int returning = FALSE;
24547
24548 if (current_funccal == NULL)
24549 {
24550 EMSG(_("E133: :return not inside a function"));
24551 return;
24552 }
24553
24554 if (eap->skip)
24555 ++emsg_skip;
24556
24557 eap->nextcmd = NULL;
24558 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024559 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560 {
24561 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024562 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024564 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565 }
24566 /* It's safer to return also on error. */
24567 else if (!eap->skip)
24568 {
24569 /*
24570 * Return unless the expression evaluation has been cancelled due to an
24571 * aborting error, an interrupt, or an exception.
24572 */
24573 if (!aborting())
24574 returning = do_return(eap, FALSE, TRUE, NULL);
24575 }
24576
24577 /* When skipping or the return gets pending, advance to the next command
24578 * in this line (!returning). Otherwise, ignore the rest of the line.
24579 * Following lines will be ignored by get_func_line(). */
24580 if (returning)
24581 eap->nextcmd = NULL;
24582 else if (eap->nextcmd == NULL) /* no argument */
24583 eap->nextcmd = check_nextcmd(arg);
24584
24585 if (eap->skip)
24586 --emsg_skip;
24587}
24588
24589/*
24590 * Return from a function. Possibly makes the return pending. Also called
24591 * for a pending return at the ":endtry" or after returning from an extra
24592 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024593 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024594 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 * FALSE when the return gets pending.
24596 */
24597 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024598do_return(
24599 exarg_T *eap,
24600 int reanimate,
24601 int is_cmd,
24602 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603{
24604 int idx;
24605 struct condstack *cstack = eap->cstack;
24606
24607 if (reanimate)
24608 /* Undo the return. */
24609 current_funccal->returned = FALSE;
24610
24611 /*
24612 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24613 * not in its finally clause (which then is to be executed next) is found.
24614 * In this case, make the ":return" pending for execution at the ":endtry".
24615 * Otherwise, return normally.
24616 */
24617 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24618 if (idx >= 0)
24619 {
24620 cstack->cs_pending[idx] = CSTP_RETURN;
24621
24622 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024623 /* A pending return again gets pending. "rettv" points to an
24624 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024626 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627 else
24628 {
24629 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024630 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024632 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024634 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 {
24636 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024637 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024638 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 else
24640 EMSG(_(e_outofmem));
24641 }
24642 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024643 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644
24645 if (reanimate)
24646 {
24647 /* The pending return value could be overwritten by a ":return"
24648 * without argument in a finally clause; reset the default
24649 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024650 current_funccal->rettv->v_type = VAR_NUMBER;
24651 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 }
24653 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024654 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 }
24656 else
24657 {
24658 current_funccal->returned = TRUE;
24659
24660 /* If the return is carried out now, store the return value. For
24661 * a return immediately after reanimation, the value is already
24662 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024663 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024665 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024666 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024668 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 }
24670 }
24671
24672 return idx < 0;
24673}
24674
24675/*
24676 * Free the variable with a pending return value.
24677 */
24678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024679discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680{
Bram Moolenaar33570922005-01-25 22:26:29 +000024681 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682}
24683
24684/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024685 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686 * is an allocated string. Used by report_pending() for verbose messages.
24687 */
24688 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024689get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024690{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024691 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024692 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024693 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024695 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024696 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024697 if (s == NULL)
24698 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024699
24700 STRCPY(IObuff, ":return ");
24701 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24702 if (STRLEN(s) + 8 >= IOSIZE)
24703 STRCPY(IObuff + IOSIZE - 4, "...");
24704 vim_free(tofree);
24705 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706}
24707
24708/*
24709 * Get next function line.
24710 * Called by do_cmdline() to get the next line.
24711 * Returns allocated string, or NULL for end of function.
24712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024714get_func_line(
24715 int c UNUSED,
24716 void *cookie,
24717 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718{
Bram Moolenaar33570922005-01-25 22:26:29 +000024719 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024720 ufunc_T *fp = fcp->func;
24721 char_u *retval;
24722 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723
24724 /* If breakpoints have been added/deleted need to check for it. */
24725 if (fcp->dbg_tick != debug_tick)
24726 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024727 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 sourcing_lnum);
24729 fcp->dbg_tick = debug_tick;
24730 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024731#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024732 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024733 func_line_end(cookie);
24734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735
Bram Moolenaar05159a02005-02-26 23:04:13 +000024736 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024737 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24738 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 retval = NULL;
24740 else
24741 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024742 /* Skip NULL lines (continuation lines). */
24743 while (fcp->linenr < gap->ga_len
24744 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24745 ++fcp->linenr;
24746 if (fcp->linenr >= gap->ga_len)
24747 retval = NULL;
24748 else
24749 {
24750 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24751 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024752#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024753 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024754 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024755#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757 }
24758
24759 /* Did we encounter a breakpoint? */
24760 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24761 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024762 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024764 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765 sourcing_lnum);
24766 fcp->dbg_tick = debug_tick;
24767 }
24768
24769 return retval;
24770}
24771
Bram Moolenaar05159a02005-02-26 23:04:13 +000024772#if defined(FEAT_PROFILE) || defined(PROTO)
24773/*
24774 * Called when starting to read a function line.
24775 * "sourcing_lnum" must be correct!
24776 * When skipping lines it may not actually be executed, but we won't find out
24777 * until later and we need to store the time now.
24778 */
24779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024780func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781{
24782 funccall_T *fcp = (funccall_T *)cookie;
24783 ufunc_T *fp = fcp->func;
24784
24785 if (fp->uf_profiling && sourcing_lnum >= 1
24786 && sourcing_lnum <= fp->uf_lines.ga_len)
24787 {
24788 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024789 /* Skip continuation lines. */
24790 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24791 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024792 fp->uf_tml_execed = FALSE;
24793 profile_start(&fp->uf_tml_start);
24794 profile_zero(&fp->uf_tml_children);
24795 profile_get_wait(&fp->uf_tml_wait);
24796 }
24797}
24798
24799/*
24800 * Called when actually executing a function line.
24801 */
24802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024803func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024804{
24805 funccall_T *fcp = (funccall_T *)cookie;
24806 ufunc_T *fp = fcp->func;
24807
24808 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24809 fp->uf_tml_execed = TRUE;
24810}
24811
24812/*
24813 * Called when done with a function line.
24814 */
24815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024816func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024817{
24818 funccall_T *fcp = (funccall_T *)cookie;
24819 ufunc_T *fp = fcp->func;
24820
24821 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24822 {
24823 if (fp->uf_tml_execed)
24824 {
24825 ++fp->uf_tml_count[fp->uf_tml_idx];
24826 profile_end(&fp->uf_tml_start);
24827 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024828 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024829 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24830 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024831 }
24832 fp->uf_tml_idx = -1;
24833 }
24834}
24835#endif
24836
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837/*
24838 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024839 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840 */
24841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024842func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843{
Bram Moolenaar33570922005-01-25 22:26:29 +000024844 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845
24846 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24847 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024848 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849 || fcp->returned);
24850}
24851
24852/*
24853 * return TRUE if cookie indicates a function which "abort"s on errors.
24854 */
24855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024856func_has_abort(
24857 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024859 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860}
24861
24862#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24863typedef enum
24864{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024865 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24866 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24867 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024868} var_flavour_T;
24869
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024870static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871
24872 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024873var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874{
24875 char_u *p = varname;
24876
24877 if (ASCII_ISUPPER(*p))
24878 {
24879 while (*(++p))
24880 if (ASCII_ISLOWER(*p))
24881 return VAR_FLAVOUR_SESSION;
24882 return VAR_FLAVOUR_VIMINFO;
24883 }
24884 else
24885 return VAR_FLAVOUR_DEFAULT;
24886}
24887#endif
24888
24889#if defined(FEAT_VIMINFO) || defined(PROTO)
24890/*
24891 * Restore global vars that start with a capital from the viminfo file
24892 */
24893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024894read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895{
24896 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024897 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024898 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024899 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900
24901 if (!writing && (find_viminfo_parameter('!') != NULL))
24902 {
24903 tab = vim_strchr(virp->vir_line + 1, '\t');
24904 if (tab != NULL)
24905 {
24906 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024907 switch (*tab)
24908 {
24909 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024910#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024911 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024912#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024913 case 'D': type = VAR_DICT; break;
24914 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024915 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917
24918 tab = vim_strchr(tab, '\t');
24919 if (tab != NULL)
24920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024921 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024922 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024923 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024925#ifdef FEAT_FLOAT
24926 else if (type == VAR_FLOAT)
24927 (void)string2float(tab + 1, &tv.vval.v_float);
24928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024930 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024931 if (type == VAR_DICT || type == VAR_LIST)
24932 {
24933 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24934
24935 if (etv == NULL)
24936 /* Failed to parse back the dict or list, use it as a
24937 * string. */
24938 tv.v_type = VAR_STRING;
24939 else
24940 {
24941 vim_free(tv.vval.v_string);
24942 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024943 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024944 }
24945 }
24946
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024947 /* when in a function use global variables */
24948 save_funccal = current_funccal;
24949 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024950 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024951 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024952
24953 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024954 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024955 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24956 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 }
24958 }
24959 }
24960
24961 return viminfo_readline(virp);
24962}
24963
24964/*
24965 * Write global vars that start with a capital to the viminfo file
24966 */
24967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024968write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969{
Bram Moolenaar33570922005-01-25 22:26:29 +000024970 hashitem_T *hi;
24971 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024972 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010024973 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024974 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024975 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024976 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977
24978 if (find_viminfo_parameter('!') == NULL)
24979 return;
24980
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024981 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024982
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024983 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024984 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024986 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024988 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024989 this_var = HI2DI(hi);
24990 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024991 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024992 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024993 {
24994 case VAR_STRING: s = "STR"; break;
24995 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024996 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024997 case VAR_DICT: s = "DIC"; break;
24998 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024999 case VAR_SPECIAL: s = "XPL"; break;
25000
25001 case VAR_UNKNOWN:
25002 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025003 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025004 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025005 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025006 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025007 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025008 if (p != NULL)
25009 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025010 vim_free(tofree);
25011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 }
25013 }
25014}
25015#endif
25016
25017#if defined(FEAT_SESSION) || defined(PROTO)
25018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025019store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020{
Bram Moolenaar33570922005-01-25 22:26:29 +000025021 hashitem_T *hi;
25022 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025023 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 char_u *p, *t;
25025
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025026 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025027 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025029 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025031 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025032 this_var = HI2DI(hi);
25033 if ((this_var->di_tv.v_type == VAR_NUMBER
25034 || this_var->di_tv.v_type == VAR_STRING)
25035 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025036 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025037 /* Escape special characters with a backslash. Turn a LF and
25038 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025039 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025040 (char_u *)"\\\"\n\r");
25041 if (p == NULL) /* out of memory */
25042 break;
25043 for (t = p; *t != NUL; ++t)
25044 if (*t == '\n')
25045 *t = 'n';
25046 else if (*t == '\r')
25047 *t = 'r';
25048 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025049 this_var->di_key,
25050 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25051 : ' ',
25052 p,
25053 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25054 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025055 || put_eol(fd) == FAIL)
25056 {
25057 vim_free(p);
25058 return FAIL;
25059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 vim_free(p);
25061 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025062#ifdef FEAT_FLOAT
25063 else if (this_var->di_tv.v_type == VAR_FLOAT
25064 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25065 {
25066 float_T f = this_var->di_tv.vval.v_float;
25067 int sign = ' ';
25068
25069 if (f < 0)
25070 {
25071 f = -f;
25072 sign = '-';
25073 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025074 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025075 this_var->di_key, sign, f) < 0)
25076 || put_eol(fd) == FAIL)
25077 return FAIL;
25078 }
25079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080 }
25081 }
25082 return OK;
25083}
25084#endif
25085
Bram Moolenaar661b1822005-07-28 22:36:45 +000025086/*
25087 * Display script name where an item was last set.
25088 * Should only be invoked when 'verbose' is non-zero.
25089 */
25090 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025091last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025092{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025093 char_u *p;
25094
Bram Moolenaar661b1822005-07-28 22:36:45 +000025095 if (scriptID != 0)
25096 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025097 p = home_replace_save(NULL, get_scriptname(scriptID));
25098 if (p != NULL)
25099 {
25100 verbose_enter();
25101 MSG_PUTS(_("\n\tLast set from "));
25102 MSG_PUTS(p);
25103 vim_free(p);
25104 verbose_leave();
25105 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025106 }
25107}
25108
Bram Moolenaard812df62008-11-09 12:46:09 +000025109/*
25110 * List v:oldfiles in a nice way.
25111 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025113ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025114{
25115 list_T *l = vimvars[VV_OLDFILES].vv_list;
25116 listitem_T *li;
25117 int nr = 0;
25118
25119 if (l == NULL)
25120 msg((char_u *)_("No old files"));
25121 else
25122 {
25123 msg_start();
25124 msg_scroll = TRUE;
25125 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25126 {
25127 msg_outnum((long)++nr);
25128 MSG_PUTS(": ");
25129 msg_outtrans(get_tv_string(&li->li_tv));
25130 msg_putchar('\n');
25131 out_flush(); /* output one line at a time */
25132 ui_breakcheck();
25133 }
25134 /* Assume "got_int" was set to truncate the listing. */
25135 got_int = FALSE;
25136
25137#ifdef FEAT_BROWSE_CMD
25138 if (cmdmod.browse)
25139 {
25140 quit_more = FALSE;
25141 nr = prompt_for_number(FALSE);
25142 msg_starthere();
25143 if (nr > 0)
25144 {
25145 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25146 (long)nr);
25147
25148 if (p != NULL)
25149 {
25150 p = expand_env_save(p);
25151 eap->arg = p;
25152 eap->cmdidx = CMD_edit;
25153 cmdmod.browse = FALSE;
25154 do_exedit(eap, NULL);
25155 vim_free(p);
25156 }
25157 }
25158 }
25159#endif
25160 }
25161}
25162
Bram Moolenaar53744302015-07-17 17:38:22 +020025163/* reset v:option_new, v:option_old and v:option_type */
25164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025165reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025166{
25167 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25168 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25169 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25170}
25171
25172
Bram Moolenaar071d4272004-06-13 20:20:40 +000025173#endif /* FEAT_EVAL */
25174
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025176#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177
25178#ifdef WIN3264
25179/*
25180 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25181 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025182static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25183static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25184static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185
25186/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025187 * Get the short path (8.3) for the filename in "fnamep".
25188 * Only works for a valid file name.
25189 * When the path gets longer "fnamep" is changed and the allocated buffer
25190 * is put in "bufp".
25191 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25192 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 */
25194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025195get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025197 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198 char_u *newbuf;
25199
25200 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 l = GetShortPathName(*fnamep, *fnamep, len);
25202 if (l > len - 1)
25203 {
25204 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025205 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 newbuf = vim_strnsave(*fnamep, l);
25207 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025208 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209
25210 vim_free(*bufp);
25211 *fnamep = *bufp = newbuf;
25212
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025213 /* Really should always succeed, as the buffer is big enough. */
25214 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 }
25216
25217 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025218 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219}
25220
25221/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025222 * Get the short path (8.3) for the filename in "fname". The converted
25223 * path is returned in "bufp".
25224 *
25225 * Some of the directories specified in "fname" may not exist. This function
25226 * will shorten the existing directories at the beginning of the path and then
25227 * append the remaining non-existing path.
25228 *
25229 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025230 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025231 * bufp - Pointer to an allocated buffer for the filename.
25232 * fnamelen - Length of the filename pointed to by fname
25233 *
25234 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 */
25236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025237shortpath_for_invalid_fname(
25238 char_u **fname,
25239 char_u **bufp,
25240 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025242 char_u *short_fname, *save_fname, *pbuf_unused;
25243 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025245 int old_len, len;
25246 int new_len, sfx_len;
25247 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248
25249 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025250 old_len = *fnamelen;
25251 save_fname = vim_strnsave(*fname, old_len);
25252 pbuf_unused = NULL;
25253 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025255 endp = save_fname + old_len - 1; /* Find the end of the copy */
25256 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025258 /*
25259 * Try shortening the supplied path till it succeeds by removing one
25260 * directory at a time from the tail of the path.
25261 */
25262 len = 0;
25263 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025265 /* go back one path-separator */
25266 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25267 --endp;
25268 if (endp <= save_fname)
25269 break; /* processed the complete path */
25270
25271 /*
25272 * Replace the path separator with a NUL and try to shorten the
25273 * resulting path.
25274 */
25275 ch = *endp;
25276 *endp = 0;
25277 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025278 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025279 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25280 {
25281 retval = FAIL;
25282 goto theend;
25283 }
25284 *endp = ch; /* preserve the string */
25285
25286 if (len > 0)
25287 break; /* successfully shortened the path */
25288
25289 /* failed to shorten the path. Skip the path separator */
25290 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 }
25292
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025293 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025295 /*
25296 * Succeeded in shortening the path. Now concatenate the shortened
25297 * path with the remaining path at the tail.
25298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025300 /* Compute the length of the new path. */
25301 sfx_len = (int)(save_endp - endp) + 1;
25302 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025304 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025306 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025307 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025308 /* There is not enough space in the currently allocated string,
25309 * copy it to a buffer big enough. */
25310 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025311 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025312 {
25313 retval = FAIL;
25314 goto theend;
25315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 }
25317 else
25318 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025319 /* Transfer short_fname to the main buffer (it's big enough),
25320 * unless get_short_pathname() did its work in-place. */
25321 *fname = *bufp = save_fname;
25322 if (short_fname != save_fname)
25323 vim_strncpy(save_fname, short_fname, len);
25324 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025325 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025326
25327 /* concat the not-shortened part of the path */
25328 vim_strncpy(*fname + len, endp, sfx_len);
25329 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025331
25332theend:
25333 vim_free(pbuf_unused);
25334 vim_free(save_fname);
25335
25336 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337}
25338
25339/*
25340 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025341 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 */
25343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025344shortpath_for_partial(
25345 char_u **fnamep,
25346 char_u **bufp,
25347 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348{
25349 int sepcount, len, tflen;
25350 char_u *p;
25351 char_u *pbuf, *tfname;
25352 int hasTilde;
25353
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025354 /* Count up the path separators from the RHS.. so we know which part
25355 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025357 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025358 if (vim_ispathsep(*p))
25359 ++sepcount;
25360
25361 /* Need full path first (use expand_env() to remove a "~/") */
25362 hasTilde = (**fnamep == '~');
25363 if (hasTilde)
25364 pbuf = tfname = expand_env_save(*fnamep);
25365 else
25366 pbuf = tfname = FullName_save(*fnamep, FALSE);
25367
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025368 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025370 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372
25373 if (len == 0)
25374 {
25375 /* Don't have a valid filename, so shorten the rest of the
25376 * path if we can. This CAN give us invalid 8.3 filenames, but
25377 * there's not a lot of point in guessing what it might be.
25378 */
25379 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025380 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25381 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 }
25383
25384 /* Count the paths backward to find the beginning of the desired string. */
25385 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025386 {
25387#ifdef FEAT_MBYTE
25388 if (has_mbyte)
25389 p -= mb_head_off(tfname, p);
25390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 if (vim_ispathsep(*p))
25392 {
25393 if (sepcount == 0 || (hasTilde && sepcount == 1))
25394 break;
25395 else
25396 sepcount --;
25397 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399 if (hasTilde)
25400 {
25401 --p;
25402 if (p >= tfname)
25403 *p = '~';
25404 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025405 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 }
25407 else
25408 ++p;
25409
25410 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25411 vim_free(*bufp);
25412 *fnamelen = (int)STRLEN(p);
25413 *bufp = pbuf;
25414 *fnamep = p;
25415
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025416 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417}
25418#endif /* WIN3264 */
25419
25420/*
25421 * Adjust a filename, according to a string of modifiers.
25422 * *fnamep must be NUL terminated when called. When returning, the length is
25423 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025424 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 * When there is an error, *fnamep is set to NULL.
25426 */
25427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025428modify_fname(
25429 char_u *src, /* string with modifiers */
25430 int *usedlen, /* characters after src that are used */
25431 char_u **fnamep, /* file name so far */
25432 char_u **bufp, /* buffer for allocated file name or NULL */
25433 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434{
25435 int valid = 0;
25436 char_u *tail;
25437 char_u *s, *p, *pbuf;
25438 char_u dirname[MAXPATHL];
25439 int c;
25440 int has_fullname = 0;
25441#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025442 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 int has_shortname = 0;
25444#endif
25445
25446repeat:
25447 /* ":p" - full path/file_name */
25448 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25449 {
25450 has_fullname = 1;
25451
25452 valid |= VALID_PATH;
25453 *usedlen += 2;
25454
25455 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25456 if ((*fnamep)[0] == '~'
25457#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25458 && ((*fnamep)[1] == '/'
25459# ifdef BACKSLASH_IN_FILENAME
25460 || (*fnamep)[1] == '\\'
25461# endif
25462 || (*fnamep)[1] == NUL)
25463
25464#endif
25465 )
25466 {
25467 *fnamep = expand_env_save(*fnamep);
25468 vim_free(*bufp); /* free any allocated file name */
25469 *bufp = *fnamep;
25470 if (*fnamep == NULL)
25471 return -1;
25472 }
25473
25474 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025475 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 {
25477 if (vim_ispathsep(*p)
25478 && p[1] == '.'
25479 && (p[2] == NUL
25480 || vim_ispathsep(p[2])
25481 || (p[2] == '.'
25482 && (p[3] == NUL || vim_ispathsep(p[3])))))
25483 break;
25484 }
25485
25486 /* FullName_save() is slow, don't use it when not needed. */
25487 if (*p != NUL || !vim_isAbsName(*fnamep))
25488 {
25489 *fnamep = FullName_save(*fnamep, *p != NUL);
25490 vim_free(*bufp); /* free any allocated file name */
25491 *bufp = *fnamep;
25492 if (*fnamep == NULL)
25493 return -1;
25494 }
25495
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025496#ifdef WIN3264
25497# if _WIN32_WINNT >= 0x0500
25498 if (vim_strchr(*fnamep, '~') != NULL)
25499 {
25500 /* Expand 8.3 filename to full path. Needed to make sure the same
25501 * file does not have two different names.
25502 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25503 p = alloc(_MAX_PATH + 1);
25504 if (p != NULL)
25505 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025506 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025507 {
25508 vim_free(*bufp);
25509 *bufp = *fnamep = p;
25510 }
25511 else
25512 vim_free(p);
25513 }
25514 }
25515# endif
25516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517 /* Append a path separator to a directory. */
25518 if (mch_isdir(*fnamep))
25519 {
25520 /* Make room for one or two extra characters. */
25521 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25522 vim_free(*bufp); /* free any allocated file name */
25523 *bufp = *fnamep;
25524 if (*fnamep == NULL)
25525 return -1;
25526 add_pathsep(*fnamep);
25527 }
25528 }
25529
25530 /* ":." - path relative to the current directory */
25531 /* ":~" - path relative to the home directory */
25532 /* ":8" - shortname path - postponed till after */
25533 while (src[*usedlen] == ':'
25534 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25535 {
25536 *usedlen += 2;
25537 if (c == '8')
25538 {
25539#ifdef WIN3264
25540 has_shortname = 1; /* Postpone this. */
25541#endif
25542 continue;
25543 }
25544 pbuf = NULL;
25545 /* Need full path first (use expand_env() to remove a "~/") */
25546 if (!has_fullname)
25547 {
25548 if (c == '.' && **fnamep == '~')
25549 p = pbuf = expand_env_save(*fnamep);
25550 else
25551 p = pbuf = FullName_save(*fnamep, FALSE);
25552 }
25553 else
25554 p = *fnamep;
25555
25556 has_fullname = 0;
25557
25558 if (p != NULL)
25559 {
25560 if (c == '.')
25561 {
25562 mch_dirname(dirname, MAXPATHL);
25563 s = shorten_fname(p, dirname);
25564 if (s != NULL)
25565 {
25566 *fnamep = s;
25567 if (pbuf != NULL)
25568 {
25569 vim_free(*bufp); /* free any allocated file name */
25570 *bufp = pbuf;
25571 pbuf = NULL;
25572 }
25573 }
25574 }
25575 else
25576 {
25577 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25578 /* Only replace it when it starts with '~' */
25579 if (*dirname == '~')
25580 {
25581 s = vim_strsave(dirname);
25582 if (s != NULL)
25583 {
25584 *fnamep = s;
25585 vim_free(*bufp);
25586 *bufp = s;
25587 }
25588 }
25589 }
25590 vim_free(pbuf);
25591 }
25592 }
25593
25594 tail = gettail(*fnamep);
25595 *fnamelen = (int)STRLEN(*fnamep);
25596
25597 /* ":h" - head, remove "/file_name", can be repeated */
25598 /* Don't remove the first "/" or "c:\" */
25599 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25600 {
25601 valid |= VALID_HEAD;
25602 *usedlen += 2;
25603 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025604 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025605 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 *fnamelen = (int)(tail - *fnamep);
25607#ifdef VMS
25608 if (*fnamelen > 0)
25609 *fnamelen += 1; /* the path separator is part of the path */
25610#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025611 if (*fnamelen == 0)
25612 {
25613 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25614 p = vim_strsave((char_u *)".");
25615 if (p == NULL)
25616 return -1;
25617 vim_free(*bufp);
25618 *bufp = *fnamep = tail = p;
25619 *fnamelen = 1;
25620 }
25621 else
25622 {
25623 while (tail > s && !after_pathsep(s, tail))
25624 mb_ptr_back(*fnamep, tail);
25625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 }
25627
25628 /* ":8" - shortname */
25629 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25630 {
25631 *usedlen += 2;
25632#ifdef WIN3264
25633 has_shortname = 1;
25634#endif
25635 }
25636
25637#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025638 /*
25639 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 */
25641 if (has_shortname)
25642 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025643 /* Copy the string if it is shortened by :h and when it wasn't copied
25644 * yet, because we are going to change it in place. Avoids changing
25645 * the buffer name for "%:8". */
25646 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025647 {
25648 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025649 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650 return -1;
25651 vim_free(*bufp);
25652 *bufp = *fnamep = p;
25653 }
25654
25655 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025656 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 if (!has_fullname && !vim_isAbsName(*fnamep))
25658 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025659 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 return -1;
25661 }
25662 else
25663 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025664 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665
Bram Moolenaardc935552011-08-17 15:23:23 +020025666 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025668 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 return -1;
25670
25671 if (l == 0)
25672 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025673 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025675 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 return -1;
25677 }
25678 *fnamelen = l;
25679 }
25680 }
25681#endif /* WIN3264 */
25682
25683 /* ":t" - tail, just the basename */
25684 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25685 {
25686 *usedlen += 2;
25687 *fnamelen -= (int)(tail - *fnamep);
25688 *fnamep = tail;
25689 }
25690
25691 /* ":e" - extension, can be repeated */
25692 /* ":r" - root, without extension, can be repeated */
25693 while (src[*usedlen] == ':'
25694 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25695 {
25696 /* find a '.' in the tail:
25697 * - for second :e: before the current fname
25698 * - otherwise: The last '.'
25699 */
25700 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25701 s = *fnamep - 2;
25702 else
25703 s = *fnamep + *fnamelen - 1;
25704 for ( ; s > tail; --s)
25705 if (s[0] == '.')
25706 break;
25707 if (src[*usedlen + 1] == 'e') /* :e */
25708 {
25709 if (s > tail)
25710 {
25711 *fnamelen += (int)(*fnamep - (s + 1));
25712 *fnamep = s + 1;
25713#ifdef VMS
25714 /* cut version from the extension */
25715 s = *fnamep + *fnamelen - 1;
25716 for ( ; s > *fnamep; --s)
25717 if (s[0] == ';')
25718 break;
25719 if (s > *fnamep)
25720 *fnamelen = s - *fnamep;
25721#endif
25722 }
25723 else if (*fnamep <= tail)
25724 *fnamelen = 0;
25725 }
25726 else /* :r */
25727 {
25728 if (s > tail) /* remove one extension */
25729 *fnamelen = (int)(s - *fnamep);
25730 }
25731 *usedlen += 2;
25732 }
25733
25734 /* ":s?pat?foo?" - substitute */
25735 /* ":gs?pat?foo?" - global substitute */
25736 if (src[*usedlen] == ':'
25737 && (src[*usedlen + 1] == 's'
25738 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25739 {
25740 char_u *str;
25741 char_u *pat;
25742 char_u *sub;
25743 int sep;
25744 char_u *flags;
25745 int didit = FALSE;
25746
25747 flags = (char_u *)"";
25748 s = src + *usedlen + 2;
25749 if (src[*usedlen + 1] == 'g')
25750 {
25751 flags = (char_u *)"g";
25752 ++s;
25753 }
25754
25755 sep = *s++;
25756 if (sep)
25757 {
25758 /* find end of pattern */
25759 p = vim_strchr(s, sep);
25760 if (p != NULL)
25761 {
25762 pat = vim_strnsave(s, (int)(p - s));
25763 if (pat != NULL)
25764 {
25765 s = p + 1;
25766 /* find end of substitution */
25767 p = vim_strchr(s, sep);
25768 if (p != NULL)
25769 {
25770 sub = vim_strnsave(s, (int)(p - s));
25771 str = vim_strnsave(*fnamep, *fnamelen);
25772 if (sub != NULL && str != NULL)
25773 {
25774 *usedlen = (int)(p + 1 - src);
25775 s = do_string_sub(str, pat, sub, flags);
25776 if (s != NULL)
25777 {
25778 *fnamep = s;
25779 *fnamelen = (int)STRLEN(s);
25780 vim_free(*bufp);
25781 *bufp = s;
25782 didit = TRUE;
25783 }
25784 }
25785 vim_free(sub);
25786 vim_free(str);
25787 }
25788 vim_free(pat);
25789 }
25790 }
25791 /* after using ":s", repeat all the modifiers */
25792 if (didit)
25793 goto repeat;
25794 }
25795 }
25796
Bram Moolenaar26df0922014-02-23 23:39:13 +010025797 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25798 {
25799 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25800 if (p == NULL)
25801 return -1;
25802 vim_free(*bufp);
25803 *bufp = *fnamep = p;
25804 *fnamelen = (int)STRLEN(p);
25805 *usedlen += 2;
25806 }
25807
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808 return valid;
25809}
25810
25811/*
25812 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25813 * "flags" can be "g" to do a global substitute.
25814 * Returns an allocated string, NULL for error.
25815 */
25816 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025817do_string_sub(
25818 char_u *str,
25819 char_u *pat,
25820 char_u *sub,
25821 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822{
25823 int sublen;
25824 regmatch_T regmatch;
25825 int i;
25826 int do_all;
25827 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025828 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 garray_T ga;
25830 char_u *ret;
25831 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025832 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833
25834 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25835 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025836 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837
25838 ga_init2(&ga, 1, 200);
25839
25840 do_all = (flags[0] == 'g');
25841
25842 regmatch.rm_ic = p_ic;
25843 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25844 if (regmatch.regprog != NULL)
25845 {
25846 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025847 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25849 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025850 /* Skip empty match except for first match. */
25851 if (regmatch.startp[0] == regmatch.endp[0])
25852 {
25853 if (zero_width == regmatch.startp[0])
25854 {
25855 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025856 i = MB_PTR2LEN(tail);
25857 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25858 (size_t)i);
25859 ga.ga_len += i;
25860 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025861 continue;
25862 }
25863 zero_width = regmatch.startp[0];
25864 }
25865
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 /*
25867 * Get some space for a temporary buffer to do the substitution
25868 * into. It will contain:
25869 * - The text up to where the match is.
25870 * - The substituted text.
25871 * - The text after the match.
25872 */
25873 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025874 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25876 {
25877 ga_clear(&ga);
25878 break;
25879 }
25880
25881 /* copy the text up to where the match is */
25882 i = (int)(regmatch.startp[0] - tail);
25883 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25884 /* add the substituted text */
25885 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25886 + ga.ga_len + i, TRUE, TRUE, FALSE);
25887 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025888 tail = regmatch.endp[0];
25889 if (*tail == NUL)
25890 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 if (!do_all)
25892 break;
25893 }
25894
25895 if (ga.ga_data != NULL)
25896 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25897
Bram Moolenaar473de612013-06-08 18:19:48 +020025898 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899 }
25900
25901 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25902 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025903 if (p_cpo == empty_option)
25904 p_cpo = save_cpo;
25905 else
25906 /* Darn, evaluating {sub} expression changed the value. */
25907 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908
25909 return ret;
25910}
25911
25912#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */