blob: 42f305e4ae001bd902aa8c21f010419056a362de [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 Moolenaarb6a4fee2016-02-11 20:48:34 +01009874 dict_T *dict = argvars[1].vval.v_dict;
9875 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009876
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009877 /* parse argdict */
9878 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009879 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009880 mode = get_tv_string(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009881 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009882 ch_mode = MODE_RAW;
9883 else if (STRCMP(mode, "js") == 0)
9884 ch_mode = MODE_JS;
9885 else if (STRCMP(mode, "json") == 0)
9886 ch_mode = MODE_JSON;
9887 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009888 {
9889 EMSG2(_(e_invarg2), mode);
9890 return;
9891 }
9892 }
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009893 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
9894 waittime = get_tv_number(&item->di_tv);
9895 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
9896 timeout = get_tv_number(&item->di_tv);
9897 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
9898 callback = get_callback(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009899 }
9900 if (waittime < 0 || timeout < 0)
9901 {
9902 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009903 return;
9904 }
9905
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009906 ch_idx = channel_open((char *)address, port, waittime, NULL);
9907 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009908 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009909 channel_set_json_mode(ch_idx, ch_mode);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009910 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009911 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009912 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009913 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009914 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009915}
9916
9917/*
9918 * common for "sendexpr()" and "sendraw()"
9919 * Returns the channel index if the caller should read the response.
9920 * Otherwise returns -1.
9921 */
9922 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009923send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009924{
9925 int ch_idx;
9926 char_u *callback = NULL;
9927
9928 ch_idx = get_channel_arg(&argvars[0]);
9929 if (ch_idx < 0)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009930 {
9931 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009932 return -1;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009933 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009934
9935 if (argvars[2].v_type != VAR_UNKNOWN)
9936 {
9937 callback = get_callback(&argvars[2]);
9938 if (callback == NULL)
9939 return -1;
9940 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009941 /* Set the callback. An empty callback means no callback and not reading
9942 * the response. */
9943 if (callback != NULL && *callback != NUL)
9944 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009945
9946 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9947 return ch_idx;
9948 return -1;
9949}
9950
9951/*
9952 * "ch_sendexpr()" function
9953 */
9954 static void
9955f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9956{
9957 char_u *text;
9958 typval_T *listtv;
9959 int ch_idx;
9960 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009961 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009962
9963 /* return an empty string by default */
9964 rettv->v_type = VAR_STRING;
9965 rettv->vval.v_string = NULL;
9966
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009967 ch_idx = get_channel_arg(&argvars[0]);
9968 if (ch_idx < 0)
9969 {
9970 EMSG(_(e_invarg));
9971 return;
9972 }
9973
9974 ch_mode = channel_get_mode(ch_idx);
9975 if (ch_mode == MODE_RAW)
9976 {
9977 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
9978 return;
9979 }
9980
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009981 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009982 text = json_encode_nr_expr(id, &argvars[1],
9983 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009984 if (text == NULL)
9985 return;
9986
Bram Moolenaara07fec92016-02-05 21:04:08 +01009987 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009988 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009989 if (ch_idx >= 0)
9990 {
9991 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9992 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009993 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009994
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009995 /* Move the item from the list and then change the type to
9996 * avoid the value being freed. */
9997 *rettv = list->lv_last->li_tv;
9998 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009999 clear_tv(listtv);
10000 }
10001 }
10002}
10003
10004/*
10005 * "ch_sendraw()" function
10006 */
10007 static void
10008f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10009{
10010 char_u buf[NUMBUFLEN];
10011 char_u *text;
10012 int ch_idx;
10013
10014 /* return an empty string by default */
10015 rettv->v_type = VAR_STRING;
10016 rettv->vval.v_string = NULL;
10017
10018 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +010010019 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010020 if (ch_idx >= 0)
10021 rettv->vval.v_string = channel_read_block(ch_idx);
10022}
10023#endif
10024
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010025/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010026 * "changenr()" function
10027 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010029f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010030{
10031 rettv->vval.v_number = curbuf->b_u_seq_cur;
10032}
10033
10034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 * "char2nr(string)" function
10036 */
10037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010038f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
10040#ifdef FEAT_MBYTE
10041 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010042 {
10043 int utf8 = 0;
10044
10045 if (argvars[1].v_type != VAR_UNKNOWN)
10046 utf8 = get_tv_number_chk(&argvars[1], NULL);
10047
10048 if (utf8)
10049 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10050 else
10051 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 else
10054#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056}
10057
10058/*
10059 * "cindent(lnum)" function
10060 */
10061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010062f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063{
10064#ifdef FEAT_CINDENT
10065 pos_T pos;
10066 linenr_T lnum;
10067
10068 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10071 {
10072 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 curwin->w_cursor = pos;
10075 }
10076 else
10077#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079}
10080
10081/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010082 * "clearmatches()" function
10083 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010085f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010086{
10087#ifdef FEAT_SEARCH_EXTRA
10088 clear_matches(curwin);
10089#endif
10090}
10091
10092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 * "col(string)" function
10094 */
10095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010096f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
10098 colnr_T col = 0;
10099 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010100 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010102 fp = var2fpos(&argvars[0], FALSE, &fnum);
10103 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 {
10105 if (fp->col == MAXCOL)
10106 {
10107 /* '> can be MAXCOL, get the length of the line then */
10108 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010109 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 else
10111 col = MAXCOL;
10112 }
10113 else
10114 {
10115 col = fp->col + 1;
10116#ifdef FEAT_VIRTUALEDIT
10117 /* col(".") when the cursor is on the NUL at the end of the line
10118 * because of "coladd" can be seen as an extra column. */
10119 if (virtual_active() && fp == &curwin->w_cursor)
10120 {
10121 char_u *p = ml_get_cursor();
10122
10123 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10124 curwin->w_virtcol - curwin->w_cursor.coladd))
10125 {
10126# ifdef FEAT_MBYTE
10127 int l;
10128
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010129 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 col += l;
10131# else
10132 if (*p != NUL && p[1] == NUL)
10133 ++col;
10134# endif
10135 }
10136 }
10137#endif
10138 }
10139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141}
10142
Bram Moolenaar572cb562005-08-05 21:35:02 +000010143#if defined(FEAT_INS_EXPAND)
10144/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010145 * "complete()" function
10146 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010148f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010149{
10150 int startcol;
10151
10152 if ((State & INSERT) == 0)
10153 {
10154 EMSG(_("E785: complete() can only be used in Insert mode"));
10155 return;
10156 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010157
10158 /* Check for undo allowed here, because if something was already inserted
10159 * the line was already saved for undo and this check isn't done. */
10160 if (!undo_allowed())
10161 return;
10162
Bram Moolenaarade00832006-03-10 21:46:58 +000010163 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10164 {
10165 EMSG(_(e_invarg));
10166 return;
10167 }
10168
10169 startcol = get_tv_number_chk(&argvars[0], NULL);
10170 if (startcol <= 0)
10171 return;
10172
10173 set_completion(startcol - 1, argvars[1].vval.v_list);
10174}
10175
10176/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010177 * "complete_add()" function
10178 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010180f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010181{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010182 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010183}
10184
10185/*
10186 * "complete_check()" function
10187 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010189f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010190{
10191 int saved = RedrawingDisabled;
10192
10193 RedrawingDisabled = 0;
10194 ins_compl_check_keys(0);
10195 rettv->vval.v_number = compl_interrupted;
10196 RedrawingDisabled = saved;
10197}
10198#endif
10199
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200/*
10201 * "confirm(message, buttons[, default [, type]])" function
10202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010204f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205{
10206#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10207 char_u *message;
10208 char_u *buttons = NULL;
10209 char_u buf[NUMBUFLEN];
10210 char_u buf2[NUMBUFLEN];
10211 int def = 1;
10212 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 char_u *typestr;
10214 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 message = get_tv_string_chk(&argvars[0]);
10217 if (message == NULL)
10218 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010219 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10222 if (buttons == NULL)
10223 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010224 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010227 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10230 if (typestr == NULL)
10231 error = TRUE;
10232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 switch (TOUPPER_ASC(*typestr))
10235 {
10236 case 'E': type = VIM_ERROR; break;
10237 case 'Q': type = VIM_QUESTION; break;
10238 case 'I': type = VIM_INFO; break;
10239 case 'W': type = VIM_WARNING; break;
10240 case 'G': type = VIM_GENERIC; break;
10241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 }
10243 }
10244 }
10245 }
10246
10247 if (buttons == NULL || *buttons == NUL)
10248 buttons = (char_u *)_("&Ok");
10249
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010250 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010251 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010252 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253#endif
10254}
10255
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010256/*
10257 * "copy()" function
10258 */
10259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010260f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010261{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010262 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010263}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010265#ifdef FEAT_FLOAT
10266/*
10267 * "cos()" function
10268 */
10269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010270f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010271{
10272 float_T f;
10273
10274 rettv->v_type = VAR_FLOAT;
10275 if (get_float_arg(argvars, &f) == OK)
10276 rettv->vval.v_float = cos(f);
10277 else
10278 rettv->vval.v_float = 0.0;
10279}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010280
10281/*
10282 * "cosh()" function
10283 */
10284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010285f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010286{
10287 float_T f;
10288
10289 rettv->v_type = VAR_FLOAT;
10290 if (get_float_arg(argvars, &f) == OK)
10291 rettv->vval.v_float = cosh(f);
10292 else
10293 rettv->vval.v_float = 0.0;
10294}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010295#endif
10296
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010298 * "count()" function
10299 */
10300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010302{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010303 long n = 0;
10304 int ic = FALSE;
10305
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010307 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010308 listitem_T *li;
10309 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010311
Bram Moolenaare9a41262005-01-15 22:18:47 +000010312 if ((l = argvars[0].vval.v_list) != NULL)
10313 {
10314 li = l->lv_first;
10315 if (argvars[2].v_type != VAR_UNKNOWN)
10316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010317 int error = FALSE;
10318
10319 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 if (argvars[3].v_type != VAR_UNKNOWN)
10321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 idx = get_tv_number_chk(&argvars[3], &error);
10323 if (!error)
10324 {
10325 li = list_find(l, idx);
10326 if (li == NULL)
10327 EMSGN(_(e_listidx), idx);
10328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010329 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010330 if (error)
10331 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 }
10333
10334 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010335 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010336 ++n;
10337 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010338 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010339 else if (argvars[0].v_type == VAR_DICT)
10340 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010341 int todo;
10342 dict_T *d;
10343 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010344
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010345 if ((d = argvars[0].vval.v_dict) != NULL)
10346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010347 int error = FALSE;
10348
Bram Moolenaare9a41262005-01-15 22:18:47 +000010349 if (argvars[2].v_type != VAR_UNKNOWN)
10350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010351 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010352 if (argvars[3].v_type != VAR_UNKNOWN)
10353 EMSG(_(e_invarg));
10354 }
10355
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010356 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010358 {
10359 if (!HASHITEM_EMPTY(hi))
10360 {
10361 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010362 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010363 ++n;
10364 }
10365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010366 }
10367 }
10368 else
10369 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010370 rettv->vval.v_number = n;
10371}
10372
10373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10375 *
10376 * Checks the existence of a cscope connection.
10377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010379f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380{
10381#ifdef FEAT_CSCOPE
10382 int num = 0;
10383 char_u *dbpath = NULL;
10384 char_u *prepend = NULL;
10385 char_u buf[NUMBUFLEN];
10386
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010387 if (argvars[0].v_type != VAR_UNKNOWN
10388 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390 num = (int)get_tv_number(&argvars[0]);
10391 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010392 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
10395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010396 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397#endif
10398}
10399
10400/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010401 * "cursor(lnum, col)" function, or
10402 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010404 * Moves the cursor to the specified line and column.
10405 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010408f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
10410 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010411#ifdef FEAT_VIRTUALEDIT
10412 long coladd = 0;
10413#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010414 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010416 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010417 if (argvars[1].v_type == VAR_UNKNOWN)
10418 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010419 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010420 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010421
Bram Moolenaar493c1782014-05-28 14:34:46 +020010422 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010423 {
10424 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010425 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010426 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010427 line = pos.lnum;
10428 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010429#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010430 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010431#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010432 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010433 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010434 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010435 set_curswant = FALSE;
10436 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010437 }
10438 else
10439 {
10440 line = get_tv_lnum(argvars);
10441 col = get_tv_number_chk(&argvars[1], NULL);
10442#ifdef FEAT_VIRTUALEDIT
10443 if (argvars[2].v_type != VAR_UNKNOWN)
10444 coladd = get_tv_number_chk(&argvars[2], NULL);
10445#endif
10446 }
10447 if (line < 0 || col < 0
10448#ifdef FEAT_VIRTUALEDIT
10449 || coladd < 0
10450#endif
10451 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 if (line > 0)
10454 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 if (col > 0)
10456 curwin->w_cursor.col = col - 1;
10457#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010458 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459#endif
10460
10461 /* Make sure the cursor is in a valid position. */
10462 check_cursor();
10463#ifdef FEAT_MBYTE
10464 /* Correct cursor for multi-byte character. */
10465 if (has_mbyte)
10466 mb_adjust_cursor();
10467#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010468
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010469 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010470 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471}
10472
10473/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010474 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 */
10476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010477f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010479 int noref = 0;
10480
10481 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010483 if (noref < 0 || noref > 1)
10484 EMSG(_(e_invarg));
10485 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010486 {
10487 current_copyID += COPYID_INC;
10488 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490}
10491
10492/*
10493 * "delete()" function
10494 */
10495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010496f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010498 char_u nbuf[NUMBUFLEN];
10499 char_u *name;
10500 char_u *flags;
10501
10502 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010504 return;
10505
10506 name = get_tv_string(&argvars[0]);
10507 if (name == NULL || *name == NUL)
10508 {
10509 EMSG(_(e_invarg));
10510 return;
10511 }
10512
10513 if (argvars[1].v_type != VAR_UNKNOWN)
10514 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010516 flags = (char_u *)"";
10517
10518 if (*flags == NUL)
10519 /* delete a file */
10520 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10521 else if (STRCMP(flags, "d") == 0)
10522 /* delete an empty directory */
10523 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10524 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010525 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010526 rettv->vval.v_number = delete_recursive(name);
10527 else
10528 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529}
10530
10531/*
10532 * "did_filetype()" function
10533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010535f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536{
10537#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539#endif
10540}
10541
10542/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010543 * "diff_filler()" function
10544 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010546f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010547{
10548#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010550#endif
10551}
10552
10553/*
10554 * "diff_hlID()" function
10555 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010557f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010558{
10559#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010561 static linenr_T prev_lnum = 0;
10562 static int changedtick = 0;
10563 static int fnum = 0;
10564 static int change_start = 0;
10565 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010566 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010567 int filler_lines;
10568 int col;
10569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 if (lnum < 0) /* ignore type error in {lnum} arg */
10571 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010572 if (lnum != prev_lnum
10573 || changedtick != curbuf->b_changedtick
10574 || fnum != curbuf->b_fnum)
10575 {
10576 /* New line, buffer, change: need to get the values. */
10577 filler_lines = diff_check(curwin, lnum);
10578 if (filler_lines < 0)
10579 {
10580 if (filler_lines == -1)
10581 {
10582 change_start = MAXCOL;
10583 change_end = -1;
10584 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10585 hlID = HLF_ADD; /* added line */
10586 else
10587 hlID = HLF_CHD; /* changed line */
10588 }
10589 else
10590 hlID = HLF_ADD; /* added line */
10591 }
10592 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010593 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010594 prev_lnum = lnum;
10595 changedtick = curbuf->b_changedtick;
10596 fnum = curbuf->b_fnum;
10597 }
10598
10599 if (hlID == HLF_CHD || hlID == HLF_TXD)
10600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010602 if (col >= change_start && col <= change_end)
10603 hlID = HLF_TXD; /* changed text */
10604 else
10605 hlID = HLF_CHD; /* changed line */
10606 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010607 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010608#endif
10609}
10610
10611/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010612 * "disable_char_avail_for_testing({expr})" function
10613 */
10614 static void
10615f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10616{
10617 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10618}
10619
10620/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010621 * "empty({expr})" function
10622 */
10623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010624f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010625{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010626 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010627
10628 switch (argvars[0].v_type)
10629 {
10630 case VAR_STRING:
10631 case VAR_FUNC:
10632 n = argvars[0].vval.v_string == NULL
10633 || *argvars[0].vval.v_string == NUL;
10634 break;
10635 case VAR_NUMBER:
10636 n = argvars[0].vval.v_number == 0;
10637 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010638 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010639#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010640 n = argvars[0].vval.v_float == 0.0;
10641 break;
10642#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010643 case VAR_LIST:
10644 n = argvars[0].vval.v_list == NULL
10645 || argvars[0].vval.v_list->lv_first == NULL;
10646 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 case VAR_DICT:
10648 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010650 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010651 case VAR_SPECIAL:
10652 n = argvars[0].vval.v_number != VVAL_TRUE;
10653 break;
10654
Bram Moolenaar835dc632016-02-07 14:27:38 +010010655 case VAR_JOB:
10656#ifdef FEAT_JOB
10657 n = argvars[0].vval.v_job->jv_status != JOB_STARTED;
10658 break;
10659#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010660 case VAR_UNKNOWN:
10661 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10662 n = TRUE;
10663 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010664 }
10665
10666 rettv->vval.v_number = n;
10667}
10668
10669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 * "escape({string}, {chars})" function
10671 */
10672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674{
10675 char_u buf[NUMBUFLEN];
10676
Bram Moolenaar758711c2005-02-02 23:11:38 +000010677 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10678 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680}
10681
10682/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010683 * "eval()" function
10684 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010687{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010688 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 s = get_tv_string_chk(&argvars[0]);
10691 if (s != NULL)
10692 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010693
Bram Moolenaar615b9972015-01-14 17:15:05 +010010694 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10696 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010697 if (p != NULL && !aborting())
10698 EMSG2(_(e_invexpr2), p);
10699 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010701 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010703 else if (*s != NUL)
10704 EMSG(_(e_trailing));
10705}
10706
10707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 * "eventhandler()" function
10709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010711f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714}
10715
10716/*
10717 * "executable()" function
10718 */
10719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010720f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010722 char_u *name = get_tv_string(&argvars[0]);
10723
10724 /* Check in $PATH and also check directly if there is a directory name. */
10725 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10726 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010727}
10728
10729/*
10730 * "exepath()" function
10731 */
10732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010733f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010734{
10735 char_u *p = NULL;
10736
Bram Moolenaarb5971142015-03-21 17:32:19 +010010737 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010738 rettv->v_type = VAR_STRING;
10739 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740}
10741
10742/*
10743 * "exists()" function
10744 */
10745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010746f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747{
10748 char_u *p;
10749 char_u *name;
10750 int n = FALSE;
10751 int len = 0;
10752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 if (*p == '$') /* environment variable */
10755 {
10756 /* first try "normal" environment variables (fast) */
10757 if (mch_getenv(p + 1) != NULL)
10758 n = TRUE;
10759 else
10760 {
10761 /* try expanding things like $VIM and ${HOME} */
10762 p = expand_env_save(p);
10763 if (p != NULL && *p != '$')
10764 n = TRUE;
10765 vim_free(p);
10766 }
10767 }
10768 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010771 if (*skipwhite(p) != NUL)
10772 n = FALSE; /* trailing garbage */
10773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 else if (*p == '*') /* internal or user defined function */
10775 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010776 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 }
10778 else if (*p == ':')
10779 {
10780 n = cmd_exists(p + 1);
10781 }
10782 else if (*p == '#')
10783 {
10784#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010785 if (p[1] == '#')
10786 n = autocmd_supported(p + 2);
10787 else
10788 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789#endif
10790 }
10791 else /* internal variable */
10792 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010793 char_u *tofree;
10794 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010796 /* get_name_len() takes care of expanding curly braces */
10797 name = p;
10798 len = get_name_len(&p, &tofree, TRUE, FALSE);
10799 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010801 if (tofree != NULL)
10802 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010803 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010804 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010806 /* handle d.key, l[idx], f(expr) */
10807 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10808 if (n)
10809 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 }
10811 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010812 if (*p != NUL)
10813 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010815 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 }
10817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819}
10820
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010821#ifdef FEAT_FLOAT
10822/*
10823 * "exp()" function
10824 */
10825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010826f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010827{
10828 float_T f;
10829
10830 rettv->v_type = VAR_FLOAT;
10831 if (get_float_arg(argvars, &f) == OK)
10832 rettv->vval.v_float = exp(f);
10833 else
10834 rettv->vval.v_float = 0.0;
10835}
10836#endif
10837
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838/*
10839 * "expand()" function
10840 */
10841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010842f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843{
10844 char_u *s;
10845 int len;
10846 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010847 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010850 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010853 if (argvars[1].v_type != VAR_UNKNOWN
10854 && argvars[2].v_type != VAR_UNKNOWN
10855 && get_tv_number_chk(&argvars[2], &error)
10856 && !error)
10857 {
10858 rettv->v_type = VAR_LIST;
10859 rettv->vval.v_list = NULL;
10860 }
10861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 if (*s == '%' || *s == '#' || *s == '<')
10864 {
10865 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010866 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010868 if (rettv->v_type == VAR_LIST)
10869 {
10870 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10871 list_append_string(rettv->vval.v_list, result, -1);
10872 else
10873 vim_free(result);
10874 }
10875 else
10876 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 }
10878 else
10879 {
10880 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010881 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 if (argvars[1].v_type != VAR_UNKNOWN
10883 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010884 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 if (!error)
10886 {
10887 ExpandInit(&xpc);
10888 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010889 if (p_wic)
10890 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010891 if (rettv->v_type == VAR_STRING)
10892 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10893 options, WILD_ALL);
10894 else if (rettv_list_alloc(rettv) != FAIL)
10895 {
10896 int i;
10897
10898 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10899 for (i = 0; i < xpc.xp_numfiles; i++)
10900 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10901 ExpandCleanup(&xpc);
10902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 }
10904 else
10905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 }
10907}
10908
10909/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010910 * Go over all entries in "d2" and add them to "d1".
10911 * When "action" is "error" then a duplicate key is an error.
10912 * When "action" is "force" then a duplicate key is overwritten.
10913 * Otherwise duplicate keys are ignored ("action" is "keep").
10914 */
10915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010916dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010917{
10918 dictitem_T *di1;
10919 hashitem_T *hi2;
10920 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010921 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010922
10923 todo = (int)d2->dv_hashtab.ht_used;
10924 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10925 {
10926 if (!HASHITEM_EMPTY(hi2))
10927 {
10928 --todo;
10929 di1 = dict_find(d1, hi2->hi_key, -1);
10930 if (d1->dv_scope != 0)
10931 {
10932 /* Disallow replacing a builtin function in l: and g:.
10933 * Check the key to be valid when adding to any
10934 * scope. */
10935 if (d1->dv_scope == VAR_DEF_SCOPE
10936 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10937 && var_check_func_name(hi2->hi_key,
10938 di1 == NULL))
10939 break;
10940 if (!valid_varname(hi2->hi_key))
10941 break;
10942 }
10943 if (di1 == NULL)
10944 {
10945 di1 = dictitem_copy(HI2DI(hi2));
10946 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10947 dictitem_free(di1);
10948 }
10949 else if (*action == 'e')
10950 {
10951 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10952 break;
10953 }
10954 else if (*action == 'f' && HI2DI(hi2) != di1)
10955 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010956 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10957 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010958 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010959 clear_tv(&di1->di_tv);
10960 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10961 }
10962 }
10963 }
10964}
10965
10966/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010967 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010969 */
10970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010971f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010972{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010973 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010974
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010976 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010977 list_T *l1, *l2;
10978 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010981
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 l1 = argvars[0].vval.v_list;
10983 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010984 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010985 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 {
10987 if (argvars[2].v_type != VAR_UNKNOWN)
10988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989 before = get_tv_number_chk(&argvars[2], &error);
10990 if (error)
10991 return; /* type error; errmsg already given */
10992
Bram Moolenaar758711c2005-02-02 23:11:38 +000010993 if (before == l1->lv_len)
10994 item = NULL;
10995 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010996 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010997 item = list_find(l1, before);
10998 if (item == NULL)
10999 {
11000 EMSGN(_(e_listidx), before);
11001 return;
11002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 }
11004 }
11005 else
11006 item = NULL;
11007 list_extend(l1, l2, item);
11008
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 copy_tv(&argvars[0], rettv);
11010 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011011 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011012 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11013 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011014 dict_T *d1, *d2;
11015 char_u *action;
11016 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011017
11018 d1 = argvars[0].vval.v_dict;
11019 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011020 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011021 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 {
11023 /* Check the third argument. */
11024 if (argvars[2].v_type != VAR_UNKNOWN)
11025 {
11026 static char *(av[]) = {"keep", "force", "error"};
11027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 action = get_tv_string_chk(&argvars[2]);
11029 if (action == NULL)
11030 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011031 for (i = 0; i < 3; ++i)
11032 if (STRCMP(action, av[i]) == 0)
11033 break;
11034 if (i == 3)
11035 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011036 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011037 return;
11038 }
11039 }
11040 else
11041 action = (char_u *)"force";
11042
Bram Moolenaara9922d62013-05-30 13:01:18 +020011043 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 copy_tv(&argvars[0], rettv);
11046 }
11047 }
11048 else
11049 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011050}
11051
11052/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011053 * "feedkeys()" function
11054 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011056f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011057{
11058 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011059 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011060 char_u *keys, *flags;
11061 char_u nbuf[NUMBUFLEN];
11062 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011063 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011064 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011065
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011066 /* This is not allowed in the sandbox. If the commands would still be
11067 * executed in the sandbox it would be OK, but it probably happens later,
11068 * when "sandbox" is no longer set. */
11069 if (check_secure())
11070 return;
11071
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011072 keys = get_tv_string(&argvars[0]);
11073 if (*keys != NUL)
11074 {
11075 if (argvars[1].v_type != VAR_UNKNOWN)
11076 {
11077 flags = get_tv_string_buf(&argvars[1], nbuf);
11078 for ( ; *flags != NUL; ++flags)
11079 {
11080 switch (*flags)
11081 {
11082 case 'n': remap = FALSE; break;
11083 case 'm': remap = TRUE; break;
11084 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011085 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011086 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011087 }
11088 }
11089 }
11090
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011091 /* Need to escape K_SPECIAL and CSI before putting the string in the
11092 * typeahead buffer. */
11093 keys_esc = vim_strsave_escape_csi(keys);
11094 if (keys_esc != NULL)
11095 {
11096 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011097 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011098 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011099 if (vgetc_busy)
11100 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011101 if (execute)
11102 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011103 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011104 }
11105}
11106
11107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 * "filereadable()" function
11109 */
11110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011111f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011113 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 char_u *p;
11115 int n;
11116
Bram Moolenaarc236c162008-07-13 17:41:49 +000011117#ifndef O_NONBLOCK
11118# define O_NONBLOCK 0
11119#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011121 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11122 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 {
11124 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011125 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 }
11127 else
11128 n = FALSE;
11129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131}
11132
11133/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011134 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 * rights to write into.
11136 */
11137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011138f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011140 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141}
11142
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011143 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011144findfilendir(
11145 typval_T *argvars UNUSED,
11146 typval_T *rettv,
11147 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011148{
11149#ifdef FEAT_SEARCHPATH
11150 char_u *fname;
11151 char_u *fresult = NULL;
11152 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11153 char_u *p;
11154 char_u pathbuf[NUMBUFLEN];
11155 int count = 1;
11156 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011157 int error = FALSE;
11158#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011159
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011160 rettv->vval.v_string = NULL;
11161 rettv->v_type = VAR_STRING;
11162
11163#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011165
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011166 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011168 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11169 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011170 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 else
11172 {
11173 if (*p != NUL)
11174 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011176 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011177 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011179 }
11180
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011181 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11182 error = TRUE;
11183
11184 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 do
11187 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011188 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011189 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 fresult = find_file_in_path_option(first ? fname : NULL,
11191 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011192 0, first, path,
11193 find_what,
11194 curbuf->b_ffname,
11195 find_what == FINDFILE_DIR
11196 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011198
11199 if (fresult != NULL && rettv->v_type == VAR_LIST)
11200 list_append_string(rettv->vval.v_list, fresult, -1);
11201
11202 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011204
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011205 if (rettv->v_type == VAR_STRING)
11206 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011207#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011208}
11209
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011210static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11211static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011212
11213/*
11214 * Implementation of map() and filter().
11215 */
11216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011217filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011218{
11219 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 listitem_T *li, *nli;
11222 list_T *l = NULL;
11223 dictitem_T *di;
11224 hashtab_T *ht;
11225 hashitem_T *hi;
11226 dict_T *d = NULL;
11227 typval_T save_val;
11228 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011230 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011231 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011232 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011233 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011234 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011235 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011236
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011238 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011239 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011240 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011241 return;
11242 }
11243 else if (argvars[0].v_type == VAR_DICT)
11244 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011245 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011246 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011247 return;
11248 }
11249 else
11250 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011251 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011252 return;
11253 }
11254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011255 expr = get_tv_string_buf_chk(&argvars[1], buf);
11256 /* On type errors, the preceding call has already displayed an error
11257 * message. Avoid a misleading error message for an empty string that
11258 * was not passed as argument. */
11259 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261 prepare_vimvar(VV_VAL, &save_val);
11262 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011263
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011264 /* We reset "did_emsg" to be able to detect whether an error
11265 * occurred during evaluation of the expression. */
11266 save_did_emsg = did_emsg;
11267 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011268
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011269 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 vimvars[VV_KEY].vv_type = VAR_STRING;
11273
11274 ht = &d->dv_hashtab;
11275 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011276 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 if (!HASHITEM_EMPTY(hi))
11280 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011281 int r;
11282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011283 --todo;
11284 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011285 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011286 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11287 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 break;
11289 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011290 r = filter_map_one(&di->di_tv, expr, map, &rem);
11291 clear_tv(&vimvars[VV_KEY].vv_tv);
11292 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011293 break;
11294 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011295 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011296 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11297 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011298 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 }
11302 }
11303 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 }
11305 else
11306 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011307 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011309 for (li = l->lv_first; li != NULL; li = nli)
11310 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011311 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011312 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011314 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011315 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011316 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011317 break;
11318 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011319 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011320 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011321 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011322 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011323
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011324 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011326
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011327 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011329
11330 copy_tv(&argvars[0], rettv);
11331}
11332
11333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011334filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011335{
Bram Moolenaar33570922005-01-25 22:26:29 +000011336 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011337 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011338 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011339
Bram Moolenaar33570922005-01-25 22:26:29 +000011340 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011341 s = expr;
11342 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011343 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011344 if (*s != NUL) /* check for trailing chars after expr */
11345 {
11346 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011347 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011348 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011349 }
11350 if (map)
11351 {
11352 /* map(): replace the list item value */
11353 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011354 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011355 *tv = rettv;
11356 }
11357 else
11358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 int error = FALSE;
11360
Bram Moolenaare9a41262005-01-15 22:18:47 +000011361 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011363 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 /* On type error, nothing has been removed; return FAIL to stop the
11365 * loop. The error message was given by get_tv_number_chk(). */
11366 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011367 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011368 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011369 retval = OK;
11370theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011371 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011372 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011373}
11374
11375/*
11376 * "filter()" function
11377 */
11378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011379f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011380{
11381 filter_map(argvars, rettv, FALSE);
11382}
11383
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011384/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011385 * "finddir({fname}[, {path}[, {count}]])" function
11386 */
11387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011388f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011389{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011390 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011391}
11392
11393/*
11394 * "findfile({fname}[, {path}[, {count}]])" function
11395 */
11396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011397f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011398{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011399 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011400}
11401
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011402#ifdef FEAT_FLOAT
11403/*
11404 * "float2nr({float})" function
11405 */
11406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011407f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011408{
11409 float_T f;
11410
11411 if (get_float_arg(argvars, &f) == OK)
11412 {
11413 if (f < -0x7fffffff)
11414 rettv->vval.v_number = -0x7fffffff;
11415 else if (f > 0x7fffffff)
11416 rettv->vval.v_number = 0x7fffffff;
11417 else
11418 rettv->vval.v_number = (varnumber_T)f;
11419 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011420}
11421
11422/*
11423 * "floor({float})" function
11424 */
11425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011426f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011427{
11428 float_T f;
11429
11430 rettv->v_type = VAR_FLOAT;
11431 if (get_float_arg(argvars, &f) == OK)
11432 rettv->vval.v_float = floor(f);
11433 else
11434 rettv->vval.v_float = 0.0;
11435}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011436
11437/*
11438 * "fmod()" function
11439 */
11440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011441f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011442{
11443 float_T fx, fy;
11444
11445 rettv->v_type = VAR_FLOAT;
11446 if (get_float_arg(argvars, &fx) == OK
11447 && get_float_arg(&argvars[1], &fy) == OK)
11448 rettv->vval.v_float = fmod(fx, fy);
11449 else
11450 rettv->vval.v_float = 0.0;
11451}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011452#endif
11453
Bram Moolenaar0d660222005-01-07 21:51:51 +000011454/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011455 * "fnameescape({string})" function
11456 */
11457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011458f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011459{
11460 rettv->vval.v_string = vim_strsave_fnameescape(
11461 get_tv_string(&argvars[0]), FALSE);
11462 rettv->v_type = VAR_STRING;
11463}
11464
11465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 * "fnamemodify({fname}, {mods})" function
11467 */
11468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011469f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470{
11471 char_u *fname;
11472 char_u *mods;
11473 int usedlen = 0;
11474 int len;
11475 char_u *fbuf = NULL;
11476 char_u buf[NUMBUFLEN];
11477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 fname = get_tv_string_chk(&argvars[0]);
11479 mods = get_tv_string_buf_chk(&argvars[1], buf);
11480 if (fname == NULL || mods == NULL)
11481 fname = NULL;
11482 else
11483 {
11484 len = (int)STRLEN(fname);
11485 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 vim_free(fbuf);
11494}
11495
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011496static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497
11498/*
11499 * "foldclosed()" function
11500 */
11501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011502foldclosed_both(
11503 typval_T *argvars UNUSED,
11504 typval_T *rettv,
11505 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507#ifdef FEAT_FOLDING
11508 linenr_T lnum;
11509 linenr_T first, last;
11510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11513 {
11514 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11515 {
11516 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 return;
11521 }
11522 }
11523#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525}
11526
11527/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011528 * "foldclosed()" function
11529 */
11530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011531f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532{
11533 foldclosed_both(argvars, rettv, FALSE);
11534}
11535
11536/*
11537 * "foldclosedend()" function
11538 */
11539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011540f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541{
11542 foldclosed_both(argvars, rettv, TRUE);
11543}
11544
11545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 * "foldlevel()" function
11547 */
11548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011549f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550{
11551#ifdef FEAT_FOLDING
11552 linenr_T lnum;
11553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558}
11559
11560/*
11561 * "foldtext()" function
11562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011564f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565{
11566#ifdef FEAT_FOLDING
11567 linenr_T lnum;
11568 char_u *s;
11569 char_u *r;
11570 int len;
11571 char *txt;
11572#endif
11573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 rettv->v_type = VAR_STRING;
11575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011577 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11578 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11579 <= curbuf->b_ml.ml_line_count
11580 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 {
11582 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11584 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 {
11586 if (!linewhite(lnum))
11587 break;
11588 ++lnum;
11589 }
11590
11591 /* Find interesting text in this line. */
11592 s = skipwhite(ml_get(lnum));
11593 /* skip C comment-start */
11594 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011597 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011598 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011599 {
11600 s = skipwhite(ml_get(lnum + 1));
11601 if (*s == '*')
11602 s = skipwhite(s + 1);
11603 }
11604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 txt = _("+-%s%3ld lines: ");
11606 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011607 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 + 20 /* for %3ld */
11609 + STRLEN(s))); /* concatenated */
11610 if (r != NULL)
11611 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011612 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11613 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11614 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 len = (int)STRLEN(r);
11616 STRCAT(r, s);
11617 /* remove 'foldmarker' and 'commentstring' */
11618 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 }
11621 }
11622#endif
11623}
11624
11625/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011626 * "foldtextresult(lnum)" function
11627 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011629f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011630{
11631#ifdef FEAT_FOLDING
11632 linenr_T lnum;
11633 char_u *text;
11634 char_u buf[51];
11635 foldinfo_T foldinfo;
11636 int fold_count;
11637#endif
11638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639 rettv->v_type = VAR_STRING;
11640 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011641#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643 /* treat illegal types and illegal string values for {lnum} the same */
11644 if (lnum < 0)
11645 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011646 fold_count = foldedCount(curwin, lnum, &foldinfo);
11647 if (fold_count > 0)
11648 {
11649 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11650 &foldinfo, buf);
11651 if (text == buf)
11652 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011654 }
11655#endif
11656}
11657
11658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 * "foreground()" function
11660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011662f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664#ifdef FEAT_GUI
11665 if (gui.in_use)
11666 gui_mch_set_foreground();
11667#else
11668# ifdef WIN32
11669 win32_set_foreground();
11670# endif
11671#endif
11672}
11673
11674/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011675 * "function()" function
11676 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011678f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011679{
11680 char_u *s;
11681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011683 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011684 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011685 /* Don't check an autoload name for existence here. */
11686 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011687 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011688 else
11689 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011690 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011691 {
11692 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011693 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011694
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011695 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11696 * also be called from another script. Using trans_function_name()
11697 * would also work, but some plugins depend on the name being
11698 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011699 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011700 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011701 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011702 if (rettv->vval.v_string != NULL)
11703 {
11704 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011705 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011706 }
11707 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011708 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011709 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011711 }
11712}
11713
11714/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011715 * "garbagecollect()" function
11716 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011718f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011719{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011720 /* This is postponed until we are back at the toplevel, because we may be
11721 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11722 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011723
11724 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11725 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011726}
11727
11728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729 * "get()" function
11730 */
11731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011732f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011733{
Bram Moolenaar33570922005-01-25 22:26:29 +000011734 listitem_T *li;
11735 list_T *l;
11736 dictitem_T *di;
11737 dict_T *d;
11738 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011739
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 int error = FALSE;
11745
11746 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11747 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011749 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011750 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 else if (argvars[0].v_type == VAR_DICT)
11752 {
11753 if ((d = argvars[0].vval.v_dict) != NULL)
11754 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011755 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011756 if (di != NULL)
11757 tv = &di->di_tv;
11758 }
11759 }
11760 else
11761 EMSG2(_(e_listdictarg), "get()");
11762
11763 if (tv == NULL)
11764 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011766 copy_tv(&argvars[2], rettv);
11767 }
11768 else
11769 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011770}
11771
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011772static 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 +000011773
11774/*
11775 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011776 * Return a range (from start to end) of lines in rettv from the specified
11777 * buffer.
11778 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011779 */
11780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011781get_buffer_lines(
11782 buf_T *buf,
11783 linenr_T start,
11784 linenr_T end,
11785 int retlist,
11786 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011787{
11788 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011789
Bram Moolenaar959a1432013-12-14 12:17:38 +010011790 rettv->v_type = VAR_STRING;
11791 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011792 if (retlist && rettv_list_alloc(rettv) == FAIL)
11793 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011794
11795 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11796 return;
11797
11798 if (!retlist)
11799 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011800 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11801 p = ml_get_buf(buf, start, FALSE);
11802 else
11803 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011804 rettv->vval.v_string = vim_strsave(p);
11805 }
11806 else
11807 {
11808 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011809 return;
11810
11811 if (start < 1)
11812 start = 1;
11813 if (end > buf->b_ml.ml_line_count)
11814 end = buf->b_ml.ml_line_count;
11815 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011816 if (list_append_string(rettv->vval.v_list,
11817 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011818 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011819 }
11820}
11821
11822/*
11823 * "getbufline()" function
11824 */
11825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011826f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011827{
11828 linenr_T lnum;
11829 linenr_T end;
11830 buf_T *buf;
11831
11832 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11833 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011834 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011835 --emsg_off;
11836
Bram Moolenaar661b1822005-07-28 22:36:45 +000011837 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011838 if (argvars[2].v_type == VAR_UNKNOWN)
11839 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011840 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011841 end = get_tv_lnum_buf(&argvars[2], buf);
11842
Bram Moolenaar342337a2005-07-21 21:11:17 +000011843 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011844}
11845
Bram Moolenaar0d660222005-01-07 21:51:51 +000011846/*
11847 * "getbufvar()" function
11848 */
11849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011850f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011851{
11852 buf_T *buf;
11853 buf_T *save_curbuf;
11854 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011856 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011858 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11859 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011861 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011862
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011863 rettv->v_type = VAR_STRING;
11864 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011865
11866 if (buf != NULL && varname != NULL)
11867 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011868 /* set curbuf to be our buf, temporarily */
11869 save_curbuf = curbuf;
11870 curbuf = buf;
11871
Bram Moolenaar0d660222005-01-07 21:51:51 +000011872 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011873 {
11874 if (get_option_tv(&varname, rettv, TRUE) == OK)
11875 done = TRUE;
11876 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011877 else if (STRCMP(varname, "changedtick") == 0)
11878 {
11879 rettv->v_type = VAR_NUMBER;
11880 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011881 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011882 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011883 else
11884 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011885 /* Look up the variable. */
11886 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11887 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11888 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011889 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011890 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011892 done = TRUE;
11893 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011894 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011895
11896 /* restore previous notion of curbuf */
11897 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898 }
11899
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011900 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11901 /* use the default value */
11902 copy_tv(&argvars[2], rettv);
11903
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904 --emsg_off;
11905}
11906
11907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 * "getchar()" function
11909 */
11910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011911f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912{
11913 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011916 /* Position the cursor. Needed after a message that ends in a space. */
11917 windgoto(msg_row, msg_col);
11918
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 ++no_mapping;
11920 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011921 for (;;)
11922 {
11923 if (argvars[0].v_type == VAR_UNKNOWN)
11924 /* getchar(): blocking wait. */
11925 n = safe_vgetc();
11926 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11927 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011928 n = vpeekc_any();
11929 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011930 /* illegal argument or getchar(0) and no char avail: return zero */
11931 n = 0;
11932 else
11933 /* getchar(0) and char avail: return char */
11934 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011935
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011936 if (n == K_IGNORE)
11937 continue;
11938 break;
11939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 --no_mapping;
11941 --allow_keys;
11942
Bram Moolenaar219b8702006-11-01 14:32:36 +000011943 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11944 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11945 vimvars[VV_MOUSE_COL].vv_nr = 0;
11946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 if (IS_SPECIAL(n) || mod_mask != 0)
11949 {
11950 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11951 int i = 0;
11952
11953 /* Turn a special key into three bytes, plus modifier. */
11954 if (mod_mask != 0)
11955 {
11956 temp[i++] = K_SPECIAL;
11957 temp[i++] = KS_MODIFIER;
11958 temp[i++] = mod_mask;
11959 }
11960 if (IS_SPECIAL(n))
11961 {
11962 temp[i++] = K_SPECIAL;
11963 temp[i++] = K_SECOND(n);
11964 temp[i++] = K_THIRD(n);
11965 }
11966#ifdef FEAT_MBYTE
11967 else if (has_mbyte)
11968 i += (*mb_char2bytes)(n, temp + i);
11969#endif
11970 else
11971 temp[i++] = n;
11972 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973 rettv->v_type = VAR_STRING;
11974 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011975
11976#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011977 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011978 {
11979 int row = mouse_row;
11980 int col = mouse_col;
11981 win_T *win;
11982 linenr_T lnum;
11983# ifdef FEAT_WINDOWS
11984 win_T *wp;
11985# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011986 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011987
11988 if (row >= 0 && col >= 0)
11989 {
11990 /* Find the window at the mouse coordinates and compute the
11991 * text position. */
11992 win = mouse_find_win(&row, &col);
11993 (void)mouse_comp_pos(win, &row, &col, &lnum);
11994# ifdef FEAT_WINDOWS
11995 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011996 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011997# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011998 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011999 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12000 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12001 }
12002 }
12003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 }
12005}
12006
12007/*
12008 * "getcharmod()" function
12009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012011f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014}
12015
12016/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012017 * "getcharsearch()" function
12018 */
12019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012020f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012021{
12022 if (rettv_dict_alloc(rettv) != FAIL)
12023 {
12024 dict_T *dict = rettv->vval.v_dict;
12025
12026 dict_add_nr_str(dict, "char", 0L, last_csearch());
12027 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12028 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12029 }
12030}
12031
12032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 * "getcmdline()" function
12034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012036f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 rettv->v_type = VAR_STRING;
12039 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040}
12041
12042/*
12043 * "getcmdpos()" function
12044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012046f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049}
12050
12051/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012052 * "getcmdtype()" function
12053 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012055f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012056{
12057 rettv->v_type = VAR_STRING;
12058 rettv->vval.v_string = alloc(2);
12059 if (rettv->vval.v_string != NULL)
12060 {
12061 rettv->vval.v_string[0] = get_cmdline_type();
12062 rettv->vval.v_string[1] = NUL;
12063 }
12064}
12065
12066/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012067 * "getcmdwintype()" function
12068 */
12069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012070f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012071{
12072 rettv->v_type = VAR_STRING;
12073 rettv->vval.v_string = NULL;
12074#ifdef FEAT_CMDWIN
12075 rettv->vval.v_string = alloc(2);
12076 if (rettv->vval.v_string != NULL)
12077 {
12078 rettv->vval.v_string[0] = cmdwin_type;
12079 rettv->vval.v_string[1] = NUL;
12080 }
12081#endif
12082}
12083
12084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 * "getcwd()" function
12086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012088f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012090 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012091 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012094 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012095
12096 wp = find_tabwin(&argvars[0], &argvars[1]);
12097 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012099 if (wp->w_localdir != NULL)
12100 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12101 else if(globaldir != NULL)
12102 rettv->vval.v_string = vim_strsave(globaldir);
12103 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012104 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012105 cwd = alloc(MAXPATHL);
12106 if (cwd != NULL)
12107 {
12108 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12109 rettv->vval.v_string = vim_strsave(cwd);
12110 vim_free(cwd);
12111 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012112 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012113#ifdef BACKSLASH_IN_FILENAME
12114 if (rettv->vval.v_string != NULL)
12115 slash_adjust(rettv->vval.v_string);
12116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 }
12118}
12119
12120/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012121 * "getfontname()" function
12122 */
12123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012124f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012125{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126 rettv->v_type = VAR_STRING;
12127 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012128#ifdef FEAT_GUI
12129 if (gui.in_use)
12130 {
12131 GuiFont font;
12132 char_u *name = NULL;
12133
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012134 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012135 {
12136 /* Get the "Normal" font. Either the name saved by
12137 * hl_set_font_name() or from the font ID. */
12138 font = gui.norm_font;
12139 name = hl_get_font_name();
12140 }
12141 else
12142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012144 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12145 return;
12146 font = gui_mch_get_font(name, FALSE);
12147 if (font == NOFONT)
12148 return; /* Invalid font name, return empty string. */
12149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012151 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012152 gui_mch_free_font(font);
12153 }
12154#endif
12155}
12156
12157/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012158 * "getfperm({fname})" function
12159 */
12160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012161f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012162{
12163 char_u *fname;
12164 struct stat st;
12165 char_u *perm = NULL;
12166 char_u flags[] = "rwx";
12167 int i;
12168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012172 if (mch_stat((char *)fname, &st) >= 0)
12173 {
12174 perm = vim_strsave((char_u *)"---------");
12175 if (perm != NULL)
12176 {
12177 for (i = 0; i < 9; i++)
12178 {
12179 if (st.st_mode & (1 << (8 - i)))
12180 perm[i] = flags[i % 3];
12181 }
12182 }
12183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012185}
12186
12187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 * "getfsize({fname})" function
12189 */
12190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012191f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192{
12193 char_u *fname;
12194 struct stat st;
12195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199
12200 if (mch_stat((char *)fname, &st) >= 0)
12201 {
12202 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012207
12208 /* non-perfect check for overflow */
12209 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12210 rettv->vval.v_number = -2;
12211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 }
12213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215}
12216
12217/*
12218 * "getftime({fname})" function
12219 */
12220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012221f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
12223 char_u *fname;
12224 struct stat st;
12225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227
12228 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232}
12233
12234/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012235 * "getftype({fname})" function
12236 */
12237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012238f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012239{
12240 char_u *fname;
12241 struct stat st;
12242 char_u *type = NULL;
12243 char *t;
12244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012247 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012248 if (mch_lstat((char *)fname, &st) >= 0)
12249 {
12250#ifdef S_ISREG
12251 if (S_ISREG(st.st_mode))
12252 t = "file";
12253 else if (S_ISDIR(st.st_mode))
12254 t = "dir";
12255# ifdef S_ISLNK
12256 else if (S_ISLNK(st.st_mode))
12257 t = "link";
12258# endif
12259# ifdef S_ISBLK
12260 else if (S_ISBLK(st.st_mode))
12261 t = "bdev";
12262# endif
12263# ifdef S_ISCHR
12264 else if (S_ISCHR(st.st_mode))
12265 t = "cdev";
12266# endif
12267# ifdef S_ISFIFO
12268 else if (S_ISFIFO(st.st_mode))
12269 t = "fifo";
12270# endif
12271# ifdef S_ISSOCK
12272 else if (S_ISSOCK(st.st_mode))
12273 t = "fifo";
12274# endif
12275 else
12276 t = "other";
12277#else
12278# ifdef S_IFMT
12279 switch (st.st_mode & S_IFMT)
12280 {
12281 case S_IFREG: t = "file"; break;
12282 case S_IFDIR: t = "dir"; break;
12283# ifdef S_IFLNK
12284 case S_IFLNK: t = "link"; break;
12285# endif
12286# ifdef S_IFBLK
12287 case S_IFBLK: t = "bdev"; break;
12288# endif
12289# ifdef S_IFCHR
12290 case S_IFCHR: t = "cdev"; break;
12291# endif
12292# ifdef S_IFIFO
12293 case S_IFIFO: t = "fifo"; break;
12294# endif
12295# ifdef S_IFSOCK
12296 case S_IFSOCK: t = "socket"; break;
12297# endif
12298 default: t = "other";
12299 }
12300# else
12301 if (mch_isdir(fname))
12302 t = "dir";
12303 else
12304 t = "file";
12305# endif
12306#endif
12307 type = vim_strsave((char_u *)t);
12308 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012309 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012310}
12311
12312/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012313 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012314 */
12315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012316f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012317{
12318 linenr_T lnum;
12319 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012320 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012321
12322 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012323 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012324 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012325 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012326 retlist = FALSE;
12327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012329 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012331 retlist = TRUE;
12332 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012333
Bram Moolenaar342337a2005-07-21 21:11:17 +000012334 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335}
12336
12337/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012338 * "getmatches()" function
12339 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012341f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012342{
12343#ifdef FEAT_SEARCH_EXTRA
12344 dict_T *dict;
12345 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012346 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012347
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012348 if (rettv_list_alloc(rettv) == OK)
12349 {
12350 while (cur != NULL)
12351 {
12352 dict = dict_alloc();
12353 if (dict == NULL)
12354 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012355 if (cur->match.regprog == NULL)
12356 {
12357 /* match added with matchaddpos() */
12358 for (i = 0; i < MAXPOSMATCH; ++i)
12359 {
12360 llpos_T *llpos;
12361 char buf[6];
12362 list_T *l;
12363
12364 llpos = &cur->pos.pos[i];
12365 if (llpos->lnum == 0)
12366 break;
12367 l = list_alloc();
12368 if (l == NULL)
12369 break;
12370 list_append_number(l, (varnumber_T)llpos->lnum);
12371 if (llpos->col > 0)
12372 {
12373 list_append_number(l, (varnumber_T)llpos->col);
12374 list_append_number(l, (varnumber_T)llpos->len);
12375 }
12376 sprintf(buf, "pos%d", i + 1);
12377 dict_add_list(dict, buf, l);
12378 }
12379 }
12380 else
12381 {
12382 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12383 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012384 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012385 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12386 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012387# ifdef FEAT_CONCEAL
12388 if (cur->conceal_char)
12389 {
12390 char_u buf[MB_MAXBYTES + 1];
12391
12392 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12393 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12394 }
12395# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012396 list_append_dict(rettv->vval.v_list, dict);
12397 cur = cur->next;
12398 }
12399 }
12400#endif
12401}
12402
12403/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012404 * "getpid()" function
12405 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012407f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012408{
12409 rettv->vval.v_number = mch_get_pid();
12410}
12411
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012412static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012413
12414/*
12415 * "getcurpos()" function
12416 */
12417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012418f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012419{
12420 getpos_both(argvars, rettv, TRUE);
12421}
12422
Bram Moolenaar18081e32008-02-20 19:11:07 +000012423/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012424 * "getpos(string)" function
12425 */
12426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012427f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012428{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012429 getpos_both(argvars, rettv, FALSE);
12430}
12431
12432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012433getpos_both(
12434 typval_T *argvars,
12435 typval_T *rettv,
12436 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012437{
Bram Moolenaara5525202006-03-02 22:52:09 +000012438 pos_T *fp;
12439 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012440 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012441
12442 if (rettv_list_alloc(rettv) == OK)
12443 {
12444 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012445 if (getcurpos)
12446 fp = &curwin->w_cursor;
12447 else
12448 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012449 if (fnum != -1)
12450 list_append_number(l, (varnumber_T)fnum);
12451 else
12452 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012453 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12454 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012455 list_append_number(l, (fp != NULL)
12456 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012457 : (varnumber_T)0);
12458 list_append_number(l,
12459#ifdef FEAT_VIRTUALEDIT
12460 (fp != NULL) ? (varnumber_T)fp->coladd :
12461#endif
12462 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012463 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012464 {
12465 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012466 list_append_number(l, curwin->w_curswant == MAXCOL ?
12467 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012468 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012469 }
12470 else
12471 rettv->vval.v_number = FALSE;
12472}
12473
12474/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012475 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012476 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012478f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012479{
12480#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012481 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012482#endif
12483
Bram Moolenaar2641f772005-03-25 21:58:17 +000012484#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012485 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012486 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012487 wp = NULL;
12488 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12489 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012490 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012491 if (wp == NULL)
12492 return;
12493 }
12494
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012495 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012496 }
12497#endif
12498}
12499
12500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 * "getreg()" function
12502 */
12503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012504f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505{
12506 char_u *strregname;
12507 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012508 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012509 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012512 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 strregname = get_tv_string_chk(&argvars[0]);
12515 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012518 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012519 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12520 return_list = get_tv_number_chk(&argvars[2], &error);
12521 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012524 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012525
12526 if (error)
12527 return;
12528
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 regname = (strregname == NULL ? '"' : *strregname);
12530 if (regname == 0)
12531 regname = '"';
12532
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012533 if (return_list)
12534 {
12535 rettv->v_type = VAR_LIST;
12536 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12537 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012538 if (rettv->vval.v_list != NULL)
12539 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012540 }
12541 else
12542 {
12543 rettv->v_type = VAR_STRING;
12544 rettv->vval.v_string = get_reg_contents(regname,
12545 arg2 ? GREG_EXPR_SRC : 0);
12546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547}
12548
12549/*
12550 * "getregtype()" function
12551 */
12552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012553f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554{
12555 char_u *strregname;
12556 int regname;
12557 char_u buf[NUMBUFLEN + 2];
12558 long reglen = 0;
12559
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012560 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012561 {
12562 strregname = get_tv_string_chk(&argvars[0]);
12563 if (strregname == NULL) /* type error; errmsg already given */
12564 {
12565 rettv->v_type = VAR_STRING;
12566 rettv->vval.v_string = NULL;
12567 return;
12568 }
12569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 else
12571 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012572 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573
12574 regname = (strregname == NULL ? '"' : *strregname);
12575 if (regname == 0)
12576 regname = '"';
12577
12578 buf[0] = NUL;
12579 buf[1] = NUL;
12580 switch (get_reg_type(regname, &reglen))
12581 {
12582 case MLINE: buf[0] = 'V'; break;
12583 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 case MBLOCK:
12585 buf[0] = Ctrl_V;
12586 sprintf((char *)buf + 1, "%ld", reglen + 1);
12587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012589 rettv->v_type = VAR_STRING;
12590 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591}
12592
12593/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012594 * "gettabvar()" function
12595 */
12596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012597f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012598{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012599 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012600 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012601 dictitem_T *v;
12602 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012603 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012604
12605 rettv->v_type = VAR_STRING;
12606 rettv->vval.v_string = NULL;
12607
12608 varname = get_tv_string_chk(&argvars[1]);
12609 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12610 if (tp != NULL && varname != NULL)
12611 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012612 /* Set tp to be our tabpage, temporarily. Also set the window to the
12613 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012614 if (switch_win(&oldcurwin, &oldtabpage,
12615 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012616 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012617 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012618 /* look up the variable */
12619 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12620 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12621 if (v != NULL)
12622 {
12623 copy_tv(&v->di_tv, rettv);
12624 done = TRUE;
12625 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012626 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012627
12628 /* restore previous notion of curwin */
12629 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012630 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012631
12632 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012633 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012634}
12635
12636/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012637 * "gettabwinvar()" function
12638 */
12639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012640f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012641{
12642 getwinvar(argvars, rettv, 1);
12643}
12644
12645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 * "getwinposx()" function
12647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012649f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#ifdef FEAT_GUI
12653 if (gui.in_use)
12654 {
12655 int x, y;
12656
12657 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659 }
12660#endif
12661}
12662
12663/*
12664 * "getwinposy()" function
12665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012667f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670#ifdef FEAT_GUI
12671 if (gui.in_use)
12672 {
12673 int x, y;
12674
12675 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 }
12678#endif
12679}
12680
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012681/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012682 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012683 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012684 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012685find_win_by_nr(
12686 typval_T *vp,
12687 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012688{
12689#ifdef FEAT_WINDOWS
12690 win_T *wp;
12691#endif
12692 int nr;
12693
12694 nr = get_tv_number_chk(vp, NULL);
12695
12696#ifdef FEAT_WINDOWS
12697 if (nr < 0)
12698 return NULL;
12699 if (nr == 0)
12700 return curwin;
12701
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012702 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12703 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012704 if (--nr <= 0)
12705 break;
12706 return wp;
12707#else
12708 if (nr == 0 || nr == 1)
12709 return curwin;
12710 return NULL;
12711#endif
12712}
12713
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012715 * Find window specified by "wvp" in tabpage "tvp".
12716 */
12717 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012718find_tabwin(
12719 typval_T *wvp, /* VAR_UNKNOWN for current window */
12720 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012721{
12722 win_T *wp = NULL;
12723 tabpage_T *tp = NULL;
12724 long n;
12725
12726 if (wvp->v_type != VAR_UNKNOWN)
12727 {
12728 if (tvp->v_type != VAR_UNKNOWN)
12729 {
12730 n = get_tv_number(tvp);
12731 if (n >= 0)
12732 tp = find_tabpage(n);
12733 }
12734 else
12735 tp = curtab;
12736
12737 if (tp != NULL)
12738 wp = find_win_by_nr(wvp, tp);
12739 }
12740 else
12741 wp = curwin;
12742
12743 return wp;
12744}
12745
12746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 * "getwinvar()" function
12748 */
12749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012750f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012752 getwinvar(argvars, rettv, 0);
12753}
12754
12755/*
12756 * getwinvar() and gettabwinvar()
12757 */
12758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012759getwinvar(
12760 typval_T *argvars,
12761 typval_T *rettv,
12762 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012763{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012764 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012767 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012768 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012769#ifdef FEAT_WINDOWS
12770 win_T *oldcurwin;
12771 tabpage_T *oldtabpage;
12772 int need_switch_win;
12773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012775#ifdef FEAT_WINDOWS
12776 if (off == 1)
12777 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12778 else
12779 tp = curtab;
12780#endif
12781 win = find_win_by_nr(&argvars[off], tp);
12782 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012785 rettv->v_type = VAR_STRING;
12786 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787
12788 if (win != NULL && varname != NULL)
12789 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012790#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012791 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012792 * otherwise the window is not valid. Only do this when needed,
12793 * autocommands get blocked. */
12794 need_switch_win = !(tp == curtab && win == curwin);
12795 if (!need_switch_win
12796 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12797#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012798 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012799 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012800 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012801 if (get_option_tv(&varname, rettv, 1) == OK)
12802 done = TRUE;
12803 }
12804 else
12805 {
12806 /* Look up the variable. */
12807 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12808 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12809 varname, FALSE);
12810 if (v != NULL)
12811 {
12812 copy_tv(&v->di_tv, rettv);
12813 done = TRUE;
12814 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012817
Bram Moolenaarba117c22015-09-29 16:53:22 +020012818#ifdef FEAT_WINDOWS
12819 if (need_switch_win)
12820 /* restore previous notion of curwin */
12821 restore_win(oldcurwin, oldtabpage, TRUE);
12822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 }
12824
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012825 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12826 /* use the default return value */
12827 copy_tv(&argvars[off + 2], rettv);
12828
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 --emsg_off;
12830}
12831
12832/*
12833 * "glob()" function
12834 */
12835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012836f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012838 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012840 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012842 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012843 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012845 if (argvars[1].v_type != VAR_UNKNOWN)
12846 {
12847 if (get_tv_number_chk(&argvars[1], &error))
12848 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012849 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012850 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012851 if (get_tv_number_chk(&argvars[2], &error))
12852 {
12853 rettv->v_type = VAR_LIST;
12854 rettv->vval.v_list = NULL;
12855 }
12856 if (argvars[3].v_type != VAR_UNKNOWN
12857 && get_tv_number_chk(&argvars[3], &error))
12858 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012859 }
12860 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012861 if (!error)
12862 {
12863 ExpandInit(&xpc);
12864 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012865 if (p_wic)
12866 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012867 if (rettv->v_type == VAR_STRING)
12868 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012869 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012870 else if (rettv_list_alloc(rettv) != FAIL)
12871 {
12872 int i;
12873
12874 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12875 NULL, options, WILD_ALL_KEEP);
12876 for (i = 0; i < xpc.xp_numfiles; i++)
12877 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12878
12879 ExpandCleanup(&xpc);
12880 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012881 }
12882 else
12883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884}
12885
12886/*
12887 * "globpath()" function
12888 */
12889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012890f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012892 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012894 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012895 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012896 garray_T ga;
12897 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012899 /* When the optional second argument is non-zero, don't remove matches
12900 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012902 if (argvars[2].v_type != VAR_UNKNOWN)
12903 {
12904 if (get_tv_number_chk(&argvars[2], &error))
12905 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012906 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012907 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012908 if (get_tv_number_chk(&argvars[3], &error))
12909 {
12910 rettv->v_type = VAR_LIST;
12911 rettv->vval.v_list = NULL;
12912 }
12913 if (argvars[4].v_type != VAR_UNKNOWN
12914 && get_tv_number_chk(&argvars[4], &error))
12915 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012916 }
12917 }
12918 if (file != NULL && !error)
12919 {
12920 ga_init2(&ga, (int)sizeof(char_u *), 10);
12921 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12922 if (rettv->v_type == VAR_STRING)
12923 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12924 else if (rettv_list_alloc(rettv) != FAIL)
12925 for (i = 0; i < ga.ga_len; ++i)
12926 list_append_string(rettv->vval.v_list,
12927 ((char_u **)(ga.ga_data))[i], -1);
12928 ga_clear_strings(&ga);
12929 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012930 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
12934/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012935 * "glob2regpat()" function
12936 */
12937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012938f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012939{
12940 char_u *pat = get_tv_string_chk(&argvars[0]);
12941
12942 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012943 rettv->vval.v_string = (pat == NULL)
12944 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012945}
12946
12947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 * "has()" function
12949 */
12950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012951f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952{
12953 int i;
12954 char_u *name;
12955 int n = FALSE;
12956 static char *(has_list[]) =
12957 {
12958#ifdef AMIGA
12959 "amiga",
12960# ifdef FEAT_ARP
12961 "arp",
12962# endif
12963#endif
12964#ifdef __BEOS__
12965 "beos",
12966#endif
12967#ifdef MSDOS
12968# ifdef DJGPP
12969 "dos32",
12970# else
12971 "dos16",
12972# endif
12973#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012974#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 "mac",
12976#endif
12977#if defined(MACOS_X_UNIX)
12978 "macunix",
12979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980#ifdef __QNX__
12981 "qnx",
12982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#ifdef UNIX
12984 "unix",
12985#endif
12986#ifdef VMS
12987 "vms",
12988#endif
12989#ifdef WIN16
12990 "win16",
12991#endif
12992#ifdef WIN32
12993 "win32",
12994#endif
12995#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12996 "win32unix",
12997#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012998#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 "win64",
13000#endif
13001#ifdef EBCDIC
13002 "ebcdic",
13003#endif
13004#ifndef CASE_INSENSITIVE_FILENAME
13005 "fname_case",
13006#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013007#ifdef HAVE_ACL
13008 "acl",
13009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010#ifdef FEAT_ARABIC
13011 "arabic",
13012#endif
13013#ifdef FEAT_AUTOCMD
13014 "autocmd",
13015#endif
13016#ifdef FEAT_BEVAL
13017 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013018# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13019 "balloon_multiline",
13020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021#endif
13022#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13023 "builtin_terms",
13024# ifdef ALL_BUILTIN_TCAPS
13025 "all_builtin_terms",
13026# endif
13027#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013028#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13029 || defined(FEAT_GUI_W32) \
13030 || defined(FEAT_GUI_MOTIF))
13031 "browsefilter",
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef FEAT_BYTEOFF
13034 "byte_offset",
13035#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013036#ifdef FEAT_CHANNEL
13037 "channel",
13038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039#ifdef FEAT_CINDENT
13040 "cindent",
13041#endif
13042#ifdef FEAT_CLIENTSERVER
13043 "clientserver",
13044#endif
13045#ifdef FEAT_CLIPBOARD
13046 "clipboard",
13047#endif
13048#ifdef FEAT_CMDL_COMPL
13049 "cmdline_compl",
13050#endif
13051#ifdef FEAT_CMDHIST
13052 "cmdline_hist",
13053#endif
13054#ifdef FEAT_COMMENTS
13055 "comments",
13056#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013057#ifdef FEAT_CONCEAL
13058 "conceal",
13059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060#ifdef FEAT_CRYPT
13061 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013062 "crypt-blowfish",
13063 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064#endif
13065#ifdef FEAT_CSCOPE
13066 "cscope",
13067#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013068#ifdef FEAT_CURSORBIND
13069 "cursorbind",
13070#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013071#ifdef CURSOR_SHAPE
13072 "cursorshape",
13073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074#ifdef DEBUG
13075 "debug",
13076#endif
13077#ifdef FEAT_CON_DIALOG
13078 "dialog_con",
13079#endif
13080#ifdef FEAT_GUI_DIALOG
13081 "dialog_gui",
13082#endif
13083#ifdef FEAT_DIFF
13084 "diff",
13085#endif
13086#ifdef FEAT_DIGRAPHS
13087 "digraphs",
13088#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013089#ifdef FEAT_DIRECTX
13090 "directx",
13091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092#ifdef FEAT_DND
13093 "dnd",
13094#endif
13095#ifdef FEAT_EMACS_TAGS
13096 "emacs_tags",
13097#endif
13098 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013099 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100#ifdef FEAT_SEARCH_EXTRA
13101 "extra_search",
13102#endif
13103#ifdef FEAT_FKMAP
13104 "farsi",
13105#endif
13106#ifdef FEAT_SEARCHPATH
13107 "file_in_path",
13108#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013109#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013110 "filterpipe",
13111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112#ifdef FEAT_FIND_ID
13113 "find_in_path",
13114#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013115#ifdef FEAT_FLOAT
13116 "float",
13117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118#ifdef FEAT_FOLDING
13119 "folding",
13120#endif
13121#ifdef FEAT_FOOTER
13122 "footer",
13123#endif
13124#if !defined(USE_SYSTEM) && defined(UNIX)
13125 "fork",
13126#endif
13127#ifdef FEAT_GETTEXT
13128 "gettext",
13129#endif
13130#ifdef FEAT_GUI
13131 "gui",
13132#endif
13133#ifdef FEAT_GUI_ATHENA
13134# ifdef FEAT_GUI_NEXTAW
13135 "gui_neXtaw",
13136# else
13137 "gui_athena",
13138# endif
13139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140#ifdef FEAT_GUI_GTK
13141 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013144#ifdef FEAT_GUI_GNOME
13145 "gui_gnome",
13146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147#ifdef FEAT_GUI_MAC
13148 "gui_mac",
13149#endif
13150#ifdef FEAT_GUI_MOTIF
13151 "gui_motif",
13152#endif
13153#ifdef FEAT_GUI_PHOTON
13154 "gui_photon",
13155#endif
13156#ifdef FEAT_GUI_W16
13157 "gui_win16",
13158#endif
13159#ifdef FEAT_GUI_W32
13160 "gui_win32",
13161#endif
13162#ifdef FEAT_HANGULIN
13163 "hangul_input",
13164#endif
13165#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13166 "iconv",
13167#endif
13168#ifdef FEAT_INS_EXPAND
13169 "insert_expand",
13170#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013171#ifdef FEAT_JOB
13172 "job",
13173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174#ifdef FEAT_JUMPLIST
13175 "jumplist",
13176#endif
13177#ifdef FEAT_KEYMAP
13178 "keymap",
13179#endif
13180#ifdef FEAT_LANGMAP
13181 "langmap",
13182#endif
13183#ifdef FEAT_LIBCALL
13184 "libcall",
13185#endif
13186#ifdef FEAT_LINEBREAK
13187 "linebreak",
13188#endif
13189#ifdef FEAT_LISP
13190 "lispindent",
13191#endif
13192#ifdef FEAT_LISTCMDS
13193 "listcmds",
13194#endif
13195#ifdef FEAT_LOCALMAP
13196 "localmap",
13197#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013198#ifdef FEAT_LUA
13199# ifndef DYNAMIC_LUA
13200 "lua",
13201# endif
13202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203#ifdef FEAT_MENU
13204 "menu",
13205#endif
13206#ifdef FEAT_SESSION
13207 "mksession",
13208#endif
13209#ifdef FEAT_MODIFY_FNAME
13210 "modify_fname",
13211#endif
13212#ifdef FEAT_MOUSE
13213 "mouse",
13214#endif
13215#ifdef FEAT_MOUSESHAPE
13216 "mouseshape",
13217#endif
13218#if defined(UNIX) || defined(VMS)
13219# ifdef FEAT_MOUSE_DEC
13220 "mouse_dec",
13221# endif
13222# ifdef FEAT_MOUSE_GPM
13223 "mouse_gpm",
13224# endif
13225# ifdef FEAT_MOUSE_JSB
13226 "mouse_jsbterm",
13227# endif
13228# ifdef FEAT_MOUSE_NET
13229 "mouse_netterm",
13230# endif
13231# ifdef FEAT_MOUSE_PTERM
13232 "mouse_pterm",
13233# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013234# ifdef FEAT_MOUSE_SGR
13235 "mouse_sgr",
13236# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013237# ifdef FEAT_SYSMOUSE
13238 "mouse_sysmouse",
13239# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013240# ifdef FEAT_MOUSE_URXVT
13241 "mouse_urxvt",
13242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243# ifdef FEAT_MOUSE_XTERM
13244 "mouse_xterm",
13245# endif
13246#endif
13247#ifdef FEAT_MBYTE
13248 "multi_byte",
13249#endif
13250#ifdef FEAT_MBYTE_IME
13251 "multi_byte_ime",
13252#endif
13253#ifdef FEAT_MULTI_LANG
13254 "multi_lang",
13255#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013256#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013257#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013258 "mzscheme",
13259#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261#ifdef FEAT_OLE
13262 "ole",
13263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264#ifdef FEAT_PATH_EXTRA
13265 "path_extra",
13266#endif
13267#ifdef FEAT_PERL
13268#ifndef DYNAMIC_PERL
13269 "perl",
13270#endif
13271#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013272#ifdef FEAT_PERSISTENT_UNDO
13273 "persistent_undo",
13274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275#ifdef FEAT_PYTHON
13276#ifndef DYNAMIC_PYTHON
13277 "python",
13278#endif
13279#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013280#ifdef FEAT_PYTHON3
13281#ifndef DYNAMIC_PYTHON3
13282 "python3",
13283#endif
13284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285#ifdef FEAT_POSTSCRIPT
13286 "postscript",
13287#endif
13288#ifdef FEAT_PRINTER
13289 "printer",
13290#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013291#ifdef FEAT_PROFILE
13292 "profile",
13293#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013294#ifdef FEAT_RELTIME
13295 "reltime",
13296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297#ifdef FEAT_QUICKFIX
13298 "quickfix",
13299#endif
13300#ifdef FEAT_RIGHTLEFT
13301 "rightleft",
13302#endif
13303#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13304 "ruby",
13305#endif
13306#ifdef FEAT_SCROLLBIND
13307 "scrollbind",
13308#endif
13309#ifdef FEAT_CMDL_INFO
13310 "showcmd",
13311 "cmdline_info",
13312#endif
13313#ifdef FEAT_SIGNS
13314 "signs",
13315#endif
13316#ifdef FEAT_SMARTINDENT
13317 "smartindent",
13318#endif
13319#ifdef FEAT_SNIFF
13320 "sniff",
13321#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013322#ifdef STARTUPTIME
13323 "startuptime",
13324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325#ifdef FEAT_STL_OPT
13326 "statusline",
13327#endif
13328#ifdef FEAT_SUN_WORKSHOP
13329 "sun_workshop",
13330#endif
13331#ifdef FEAT_NETBEANS_INTG
13332 "netbeans_intg",
13333#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013334#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013335 "spell",
13336#endif
13337#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 "syntax",
13339#endif
13340#if defined(USE_SYSTEM) || !defined(UNIX)
13341 "system",
13342#endif
13343#ifdef FEAT_TAG_BINS
13344 "tag_binary",
13345#endif
13346#ifdef FEAT_TAG_OLDSTATIC
13347 "tag_old_static",
13348#endif
13349#ifdef FEAT_TAG_ANYWHITE
13350 "tag_any_white",
13351#endif
13352#ifdef FEAT_TCL
13353# ifndef DYNAMIC_TCL
13354 "tcl",
13355# endif
13356#endif
13357#ifdef TERMINFO
13358 "terminfo",
13359#endif
13360#ifdef FEAT_TERMRESPONSE
13361 "termresponse",
13362#endif
13363#ifdef FEAT_TEXTOBJ
13364 "textobjects",
13365#endif
13366#ifdef HAVE_TGETENT
13367 "tgetent",
13368#endif
13369#ifdef FEAT_TITLE
13370 "title",
13371#endif
13372#ifdef FEAT_TOOLBAR
13373 "toolbar",
13374#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013375#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13376 "unnamedplus",
13377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378#ifdef FEAT_USR_CMDS
13379 "user-commands", /* was accidentally included in 5.4 */
13380 "user_commands",
13381#endif
13382#ifdef FEAT_VIMINFO
13383 "viminfo",
13384#endif
13385#ifdef FEAT_VERTSPLIT
13386 "vertsplit",
13387#endif
13388#ifdef FEAT_VIRTUALEDIT
13389 "virtualedit",
13390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392#ifdef FEAT_VISUALEXTRA
13393 "visualextra",
13394#endif
13395#ifdef FEAT_VREPLACE
13396 "vreplace",
13397#endif
13398#ifdef FEAT_WILDIGN
13399 "wildignore",
13400#endif
13401#ifdef FEAT_WILDMENU
13402 "wildmenu",
13403#endif
13404#ifdef FEAT_WINDOWS
13405 "windows",
13406#endif
13407#ifdef FEAT_WAK
13408 "winaltkeys",
13409#endif
13410#ifdef FEAT_WRITEBACKUP
13411 "writebackup",
13412#endif
13413#ifdef FEAT_XIM
13414 "xim",
13415#endif
13416#ifdef FEAT_XFONTSET
13417 "xfontset",
13418#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013419#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013420 "xpm",
13421 "xpm_w32", /* for backward compatibility */
13422#else
13423# if defined(HAVE_XPM)
13424 "xpm",
13425# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427#ifdef USE_XSMP
13428 "xsmp",
13429#endif
13430#ifdef USE_XSMP_INTERACT
13431 "xsmp_interact",
13432#endif
13433#ifdef FEAT_XCLIPBOARD
13434 "xterm_clipboard",
13435#endif
13436#ifdef FEAT_XTERM_SAVE
13437 "xterm_save",
13438#endif
13439#if defined(UNIX) && defined(FEAT_X11)
13440 "X11",
13441#endif
13442 NULL
13443 };
13444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013445 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 for (i = 0; has_list[i] != NULL; ++i)
13447 if (STRICMP(name, has_list[i]) == 0)
13448 {
13449 n = TRUE;
13450 break;
13451 }
13452
13453 if (n == FALSE)
13454 {
13455 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013456 {
13457 if (name[5] == '-'
13458 && STRLEN(name) > 11
13459 && vim_isdigit(name[6])
13460 && vim_isdigit(name[8])
13461 && vim_isdigit(name[10]))
13462 {
13463 int major = atoi((char *)name + 6);
13464 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013465
13466 /* Expect "patch-9.9.01234". */
13467 n = (major < VIM_VERSION_MAJOR
13468 || (major == VIM_VERSION_MAJOR
13469 && (minor < VIM_VERSION_MINOR
13470 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013471 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013472 }
13473 else
13474 n = has_patch(atoi((char *)name + 5));
13475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 else if (STRICMP(name, "vim_starting") == 0)
13477 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013478#ifdef FEAT_MBYTE
13479 else if (STRICMP(name, "multi_byte_encoding") == 0)
13480 n = has_mbyte;
13481#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013482#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13483 else if (STRICMP(name, "balloon_multiline") == 0)
13484 n = multiline_balloon_available();
13485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486#ifdef DYNAMIC_TCL
13487 else if (STRICMP(name, "tcl") == 0)
13488 n = tcl_enabled(FALSE);
13489#endif
13490#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13491 else if (STRICMP(name, "iconv") == 0)
13492 n = iconv_enabled(FALSE);
13493#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013494#ifdef DYNAMIC_LUA
13495 else if (STRICMP(name, "lua") == 0)
13496 n = lua_enabled(FALSE);
13497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013498#ifdef DYNAMIC_MZSCHEME
13499 else if (STRICMP(name, "mzscheme") == 0)
13500 n = mzscheme_enabled(FALSE);
13501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502#ifdef DYNAMIC_RUBY
13503 else if (STRICMP(name, "ruby") == 0)
13504 n = ruby_enabled(FALSE);
13505#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013506#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507#ifdef DYNAMIC_PYTHON
13508 else if (STRICMP(name, "python") == 0)
13509 n = python_enabled(FALSE);
13510#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013511#endif
13512#ifdef FEAT_PYTHON3
13513#ifdef DYNAMIC_PYTHON3
13514 else if (STRICMP(name, "python3") == 0)
13515 n = python3_enabled(FALSE);
13516#endif
13517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518#ifdef DYNAMIC_PERL
13519 else if (STRICMP(name, "perl") == 0)
13520 n = perl_enabled(FALSE);
13521#endif
13522#ifdef FEAT_GUI
13523 else if (STRICMP(name, "gui_running") == 0)
13524 n = (gui.in_use || gui.starting);
13525# ifdef FEAT_GUI_W32
13526 else if (STRICMP(name, "gui_win32s") == 0)
13527 n = gui_is_win32s();
13528# endif
13529# ifdef FEAT_BROWSE
13530 else if (STRICMP(name, "browse") == 0)
13531 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13532# endif
13533#endif
13534#ifdef FEAT_SYN_HL
13535 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013536 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537#endif
13538#if defined(WIN3264)
13539 else if (STRICMP(name, "win95") == 0)
13540 n = mch_windows95();
13541#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013542#ifdef FEAT_NETBEANS_INTG
13543 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013544 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 }
13547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549}
13550
13551/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013552 * "has_key()" function
13553 */
13554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013555f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013556{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013557 if (argvars[0].v_type != VAR_DICT)
13558 {
13559 EMSG(_(e_dictreq));
13560 return;
13561 }
13562 if (argvars[0].vval.v_dict == NULL)
13563 return;
13564
13565 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013566 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013567}
13568
13569/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013570 * "haslocaldir()" function
13571 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013573f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013574{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013575 win_T *wp = NULL;
13576
13577 wp = find_tabwin(&argvars[0], &argvars[1]);
13578 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013579}
13580
13581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 * "hasmapto()" function
13583 */
13584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013585f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586{
13587 char_u *name;
13588 char_u *mode;
13589 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013590 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013593 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 mode = (char_u *)"nvo";
13595 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013598 if (argvars[2].v_type != VAR_UNKNOWN)
13599 abbr = get_tv_number(&argvars[2]);
13600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601
Bram Moolenaar2c932302006-03-18 21:42:09 +000013602 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606}
13607
13608/*
13609 * "histadd()" function
13610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013612f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613{
13614#ifdef FEAT_CMDHIST
13615 int histype;
13616 char_u *str;
13617 char_u buf[NUMBUFLEN];
13618#endif
13619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 if (check_restricted() || check_secure())
13622 return;
13623#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13625 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 if (histype >= 0)
13627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 if (*str != NUL)
13630 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013631 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 return;
13635 }
13636 }
13637#endif
13638}
13639
13640/*
13641 * "histdel()" function
13642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013644f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645{
13646#ifdef FEAT_CMDHIST
13647 int n;
13648 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013649 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013651 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13652 if (str == NULL)
13653 n = 0;
13654 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013657 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661 else
13662 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 get_tv_string_buf(&argvars[1], buf));
13665 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666#endif
13667}
13668
13669/*
13670 * "histget()" function
13671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013673f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674{
13675#ifdef FEAT_CMDHIST
13676 int type;
13677 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013678 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13681 if (str == NULL)
13682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013684 {
13685 type = get_histtype(str);
13686 if (argvars[1].v_type == VAR_UNKNOWN)
13687 idx = get_history_idx(type);
13688 else
13689 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13690 /* -1 on type error */
13691 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013696 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697}
13698
13699/*
13700 * "histnr()" function
13701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013703f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704{
13705 int i;
13706
13707#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 char_u *history = get_tv_string_chk(&argvars[0]);
13709
13710 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 if (i >= HIST_CMD && i < HIST_COUNT)
13712 i = get_history_idx(i);
13713 else
13714#endif
13715 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717}
13718
13719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 * "highlightID(name)" function
13721 */
13722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013723f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726}
13727
13728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013729 * "highlight_exists()" function
13730 */
13731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013732f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733{
13734 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13735}
13736
13737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 * "hostname()" function
13739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013741f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742{
13743 char_u hostname[256];
13744
13745 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->v_type = VAR_STRING;
13747 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748}
13749
13750/*
13751 * iconv() function
13752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013754f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755{
13756#ifdef FEAT_MBYTE
13757 char_u buf1[NUMBUFLEN];
13758 char_u buf2[NUMBUFLEN];
13759 char_u *from, *to, *str;
13760 vimconv_T vimconv;
13761#endif
13762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013763 rettv->v_type = VAR_STRING;
13764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765
13766#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013767 str = get_tv_string(&argvars[0]);
13768 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13769 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 vimconv.vc_type = CONV_NONE;
13771 convert_setup(&vimconv, from, to);
13772
13773 /* If the encodings are equal, no conversion needed. */
13774 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778
13779 convert_setup(&vimconv, NULL, NULL);
13780 vim_free(from);
13781 vim_free(to);
13782#endif
13783}
13784
13785/*
13786 * "indent()" function
13787 */
13788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013789f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790{
13791 linenr_T lnum;
13792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798}
13799
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013800/*
13801 * "index()" function
13802 */
13803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013804f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013805{
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 list_T *l;
13807 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013808 long idx = 0;
13809 int ic = FALSE;
13810
13811 rettv->vval.v_number = -1;
13812 if (argvars[0].v_type != VAR_LIST)
13813 {
13814 EMSG(_(e_listreq));
13815 return;
13816 }
13817 l = argvars[0].vval.v_list;
13818 if (l != NULL)
13819 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013820 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013821 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 int error = FALSE;
13824
Bram Moolenaar758711c2005-02-02 23:11:38 +000013825 /* Start at specified item. Use the cached index that list_find()
13826 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013828 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013829 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 ic = get_tv_number_chk(&argvars[3], &error);
13831 if (error)
13832 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013833 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013834
Bram Moolenaar758711c2005-02-02 23:11:38 +000013835 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013836 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013837 {
13838 rettv->vval.v_number = idx;
13839 break;
13840 }
13841 }
13842}
13843
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844static int inputsecret_flag = 0;
13845
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013846static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013847
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013849 * This function is used by f_input() and f_inputdialog() functions. The third
13850 * argument to f_input() specifies the type of completion to use at the
13851 * prompt. The third argument to f_inputdialog() specifies the value to return
13852 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 */
13854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013855get_user_input(
13856 typval_T *argvars,
13857 typval_T *rettv,
13858 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013860 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 char_u *p = NULL;
13862 int c;
13863 char_u buf[NUMBUFLEN];
13864 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013866 int xp_type = EXPAND_NOTHING;
13867 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013869 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013870 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871
13872#ifdef NO_CONSOLE_INPUT
13873 /* While starting up, there is no place to enter text. */
13874 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876#endif
13877
13878 cmd_silent = FALSE; /* Want to see the prompt. */
13879 if (prompt != NULL)
13880 {
13881 /* Only the part of the message after the last NL is considered as
13882 * prompt for the command line */
13883 p = vim_strrchr(prompt, '\n');
13884 if (p == NULL)
13885 p = prompt;
13886 else
13887 {
13888 ++p;
13889 c = *p;
13890 *p = NUL;
13891 msg_start();
13892 msg_clr_eos();
13893 msg_puts_attr(prompt, echo_attr);
13894 msg_didout = FALSE;
13895 msg_starthere();
13896 *p = c;
13897 }
13898 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 if (argvars[1].v_type != VAR_UNKNOWN)
13901 {
13902 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13903 if (defstr != NULL)
13904 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013906 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013907 {
13908 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013909 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013910 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013911
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013912 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013913 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013914
Bram Moolenaar4463f292005-09-25 22:20:24 +000013915 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13916 if (xp_name == NULL)
13917 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013918
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013919 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013920
Bram Moolenaar4463f292005-09-25 22:20:24 +000013921 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13922 &xp_arg) == FAIL)
13923 return;
13924 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013925 }
13926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013928 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013929 int save_ex_normal_busy = ex_normal_busy;
13930 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013931 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013932 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13933 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013934 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013935 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013936 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013937 && argvars[1].v_type != VAR_UNKNOWN
13938 && argvars[2].v_type != VAR_UNKNOWN)
13939 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13940 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013941
13942 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 /* since the user typed this, no need to wait for return */
13945 need_wait_return = FALSE;
13946 msg_didout = FALSE;
13947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 cmd_silent = cmd_silent_save;
13949}
13950
13951/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013952 * "input()" function
13953 * Also handles inputsecret() when inputsecret is set.
13954 */
13955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013956f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013957{
13958 get_user_input(argvars, rettv, FALSE);
13959}
13960
13961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 * "inputdialog()" function
13963 */
13964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013965f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966{
13967#if defined(FEAT_GUI_TEXTDIALOG)
13968 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13969 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13970 {
13971 char_u *message;
13972 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 message = get_tv_string_chk(&argvars[0]);
13976 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013977 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013978 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 else
13980 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 if (message != NULL && defstr != NULL
13982 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013983 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013984 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 else
13986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 if (message != NULL && defstr != NULL
13988 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013989 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013990 rettv->vval.v_string = vim_strsave(
13991 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 }
13997 else
13998#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013999 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000}
14001
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014002/*
14003 * "inputlist()" function
14004 */
14005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014006f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014007{
14008 listitem_T *li;
14009 int selected;
14010 int mouse_used;
14011
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014012#ifdef NO_CONSOLE_INPUT
14013 /* While starting up, there is no place to enter text. */
14014 if (no_console_input())
14015 return;
14016#endif
14017 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14018 {
14019 EMSG2(_(e_listarg), "inputlist()");
14020 return;
14021 }
14022
14023 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014024 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014025 lines_left = Rows; /* avoid more prompt */
14026 msg_scroll = TRUE;
14027 msg_clr_eos();
14028
14029 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14030 {
14031 msg_puts(get_tv_string(&li->li_tv));
14032 msg_putchar('\n');
14033 }
14034
14035 /* Ask for choice. */
14036 selected = prompt_for_number(&mouse_used);
14037 if (mouse_used)
14038 selected -= lines_left;
14039
14040 rettv->vval.v_number = selected;
14041}
14042
14043
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14045
14046/*
14047 * "inputrestore()" function
14048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014050f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051{
14052 if (ga_userinput.ga_len > 0)
14053 {
14054 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14056 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014057 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 }
14059 else if (p_verbose > 1)
14060 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014061 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014062 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 }
14064}
14065
14066/*
14067 * "inputsave()" function
14068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014070f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014072 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 if (ga_grow(&ga_userinput, 1) == OK)
14074 {
14075 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14076 + ga_userinput.ga_len);
14077 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014078 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 }
14080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014081 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082}
14083
14084/*
14085 * "inputsecret()" function
14086 */
14087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014088f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089{
14090 ++cmdline_star;
14091 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 --cmdline_star;
14094 --inputsecret_flag;
14095}
14096
14097/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014098 * "insert()" function
14099 */
14100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014101f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014102{
14103 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014104 listitem_T *item;
14105 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014107
14108 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014109 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014110 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014111 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014112 {
14113 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014114 before = get_tv_number_chk(&argvars[2], &error);
14115 if (error)
14116 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014117
Bram Moolenaar758711c2005-02-02 23:11:38 +000014118 if (before == l->lv_len)
14119 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014120 else
14121 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014122 item = list_find(l, before);
14123 if (item == NULL)
14124 {
14125 EMSGN(_(e_listidx), before);
14126 l = NULL;
14127 }
14128 }
14129 if (l != NULL)
14130 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014131 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014132 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014133 }
14134 }
14135}
14136
14137/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014138 * "invert(expr)" function
14139 */
14140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014141f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014142{
14143 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14144}
14145
14146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 * "isdirectory()" function
14148 */
14149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014150f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153}
14154
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014155/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014156 * "islocked()" function
14157 */
14158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014159f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014160{
14161 lval_T lv;
14162 char_u *end;
14163 dictitem_T *di;
14164
14165 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014166 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14167 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014168 if (end != NULL && lv.ll_name != NULL)
14169 {
14170 if (*end != NUL)
14171 EMSG(_(e_trailing));
14172 else
14173 {
14174 if (lv.ll_tv == NULL)
14175 {
14176 if (check_changedtick(lv.ll_name))
14177 rettv->vval.v_number = 1; /* always locked */
14178 else
14179 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014180 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014181 if (di != NULL)
14182 {
14183 /* Consider a variable locked when:
14184 * 1. the variable itself is locked
14185 * 2. the value of the variable is locked.
14186 * 3. the List or Dict value is locked.
14187 */
14188 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14189 || tv_islocked(&di->di_tv));
14190 }
14191 }
14192 }
14193 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014194 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014195 else if (lv.ll_newkey != NULL)
14196 EMSG2(_(e_dictkey), lv.ll_newkey);
14197 else if (lv.ll_list != NULL)
14198 /* List item. */
14199 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14200 else
14201 /* Dictionary item. */
14202 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14203 }
14204 }
14205
14206 clear_lval(&lv);
14207}
14208
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014209static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014210
14211/*
14212 * Turn a dict into a list:
14213 * "what" == 0: list of keys
14214 * "what" == 1: list of values
14215 * "what" == 2: list of items
14216 */
14217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014218dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014219{
Bram Moolenaar33570922005-01-25 22:26:29 +000014220 list_T *l2;
14221 dictitem_T *di;
14222 hashitem_T *hi;
14223 listitem_T *li;
14224 listitem_T *li2;
14225 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014226 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014227
Bram Moolenaar8c711452005-01-14 21:53:12 +000014228 if (argvars[0].v_type != VAR_DICT)
14229 {
14230 EMSG(_(e_dictreq));
14231 return;
14232 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014233 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014234 return;
14235
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014236 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014237 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014238
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014239 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014241 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014242 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014243 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014244 --todo;
14245 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014246
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014247 li = listitem_alloc();
14248 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014249 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014250 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014251
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014252 if (what == 0)
14253 {
14254 /* keys() */
14255 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014256 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014257 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14258 }
14259 else if (what == 1)
14260 {
14261 /* values() */
14262 copy_tv(&di->di_tv, &li->li_tv);
14263 }
14264 else
14265 {
14266 /* items() */
14267 l2 = list_alloc();
14268 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014269 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014270 li->li_tv.vval.v_list = l2;
14271 if (l2 == NULL)
14272 break;
14273 ++l2->lv_refcount;
14274
14275 li2 = listitem_alloc();
14276 if (li2 == NULL)
14277 break;
14278 list_append(l2, li2);
14279 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014280 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014281 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14282
14283 li2 = listitem_alloc();
14284 if (li2 == NULL)
14285 break;
14286 list_append(l2, li2);
14287 copy_tv(&di->di_tv, &li2->li_tv);
14288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014289 }
14290 }
14291}
14292
14293/*
14294 * "items(dict)" function
14295 */
14296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014297f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014298{
14299 dict_list(argvars, rettv, 2);
14300}
14301
Bram Moolenaar835dc632016-02-07 14:27:38 +010014302#ifdef FEAT_JOB
14303/*
14304 * "job_start()" function
14305 */
14306 static void
14307f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14308{
14309 job_T *job;
14310 char_u *cmd = NULL;
14311#if defined(UNIX)
14312# define USE_ARGV
14313 char **argv = NULL;
14314 int argc = 0;
14315#else
14316 garray_T ga;
14317#endif
14318
14319 rettv->v_type = VAR_JOB;
14320 job = job_alloc();
14321 rettv->vval.v_job = job;
14322 if (job == NULL)
14323 return;
14324
14325 rettv->vval.v_job->jv_status = JOB_FAILED;
14326#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014327 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014328#endif
14329
14330 if (argvars[0].v_type == VAR_STRING)
14331 {
14332 /* Command is a string. */
14333 cmd = argvars[0].vval.v_string;
14334#ifdef USE_ARGV
14335 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14336 return;
14337 argv[argc] = NULL;
14338#endif
14339 }
14340 else if (argvars[0].v_type != VAR_LIST
14341 || argvars[0].vval.v_list == NULL
14342 || argvars[0].vval.v_list->lv_len < 1)
14343 {
14344 EMSG(_(e_invarg));
14345 return;
14346 }
14347 else
14348 {
14349 list_T *l = argvars[0].vval.v_list;
14350 listitem_T *li;
14351 char_u *s;
14352
14353#ifdef USE_ARGV
14354 /* Pass argv[] to mch_call_shell(). */
14355 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14356 if (argv == NULL)
14357 return;
14358#endif
14359 for (li = l->lv_first; li != NULL; li = li->li_next)
14360 {
14361 s = get_tv_string_chk(&li->li_tv);
14362 if (s == NULL)
14363 goto theend;
14364#ifdef USE_ARGV
14365 argv[argc++] = (char *)s;
14366#else
14367 if (li != l->lv_first)
14368 {
14369 s = vim_strsave_shellescape(s, FALSE, TRUE);
14370 if (s == NULL)
14371 goto theend;
14372 }
14373 ga_concat(&ga, s);
14374 vim_free(s);
14375 if (li->li_next != NULL)
14376 ga_append(&ga, ' ');
14377#endif
14378 }
14379#ifdef USE_ARGV
14380 argv[argc] = NULL;
14381#else
14382 cmd = ga.ga_data;
14383#endif
14384 }
14385#ifdef USE_ARGV
14386 mch_start_job(argv, job);
14387#else
14388 mch_start_job(cmd, job);
14389#endif
14390
14391theend:
14392#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014393 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014394#else
14395 vim_free(ga.ga_data);
14396#endif
14397}
14398
14399/*
14400 * "job_status()" function
14401 */
14402 static void
14403f_job_status(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14404{
14405 char *result;
14406
14407 if (argvars[0].v_type != VAR_JOB)
14408 EMSG(_(e_invarg));
14409 else
14410 {
14411 job_T *job = argvars[0].vval.v_job;
14412
14413 if (job->jv_status == JOB_ENDED)
14414 /* No need to check, dead is dead. */
14415 result = "dead";
14416 else if (job->jv_status == JOB_FAILED)
14417 result = "fail";
14418 else
14419 result = mch_job_status(job);
14420 rettv->v_type = VAR_STRING;
14421 rettv->vval.v_string = vim_strsave((char_u *)result);
14422 }
14423}
14424
14425/*
14426 * "job_stop()" function
14427 */
14428 static void
14429f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14430{
14431 if (argvars[0].v_type != VAR_JOB)
14432 EMSG(_(e_invarg));
14433 else
14434 {
14435 char_u *arg;
14436
14437 if (argvars[1].v_type == VAR_UNKNOWN)
14438 arg = (char_u *)"";
14439 else
14440 {
14441 arg = get_tv_string_chk(&argvars[1]);
14442 if (arg == NULL)
14443 {
14444 EMSG(_(e_invarg));
14445 return;
14446 }
14447 }
14448 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14449 rettv->vval.v_number = 0;
14450 else
14451 rettv->vval.v_number = 1;
14452 }
14453}
14454#endif
14455
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014457 * "join()" function
14458 */
14459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014460f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014461{
14462 garray_T ga;
14463 char_u *sep;
14464
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014465 if (argvars[0].v_type != VAR_LIST)
14466 {
14467 EMSG(_(e_listreq));
14468 return;
14469 }
14470 if (argvars[0].vval.v_list == NULL)
14471 return;
14472 if (argvars[1].v_type == VAR_UNKNOWN)
14473 sep = (char_u *)" ";
14474 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014475 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014476
14477 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014478
14479 if (sep != NULL)
14480 {
14481 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014482 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 ga_append(&ga, NUL);
14484 rettv->vval.v_string = (char_u *)ga.ga_data;
14485 }
14486 else
14487 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014488}
14489
14490/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014491 * "jsdecode()" function
14492 */
14493 static void
14494f_jsdecode(typval_T *argvars, typval_T *rettv)
14495{
14496 js_read_T reader;
14497
14498 reader.js_buf = get_tv_string(&argvars[0]);
14499 reader.js_fill = NULL;
14500 reader.js_used = 0;
14501 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14502 EMSG(_(e_invarg));
14503}
14504
14505/*
14506 * "jsencode()" function
14507 */
14508 static void
14509f_jsencode(typval_T *argvars, typval_T *rettv)
14510{
14511 rettv->v_type = VAR_STRING;
14512 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14513}
14514
14515/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014516 * "jsondecode()" function
14517 */
14518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014519f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014520{
14521 js_read_T reader;
14522
14523 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014524 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014525 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014526 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014527 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014528}
14529
14530/*
14531 * "jsonencode()" function
14532 */
14533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014534f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014535{
14536 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014537 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014538}
14539
14540/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014541 * "keys()" function
14542 */
14543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014544f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014545{
14546 dict_list(argvars, rettv, 0);
14547}
14548
14549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 * "last_buffer_nr()" function.
14551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014553f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554{
14555 int n = 0;
14556 buf_T *buf;
14557
14558 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14559 if (n < buf->b_fnum)
14560 n = buf->b_fnum;
14561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014562 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014563}
14564
14565/*
14566 * "len()" function
14567 */
14568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014569f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014570{
14571 switch (argvars[0].v_type)
14572 {
14573 case VAR_STRING:
14574 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014575 rettv->vval.v_number = (varnumber_T)STRLEN(
14576 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014577 break;
14578 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014579 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014580 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014581 case VAR_DICT:
14582 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14583 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014584 case VAR_UNKNOWN:
14585 case VAR_SPECIAL:
14586 case VAR_FLOAT:
14587 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014588 case VAR_JOB:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014589 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014590 break;
14591 }
14592}
14593
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014594static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014595
14596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014597libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014598{
14599#ifdef FEAT_LIBCALL
14600 char_u *string_in;
14601 char_u **string_result;
14602 int nr_result;
14603#endif
14604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014606 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014608
14609 if (check_restricted() || check_secure())
14610 return;
14611
14612#ifdef FEAT_LIBCALL
14613 /* The first two args must be strings, otherwise its meaningless */
14614 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14615 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014616 string_in = NULL;
14617 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014618 string_in = argvars[2].vval.v_string;
14619 if (type == VAR_NUMBER)
14620 string_result = NULL;
14621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014622 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014623 if (mch_libcall(argvars[0].vval.v_string,
14624 argvars[1].vval.v_string,
14625 string_in,
14626 argvars[2].vval.v_number,
14627 string_result,
14628 &nr_result) == OK
14629 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014631 }
14632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633}
14634
14635/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014636 * "libcall()" function
14637 */
14638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014639f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640{
14641 libcall_common(argvars, rettv, VAR_STRING);
14642}
14643
14644/*
14645 * "libcallnr()" function
14646 */
14647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014648f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649{
14650 libcall_common(argvars, rettv, VAR_NUMBER);
14651}
14652
14653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 * "line(string)" function
14655 */
14656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014657f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658{
14659 linenr_T lnum = 0;
14660 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014661 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014663 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 if (fp != NULL)
14665 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014666 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667}
14668
14669/*
14670 * "line2byte(lnum)" function
14671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014673f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674{
14675#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677#else
14678 linenr_T lnum;
14679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14685 if (rettv->vval.v_number >= 0)
14686 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687#endif
14688}
14689
14690/*
14691 * "lispindent(lnum)" function
14692 */
14693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014694f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695{
14696#ifdef FEAT_LISP
14697 pos_T pos;
14698 linenr_T lnum;
14699
14700 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014701 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14703 {
14704 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014705 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 curwin->w_cursor = pos;
14707 }
14708 else
14709#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711}
14712
14713/*
14714 * "localtime()" function
14715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014717f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720}
14721
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014722static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723
14724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014725get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726{
14727 char_u *keys;
14728 char_u *which;
14729 char_u buf[NUMBUFLEN];
14730 char_u *keys_buf = NULL;
14731 char_u *rhs;
14732 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014733 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014734 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014735 mapblock_T *mp;
14736 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737
14738 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014739 rettv->v_type = VAR_STRING;
14740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 if (*keys == NUL)
14744 return;
14745
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014746 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014748 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014750 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014751 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014752 if (argvars[3].v_type != VAR_UNKNOWN)
14753 get_dict = get_tv_number(&argvars[3]);
14754 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 else
14757 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 if (which == NULL)
14759 return;
14760
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 mode = get_map_mode(&which, 0);
14762
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014763 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014764 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014766
14767 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014769 /* Return a string. */
14770 if (rhs != NULL)
14771 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772
Bram Moolenaarbd743252010-10-20 21:23:33 +020014773 }
14774 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14775 {
14776 /* Return a dictionary. */
14777 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14778 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14779 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780
Bram Moolenaarbd743252010-10-20 21:23:33 +020014781 dict_add_nr_str(dict, "lhs", 0L, lhs);
14782 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14783 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14784 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14785 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14786 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14787 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014788 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014789 dict_add_nr_str(dict, "mode", 0L, mapmode);
14790
14791 vim_free(lhs);
14792 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 }
14794}
14795
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014796#ifdef FEAT_FLOAT
14797/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014798 * "log()" function
14799 */
14800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014801f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014802{
14803 float_T f;
14804
14805 rettv->v_type = VAR_FLOAT;
14806 if (get_float_arg(argvars, &f) == OK)
14807 rettv->vval.v_float = log(f);
14808 else
14809 rettv->vval.v_float = 0.0;
14810}
14811
14812/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014813 * "log10()" function
14814 */
14815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014816f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014817{
14818 float_T f;
14819
14820 rettv->v_type = VAR_FLOAT;
14821 if (get_float_arg(argvars, &f) == OK)
14822 rettv->vval.v_float = log10(f);
14823 else
14824 rettv->vval.v_float = 0.0;
14825}
14826#endif
14827
Bram Moolenaar1dced572012-04-05 16:54:08 +020014828#ifdef FEAT_LUA
14829/*
14830 * "luaeval()" function
14831 */
14832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014833f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014834{
14835 char_u *str;
14836 char_u buf[NUMBUFLEN];
14837
14838 str = get_tv_string_buf(&argvars[0], buf);
14839 do_luaeval(str, argvars + 1, rettv);
14840}
14841#endif
14842
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014844 * "map()" function
14845 */
14846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014847f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014848{
14849 filter_map(argvars, rettv, TRUE);
14850}
14851
14852/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014853 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 */
14855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014856f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014858 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859}
14860
14861/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014862 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 */
14864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014865f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014867 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868}
14869
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014870static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871
14872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014873find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014875 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014876 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014877 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 char_u *pat;
14879 regmatch_T regmatch;
14880 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014881 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 char_u *save_cpo;
14883 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014884 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014885 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014886 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014887 list_T *l = NULL;
14888 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014889 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014890 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891
14892 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14893 save_cpo = p_cpo;
14894 p_cpo = (char_u *)"";
14895
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014896 rettv->vval.v_number = -1;
14897 if (type == 3)
14898 {
14899 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014900 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014901 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014902 }
14903 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014905 rettv->v_type = VAR_STRING;
14906 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014909 if (argvars[0].v_type == VAR_LIST)
14910 {
14911 if ((l = argvars[0].vval.v_list) == NULL)
14912 goto theend;
14913 li = l->lv_first;
14914 }
14915 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014916 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014917 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014918 len = (long)STRLEN(str);
14919 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014921 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14922 if (pat == NULL)
14923 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014924
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014925 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014927 int error = FALSE;
14928
14929 start = get_tv_number_chk(&argvars[2], &error);
14930 if (error)
14931 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014932 if (l != NULL)
14933 {
14934 li = list_find(l, start);
14935 if (li == NULL)
14936 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014937 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014938 }
14939 else
14940 {
14941 if (start < 0)
14942 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014943 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014944 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014945 /* When "count" argument is there ignore matches before "start",
14946 * otherwise skip part of the string. Differs when pattern is "^"
14947 * or "\<". */
14948 if (argvars[3].v_type != VAR_UNKNOWN)
14949 startcol = start;
14950 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014951 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014952 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014953 len -= start;
14954 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014955 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014957 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014958 nth = get_tv_number_chk(&argvars[3], &error);
14959 if (error)
14960 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 }
14962
14963 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14964 if (regmatch.regprog != NULL)
14965 {
14966 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014967
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014968 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014969 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014970 if (l != NULL)
14971 {
14972 if (li == NULL)
14973 {
14974 match = FALSE;
14975 break;
14976 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014977 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014978 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014979 if (str == NULL)
14980 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014981 }
14982
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014983 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014984
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014985 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014986 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014987 if (l == NULL && !match)
14988 break;
14989
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014990 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014991 if (l != NULL)
14992 {
14993 li = li->li_next;
14994 ++idx;
14995 }
14996 else
14997 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014998#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014999 startcol = (colnr_T)(regmatch.startp[0]
15000 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015001#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015002 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015003#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015004 if (startcol > (colnr_T)len
15005 || str + startcol <= regmatch.startp[0])
15006 {
15007 match = FALSE;
15008 break;
15009 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015010 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015011 }
15012
15013 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015015 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015016 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015017 int i;
15018
15019 /* return list with matched string and submatches */
15020 for (i = 0; i < NSUBEXP; ++i)
15021 {
15022 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015023 {
15024 if (list_append_string(rettv->vval.v_list,
15025 (char_u *)"", 0) == FAIL)
15026 break;
15027 }
15028 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015029 regmatch.startp[i],
15030 (int)(regmatch.endp[i] - regmatch.startp[i]))
15031 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015032 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015033 }
15034 }
15035 else if (type == 2)
15036 {
15037 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015038 if (l != NULL)
15039 copy_tv(&li->li_tv, rettv);
15040 else
15041 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015043 }
15044 else if (l != NULL)
15045 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 else
15047 {
15048 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 (varnumber_T)(regmatch.startp[0] - str);
15051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015054 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 }
15056 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015057 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 }
15059
15060theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015061 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062 p_cpo = save_cpo;
15063}
15064
15065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066 * "match()" function
15067 */
15068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015069f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015070{
15071 find_some_match(argvars, rettv, 1);
15072}
15073
15074/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015075 * "matchadd()" function
15076 */
15077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015078f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015079{
15080#ifdef FEAT_SEARCH_EXTRA
15081 char_u buf[NUMBUFLEN];
15082 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15083 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15084 int prio = 10; /* default priority */
15085 int id = -1;
15086 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015087 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015088
15089 rettv->vval.v_number = -1;
15090
15091 if (grp == NULL || pat == NULL)
15092 return;
15093 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015094 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015095 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015096 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015097 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015098 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015099 if (argvars[4].v_type != VAR_UNKNOWN)
15100 {
15101 if (argvars[4].v_type != VAR_DICT)
15102 {
15103 EMSG(_(e_dictreq));
15104 return;
15105 }
15106 if (dict_find(argvars[4].vval.v_dict,
15107 (char_u *)"conceal", -1) != NULL)
15108 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15109 (char_u *)"conceal", FALSE);
15110 }
15111 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015112 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015113 if (error == TRUE)
15114 return;
15115 if (id >= 1 && id <= 3)
15116 {
15117 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15118 return;
15119 }
15120
Bram Moolenaar6561d522015-07-21 15:48:27 +020015121 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15122 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015123#endif
15124}
15125
15126/*
15127 * "matchaddpos()" function
15128 */
15129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015130f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015131{
15132#ifdef FEAT_SEARCH_EXTRA
15133 char_u buf[NUMBUFLEN];
15134 char_u *group;
15135 int prio = 10;
15136 int id = -1;
15137 int error = FALSE;
15138 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015139 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015140
15141 rettv->vval.v_number = -1;
15142
15143 group = get_tv_string_buf_chk(&argvars[0], buf);
15144 if (group == NULL)
15145 return;
15146
15147 if (argvars[1].v_type != VAR_LIST)
15148 {
15149 EMSG2(_(e_listarg), "matchaddpos()");
15150 return;
15151 }
15152 l = argvars[1].vval.v_list;
15153 if (l == NULL)
15154 return;
15155
15156 if (argvars[2].v_type != VAR_UNKNOWN)
15157 {
15158 prio = get_tv_number_chk(&argvars[2], &error);
15159 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015160 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015161 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015162 if (argvars[4].v_type != VAR_UNKNOWN)
15163 {
15164 if (argvars[4].v_type != VAR_DICT)
15165 {
15166 EMSG(_(e_dictreq));
15167 return;
15168 }
15169 if (dict_find(argvars[4].vval.v_dict,
15170 (char_u *)"conceal", -1) != NULL)
15171 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15172 (char_u *)"conceal", FALSE);
15173 }
15174 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015175 }
15176 if (error == TRUE)
15177 return;
15178
15179 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15180 if (id == 1 || id == 2)
15181 {
15182 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15183 return;
15184 }
15185
Bram Moolenaar6561d522015-07-21 15:48:27 +020015186 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15187 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015188#endif
15189}
15190
15191/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015192 * "matcharg()" function
15193 */
15194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015195f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015196{
15197 if (rettv_list_alloc(rettv) == OK)
15198 {
15199#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015200 int id = get_tv_number(&argvars[0]);
15201 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015202
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015203 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015204 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015205 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15206 {
15207 list_append_string(rettv->vval.v_list,
15208 syn_id2name(m->hlg_id), -1);
15209 list_append_string(rettv->vval.v_list, m->pattern, -1);
15210 }
15211 else
15212 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015213 list_append_string(rettv->vval.v_list, NULL, -1);
15214 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015215 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015216 }
15217#endif
15218 }
15219}
15220
15221/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015222 * "matchdelete()" function
15223 */
15224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015225f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015226{
15227#ifdef FEAT_SEARCH_EXTRA
15228 rettv->vval.v_number = match_delete(curwin,
15229 (int)get_tv_number(&argvars[0]), TRUE);
15230#endif
15231}
15232
15233/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015234 * "matchend()" function
15235 */
15236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015237f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015238{
15239 find_some_match(argvars, rettv, 0);
15240}
15241
15242/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015243 * "matchlist()" function
15244 */
15245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015246f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015247{
15248 find_some_match(argvars, rettv, 3);
15249}
15250
15251/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252 * "matchstr()" function
15253 */
15254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015255f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256{
15257 find_some_match(argvars, rettv, 2);
15258}
15259
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015260static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015261
15262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015263max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015264{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015265 long n = 0;
15266 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015268
15269 if (argvars[0].v_type == VAR_LIST)
15270 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015271 list_T *l;
15272 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015273
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015274 l = argvars[0].vval.v_list;
15275 if (l != NULL)
15276 {
15277 li = l->lv_first;
15278 if (li != NULL)
15279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015281 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015282 {
15283 li = li->li_next;
15284 if (li == NULL)
15285 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015287 if (domax ? i > n : i < n)
15288 n = i;
15289 }
15290 }
15291 }
15292 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015293 else if (argvars[0].v_type == VAR_DICT)
15294 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015295 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015296 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015297 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015298 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015299
15300 d = argvars[0].vval.v_dict;
15301 if (d != NULL)
15302 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015303 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015304 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015306 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015308 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015310 if (first)
15311 {
15312 n = i;
15313 first = FALSE;
15314 }
15315 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015316 n = i;
15317 }
15318 }
15319 }
15320 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015321 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015322 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015323 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015324}
15325
15326/*
15327 * "max()" function
15328 */
15329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015330f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015331{
15332 max_min(argvars, rettv, TRUE);
15333}
15334
15335/*
15336 * "min()" function
15337 */
15338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015339f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015340{
15341 max_min(argvars, rettv, FALSE);
15342}
15343
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015344static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015345
15346/*
15347 * Create the directory in which "dir" is located, and higher levels when
15348 * needed.
15349 */
15350 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015351mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015352{
15353 char_u *p;
15354 char_u *updir;
15355 int r = FAIL;
15356
15357 /* Get end of directory name in "dir".
15358 * We're done when it's "/" or "c:/". */
15359 p = gettail_sep(dir);
15360 if (p <= get_past_head(dir))
15361 return OK;
15362
15363 /* If the directory exists we're done. Otherwise: create it.*/
15364 updir = vim_strnsave(dir, (int)(p - dir));
15365 if (updir == NULL)
15366 return FAIL;
15367 if (mch_isdir(updir))
15368 r = OK;
15369 else if (mkdir_recurse(updir, prot) == OK)
15370 r = vim_mkdir_emsg(updir, prot);
15371 vim_free(updir);
15372 return r;
15373}
15374
15375#ifdef vim_mkdir
15376/*
15377 * "mkdir()" function
15378 */
15379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015380f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015381{
15382 char_u *dir;
15383 char_u buf[NUMBUFLEN];
15384 int prot = 0755;
15385
15386 rettv->vval.v_number = FAIL;
15387 if (check_restricted() || check_secure())
15388 return;
15389
15390 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015391 if (*dir == NUL)
15392 rettv->vval.v_number = FAIL;
15393 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015394 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015395 if (*gettail(dir) == NUL)
15396 /* remove trailing slashes */
15397 *gettail_sep(dir) = NUL;
15398
15399 if (argvars[1].v_type != VAR_UNKNOWN)
15400 {
15401 if (argvars[2].v_type != VAR_UNKNOWN)
15402 prot = get_tv_number_chk(&argvars[2], NULL);
15403 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15404 mkdir_recurse(dir, prot);
15405 }
15406 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015407 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015408}
15409#endif
15410
Bram Moolenaar0d660222005-01-07 21:51:51 +000015411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 * "mode()" function
15413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015415f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015417 char_u buf[3];
15418
15419 buf[1] = NUL;
15420 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 if (VIsual_active)
15423 {
15424 if (VIsual_select)
15425 buf[0] = VIsual_mode + 's' - 'v';
15426 else
15427 buf[0] = VIsual_mode;
15428 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015429 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015430 || State == CONFIRM)
15431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015433 if (State == ASKMORE)
15434 buf[1] = 'm';
15435 else if (State == CONFIRM)
15436 buf[1] = '?';
15437 }
15438 else if (State == EXTERNCMD)
15439 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 else if (State & INSERT)
15441 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015442#ifdef FEAT_VREPLACE
15443 if (State & VREPLACE_FLAG)
15444 {
15445 buf[0] = 'R';
15446 buf[1] = 'v';
15447 }
15448 else
15449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 if (State & REPLACE_FLAG)
15451 buf[0] = 'R';
15452 else
15453 buf[0] = 'i';
15454 }
15455 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015458 if (exmode_active)
15459 buf[1] = 'v';
15460 }
15461 else if (exmode_active)
15462 {
15463 buf[0] = 'c';
15464 buf[1] = 'e';
15465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015469 if (finish_op)
15470 buf[1] = 'o';
15471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015473 /* Clear out the minor mode when the argument is not a non-zero number or
15474 * non-empty string. */
15475 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015476 buf[1] = NUL;
15477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 rettv->vval.v_string = vim_strsave(buf);
15479 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480}
15481
Bram Moolenaar429fa852013-04-15 12:27:36 +020015482#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015483/*
15484 * "mzeval()" function
15485 */
15486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015487f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015488{
15489 char_u *str;
15490 char_u buf[NUMBUFLEN];
15491
15492 str = get_tv_string_buf(&argvars[0], buf);
15493 do_mzeval(str, rettv);
15494}
Bram Moolenaar75676462013-01-30 14:55:42 +010015495
15496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015498{
15499 typval_T argvars[3];
15500
15501 argvars[0].v_type = VAR_STRING;
15502 argvars[0].vval.v_string = name;
15503 copy_tv(args, &argvars[1]);
15504 argvars[2].v_type = VAR_UNKNOWN;
15505 f_call(argvars, rettv);
15506 clear_tv(&argvars[1]);
15507}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015508#endif
15509
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511 * "nextnonblank()" function
15512 */
15513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015514f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515{
15516 linenr_T lnum;
15517
15518 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015520 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015521 {
15522 lnum = 0;
15523 break;
15524 }
15525 if (*skipwhite(ml_get(lnum)) != NUL)
15526 break;
15527 }
15528 rettv->vval.v_number = lnum;
15529}
15530
15531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 * "nr2char()" function
15533 */
15534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015535f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536{
15537 char_u buf[NUMBUFLEN];
15538
15539#ifdef FEAT_MBYTE
15540 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015541 {
15542 int utf8 = 0;
15543
15544 if (argvars[1].v_type != VAR_UNKNOWN)
15545 utf8 = get_tv_number_chk(&argvars[1], NULL);
15546 if (utf8)
15547 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15548 else
15549 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 else
15552#endif
15553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 buf[1] = NUL;
15556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 rettv->v_type = VAR_STRING;
15558 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015559}
15560
15561/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015562 * "or(expr, expr)" function
15563 */
15564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015565f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015566{
15567 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15568 | get_tv_number_chk(&argvars[1], NULL);
15569}
15570
15571/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015572 * "pathshorten()" function
15573 */
15574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015575f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015576{
15577 char_u *p;
15578
15579 rettv->v_type = VAR_STRING;
15580 p = get_tv_string_chk(&argvars[0]);
15581 if (p == NULL)
15582 rettv->vval.v_string = NULL;
15583 else
15584 {
15585 p = vim_strsave(p);
15586 rettv->vval.v_string = p;
15587 if (p != NULL)
15588 shorten_dir(p);
15589 }
15590}
15591
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015592#ifdef FEAT_PERL
15593/*
15594 * "perleval()" function
15595 */
15596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015597f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015598{
15599 char_u *str;
15600 char_u buf[NUMBUFLEN];
15601
15602 str = get_tv_string_buf(&argvars[0], buf);
15603 do_perleval(str, rettv);
15604}
15605#endif
15606
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015607#ifdef FEAT_FLOAT
15608/*
15609 * "pow()" function
15610 */
15611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015612f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015613{
15614 float_T fx, fy;
15615
15616 rettv->v_type = VAR_FLOAT;
15617 if (get_float_arg(argvars, &fx) == OK
15618 && get_float_arg(&argvars[1], &fy) == OK)
15619 rettv->vval.v_float = pow(fx, fy);
15620 else
15621 rettv->vval.v_float = 0.0;
15622}
15623#endif
15624
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015625/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015626 * "prevnonblank()" function
15627 */
15628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015629f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015630{
15631 linenr_T lnum;
15632
15633 lnum = get_tv_lnum(argvars);
15634 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15635 lnum = 0;
15636 else
15637 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15638 --lnum;
15639 rettv->vval.v_number = lnum;
15640}
15641
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015642/* This dummy va_list is here because:
15643 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15644 * - locally in the function results in a "used before set" warning
15645 * - using va_start() to initialize it gives "function with fixed args" error */
15646static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015647
Bram Moolenaar8c711452005-01-14 21:53:12 +000015648/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015649 * "printf()" function
15650 */
15651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015652f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015653{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015654 char_u buf[NUMBUFLEN];
15655 int len;
15656 char_u *s;
15657 int saved_did_emsg = did_emsg;
15658 char *fmt;
15659
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015660 rettv->v_type = VAR_STRING;
15661 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015662
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015663 /* Get the required length, allocate the buffer and do it for real. */
15664 did_emsg = FALSE;
15665 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15666 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15667 if (!did_emsg)
15668 {
15669 s = alloc(len + 1);
15670 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015671 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015672 rettv->vval.v_string = s;
15673 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015674 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015675 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015676 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015677}
15678
15679/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015680 * "pumvisible()" function
15681 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015683f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015684{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015685#ifdef FEAT_INS_EXPAND
15686 if (pum_visible())
15687 rettv->vval.v_number = 1;
15688#endif
15689}
15690
Bram Moolenaardb913952012-06-29 12:54:53 +020015691#ifdef FEAT_PYTHON3
15692/*
15693 * "py3eval()" function
15694 */
15695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015696f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015697{
15698 char_u *str;
15699 char_u buf[NUMBUFLEN];
15700
15701 str = get_tv_string_buf(&argvars[0], buf);
15702 do_py3eval(str, rettv);
15703}
15704#endif
15705
15706#ifdef FEAT_PYTHON
15707/*
15708 * "pyeval()" function
15709 */
15710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015711f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015712{
15713 char_u *str;
15714 char_u buf[NUMBUFLEN];
15715
15716 str = get_tv_string_buf(&argvars[0], buf);
15717 do_pyeval(str, rettv);
15718}
15719#endif
15720
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015721/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015722 * "range()" function
15723 */
15724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015725f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015726{
15727 long start;
15728 long end;
15729 long stride = 1;
15730 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015731 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015733 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015734 if (argvars[1].v_type == VAR_UNKNOWN)
15735 {
15736 end = start - 1;
15737 start = 0;
15738 }
15739 else
15740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015741 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015742 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015743 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015744 }
15745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015746 if (error)
15747 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015748 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015749 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015750 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015751 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015752 else
15753 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015754 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015755 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015756 if (list_append_number(rettv->vval.v_list,
15757 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015758 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015759 }
15760}
15761
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015762/*
15763 * "readfile()" function
15764 */
15765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015766f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015767{
15768 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015769 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015770 char_u *fname;
15771 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015772 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15773 int io_size = sizeof(buf);
15774 int readlen; /* size of last fread() */
15775 char_u *prev = NULL; /* previously read bytes, if any */
15776 long prevlen = 0; /* length of data in prev */
15777 long prevsize = 0; /* size of prev buffer */
15778 long maxline = MAXLNUM;
15779 long cnt = 0;
15780 char_u *p; /* position in buf */
15781 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015782
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015783 if (argvars[1].v_type != VAR_UNKNOWN)
15784 {
15785 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15786 binary = TRUE;
15787 if (argvars[2].v_type != VAR_UNKNOWN)
15788 maxline = get_tv_number(&argvars[2]);
15789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015790
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015791 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015792 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015793
15794 /* Always open the file in binary mode, library functions have a mind of
15795 * their own about CR-LF conversion. */
15796 fname = get_tv_string(&argvars[0]);
15797 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15798 {
15799 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15800 return;
15801 }
15802
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015803 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015804 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015805 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015806
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015807 /* This for loop processes what was read, but is also entered at end
15808 * of file so that either:
15809 * - an incomplete line gets written
15810 * - a "binary" file gets an empty line at the end if it ends in a
15811 * newline. */
15812 for (p = buf, start = buf;
15813 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15814 ++p)
15815 {
15816 if (*p == '\n' || readlen <= 0)
15817 {
15818 listitem_T *li;
15819 char_u *s = NULL;
15820 long_u len = p - start;
15821
15822 /* Finished a line. Remove CRs before NL. */
15823 if (readlen > 0 && !binary)
15824 {
15825 while (len > 0 && start[len - 1] == '\r')
15826 --len;
15827 /* removal may cross back to the "prev" string */
15828 if (len == 0)
15829 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15830 --prevlen;
15831 }
15832 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015833 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015834 else
15835 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015836 /* Change "prev" buffer to be the right size. This way
15837 * the bytes are only copied once, and very long lines are
15838 * allocated only once. */
15839 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015840 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015841 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015842 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015843 prev = NULL; /* the list will own the string */
15844 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015845 }
15846 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015847 if (s == NULL)
15848 {
15849 do_outofmem_msg((long_u) prevlen + len + 1);
15850 failed = TRUE;
15851 break;
15852 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015853
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015854 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015855 {
15856 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015857 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015858 break;
15859 }
15860 li->li_tv.v_type = VAR_STRING;
15861 li->li_tv.v_lock = 0;
15862 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015863 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015864
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015865 start = p + 1; /* step over newline */
15866 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015867 break;
15868 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015869 else if (*p == NUL)
15870 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015871#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015872 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15873 * when finding the BF and check the previous two bytes. */
15874 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015875 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015876 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15877 * + 1, these may be in the "prev" string. */
15878 char_u back1 = p >= buf + 1 ? p[-1]
15879 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15880 char_u back2 = p >= buf + 2 ? p[-2]
15881 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15882 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015883
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015884 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015885 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015886 char_u *dest = p - 2;
15887
15888 /* Usually a BOM is at the beginning of a file, and so at
15889 * the beginning of a line; then we can just step over it.
15890 */
15891 if (start == dest)
15892 start = p + 1;
15893 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015894 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015895 /* have to shuffle buf to close gap */
15896 int adjust_prevlen = 0;
15897
15898 if (dest < buf)
15899 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015900 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015901 dest = buf;
15902 }
15903 if (readlen > p - buf + 1)
15904 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15905 readlen -= 3 - adjust_prevlen;
15906 prevlen -= adjust_prevlen;
15907 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015908 }
15909 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015910 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015911#endif
15912 } /* for */
15913
15914 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15915 break;
15916 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015917 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015918 /* There's part of a line in buf, store it in "prev". */
15919 if (p - start + prevlen >= prevsize)
15920 {
15921 /* need bigger "prev" buffer */
15922 char_u *newprev;
15923
15924 /* A common use case is ordinary text files and "prev" gets a
15925 * fragment of a line, so the first allocation is made
15926 * small, to avoid repeatedly 'allocing' large and
15927 * 'reallocing' small. */
15928 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015929 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015930 else
15931 {
15932 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015933 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015934 prevsize = grow50pc > growmin ? grow50pc : growmin;
15935 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015936 newprev = prev == NULL ? alloc(prevsize)
15937 : vim_realloc(prev, prevsize);
15938 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015939 {
15940 do_outofmem_msg((long_u)prevsize);
15941 failed = TRUE;
15942 break;
15943 }
15944 prev = newprev;
15945 }
15946 /* Add the line part to end of "prev". */
15947 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015948 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015949 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015950 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015951
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015952 /*
15953 * For a negative line count use only the lines at the end of the file,
15954 * free the rest.
15955 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015956 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015957 while (cnt > -maxline)
15958 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015959 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015960 --cnt;
15961 }
15962
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015963 if (failed)
15964 {
15965 list_free(rettv->vval.v_list, TRUE);
15966 /* readfile doc says an empty list is returned on error */
15967 rettv->vval.v_list = list_alloc();
15968 }
15969
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015970 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015971 fclose(fd);
15972}
15973
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015974#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015975static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015976
15977/*
15978 * Convert a List to proftime_T.
15979 * Return FAIL when there is something wrong.
15980 */
15981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015982list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015983{
15984 long n1, n2;
15985 int error = FALSE;
15986
15987 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15988 || arg->vval.v_list->lv_len != 2)
15989 return FAIL;
15990 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15991 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15992# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015993 tm->HighPart = n1;
15994 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015995# else
15996 tm->tv_sec = n1;
15997 tm->tv_usec = n2;
15998# endif
15999 return error ? FAIL : OK;
16000}
16001#endif /* FEAT_RELTIME */
16002
16003/*
16004 * "reltime()" function
16005 */
16006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016007f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016008{
16009#ifdef FEAT_RELTIME
16010 proftime_T res;
16011 proftime_T start;
16012
16013 if (argvars[0].v_type == VAR_UNKNOWN)
16014 {
16015 /* No arguments: get current time. */
16016 profile_start(&res);
16017 }
16018 else if (argvars[1].v_type == VAR_UNKNOWN)
16019 {
16020 if (list2proftime(&argvars[0], &res) == FAIL)
16021 return;
16022 profile_end(&res);
16023 }
16024 else
16025 {
16026 /* Two arguments: compute the difference. */
16027 if (list2proftime(&argvars[0], &start) == FAIL
16028 || list2proftime(&argvars[1], &res) == FAIL)
16029 return;
16030 profile_sub(&res, &start);
16031 }
16032
16033 if (rettv_list_alloc(rettv) == OK)
16034 {
16035 long n1, n2;
16036
16037# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016038 n1 = res.HighPart;
16039 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016040# else
16041 n1 = res.tv_sec;
16042 n2 = res.tv_usec;
16043# endif
16044 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16045 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16046 }
16047#endif
16048}
16049
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016050#ifdef FEAT_FLOAT
16051/*
16052 * "reltimefloat()" function
16053 */
16054 static void
16055f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16056{
16057# ifdef FEAT_RELTIME
16058 proftime_T tm;
16059# endif
16060
16061 rettv->v_type = VAR_FLOAT;
16062 rettv->vval.v_float = 0;
16063# ifdef FEAT_RELTIME
16064 if (list2proftime(&argvars[0], &tm) == OK)
16065 rettv->vval.v_float = profile_float(&tm);
16066# endif
16067}
16068#endif
16069
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016070/*
16071 * "reltimestr()" function
16072 */
16073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016074f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016075{
16076#ifdef FEAT_RELTIME
16077 proftime_T tm;
16078#endif
16079
16080 rettv->v_type = VAR_STRING;
16081 rettv->vval.v_string = NULL;
16082#ifdef FEAT_RELTIME
16083 if (list2proftime(&argvars[0], &tm) == OK)
16084 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16085#endif
16086}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016087
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016089static void make_connection(void);
16090static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016091
16092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016093make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016094{
16095 if (X_DISPLAY == NULL
16096# ifdef FEAT_GUI
16097 && !gui.in_use
16098# endif
16099 )
16100 {
16101 x_force_connect = TRUE;
16102 setup_term_clip();
16103 x_force_connect = FALSE;
16104 }
16105}
16106
16107 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016108check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109{
16110 make_connection();
16111 if (X_DISPLAY == NULL)
16112 {
16113 EMSG(_("E240: No connection to Vim server"));
16114 return FAIL;
16115 }
16116 return OK;
16117}
16118#endif
16119
16120#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016121static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122
16123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016124remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125{
16126 char_u *server_name;
16127 char_u *keys;
16128 char_u *r = NULL;
16129 char_u buf[NUMBUFLEN];
16130# ifdef WIN32
16131 HWND w;
16132# else
16133 Window w;
16134# endif
16135
16136 if (check_restricted() || check_secure())
16137 return;
16138
16139# ifdef FEAT_X11
16140 if (check_connection() == FAIL)
16141 return;
16142# endif
16143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 server_name = get_tv_string_chk(&argvars[0]);
16145 if (server_name == NULL)
16146 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016147 keys = get_tv_string_buf(&argvars[1], buf);
16148# ifdef WIN32
16149 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16150# else
16151 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16152 < 0)
16153# endif
16154 {
16155 if (r != NULL)
16156 EMSG(r); /* sending worked but evaluation failed */
16157 else
16158 EMSG2(_("E241: Unable to send to %s"), server_name);
16159 return;
16160 }
16161
16162 rettv->vval.v_string = r;
16163
16164 if (argvars[2].v_type != VAR_UNKNOWN)
16165 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016167 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016168 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016170 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 v.di_tv.v_type = VAR_STRING;
16172 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016173 idvar = get_tv_string_chk(&argvars[2]);
16174 if (idvar != NULL)
16175 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 }
16178}
16179#endif
16180
16181/*
16182 * "remote_expr()" function
16183 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016185f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186{
16187 rettv->v_type = VAR_STRING;
16188 rettv->vval.v_string = NULL;
16189#ifdef FEAT_CLIENTSERVER
16190 remote_common(argvars, rettv, TRUE);
16191#endif
16192}
16193
16194/*
16195 * "remote_foreground()" function
16196 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016198f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200#ifdef FEAT_CLIENTSERVER
16201# ifdef WIN32
16202 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016203 {
16204 char_u *server_name = get_tv_string_chk(&argvars[0]);
16205
16206 if (server_name != NULL)
16207 serverForeground(server_name);
16208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016209# else
16210 /* Send a foreground() expression to the server. */
16211 argvars[1].v_type = VAR_STRING;
16212 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16213 argvars[2].v_type = VAR_UNKNOWN;
16214 remote_common(argvars, rettv, TRUE);
16215 vim_free(argvars[1].vval.v_string);
16216# endif
16217#endif
16218}
16219
Bram Moolenaar0d660222005-01-07 21:51:51 +000016220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016221f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222{
16223#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016224 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225 char_u *s = NULL;
16226# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016227 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016228# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016229 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230
16231 if (check_restricted() || check_secure())
16232 {
16233 rettv->vval.v_number = -1;
16234 return;
16235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 serverid = get_tv_string_chk(&argvars[0]);
16237 if (serverid == NULL)
16238 {
16239 rettv->vval.v_number = -1;
16240 return; /* type error; errmsg already given */
16241 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016243 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244 if (n == 0)
16245 rettv->vval.v_number = -1;
16246 else
16247 {
16248 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16249 rettv->vval.v_number = (s != NULL);
16250 }
16251# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 if (check_connection() == FAIL)
16253 return;
16254
16255 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016256 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257# endif
16258
16259 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 char_u *retvar;
16262
Bram Moolenaar33570922005-01-25 22:26:29 +000016263 v.di_tv.v_type = VAR_STRING;
16264 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016265 retvar = get_tv_string_chk(&argvars[1]);
16266 if (retvar != NULL)
16267 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016268 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016269 }
16270#else
16271 rettv->vval.v_number = -1;
16272#endif
16273}
16274
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016276f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277{
16278 char_u *r = NULL;
16279
16280#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016281 char_u *serverid = get_tv_string_chk(&argvars[0]);
16282
16283 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284 {
16285# ifdef WIN32
16286 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016287 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016288
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016289 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 if (n != 0)
16291 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16292 if (r == NULL)
16293# else
16294 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016295 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016296# endif
16297 EMSG(_("E277: Unable to read a server reply"));
16298 }
16299#endif
16300 rettv->v_type = VAR_STRING;
16301 rettv->vval.v_string = r;
16302}
16303
16304/*
16305 * "remote_send()" function
16306 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016308f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016309{
16310 rettv->v_type = VAR_STRING;
16311 rettv->vval.v_string = NULL;
16312#ifdef FEAT_CLIENTSERVER
16313 remote_common(argvars, rettv, FALSE);
16314#endif
16315}
16316
16317/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016318 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016319 */
16320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016321f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016322{
Bram Moolenaar33570922005-01-25 22:26:29 +000016323 list_T *l;
16324 listitem_T *item, *item2;
16325 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016326 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016327 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016328 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 dict_T *d;
16330 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016331 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016332
Bram Moolenaar8c711452005-01-14 21:53:12 +000016333 if (argvars[0].v_type == VAR_DICT)
16334 {
16335 if (argvars[2].v_type != VAR_UNKNOWN)
16336 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016337 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016338 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016340 key = get_tv_string_chk(&argvars[1]);
16341 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016343 di = dict_find(d, key, -1);
16344 if (di == NULL)
16345 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016346 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16347 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016348 {
16349 *rettv = di->di_tv;
16350 init_tv(&di->di_tv);
16351 dictitem_remove(d, di);
16352 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016354 }
16355 }
16356 else if (argvars[0].v_type != VAR_LIST)
16357 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016358 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016359 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016361 int error = FALSE;
16362
16363 idx = get_tv_number_chk(&argvars[1], &error);
16364 if (error)
16365 ; /* type error: do nothing, errmsg already given */
16366 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016367 EMSGN(_(e_listidx), idx);
16368 else
16369 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016370 if (argvars[2].v_type == VAR_UNKNOWN)
16371 {
16372 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016373 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016374 *rettv = item->li_tv;
16375 vim_free(item);
16376 }
16377 else
16378 {
16379 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016380 end = get_tv_number_chk(&argvars[2], &error);
16381 if (error)
16382 ; /* type error: do nothing */
16383 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016384 EMSGN(_(e_listidx), end);
16385 else
16386 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016387 int cnt = 0;
16388
16389 for (li = item; li != NULL; li = li->li_next)
16390 {
16391 ++cnt;
16392 if (li == item2)
16393 break;
16394 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016395 if (li == NULL) /* didn't find "item2" after "item" */
16396 EMSG(_(e_invrange));
16397 else
16398 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016399 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016400 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016401 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016402 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016403 l->lv_first = item;
16404 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016405 item->li_prev = NULL;
16406 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016407 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016408 }
16409 }
16410 }
16411 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 }
16413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414}
16415
16416/*
16417 * "rename({from}, {to})" function
16418 */
16419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016420f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421{
16422 char_u buf[NUMBUFLEN];
16423
16424 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016427 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16428 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429}
16430
16431/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016432 * "repeat()" function
16433 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016435f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016436{
16437 char_u *p;
16438 int n;
16439 int slen;
16440 int len;
16441 char_u *r;
16442 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016443
16444 n = get_tv_number(&argvars[1]);
16445 if (argvars[0].v_type == VAR_LIST)
16446 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016447 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016448 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016449 if (list_extend(rettv->vval.v_list,
16450 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016451 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016452 }
16453 else
16454 {
16455 p = get_tv_string(&argvars[0]);
16456 rettv->v_type = VAR_STRING;
16457 rettv->vval.v_string = NULL;
16458
16459 slen = (int)STRLEN(p);
16460 len = slen * n;
16461 if (len <= 0)
16462 return;
16463
16464 r = alloc(len + 1);
16465 if (r != NULL)
16466 {
16467 for (i = 0; i < n; i++)
16468 mch_memmove(r + i * slen, p, (size_t)slen);
16469 r[len] = NUL;
16470 }
16471
16472 rettv->vval.v_string = r;
16473 }
16474}
16475
16476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 * "resolve()" function
16478 */
16479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016480f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481{
16482 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016483#ifdef HAVE_READLINK
16484 char_u *buf = NULL;
16485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016487 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488#ifdef FEAT_SHORTCUT
16489 {
16490 char_u *v = NULL;
16491
16492 v = mch_resolve_shortcut(p);
16493 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016494 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 }
16498#else
16499# ifdef HAVE_READLINK
16500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 char_u *cpy;
16502 int len;
16503 char_u *remain = NULL;
16504 char_u *q;
16505 int is_relative_to_current = FALSE;
16506 int has_trailing_pathsep = FALSE;
16507 int limit = 100;
16508
16509 p = vim_strsave(p);
16510
16511 if (p[0] == '.' && (vim_ispathsep(p[1])
16512 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16513 is_relative_to_current = TRUE;
16514
16515 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016516 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016517 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016519 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
16522 q = getnextcomp(p);
16523 if (*q != NUL)
16524 {
16525 /* Separate the first path component in "p", and keep the
16526 * remainder (beginning with the path separator). */
16527 remain = vim_strsave(q - 1);
16528 q[-1] = NUL;
16529 }
16530
Bram Moolenaard9462e32011-04-11 21:35:11 +020016531 buf = alloc(MAXPATHL + 1);
16532 if (buf == NULL)
16533 goto fail;
16534
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 for (;;)
16536 {
16537 for (;;)
16538 {
16539 len = readlink((char *)p, (char *)buf, MAXPATHL);
16540 if (len <= 0)
16541 break;
16542 buf[len] = NUL;
16543
16544 if (limit-- == 0)
16545 {
16546 vim_free(p);
16547 vim_free(remain);
16548 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 goto fail;
16551 }
16552
16553 /* Ensure that the result will have a trailing path separator
16554 * if the argument has one. */
16555 if (remain == NULL && has_trailing_pathsep)
16556 add_pathsep(buf);
16557
16558 /* Separate the first path component in the link value and
16559 * concatenate the remainders. */
16560 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16561 if (*q != NUL)
16562 {
16563 if (remain == NULL)
16564 remain = vim_strsave(q - 1);
16565 else
16566 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016567 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 if (cpy != NULL)
16569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 vim_free(remain);
16571 remain = cpy;
16572 }
16573 }
16574 q[-1] = NUL;
16575 }
16576
16577 q = gettail(p);
16578 if (q > p && *q == NUL)
16579 {
16580 /* Ignore trailing path separator. */
16581 q[-1] = NUL;
16582 q = gettail(p);
16583 }
16584 if (q > p && !mch_isFullName(buf))
16585 {
16586 /* symlink is relative to directory of argument */
16587 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16588 if (cpy != NULL)
16589 {
16590 STRCPY(cpy, p);
16591 STRCPY(gettail(cpy), buf);
16592 vim_free(p);
16593 p = cpy;
16594 }
16595 }
16596 else
16597 {
16598 vim_free(p);
16599 p = vim_strsave(buf);
16600 }
16601 }
16602
16603 if (remain == NULL)
16604 break;
16605
16606 /* Append the first path component of "remain" to "p". */
16607 q = getnextcomp(remain + 1);
16608 len = q - remain - (*q != NUL);
16609 cpy = vim_strnsave(p, STRLEN(p) + len);
16610 if (cpy != NULL)
16611 {
16612 STRNCAT(cpy, remain, len);
16613 vim_free(p);
16614 p = cpy;
16615 }
16616 /* Shorten "remain". */
16617 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016618 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 else
16620 {
16621 vim_free(remain);
16622 remain = NULL;
16623 }
16624 }
16625
16626 /* If the result is a relative path name, make it explicitly relative to
16627 * the current directory if and only if the argument had this form. */
16628 if (!vim_ispathsep(*p))
16629 {
16630 if (is_relative_to_current
16631 && *p != NUL
16632 && !(p[0] == '.'
16633 && (p[1] == NUL
16634 || vim_ispathsep(p[1])
16635 || (p[1] == '.'
16636 && (p[2] == NUL
16637 || vim_ispathsep(p[2]))))))
16638 {
16639 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016640 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 if (cpy != NULL)
16642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 vim_free(p);
16644 p = cpy;
16645 }
16646 }
16647 else if (!is_relative_to_current)
16648 {
16649 /* Strip leading "./". */
16650 q = p;
16651 while (q[0] == '.' && vim_ispathsep(q[1]))
16652 q += 2;
16653 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016654 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 }
16656 }
16657
16658 /* Ensure that the result will have no trailing path separator
16659 * if the argument had none. But keep "/" or "//". */
16660 if (!has_trailing_pathsep)
16661 {
16662 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016663 if (after_pathsep(p, q))
16664 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 }
16666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016667 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 }
16669# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671# endif
16672#endif
16673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016674 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675
16676#ifdef HAVE_READLINK
16677fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016678 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016680 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681}
16682
16683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016684 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 */
16686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016687f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688{
Bram Moolenaar33570922005-01-25 22:26:29 +000016689 list_T *l;
16690 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691
Bram Moolenaar0d660222005-01-07 21:51:51 +000016692 if (argvars[0].v_type != VAR_LIST)
16693 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016694 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016695 && !tv_check_lock(l->lv_lock,
16696 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697 {
16698 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016699 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016700 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016701 while (li != NULL)
16702 {
16703 ni = li->li_prev;
16704 list_append(l, li);
16705 li = ni;
16706 }
16707 rettv->vval.v_list = l;
16708 rettv->v_type = VAR_LIST;
16709 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016710 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712}
16713
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016714#define SP_NOMOVE 0x01 /* don't move cursor */
16715#define SP_REPEAT 0x02 /* repeat to find outer pair */
16716#define SP_RETCOUNT 0x04 /* return matchcount */
16717#define SP_SETPCMARK 0x08 /* set previous context mark */
16718#define SP_START 0x10 /* accept match at start position */
16719#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16720#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016721#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016722
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016723static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724
16725/*
16726 * Get flags for a search function.
16727 * Possibly sets "p_ws".
16728 * Returns BACKWARD, FORWARD or zero (for an error).
16729 */
16730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016731get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016732{
16733 int dir = FORWARD;
16734 char_u *flags;
16735 char_u nbuf[NUMBUFLEN];
16736 int mask;
16737
16738 if (varp->v_type != VAR_UNKNOWN)
16739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016740 flags = get_tv_string_buf_chk(varp, nbuf);
16741 if (flags == NULL)
16742 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016743 while (*flags != NUL)
16744 {
16745 switch (*flags)
16746 {
16747 case 'b': dir = BACKWARD; break;
16748 case 'w': p_ws = TRUE; break;
16749 case 'W': p_ws = FALSE; break;
16750 default: mask = 0;
16751 if (flagsp != NULL)
16752 switch (*flags)
16753 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016754 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016755 case 'e': mask = SP_END; break;
16756 case 'm': mask = SP_RETCOUNT; break;
16757 case 'n': mask = SP_NOMOVE; break;
16758 case 'p': mask = SP_SUBPAT; break;
16759 case 'r': mask = SP_REPEAT; break;
16760 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016761 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016762 }
16763 if (mask == 0)
16764 {
16765 EMSG2(_(e_invarg2), flags);
16766 dir = 0;
16767 }
16768 else
16769 *flagsp |= mask;
16770 }
16771 if (dir == 0)
16772 break;
16773 ++flags;
16774 }
16775 }
16776 return dir;
16777}
16778
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016780 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016783search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016785 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 char_u *pat;
16787 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016788 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789 int save_p_ws = p_ws;
16790 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016791 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016792 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016793 proftime_T tm;
16794#ifdef FEAT_RELTIME
16795 long time_limit = 0;
16796#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016797 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016798 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016800 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016801 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016802 if (dir == 0)
16803 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016804 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016805 if (flags & SP_START)
16806 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016807 if (flags & SP_END)
16808 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016809 if (flags & SP_COLUMN)
16810 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016811
Bram Moolenaar76929292008-01-06 19:07:36 +000016812 /* Optional arguments: line number to stop searching and timeout. */
16813 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016814 {
16815 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16816 if (lnum_stop < 0)
16817 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016818#ifdef FEAT_RELTIME
16819 if (argvars[3].v_type != VAR_UNKNOWN)
16820 {
16821 time_limit = get_tv_number_chk(&argvars[3], NULL);
16822 if (time_limit < 0)
16823 goto theend;
16824 }
16825#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016826 }
16827
Bram Moolenaar76929292008-01-06 19:07:36 +000016828#ifdef FEAT_RELTIME
16829 /* Set the time limit, if there is one. */
16830 profile_setlimit(time_limit, &tm);
16831#endif
16832
Bram Moolenaar231334e2005-07-25 20:46:57 +000016833 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016834 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016835 * Check to make sure only those flags are set.
16836 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16837 * flags cannot be set. Check for that condition also.
16838 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016839 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016840 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016843 goto theend;
16844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016846 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016847 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016848 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016849 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016851 if (flags & SP_SUBPAT)
16852 retval = subpatnum;
16853 else
16854 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016855 if (flags & SP_SETPCMARK)
16856 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016858 if (match_pos != NULL)
16859 {
16860 /* Store the match cursor position */
16861 match_pos->lnum = pos.lnum;
16862 match_pos->col = pos.col + 1;
16863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 /* "/$" will put the cursor after the end of the line, may need to
16865 * correct that here */
16866 check_cursor();
16867 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016868
16869 /* If 'n' flag is used: restore cursor position. */
16870 if (flags & SP_NOMOVE)
16871 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016872 else
16873 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016874theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016876
16877 return retval;
16878}
16879
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016880#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016881
16882/*
16883 * round() is not in C90, use ceil() or floor() instead.
16884 */
16885 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016886vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016887{
16888 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16889}
16890
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016891/*
16892 * "round({float})" function
16893 */
16894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016895f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016896{
16897 float_T f;
16898
16899 rettv->v_type = VAR_FLOAT;
16900 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016901 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016902 else
16903 rettv->vval.v_float = 0.0;
16904}
16905#endif
16906
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016907/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016908 * "screenattr()" function
16909 */
16910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016911f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016912{
16913 int row;
16914 int col;
16915 int c;
16916
16917 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16918 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16919 if (row < 0 || row >= screen_Rows
16920 || col < 0 || col >= screen_Columns)
16921 c = -1;
16922 else
16923 c = ScreenAttrs[LineOffset[row] + col];
16924 rettv->vval.v_number = c;
16925}
16926
16927/*
16928 * "screenchar()" function
16929 */
16930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016931f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016932{
16933 int row;
16934 int col;
16935 int off;
16936 int c;
16937
16938 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16939 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16940 if (row < 0 || row >= screen_Rows
16941 || col < 0 || col >= screen_Columns)
16942 c = -1;
16943 else
16944 {
16945 off = LineOffset[row] + col;
16946#ifdef FEAT_MBYTE
16947 if (enc_utf8 && ScreenLinesUC[off] != 0)
16948 c = ScreenLinesUC[off];
16949 else
16950#endif
16951 c = ScreenLines[off];
16952 }
16953 rettv->vval.v_number = c;
16954}
16955
16956/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016957 * "screencol()" function
16958 *
16959 * First column is 1 to be consistent with virtcol().
16960 */
16961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016962f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016963{
16964 rettv->vval.v_number = screen_screencol() + 1;
16965}
16966
16967/*
16968 * "screenrow()" function
16969 */
16970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016971f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016972{
16973 rettv->vval.v_number = screen_screenrow() + 1;
16974}
16975
16976/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016977 * "search()" function
16978 */
16979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016980f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016981{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016982 int flags = 0;
16983
16984 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985}
16986
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016988 * "searchdecl()" function
16989 */
16990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016991f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016992{
16993 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016994 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016995 int error = FALSE;
16996 char_u *name;
16997
16998 rettv->vval.v_number = 1; /* default: FAIL */
16999
17000 name = get_tv_string_chk(&argvars[0]);
17001 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017002 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017003 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017004 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17005 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17006 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017007 if (!error && name != NULL)
17008 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017009 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017010}
17011
17012/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017013 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017016searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017{
17018 char_u *spat, *mpat, *epat;
17019 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 int dir;
17022 int flags = 0;
17023 char_u nbuf1[NUMBUFLEN];
17024 char_u nbuf2[NUMBUFLEN];
17025 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017026 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017027 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017028 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017031 spat = get_tv_string_chk(&argvars[0]);
17032 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17033 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17034 if (spat == NULL || mpat == NULL || epat == NULL)
17035 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 /* Handle the optional fourth argument: flags */
17038 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017039 if (dir == 0)
17040 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017041
17042 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017043 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17044 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017045 if ((flags & (SP_END | SP_SUBPAT)) != 0
17046 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017047 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017048 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017049 goto theend;
17050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017052 /* Using 'r' implies 'W', otherwise it doesn't work. */
17053 if (flags & SP_REPEAT)
17054 p_ws = FALSE;
17055
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017056 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017057 if (argvars[3].v_type == VAR_UNKNOWN
17058 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 skip = (char_u *)"";
17060 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017062 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017063 if (argvars[5].v_type != VAR_UNKNOWN)
17064 {
17065 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17066 if (lnum_stop < 0)
17067 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017068#ifdef FEAT_RELTIME
17069 if (argvars[6].v_type != VAR_UNKNOWN)
17070 {
17071 time_limit = get_tv_number_chk(&argvars[6], NULL);
17072 if (time_limit < 0)
17073 goto theend;
17074 }
17075#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017076 }
17077 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 if (skip == NULL)
17079 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017081 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017082 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017083
17084theend:
17085 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017086
17087 return retval;
17088}
17089
17090/*
17091 * "searchpair()" function
17092 */
17093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017094f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017095{
17096 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17097}
17098
17099/*
17100 * "searchpairpos()" function
17101 */
17102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017103f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017104{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017105 pos_T match_pos;
17106 int lnum = 0;
17107 int col = 0;
17108
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017109 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017110 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017111
17112 if (searchpair_cmn(argvars, &match_pos) > 0)
17113 {
17114 lnum = match_pos.lnum;
17115 col = match_pos.col;
17116 }
17117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017118 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17119 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017120}
17121
17122/*
17123 * Search for a start/middle/end thing.
17124 * Used by searchpair(), see its documentation for the details.
17125 * Returns 0 or -1 for no match,
17126 */
17127 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017128do_searchpair(
17129 char_u *spat, /* start pattern */
17130 char_u *mpat, /* middle pattern */
17131 char_u *epat, /* end pattern */
17132 int dir, /* BACKWARD or FORWARD */
17133 char_u *skip, /* skip expression */
17134 int flags, /* SP_SETPCMARK and other SP_ values */
17135 pos_T *match_pos,
17136 linenr_T lnum_stop, /* stop at this line if not zero */
17137 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017138{
17139 char_u *save_cpo;
17140 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17141 long retval = 0;
17142 pos_T pos;
17143 pos_T firstpos;
17144 pos_T foundpos;
17145 pos_T save_cursor;
17146 pos_T save_pos;
17147 int n;
17148 int r;
17149 int nest = 1;
17150 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017151 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017152 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017153
17154 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17155 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017156 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017157
Bram Moolenaar76929292008-01-06 19:07:36 +000017158#ifdef FEAT_RELTIME
17159 /* Set the time limit, if there is one. */
17160 profile_setlimit(time_limit, &tm);
17161#endif
17162
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017163 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17164 * start/middle/end (pat3, for the top pair). */
17165 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17166 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17167 if (pat2 == NULL || pat3 == NULL)
17168 goto theend;
17169 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17170 if (*mpat == NUL)
17171 STRCPY(pat3, pat2);
17172 else
17173 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17174 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017175 if (flags & SP_START)
17176 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017177
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 save_cursor = curwin->w_cursor;
17179 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017180 clearpos(&firstpos);
17181 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 pat = pat3;
17183 for (;;)
17184 {
17185 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017186 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17188 /* didn't find it or found the first match again: FAIL */
17189 break;
17190
17191 if (firstpos.lnum == 0)
17192 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017193 if (equalpos(pos, foundpos))
17194 {
17195 /* Found the same position again. Can happen with a pattern that
17196 * has "\zs" at the end and searching backwards. Advance one
17197 * character and try again. */
17198 if (dir == BACKWARD)
17199 decl(&pos);
17200 else
17201 incl(&pos);
17202 }
17203 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017205 /* clear the start flag to avoid getting stuck here */
17206 options &= ~SEARCH_START;
17207
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 /* If the skip pattern matches, ignore this match. */
17209 if (*skip != NUL)
17210 {
17211 save_pos = curwin->w_cursor;
17212 curwin->w_cursor = pos;
17213 r = eval_to_bool(skip, &err, NULL, FALSE);
17214 curwin->w_cursor = save_pos;
17215 if (err)
17216 {
17217 /* Evaluating {skip} caused an error, break here. */
17218 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017219 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 break;
17221 }
17222 if (r)
17223 continue;
17224 }
17225
17226 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17227 {
17228 /* Found end when searching backwards or start when searching
17229 * forward: nested pair. */
17230 ++nest;
17231 pat = pat2; /* nested, don't search for middle */
17232 }
17233 else
17234 {
17235 /* Found end when searching forward or start when searching
17236 * backward: end of (nested) pair; or found middle in outer pair. */
17237 if (--nest == 1)
17238 pat = pat3; /* outer level, search for middle */
17239 }
17240
17241 if (nest == 0)
17242 {
17243 /* Found the match: return matchcount or line number. */
17244 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017245 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017247 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017248 if (flags & SP_SETPCMARK)
17249 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 curwin->w_cursor = pos;
17251 if (!(flags & SP_REPEAT))
17252 break;
17253 nest = 1; /* search for next unmatched */
17254 }
17255 }
17256
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017257 if (match_pos != NULL)
17258 {
17259 /* Store the match cursor position */
17260 match_pos->lnum = curwin->w_cursor.lnum;
17261 match_pos->col = curwin->w_cursor.col + 1;
17262 }
17263
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017265 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 curwin->w_cursor = save_cursor;
17267
17268theend:
17269 vim_free(pat2);
17270 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017271 if (p_cpo == empty_option)
17272 p_cpo = save_cpo;
17273 else
17274 /* Darn, evaluating the {skip} expression changed the value. */
17275 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017276
17277 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278}
17279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017280/*
17281 * "searchpos()" function
17282 */
17283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017284f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017285{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017286 pos_T match_pos;
17287 int lnum = 0;
17288 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017289 int n;
17290 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017291
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017292 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017293 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017294
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017295 n = search_cmn(argvars, &match_pos, &flags);
17296 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017297 {
17298 lnum = match_pos.lnum;
17299 col = match_pos.col;
17300 }
17301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017302 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17303 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017304 if (flags & SP_SUBPAT)
17305 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017306}
17307
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017309f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017311#ifdef FEAT_CLIENTSERVER
17312 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017313 char_u *server = get_tv_string_chk(&argvars[0]);
17314 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017317 if (server == NULL || reply == NULL)
17318 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017319 if (check_restricted() || check_secure())
17320 return;
17321# ifdef FEAT_X11
17322 if (check_connection() == FAIL)
17323 return;
17324# endif
17325
17326 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017328 EMSG(_("E258: Unable to send to client"));
17329 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017331 rettv->vval.v_number = 0;
17332#else
17333 rettv->vval.v_number = -1;
17334#endif
17335}
17336
Bram Moolenaar0d660222005-01-07 21:51:51 +000017337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017338f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017339{
17340 char_u *r = NULL;
17341
17342#ifdef FEAT_CLIENTSERVER
17343# ifdef WIN32
17344 r = serverGetVimNames();
17345# else
17346 make_connection();
17347 if (X_DISPLAY != NULL)
17348 r = serverGetVimNames(X_DISPLAY);
17349# endif
17350#endif
17351 rettv->v_type = VAR_STRING;
17352 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353}
17354
17355/*
17356 * "setbufvar()" function
17357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017359f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360{
17361 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 char_u nbuf[NUMBUFLEN];
17366
17367 if (check_restricted() || check_secure())
17368 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17370 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017371 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 varp = &argvars[2];
17373
17374 if (buf != NULL && varname != NULL && varp != NULL)
17375 {
17376 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378
17379 if (*varname == '&')
17380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017381 long numval;
17382 char_u *strval;
17383 int error = FALSE;
17384
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017386 numval = get_tv_number_chk(varp, &error);
17387 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017388 if (!error && strval != NULL)
17389 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 }
17391 else
17392 {
17393 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17394 if (bufvarname != NULL)
17395 {
17396 STRCPY(bufvarname, "b:");
17397 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017398 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 vim_free(bufvarname);
17400 }
17401 }
17402
17403 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406}
17407
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017409f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017410{
17411 dict_T *d;
17412 dictitem_T *di;
17413 char_u *csearch;
17414
17415 if (argvars[0].v_type != VAR_DICT)
17416 {
17417 EMSG(_(e_dictreq));
17418 return;
17419 }
17420
17421 if ((d = argvars[0].vval.v_dict) != NULL)
17422 {
17423 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17424 if (csearch != NULL)
17425 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017426#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017427 if (enc_utf8)
17428 {
17429 int pcc[MAX_MCO];
17430 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017431
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017432 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17433 }
17434 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017435#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017436 set_last_csearch(PTR2CHAR(csearch),
17437 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017438 }
17439
17440 di = dict_find(d, (char_u *)"forward", -1);
17441 if (di != NULL)
17442 set_csearch_direction(get_tv_number(&di->di_tv)
17443 ? FORWARD : BACKWARD);
17444
17445 di = dict_find(d, (char_u *)"until", -1);
17446 if (di != NULL)
17447 set_csearch_until(!!get_tv_number(&di->di_tv));
17448 }
17449}
17450
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451/*
17452 * "setcmdpos()" function
17453 */
17454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017455f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017457 int pos = (int)get_tv_number(&argvars[0]) - 1;
17458
17459 if (pos >= 0)
17460 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461}
17462
17463/*
17464 * "setline()" function
17465 */
17466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017467f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468{
17469 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017470 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017471 list_T *l = NULL;
17472 listitem_T *li = NULL;
17473 long added = 0;
17474 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017476 lnum = get_tv_lnum(&argvars[0]);
17477 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017479 l = argvars[1].vval.v_list;
17480 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017482 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017484
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017485 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017486 for (;;)
17487 {
17488 if (l != NULL)
17489 {
17490 /* list argument, get next string */
17491 if (li == NULL)
17492 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017493 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017494 li = li->li_next;
17495 }
17496
17497 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017498 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017499 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017500
17501 /* When coming here from Insert mode, sync undo, so that this can be
17502 * undone separately from what was previously inserted. */
17503 if (u_sync_once == 2)
17504 {
17505 u_sync_once = 1; /* notify that u_sync() was called */
17506 u_sync(TRUE);
17507 }
17508
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017509 if (lnum <= curbuf->b_ml.ml_line_count)
17510 {
17511 /* existing line, replace it */
17512 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17513 {
17514 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017515 if (lnum == curwin->w_cursor.lnum)
17516 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017517 rettv->vval.v_number = 0; /* OK */
17518 }
17519 }
17520 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17521 {
17522 /* lnum is one past the last line, append the line */
17523 ++added;
17524 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17525 rettv->vval.v_number = 0; /* OK */
17526 }
17527
17528 if (l == NULL) /* only one string argument */
17529 break;
17530 ++lnum;
17531 }
17532
17533 if (added > 0)
17534 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535}
17536
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017537static 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 +000017538
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017540 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017541 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017543set_qf_ll_list(
17544 win_T *wp UNUSED,
17545 typval_T *list_arg UNUSED,
17546 typval_T *action_arg UNUSED,
17547 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017548{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017549#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017550 char_u *act;
17551 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017552#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017553
Bram Moolenaar2641f772005-03-25 21:58:17 +000017554 rettv->vval.v_number = -1;
17555
17556#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017557 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017558 EMSG(_(e_listreq));
17559 else
17560 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017561 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017562
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017563 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017564 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017565 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017566 if (act == NULL)
17567 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017568 if (*act == 'a' || *act == 'r')
17569 action = *act;
17570 }
17571
Bram Moolenaar81484f42012-12-05 15:16:47 +010017572 if (l != NULL && set_errorlist(wp, l, action,
17573 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017574 rettv->vval.v_number = 0;
17575 }
17576#endif
17577}
17578
17579/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017580 * "setloclist()" function
17581 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017583f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017584{
17585 win_T *win;
17586
17587 rettv->vval.v_number = -1;
17588
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017589 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017590 if (win != NULL)
17591 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17592}
17593
17594/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017595 * "setmatches()" function
17596 */
17597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017598f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017599{
17600#ifdef FEAT_SEARCH_EXTRA
17601 list_T *l;
17602 listitem_T *li;
17603 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017604 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017605
17606 rettv->vval.v_number = -1;
17607 if (argvars[0].v_type != VAR_LIST)
17608 {
17609 EMSG(_(e_listreq));
17610 return;
17611 }
17612 if ((l = argvars[0].vval.v_list) != NULL)
17613 {
17614
17615 /* To some extent make sure that we are dealing with a list from
17616 * "getmatches()". */
17617 li = l->lv_first;
17618 while (li != NULL)
17619 {
17620 if (li->li_tv.v_type != VAR_DICT
17621 || (d = li->li_tv.vval.v_dict) == NULL)
17622 {
17623 EMSG(_(e_invarg));
17624 return;
17625 }
17626 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017627 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17628 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017629 && dict_find(d, (char_u *)"priority", -1) != NULL
17630 && dict_find(d, (char_u *)"id", -1) != NULL))
17631 {
17632 EMSG(_(e_invarg));
17633 return;
17634 }
17635 li = li->li_next;
17636 }
17637
17638 clear_matches(curwin);
17639 li = l->lv_first;
17640 while (li != NULL)
17641 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017642 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017643 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017644 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017645 char_u *group;
17646 int priority;
17647 int id;
17648 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017649
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017650 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017651 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17652 {
17653 if (s == NULL)
17654 {
17655 s = list_alloc();
17656 if (s == NULL)
17657 return;
17658 }
17659
17660 /* match from matchaddpos() */
17661 for (i = 1; i < 9; i++)
17662 {
17663 sprintf((char *)buf, (char *)"pos%d", i);
17664 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17665 {
17666 if (di->di_tv.v_type != VAR_LIST)
17667 return;
17668
17669 list_append_tv(s, &di->di_tv);
17670 s->lv_refcount++;
17671 }
17672 else
17673 break;
17674 }
17675 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017676
17677 group = get_dict_string(d, (char_u *)"group", FALSE);
17678 priority = (int)get_dict_number(d, (char_u *)"priority");
17679 id = (int)get_dict_number(d, (char_u *)"id");
17680 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17681 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17682 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017683 if (i == 0)
17684 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017685 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017686 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017687 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017688 }
17689 else
17690 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017691 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017692 list_unref(s);
17693 s = NULL;
17694 }
17695
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017696 li = li->li_next;
17697 }
17698 rettv->vval.v_number = 0;
17699 }
17700#endif
17701}
17702
17703/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017704 * "setpos()" function
17705 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017707f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017708{
17709 pos_T pos;
17710 int fnum;
17711 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017712 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017713
Bram Moolenaar08250432008-02-13 11:42:46 +000017714 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017715 name = get_tv_string_chk(argvars);
17716 if (name != NULL)
17717 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017718 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017719 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017720 if (--pos.col < 0)
17721 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017722 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017723 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017724 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017725 if (fnum == curbuf->b_fnum)
17726 {
17727 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017728 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017729 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017730 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017731 curwin->w_set_curswant = FALSE;
17732 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017733 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017734 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017735 }
17736 else
17737 EMSG(_(e_invarg));
17738 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017739 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17740 {
17741 /* set mark */
17742 if (setmark_pos(name[1], &pos, fnum) == OK)
17743 rettv->vval.v_number = 0;
17744 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017745 else
17746 EMSG(_(e_invarg));
17747 }
17748 }
17749}
17750
17751/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017752 * "setqflist()" function
17753 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017755f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017756{
17757 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17758}
17759
17760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 * "setreg()" function
17762 */
17763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017764f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765{
17766 int regname;
17767 char_u *strregname;
17768 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017769 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 int append;
17771 char_u yank_type;
17772 long block_len;
17773
17774 block_len = -1;
17775 yank_type = MAUTO;
17776 append = FALSE;
17777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017779 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017781 if (strregname == NULL)
17782 return; /* type error; errmsg already given */
17783 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 if (regname == 0 || regname == '@')
17785 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017787 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017789 stropt = get_tv_string_chk(&argvars[2]);
17790 if (stropt == NULL)
17791 return; /* type error */
17792 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 switch (*stropt)
17794 {
17795 case 'a': case 'A': /* append */
17796 append = TRUE;
17797 break;
17798 case 'v': case 'c': /* character-wise selection */
17799 yank_type = MCHAR;
17800 break;
17801 case 'V': case 'l': /* line-wise selection */
17802 yank_type = MLINE;
17803 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 case 'b': case Ctrl_V: /* block-wise selection */
17805 yank_type = MBLOCK;
17806 if (VIM_ISDIGIT(stropt[1]))
17807 {
17808 ++stropt;
17809 block_len = getdigits(&stropt) - 1;
17810 --stropt;
17811 }
17812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 }
17814 }
17815
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017816 if (argvars[1].v_type == VAR_LIST)
17817 {
17818 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017819 char_u **allocval;
17820 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017821 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017822 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017823 int len = argvars[1].vval.v_list->lv_len;
17824 listitem_T *li;
17825
Bram Moolenaar7d647822014-04-05 21:28:56 +020017826 /* First half: use for pointers to result lines; second half: use for
17827 * pointers to allocated copies. */
17828 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017829 if (lstval == NULL)
17830 return;
17831 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017832 allocval = lstval + len + 2;
17833 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017834
17835 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17836 li = li->li_next)
17837 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017838 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017839 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017840 goto free_lstval;
17841 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017842 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017843 /* Need to make a copy, next get_tv_string_buf_chk() will
17844 * overwrite the string. */
17845 strval = vim_strsave(buf);
17846 if (strval == NULL)
17847 goto free_lstval;
17848 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017849 }
17850 *curval++ = strval;
17851 }
17852 *curval++ = NULL;
17853
17854 write_reg_contents_lst(regname, lstval, -1,
17855 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017856free_lstval:
17857 while (curallocval > allocval)
17858 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017859 vim_free(lstval);
17860 }
17861 else
17862 {
17863 strval = get_tv_string_chk(&argvars[1]);
17864 if (strval == NULL)
17865 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017866 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870}
17871
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017872/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017873 * "settabvar()" function
17874 */
17875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017876f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017877{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017878#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017879 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017880 tabpage_T *tp;
17881#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017882 char_u *varname, *tabvarname;
17883 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017884
17885 rettv->vval.v_number = 0;
17886
17887 if (check_restricted() || check_secure())
17888 return;
17889
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017890#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017891 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017892#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017893 varname = get_tv_string_chk(&argvars[1]);
17894 varp = &argvars[2];
17895
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017896 if (varname != NULL && varp != NULL
17897#ifdef FEAT_WINDOWS
17898 && tp != NULL
17899#endif
17900 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017901 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017902#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017903 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017904 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017905#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017906
17907 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17908 if (tabvarname != NULL)
17909 {
17910 STRCPY(tabvarname, "t:");
17911 STRCPY(tabvarname + 2, varname);
17912 set_var(tabvarname, varp, TRUE);
17913 vim_free(tabvarname);
17914 }
17915
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017916#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017917 /* Restore current tabpage */
17918 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017919 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017920#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017921 }
17922}
17923
17924/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017925 * "settabwinvar()" function
17926 */
17927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017928f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017929{
17930 setwinvar(argvars, rettv, 1);
17931}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932
17933/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017934 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017937f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017939 setwinvar(argvars, rettv, 0);
17940}
17941
17942/*
17943 * "setwinvar()" and "settabwinvar()" functions
17944 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017945
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017947setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017948{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 win_T *win;
17950#ifdef FEAT_WINDOWS
17951 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017952 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017953 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954#endif
17955 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017956 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017958 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959
17960 if (check_restricted() || check_secure())
17961 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017962
17963#ifdef FEAT_WINDOWS
17964 if (off == 1)
17965 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17966 else
17967 tp = curtab;
17968#endif
17969 win = find_win_by_nr(&argvars[off], tp);
17970 varname = get_tv_string_chk(&argvars[off + 1]);
17971 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972
17973 if (win != NULL && varname != NULL && varp != NULL)
17974 {
17975#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017976 need_switch_win = !(tp == curtab && win == curwin);
17977 if (!need_switch_win
17978 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017981 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017983 long numval;
17984 char_u *strval;
17985 int error = FALSE;
17986
17987 ++varname;
17988 numval = get_tv_number_chk(varp, &error);
17989 strval = get_tv_string_buf_chk(varp, nbuf);
17990 if (!error && strval != NULL)
17991 set_option_value(varname, numval, strval, OPT_LOCAL);
17992 }
17993 else
17994 {
17995 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17996 if (winvarname != NULL)
17997 {
17998 STRCPY(winvarname, "w:");
17999 STRCPY(winvarname + 2, varname);
18000 set_var(winvarname, varp, TRUE);
18001 vim_free(winvarname);
18002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 }
18004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018006 if (need_switch_win)
18007 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008#endif
18009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010}
18011
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018012#ifdef FEAT_CRYPT
18013/*
18014 * "sha256({string})" function
18015 */
18016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018017f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018018{
18019 char_u *p;
18020
18021 p = get_tv_string(&argvars[0]);
18022 rettv->vval.v_string = vim_strsave(
18023 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18024 rettv->v_type = VAR_STRING;
18025}
18026#endif /* FEAT_CRYPT */
18027
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018029 * "shellescape({string})" function
18030 */
18031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018032f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018033{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018034 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018035 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018036 rettv->v_type = VAR_STRING;
18037}
18038
18039/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018040 * shiftwidth() function
18041 */
18042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018043f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018044{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018045 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018046}
18047
18048/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018049 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 */
18051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018052f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055
Bram Moolenaar0d660222005-01-07 21:51:51 +000018056 p = get_tv_string(&argvars[0]);
18057 rettv->vval.v_string = vim_strsave(p);
18058 simplify_filename(rettv->vval.v_string); /* simplify in place */
18059 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060}
18061
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018062#ifdef FEAT_FLOAT
18063/*
18064 * "sin()" function
18065 */
18066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018067f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018068{
18069 float_T f;
18070
18071 rettv->v_type = VAR_FLOAT;
18072 if (get_float_arg(argvars, &f) == OK)
18073 rettv->vval.v_float = sin(f);
18074 else
18075 rettv->vval.v_float = 0.0;
18076}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018077
18078/*
18079 * "sinh()" function
18080 */
18081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018082f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018083{
18084 float_T f;
18085
18086 rettv->v_type = VAR_FLOAT;
18087 if (get_float_arg(argvars, &f) == OK)
18088 rettv->vval.v_float = sinh(f);
18089 else
18090 rettv->vval.v_float = 0.0;
18091}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018092#endif
18093
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094static int
18095#ifdef __BORLANDC__
18096 _RTLENTRYF
18097#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018098 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099static int
18100#ifdef __BORLANDC__
18101 _RTLENTRYF
18102#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018103 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018105/* struct used in the array that's given to qsort() */
18106typedef struct
18107{
18108 listitem_T *item;
18109 int idx;
18110} sortItem_T;
18111
Bram Moolenaar0d660222005-01-07 21:51:51 +000018112static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018113static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018114static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018115#ifdef FEAT_FLOAT
18116static int item_compare_float;
18117#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018118static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018119static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018120static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018121static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018122static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018123#define ITEM_COMPARE_FAIL 999
18124
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018126 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018128 static int
18129#ifdef __BORLANDC__
18130_RTLENTRYF
18131#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018132item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018134 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018135 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018136 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018137 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018138 int res;
18139 char_u numbuf1[NUMBUFLEN];
18140 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018142 si1 = (sortItem_T *)s1;
18143 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018144 tv1 = &si1->item->li_tv;
18145 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018146
18147 if (item_compare_numbers)
18148 {
18149 long v1 = get_tv_number(tv1);
18150 long v2 = get_tv_number(tv2);
18151
18152 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18153 }
18154
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018155#ifdef FEAT_FLOAT
18156 if (item_compare_float)
18157 {
18158 float_T v1 = get_tv_float(tv1);
18159 float_T v2 = get_tv_float(tv2);
18160
18161 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18162 }
18163#endif
18164
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018165 /* tv2string() puts quotes around a string and allocates memory. Don't do
18166 * that for string variables. Use a single quote when comparing with a
18167 * non-string to do what the docs promise. */
18168 if (tv1->v_type == VAR_STRING)
18169 {
18170 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18171 p1 = (char_u *)"'";
18172 else
18173 p1 = tv1->vval.v_string;
18174 }
18175 else
18176 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18177 if (tv2->v_type == VAR_STRING)
18178 {
18179 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18180 p2 = (char_u *)"'";
18181 else
18182 p2 = tv2->vval.v_string;
18183 }
18184 else
18185 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018186 if (p1 == NULL)
18187 p1 = (char_u *)"";
18188 if (p2 == NULL)
18189 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018190 if (!item_compare_numeric)
18191 {
18192 if (item_compare_ic)
18193 res = STRICMP(p1, p2);
18194 else
18195 res = STRCMP(p1, p2);
18196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018198 {
18199 double n1, n2;
18200 n1 = strtod((char *)p1, (char **)&p1);
18201 n2 = strtod((char *)p2, (char **)&p2);
18202 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18203 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018204
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018205 /* When the result would be zero, compare the item indexes. Makes the
18206 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018207 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018208 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018209
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210 vim_free(tofree1);
18211 vim_free(tofree2);
18212 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213}
18214
18215 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018216#ifdef __BORLANDC__
18217_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018219item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018220{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018221 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018222 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018223 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018224 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018227 /* shortcut after failure in previous call; compare all items equal */
18228 if (item_compare_func_err)
18229 return 0;
18230
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018231 si1 = (sortItem_T *)s1;
18232 si2 = (sortItem_T *)s2;
18233
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018234 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018235 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018236 copy_tv(&si1->item->li_tv, &argv[0]);
18237 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018238
18239 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018240 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018241 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18242 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018243 clear_tv(&argv[0]);
18244 clear_tv(&argv[1]);
18245
18246 if (res == FAIL)
18247 res = ITEM_COMPARE_FAIL;
18248 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018249 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18250 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018251 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018252 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018253
18254 /* When the result would be zero, compare the pointers themselves. Makes
18255 * the sort stable. */
18256 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018257 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018258
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259 return res;
18260}
18261
18262/*
18263 * "sort({list})" function
18264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018266do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267{
Bram Moolenaar33570922005-01-25 22:26:29 +000018268 list_T *l;
18269 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018270 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271 long len;
18272 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018275 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 else
18277 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018278 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018279 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018280 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18281 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282 return;
18283 rettv->vval.v_list = l;
18284 rettv->v_type = VAR_LIST;
18285 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 len = list_len(l);
18288 if (len <= 1)
18289 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290
Bram Moolenaar0d660222005-01-07 21:51:51 +000018291 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018292 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018293 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018294#ifdef FEAT_FLOAT
18295 item_compare_float = FALSE;
18296#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018297 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018298 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018299 if (argvars[1].v_type != VAR_UNKNOWN)
18300 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018301 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018302 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018303 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304 else
18305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018306 int error = FALSE;
18307
18308 i = get_tv_number_chk(&argvars[1], &error);
18309 if (error)
18310 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018311 if (i == 1)
18312 item_compare_ic = TRUE;
18313 else
18314 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018315 if (item_compare_func != NULL)
18316 {
18317 if (STRCMP(item_compare_func, "n") == 0)
18318 {
18319 item_compare_func = NULL;
18320 item_compare_numeric = TRUE;
18321 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018322 else if (STRCMP(item_compare_func, "N") == 0)
18323 {
18324 item_compare_func = NULL;
18325 item_compare_numbers = TRUE;
18326 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018327#ifdef FEAT_FLOAT
18328 else if (STRCMP(item_compare_func, "f") == 0)
18329 {
18330 item_compare_func = NULL;
18331 item_compare_float = TRUE;
18332 }
18333#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018334 else if (STRCMP(item_compare_func, "i") == 0)
18335 {
18336 item_compare_func = NULL;
18337 item_compare_ic = TRUE;
18338 }
18339 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018340 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018341
18342 if (argvars[2].v_type != VAR_UNKNOWN)
18343 {
18344 /* optional third argument: {dict} */
18345 if (argvars[2].v_type != VAR_DICT)
18346 {
18347 EMSG(_(e_dictreq));
18348 return;
18349 }
18350 item_compare_selfdict = argvars[2].vval.v_dict;
18351 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353
Bram Moolenaar0d660222005-01-07 21:51:51 +000018354 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018355 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018356 if (ptrs == NULL)
18357 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358
Bram Moolenaar327aa022014-03-25 18:24:23 +010018359 i = 0;
18360 if (sort)
18361 {
18362 /* sort(): ptrs will be the list to sort */
18363 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018364 {
18365 ptrs[i].item = li;
18366 ptrs[i].idx = i;
18367 ++i;
18368 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018369
18370 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018371 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018372 /* test the compare function */
18373 if (item_compare_func != NULL
18374 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018375 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018376 EMSG(_("E702: Sort compare function failed"));
18377 else
18378 {
18379 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018380 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018381 item_compare_func == NULL ? item_compare : item_compare2);
18382
18383 if (!item_compare_func_err)
18384 {
18385 /* Clear the List and append the items in sorted order. */
18386 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18387 l->lv_len = 0;
18388 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018389 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018390 }
18391 }
18392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018395 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018396
18397 /* f_uniq(): ptrs will be a stack of items to remove */
18398 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018399 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018400 item_compare_func_ptr = item_compare_func
18401 ? item_compare2 : item_compare;
18402
18403 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18404 li = li->li_next)
18405 {
18406 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18407 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018408 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018409 if (item_compare_func_err)
18410 {
18411 EMSG(_("E882: Uniq compare function failed"));
18412 break;
18413 }
18414 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018416 if (!item_compare_func_err)
18417 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018418 while (--i >= 0)
18419 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018420 li = ptrs[i].item->li_next;
18421 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018422 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018423 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018424 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018425 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018426 list_fix_watch(l, li);
18427 listitem_free(li);
18428 l->lv_len--;
18429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018430 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018431 }
18432
18433 vim_free(ptrs);
18434 }
18435}
18436
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018437/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018438 * "sort({list})" function
18439 */
18440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018441f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018442{
18443 do_sort_uniq(argvars, rettv, TRUE);
18444}
18445
18446/*
18447 * "uniq({list})" function
18448 */
18449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018450f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018451{
18452 do_sort_uniq(argvars, rettv, FALSE);
18453}
18454
18455/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018456 * "soundfold({word})" function
18457 */
18458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018459f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018460{
18461 char_u *s;
18462
18463 rettv->v_type = VAR_STRING;
18464 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018465#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018466 rettv->vval.v_string = eval_soundfold(s);
18467#else
18468 rettv->vval.v_string = vim_strsave(s);
18469#endif
18470}
18471
18472/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018473 * "spellbadword()" function
18474 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018476f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018477{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018478 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018479 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018480 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018482 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018483 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018484
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018485#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018486 if (argvars[0].v_type == VAR_UNKNOWN)
18487 {
18488 /* Find the start and length of the badly spelled word. */
18489 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18490 if (len != 0)
18491 word = ml_get_cursor();
18492 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018493 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018494 {
18495 char_u *str = get_tv_string_chk(&argvars[0]);
18496 int capcol = -1;
18497
18498 if (str != NULL)
18499 {
18500 /* Check the argument for spelling. */
18501 while (*str != NUL)
18502 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018503 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018504 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018505 {
18506 word = str;
18507 break;
18508 }
18509 str += len;
18510 }
18511 }
18512 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018513#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018514
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018515 list_append_string(rettv->vval.v_list, word, len);
18516 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018517 attr == HLF_SPB ? "bad" :
18518 attr == HLF_SPR ? "rare" :
18519 attr == HLF_SPL ? "local" :
18520 attr == HLF_SPC ? "caps" :
18521 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018522}
18523
18524/*
18525 * "spellsuggest()" function
18526 */
18527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018528f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018529{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018530#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018531 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018532 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018533 int maxcount;
18534 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018535 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018536 listitem_T *li;
18537 int need_capital = FALSE;
18538#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018539
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018540 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018541 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018542
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018543#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018544 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018545 {
18546 str = get_tv_string(&argvars[0]);
18547 if (argvars[1].v_type != VAR_UNKNOWN)
18548 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018549 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018550 if (maxcount <= 0)
18551 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018552 if (argvars[2].v_type != VAR_UNKNOWN)
18553 {
18554 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18555 if (typeerr)
18556 return;
18557 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018558 }
18559 else
18560 maxcount = 25;
18561
Bram Moolenaar4770d092006-01-12 23:22:24 +000018562 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018563
18564 for (i = 0; i < ga.ga_len; ++i)
18565 {
18566 str = ((char_u **)ga.ga_data)[i];
18567
18568 li = listitem_alloc();
18569 if (li == NULL)
18570 vim_free(str);
18571 else
18572 {
18573 li->li_tv.v_type = VAR_STRING;
18574 li->li_tv.v_lock = 0;
18575 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018576 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018577 }
18578 }
18579 ga_clear(&ga);
18580 }
18581#endif
18582}
18583
Bram Moolenaar0d660222005-01-07 21:51:51 +000018584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018585f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018586{
18587 char_u *str;
18588 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018589 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018590 regmatch_T regmatch;
18591 char_u patbuf[NUMBUFLEN];
18592 char_u *save_cpo;
18593 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018594 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018595 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018596 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018597
18598 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18599 save_cpo = p_cpo;
18600 p_cpo = (char_u *)"";
18601
18602 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018603 if (argvars[1].v_type != VAR_UNKNOWN)
18604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018605 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18606 if (pat == NULL)
18607 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018608 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018609 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018610 }
18611 if (pat == NULL || *pat == NUL)
18612 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018614 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018616 if (typeerr)
18617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618
Bram Moolenaar0d660222005-01-07 21:51:51 +000018619 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18620 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018622 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018623 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018624 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018625 if (*str == NUL)
18626 match = FALSE; /* empty item at the end */
18627 else
18628 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018629 if (match)
18630 end = regmatch.startp[0];
18631 else
18632 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018633 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18634 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018636 if (list_append_string(rettv->vval.v_list, str,
18637 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018638 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018639 }
18640 if (!match)
18641 break;
18642 /* Advance to just after the match. */
18643 if (regmatch.endp[0] > str)
18644 col = 0;
18645 else
18646 {
18647 /* Don't get stuck at the same match. */
18648#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018649 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650#else
18651 col = 1;
18652#endif
18653 }
18654 str = regmatch.endp[0];
18655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656
Bram Moolenaar473de612013-06-08 18:19:48 +020018657 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659
Bram Moolenaar0d660222005-01-07 21:51:51 +000018660 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661}
18662
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018663#ifdef FEAT_FLOAT
18664/*
18665 * "sqrt()" function
18666 */
18667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018668f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018669{
18670 float_T f;
18671
18672 rettv->v_type = VAR_FLOAT;
18673 if (get_float_arg(argvars, &f) == OK)
18674 rettv->vval.v_float = sqrt(f);
18675 else
18676 rettv->vval.v_float = 0.0;
18677}
18678
18679/*
18680 * "str2float()" function
18681 */
18682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018683f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018684{
18685 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18686
18687 if (*p == '+')
18688 p = skipwhite(p + 1);
18689 (void)string2float(p, &rettv->vval.v_float);
18690 rettv->v_type = VAR_FLOAT;
18691}
18692#endif
18693
Bram Moolenaar2c932302006-03-18 21:42:09 +000018694/*
18695 * "str2nr()" function
18696 */
18697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018698f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018699{
18700 int base = 10;
18701 char_u *p;
18702 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018703 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018704
18705 if (argvars[1].v_type != VAR_UNKNOWN)
18706 {
18707 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018708 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018709 {
18710 EMSG(_(e_invarg));
18711 return;
18712 }
18713 }
18714
18715 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018716 if (*p == '+')
18717 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018718 switch (base)
18719 {
18720 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18721 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18722 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18723 default: what = 0;
18724 }
18725 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018726 rettv->vval.v_number = n;
18727}
18728
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729#ifdef HAVE_STRFTIME
18730/*
18731 * "strftime({format}[, {time}])" function
18732 */
18733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018734f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735{
18736 char_u result_buf[256];
18737 struct tm *curtime;
18738 time_t seconds;
18739 char_u *p;
18740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018741 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018743 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018744 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 seconds = time(NULL);
18746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 curtime = localtime(&seconds);
18749 /* MSVC returns NULL for an invalid value of seconds. */
18750 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018751 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 else
18753 {
18754# ifdef FEAT_MBYTE
18755 vimconv_T conv;
18756 char_u *enc;
18757
18758 conv.vc_type = CONV_NONE;
18759 enc = enc_locale();
18760 convert_setup(&conv, p_enc, enc);
18761 if (conv.vc_type != CONV_NONE)
18762 p = string_convert(&conv, p, NULL);
18763# endif
18764 if (p != NULL)
18765 (void)strftime((char *)result_buf, sizeof(result_buf),
18766 (char *)p, curtime);
18767 else
18768 result_buf[0] = NUL;
18769
18770# ifdef FEAT_MBYTE
18771 if (conv.vc_type != CONV_NONE)
18772 vim_free(p);
18773 convert_setup(&conv, enc, p_enc);
18774 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 else
18777# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779
18780# ifdef FEAT_MBYTE
18781 /* Release conversion descriptors */
18782 convert_setup(&conv, NULL, NULL);
18783 vim_free(enc);
18784# endif
18785 }
18786}
18787#endif
18788
18789/*
18790 * "stridx()" function
18791 */
18792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018793f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794{
18795 char_u buf[NUMBUFLEN];
18796 char_u *needle;
18797 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018800 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018802 needle = get_tv_string_chk(&argvars[1]);
18803 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018804 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018805 if (needle == NULL || haystack == NULL)
18806 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807
Bram Moolenaar33570922005-01-25 22:26:29 +000018808 if (argvars[2].v_type != VAR_UNKNOWN)
18809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018810 int error = FALSE;
18811
18812 start_idx = get_tv_number_chk(&argvars[2], &error);
18813 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018814 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018815 if (start_idx >= 0)
18816 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 }
18818
18819 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18820 if (pos != NULL)
18821 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822}
18823
18824/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018825 * "string()" function
18826 */
18827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018828f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018829{
18830 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018831 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018833 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018834 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018835 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018836 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018837 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838}
18839
18840/*
18841 * "strlen()" function
18842 */
18843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018844f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018846 rettv->vval.v_number = (varnumber_T)(STRLEN(
18847 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848}
18849
18850/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018851 * "strchars()" function
18852 */
18853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018854f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018855{
18856 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018857 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018858#ifdef FEAT_MBYTE
18859 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018860 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018861#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018862
18863 if (argvars[1].v_type != VAR_UNKNOWN)
18864 skipcc = get_tv_number_chk(&argvars[1], NULL);
18865 if (skipcc < 0 || skipcc > 1)
18866 EMSG(_(e_invarg));
18867 else
18868 {
18869#ifdef FEAT_MBYTE
18870 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18871 while (*s != NUL)
18872 {
18873 func_mb_ptr2char_adv(&s);
18874 ++len;
18875 }
18876 rettv->vval.v_number = len;
18877#else
18878 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18879#endif
18880 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018881}
18882
18883/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018884 * "strdisplaywidth()" function
18885 */
18886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018887f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018888{
18889 char_u *s = get_tv_string(&argvars[0]);
18890 int col = 0;
18891
18892 if (argvars[1].v_type != VAR_UNKNOWN)
18893 col = get_tv_number(&argvars[1]);
18894
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018895 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018896}
18897
18898/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018899 * "strwidth()" function
18900 */
18901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018902f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018903{
18904 char_u *s = get_tv_string(&argvars[0]);
18905
18906 rettv->vval.v_number = (varnumber_T)(
18907#ifdef FEAT_MBYTE
18908 mb_string2cells(s, -1)
18909#else
18910 STRLEN(s)
18911#endif
18912 );
18913}
18914
18915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 * "strpart()" function
18917 */
18918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018919f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920{
18921 char_u *p;
18922 int n;
18923 int len;
18924 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018925 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 slen = (int)STRLEN(p);
18929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018930 n = get_tv_number_chk(&argvars[1], &error);
18931 if (error)
18932 len = 0;
18933 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018934 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 else
18936 len = slen - n; /* default len: all bytes that are available. */
18937
18938 /*
18939 * Only return the overlap between the specified part and the actual
18940 * string.
18941 */
18942 if (n < 0)
18943 {
18944 len += n;
18945 n = 0;
18946 }
18947 else if (n > slen)
18948 n = slen;
18949 if (len < 0)
18950 len = 0;
18951 else if (n + len > slen)
18952 len = slen - n;
18953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018954 rettv->v_type = VAR_STRING;
18955 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956}
18957
18958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959 * "strridx()" function
18960 */
18961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018962f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963{
18964 char_u buf[NUMBUFLEN];
18965 char_u *needle;
18966 char_u *haystack;
18967 char_u *rest;
18968 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018969 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018971 needle = get_tv_string_chk(&argvars[1]);
18972 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018973
18974 rettv->vval.v_number = -1;
18975 if (needle == NULL || haystack == NULL)
18976 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018977
18978 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018979 if (argvars[2].v_type != VAR_UNKNOWN)
18980 {
18981 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018982 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018983 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018984 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018985 }
18986 else
18987 end_idx = haystack_len;
18988
Bram Moolenaar0d660222005-01-07 21:51:51 +000018989 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018990 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018991 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018992 lastmatch = haystack + end_idx;
18993 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018995 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018996 for (rest = haystack; *rest != '\0'; ++rest)
18997 {
18998 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018999 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019000 break;
19001 lastmatch = rest;
19002 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019003 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004
19005 if (lastmatch == NULL)
19006 rettv->vval.v_number = -1;
19007 else
19008 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19009}
19010
19011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 * "strtrans()" function
19013 */
19014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019015f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019017 rettv->v_type = VAR_STRING;
19018 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019}
19020
19021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019022 * "submatch()" function
19023 */
19024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019025f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019026{
Bram Moolenaar41571762014-04-02 19:00:58 +020019027 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019028 int no;
19029 int retList = 0;
19030
19031 no = (int)get_tv_number_chk(&argvars[0], &error);
19032 if (error)
19033 return;
19034 error = FALSE;
19035 if (argvars[1].v_type != VAR_UNKNOWN)
19036 retList = get_tv_number_chk(&argvars[1], &error);
19037 if (error)
19038 return;
19039
19040 if (retList == 0)
19041 {
19042 rettv->v_type = VAR_STRING;
19043 rettv->vval.v_string = reg_submatch(no);
19044 }
19045 else
19046 {
19047 rettv->v_type = VAR_LIST;
19048 rettv->vval.v_list = reg_submatch_list(no);
19049 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019050}
19051
19052/*
19053 * "substitute()" function
19054 */
19055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019056f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057{
19058 char_u patbuf[NUMBUFLEN];
19059 char_u subbuf[NUMBUFLEN];
19060 char_u flagsbuf[NUMBUFLEN];
19061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019062 char_u *str = get_tv_string_chk(&argvars[0]);
19063 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19064 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19065 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19066
Bram Moolenaar0d660222005-01-07 21:51:51 +000019067 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019068 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19069 rettv->vval.v_string = NULL;
19070 else
19071 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019072}
19073
19074/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019075 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019078f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079{
19080 int id = 0;
19081#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019082 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 long col;
19084 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019085 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019087 lnum = get_tv_lnum(argvars); /* -1 on type error */
19088 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19089 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019091 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019092 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019093 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094#endif
19095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097}
19098
19099/*
19100 * "synIDattr(id, what [, mode])" function
19101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019103f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104{
19105 char_u *p = NULL;
19106#ifdef FEAT_SYN_HL
19107 int id;
19108 char_u *what;
19109 char_u *mode;
19110 char_u modebuf[NUMBUFLEN];
19111 int modec;
19112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113 id = get_tv_number(&argvars[0]);
19114 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019115 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019117 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019119 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 modec = 0; /* replace invalid with current */
19121 }
19122 else
19123 {
19124#ifdef FEAT_GUI
19125 if (gui.in_use)
19126 modec = 'g';
19127 else
19128#endif
19129 if (t_colors > 1)
19130 modec = 'c';
19131 else
19132 modec = 't';
19133 }
19134
19135
19136 switch (TOLOWER_ASC(what[0]))
19137 {
19138 case 'b':
19139 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19140 p = highlight_color(id, what, modec);
19141 else /* bold */
19142 p = highlight_has_attr(id, HL_BOLD, modec);
19143 break;
19144
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019145 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146 p = highlight_color(id, what, modec);
19147 break;
19148
19149 case 'i':
19150 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19151 p = highlight_has_attr(id, HL_INVERSE, modec);
19152 else /* italic */
19153 p = highlight_has_attr(id, HL_ITALIC, modec);
19154 break;
19155
19156 case 'n': /* name */
19157 p = get_highlight_name(NULL, id - 1);
19158 break;
19159
19160 case 'r': /* reverse */
19161 p = highlight_has_attr(id, HL_INVERSE, modec);
19162 break;
19163
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019164 case 's':
19165 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19166 p = highlight_color(id, what, modec);
19167 else /* standout */
19168 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 break;
19170
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019171 case 'u':
19172 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19173 /* underline */
19174 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19175 else
19176 /* undercurl */
19177 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 break;
19179 }
19180
19181 if (p != NULL)
19182 p = vim_strsave(p);
19183#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019184 rettv->v_type = VAR_STRING;
19185 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186}
19187
19188/*
19189 * "synIDtrans(id)" function
19190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019192f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193{
19194 int id;
19195
19196#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019197 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198
19199 if (id > 0)
19200 id = syn_get_final_id(id);
19201 else
19202#endif
19203 id = 0;
19204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019205 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206}
19207
19208/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019209 * "synconcealed(lnum, col)" function
19210 */
19211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019212f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019213{
19214#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19215 long lnum;
19216 long col;
19217 int syntax_flags = 0;
19218 int cchar;
19219 int matchid = 0;
19220 char_u str[NUMBUFLEN];
19221#endif
19222
19223 rettv->v_type = VAR_LIST;
19224 rettv->vval.v_list = NULL;
19225
19226#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19227 lnum = get_tv_lnum(argvars); /* -1 on type error */
19228 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19229
19230 vim_memset(str, NUL, sizeof(str));
19231
19232 if (rettv_list_alloc(rettv) != FAIL)
19233 {
19234 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19235 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19236 && curwin->w_p_cole > 0)
19237 {
19238 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19239 syntax_flags = get_syntax_info(&matchid);
19240
19241 /* get the conceal character */
19242 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19243 {
19244 cchar = syn_get_sub_char();
19245 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19246 cchar = lcs_conceal;
19247 if (cchar != NUL)
19248 {
19249# ifdef FEAT_MBYTE
19250 if (has_mbyte)
19251 (*mb_char2bytes)(cchar, str);
19252 else
19253# endif
19254 str[0] = cchar;
19255 }
19256 }
19257 }
19258
19259 list_append_number(rettv->vval.v_list,
19260 (syntax_flags & HL_CONCEAL) != 0);
19261 /* -1 to auto-determine strlen */
19262 list_append_string(rettv->vval.v_list, str, -1);
19263 list_append_number(rettv->vval.v_list, matchid);
19264 }
19265#endif
19266}
19267
19268/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019269 * "synstack(lnum, col)" function
19270 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019272f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019273{
19274#ifdef FEAT_SYN_HL
19275 long lnum;
19276 long col;
19277 int i;
19278 int id;
19279#endif
19280
19281 rettv->v_type = VAR_LIST;
19282 rettv->vval.v_list = NULL;
19283
19284#ifdef FEAT_SYN_HL
19285 lnum = get_tv_lnum(argvars); /* -1 on type error */
19286 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19287
19288 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019289 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019290 && rettv_list_alloc(rettv) != FAIL)
19291 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019292 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019293 for (i = 0; ; ++i)
19294 {
19295 id = syn_get_stack_item(i);
19296 if (id < 0)
19297 break;
19298 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19299 break;
19300 }
19301 }
19302#endif
19303}
19304
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019306get_cmd_output_as_rettv(
19307 typval_T *argvars,
19308 typval_T *rettv,
19309 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019311 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019313 char_u *infile = NULL;
19314 char_u buf[NUMBUFLEN];
19315 int err = FALSE;
19316 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019317 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019318 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019320 rettv->v_type = VAR_STRING;
19321 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019322 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019323 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019325 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019326 {
19327 /*
19328 * Write the string to a temp file, to be used for input of the shell
19329 * command.
19330 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019331 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019332 {
19333 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019334 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019335 }
19336
19337 fd = mch_fopen((char *)infile, WRITEBIN);
19338 if (fd == NULL)
19339 {
19340 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019341 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019342 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019343 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019344 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019345 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19346 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019347 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019348 else
19349 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019350 size_t len;
19351
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019352 p = get_tv_string_buf_chk(&argvars[1], buf);
19353 if (p == NULL)
19354 {
19355 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019356 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019357 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019358 len = STRLEN(p);
19359 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019360 err = TRUE;
19361 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019362 if (fclose(fd) != 0)
19363 err = TRUE;
19364 if (err)
19365 {
19366 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019367 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019368 }
19369 }
19370
Bram Moolenaar52a72462014-08-29 15:53:52 +020019371 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19372 * echoes typeahead, that messes up the display. */
19373 if (!msg_silent)
19374 flags += SHELL_COOKED;
19375
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019376 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019378 int len;
19379 listitem_T *li;
19380 char_u *s = NULL;
19381 char_u *start;
19382 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019383 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384
Bram Moolenaar52a72462014-08-29 15:53:52 +020019385 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019386 if (res == NULL)
19387 goto errret;
19388
19389 list = list_alloc();
19390 if (list == NULL)
19391 goto errret;
19392
19393 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019395 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019396 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019397 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019398 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019399
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019400 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019401 if (s == NULL)
19402 goto errret;
19403
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019404 for (p = s; start < end; ++p, ++start)
19405 *p = *start == NUL ? NL : *start;
19406 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019407
19408 li = listitem_alloc();
19409 if (li == NULL)
19410 {
19411 vim_free(s);
19412 goto errret;
19413 }
19414 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019415 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019416 li->li_tv.vval.v_string = s;
19417 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019419
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019420 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019421 rettv->v_type = VAR_LIST;
19422 rettv->vval.v_list = list;
19423 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019425 else
19426 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019427 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019428#ifdef USE_CR
19429 /* translate <CR> into <NL> */
19430 if (res != NULL)
19431 {
19432 char_u *s;
19433
19434 for (s = res; *s; ++s)
19435 {
19436 if (*s == CAR)
19437 *s = NL;
19438 }
19439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440#else
19441# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019442 /* translate <CR><NL> into <NL> */
19443 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019445 char_u *s, *d;
19446
19447 d = res;
19448 for (s = res; *s; ++s)
19449 {
19450 if (s[0] == CAR && s[1] == NL)
19451 ++s;
19452 *d++ = *s;
19453 }
19454 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456# endif
19457#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019458 rettv->vval.v_string = res;
19459 res = NULL;
19460 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019461
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019462errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019463 if (infile != NULL)
19464 {
19465 mch_remove(infile);
19466 vim_free(infile);
19467 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019468 if (res != NULL)
19469 vim_free(res);
19470 if (list != NULL)
19471 list_free(list, TRUE);
19472}
19473
19474/*
19475 * "system()" function
19476 */
19477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019478f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019479{
19480 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19481}
19482
19483/*
19484 * "systemlist()" function
19485 */
19486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019487f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019488{
19489 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490}
19491
19492/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019493 * "tabpagebuflist()" function
19494 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019496f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019497{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019498#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019499 tabpage_T *tp;
19500 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019501
19502 if (argvars[0].v_type == VAR_UNKNOWN)
19503 wp = firstwin;
19504 else
19505 {
19506 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19507 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019508 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019509 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019510 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019511 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019512 for (; wp != NULL; wp = wp->w_next)
19513 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019514 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019515 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019516 }
19517#endif
19518}
19519
19520
19521/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019522 * "tabpagenr()" function
19523 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019525f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019526{
19527 int nr = 1;
19528#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019529 char_u *arg;
19530
19531 if (argvars[0].v_type != VAR_UNKNOWN)
19532 {
19533 arg = get_tv_string_chk(&argvars[0]);
19534 nr = 0;
19535 if (arg != NULL)
19536 {
19537 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019538 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019539 else
19540 EMSG2(_(e_invexpr2), arg);
19541 }
19542 }
19543 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019544 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019545#endif
19546 rettv->vval.v_number = nr;
19547}
19548
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019549
19550#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019551static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019552
19553/*
19554 * Common code for tabpagewinnr() and winnr().
19555 */
19556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019557get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019558{
19559 win_T *twin;
19560 int nr = 1;
19561 win_T *wp;
19562 char_u *arg;
19563
19564 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19565 if (argvar->v_type != VAR_UNKNOWN)
19566 {
19567 arg = get_tv_string_chk(argvar);
19568 if (arg == NULL)
19569 nr = 0; /* type error; errmsg already given */
19570 else if (STRCMP(arg, "$") == 0)
19571 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19572 else if (STRCMP(arg, "#") == 0)
19573 {
19574 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19575 if (twin == NULL)
19576 nr = 0;
19577 }
19578 else
19579 {
19580 EMSG2(_(e_invexpr2), arg);
19581 nr = 0;
19582 }
19583 }
19584
19585 if (nr > 0)
19586 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19587 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019588 {
19589 if (wp == NULL)
19590 {
19591 /* didn't find it in this tabpage */
19592 nr = 0;
19593 break;
19594 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019595 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019596 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019597 return nr;
19598}
19599#endif
19600
19601/*
19602 * "tabpagewinnr()" function
19603 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019605f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019606{
19607 int nr = 1;
19608#ifdef FEAT_WINDOWS
19609 tabpage_T *tp;
19610
19611 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19612 if (tp == NULL)
19613 nr = 0;
19614 else
19615 nr = get_winnr(tp, &argvars[1]);
19616#endif
19617 rettv->vval.v_number = nr;
19618}
19619
19620
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019621/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019622 * "tagfiles()" function
19623 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019625f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019626{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019627 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019628 tagname_T tn;
19629 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019630
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019631 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019632 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019633 fname = alloc(MAXPATHL);
19634 if (fname == NULL)
19635 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019636
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019637 for (first = TRUE; ; first = FALSE)
19638 if (get_tagfname(&tn, first, fname) == FAIL
19639 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019640 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019641 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019642 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019643}
19644
19645/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019646 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019647 */
19648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019649f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019650{
19651 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019652
19653 tag_pattern = get_tv_string(&argvars[0]);
19654
19655 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019656 if (*tag_pattern == NUL)
19657 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019658
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019659 if (rettv_list_alloc(rettv) == OK)
19660 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019661}
19662
19663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 * "tempname()" function
19665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019667f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668{
19669 static int x = 'A';
19670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019672 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673
19674 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19675 * names. Skip 'I' and 'O', they are used for shell redirection. */
19676 do
19677 {
19678 if (x == 'Z')
19679 x = '0';
19680 else if (x == '9')
19681 x = 'A';
19682 else
19683 {
19684#ifdef EBCDIC
19685 if (x == 'I')
19686 x = 'J';
19687 else if (x == 'R')
19688 x = 'S';
19689 else
19690#endif
19691 ++x;
19692 }
19693 } while (x == 'I' || x == 'O');
19694}
19695
19696/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019697 * "test(list)" function: Just checking the walls...
19698 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019700f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019701{
19702 /* Used for unit testing. Change the code below to your liking. */
19703#if 0
19704 listitem_T *li;
19705 list_T *l;
19706 char_u *bad, *good;
19707
19708 if (argvars[0].v_type != VAR_LIST)
19709 return;
19710 l = argvars[0].vval.v_list;
19711 if (l == NULL)
19712 return;
19713 li = l->lv_first;
19714 if (li == NULL)
19715 return;
19716 bad = get_tv_string(&li->li_tv);
19717 li = li->li_next;
19718 if (li == NULL)
19719 return;
19720 good = get_tv_string(&li->li_tv);
19721 rettv->vval.v_number = test_edit_score(bad, good);
19722#endif
19723}
19724
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019725#ifdef FEAT_FLOAT
19726/*
19727 * "tan()" function
19728 */
19729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019730f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019731{
19732 float_T f;
19733
19734 rettv->v_type = VAR_FLOAT;
19735 if (get_float_arg(argvars, &f) == OK)
19736 rettv->vval.v_float = tan(f);
19737 else
19738 rettv->vval.v_float = 0.0;
19739}
19740
19741/*
19742 * "tanh()" function
19743 */
19744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019745f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019746{
19747 float_T f;
19748
19749 rettv->v_type = VAR_FLOAT;
19750 if (get_float_arg(argvars, &f) == OK)
19751 rettv->vval.v_float = tanh(f);
19752 else
19753 rettv->vval.v_float = 0.0;
19754}
19755#endif
19756
Bram Moolenaard52d9742005-08-21 22:20:28 +000019757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 * "tolower(string)" function
19759 */
19760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019761f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762{
19763 char_u *p;
19764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 p = vim_strsave(get_tv_string(&argvars[0]));
19766 rettv->v_type = VAR_STRING;
19767 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768
19769 if (p != NULL)
19770 while (*p != NUL)
19771 {
19772#ifdef FEAT_MBYTE
19773 int l;
19774
19775 if (enc_utf8)
19776 {
19777 int c, lc;
19778
19779 c = utf_ptr2char(p);
19780 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019781 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 /* TODO: reallocate string when byte count changes. */
19783 if (utf_char2len(lc) == l)
19784 utf_char2bytes(lc, p);
19785 p += l;
19786 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019787 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 p += l; /* skip multi-byte character */
19789 else
19790#endif
19791 {
19792 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19793 ++p;
19794 }
19795 }
19796}
19797
19798/*
19799 * "toupper(string)" function
19800 */
19801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019802f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019804 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019805 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806}
19807
19808/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019809 * "tr(string, fromstr, tostr)" function
19810 */
19811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019812f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019813{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019814 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019815 char_u *fromstr;
19816 char_u *tostr;
19817 char_u *p;
19818#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019819 int inlen;
19820 int fromlen;
19821 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019822 int idx;
19823 char_u *cpstr;
19824 int cplen;
19825 int first = TRUE;
19826#endif
19827 char_u buf[NUMBUFLEN];
19828 char_u buf2[NUMBUFLEN];
19829 garray_T ga;
19830
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019831 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019832 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19833 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019834
19835 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->v_type = VAR_STRING;
19837 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019838 if (fromstr == NULL || tostr == NULL)
19839 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019840 ga_init2(&ga, (int)sizeof(char), 80);
19841
19842#ifdef FEAT_MBYTE
19843 if (!has_mbyte)
19844#endif
19845 /* not multi-byte: fromstr and tostr must be the same length */
19846 if (STRLEN(fromstr) != STRLEN(tostr))
19847 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019848#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019849error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019850#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019851 EMSG2(_(e_invarg2), fromstr);
19852 ga_clear(&ga);
19853 return;
19854 }
19855
19856 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019857 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019858 {
19859#ifdef FEAT_MBYTE
19860 if (has_mbyte)
19861 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019862 inlen = (*mb_ptr2len)(in_str);
19863 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019864 cplen = inlen;
19865 idx = 0;
19866 for (p = fromstr; *p != NUL; p += fromlen)
19867 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019868 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019869 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019870 {
19871 for (p = tostr; *p != NUL; p += tolen)
19872 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019873 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019874 if (idx-- == 0)
19875 {
19876 cplen = tolen;
19877 cpstr = p;
19878 break;
19879 }
19880 }
19881 if (*p == NUL) /* tostr is shorter than fromstr */
19882 goto error;
19883 break;
19884 }
19885 ++idx;
19886 }
19887
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019888 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019889 {
19890 /* Check that fromstr and tostr have the same number of
19891 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019892 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019893 first = FALSE;
19894 for (p = tostr; *p != NUL; p += tolen)
19895 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019896 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019897 --idx;
19898 }
19899 if (idx != 0)
19900 goto error;
19901 }
19902
Bram Moolenaarcde88542015-08-11 19:14:00 +020019903 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019904 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019905 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019906
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019907 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019908 }
19909 else
19910#endif
19911 {
19912 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019913 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019914 if (p != NULL)
19915 ga_append(&ga, tostr[p - fromstr]);
19916 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019917 ga_append(&ga, *in_str);
19918 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019919 }
19920 }
19921
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019922 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019923 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019924 ga_append(&ga, NUL);
19925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019926 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019927}
19928
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019929#ifdef FEAT_FLOAT
19930/*
19931 * "trunc({float})" function
19932 */
19933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019934f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019935{
19936 float_T f;
19937
19938 rettv->v_type = VAR_FLOAT;
19939 if (get_float_arg(argvars, &f) == OK)
19940 /* trunc() is not in C90, use floor() or ceil() instead. */
19941 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19942 else
19943 rettv->vval.v_float = 0.0;
19944}
19945#endif
19946
Bram Moolenaar8299df92004-07-10 09:47:34 +000019947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 * "type(expr)" function
19949 */
19950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010019953 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019954
19955 switch (argvars[0].v_type)
19956 {
19957 case VAR_NUMBER: n = 0; break;
19958 case VAR_STRING: n = 1; break;
19959 case VAR_FUNC: n = 2; break;
19960 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019961 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019962 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019963 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019964 if (argvars[0].vval.v_number == VVAL_FALSE
19965 || argvars[0].vval.v_number == VVAL_TRUE)
19966 n = 6;
19967 else
19968 n = 7;
19969 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010019970 case VAR_JOB: n = 8; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010019971 case VAR_UNKNOWN:
19972 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19973 n = -1;
19974 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019975 }
19976 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977}
19978
19979/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019980 * "undofile(name)" function
19981 */
19982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019983f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019984{
19985 rettv->v_type = VAR_STRING;
19986#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019987 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019988 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019989
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019990 if (*fname == NUL)
19991 {
19992 /* If there is no file name there will be no undo file. */
19993 rettv->vval.v_string = NULL;
19994 }
19995 else
19996 {
19997 char_u *ffname = FullName_save(fname, FALSE);
19998
19999 if (ffname != NULL)
20000 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20001 vim_free(ffname);
20002 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020003 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020004#else
20005 rettv->vval.v_string = NULL;
20006#endif
20007}
20008
20009/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020010 * "undotree()" function
20011 */
20012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020013f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020014{
20015 if (rettv_dict_alloc(rettv) == OK)
20016 {
20017 dict_T *dict = rettv->vval.v_dict;
20018 list_T *list;
20019
Bram Moolenaar730cde92010-06-27 05:18:54 +020020020 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020021 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020022 dict_add_nr_str(dict, "save_last",
20023 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020024 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20025 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020026 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020027
20028 list = list_alloc();
20029 if (list != NULL)
20030 {
20031 u_eval_tree(curbuf->b_u_oldhead, list);
20032 dict_add_list(dict, "entries", list);
20033 }
20034 }
20035}
20036
20037/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020038 * "values(dict)" function
20039 */
20040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020041f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020042{
20043 dict_list(argvars, rettv, 1);
20044}
20045
20046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 * "virtcol(string)" function
20048 */
20049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020050f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051{
20052 colnr_T vcol = 0;
20053 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020054 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020056 fp = var2fpos(&argvars[0], FALSE, &fnum);
20057 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20058 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 {
20060 getvvcol(curwin, fp, NULL, NULL, &vcol);
20061 ++vcol;
20062 }
20063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065}
20066
20067/*
20068 * "visualmode()" function
20069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020071f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 char_u str[2];
20074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 str[0] = curbuf->b_visual_mode_eval;
20077 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020078 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079
20080 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020081 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083}
20084
20085/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020086 * "wildmenumode()" function
20087 */
20088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020089f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020090{
20091#ifdef FEAT_WILDMENU
20092 if (wild_menu_showing)
20093 rettv->vval.v_number = 1;
20094#endif
20095}
20096
20097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 * "winbufnr(nr)" function
20099 */
20100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020101f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102{
20103 win_T *wp;
20104
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020105 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020109 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110}
20111
20112/*
20113 * "wincol()" function
20114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020116f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117{
20118 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020119 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120}
20121
20122/*
20123 * "winheight(nr)" function
20124 */
20125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020126f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127{
20128 win_T *wp;
20129
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020130 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020132 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020134 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135}
20136
20137/*
20138 * "winline()" function
20139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020141f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142{
20143 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020144 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145}
20146
20147/*
20148 * "winnr()" function
20149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020151f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152{
20153 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020154
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020156 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159}
20160
20161/*
20162 * "winrestcmd()" function
20163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020165f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166{
20167#ifdef FEAT_WINDOWS
20168 win_T *wp;
20169 int winnr = 1;
20170 garray_T ga;
20171 char_u buf[50];
20172
20173 ga_init2(&ga, (int)sizeof(char), 70);
20174 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20175 {
20176 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20177 ga_concat(&ga, buf);
20178# ifdef FEAT_VERTSPLIT
20179 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20180 ga_concat(&ga, buf);
20181# endif
20182 ++winnr;
20183 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020184 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020186 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020188 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191}
20192
20193/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020194 * "winrestview()" function
20195 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020197f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020198{
20199 dict_T *dict;
20200
20201 if (argvars[0].v_type != VAR_DICT
20202 || (dict = argvars[0].vval.v_dict) == NULL)
20203 EMSG(_(e_invarg));
20204 else
20205 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020206 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20207 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20208 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20209 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020210#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020211 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20212 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020213#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020214 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20215 {
20216 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20217 curwin->w_set_curswant = FALSE;
20218 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020219
Bram Moolenaar82c25852014-05-28 16:47:16 +020020220 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20221 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020222#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020223 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20224 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020225#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020226 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20227 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20228 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20229 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020230
20231 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020232 win_new_height(curwin, curwin->w_height);
20233# ifdef FEAT_VERTSPLIT
20234 win_new_width(curwin, W_WIDTH(curwin));
20235# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020236 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020237
Bram Moolenaarb851a962014-10-31 15:45:52 +010020238 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020239 curwin->w_topline = 1;
20240 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20241 curwin->w_topline = curbuf->b_ml.ml_line_count;
20242#ifdef FEAT_DIFF
20243 check_topfill(curwin, TRUE);
20244#endif
20245 }
20246}
20247
20248/*
20249 * "winsaveview()" function
20250 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020252f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020253{
20254 dict_T *dict;
20255
Bram Moolenaara800b422010-06-27 01:15:55 +020020256 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020257 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020258 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020259
20260 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20261 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20262#ifdef FEAT_VIRTUALEDIT
20263 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20264#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020265 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020266 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20267
20268 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20269#ifdef FEAT_DIFF
20270 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20271#endif
20272 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20273 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20274}
20275
20276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 * "winwidth(nr)" function
20278 */
20279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020280f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281{
20282 win_T *wp;
20283
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020284 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 else
20288#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020289 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020291 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292#endif
20293}
20294
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020296 * "wordcount()" function
20297 */
20298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020299f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020300{
20301 if (rettv_dict_alloc(rettv) == FAIL)
20302 return;
20303 cursor_pos_info(rettv->vval.v_dict);
20304}
20305
20306/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020307 * Write list of strings to file
20308 */
20309 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020310write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020311{
20312 listitem_T *li;
20313 int c;
20314 int ret = OK;
20315 char_u *s;
20316
20317 for (li = list->lv_first; li != NULL; li = li->li_next)
20318 {
20319 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20320 {
20321 if (*s == '\n')
20322 c = putc(NUL, fd);
20323 else
20324 c = putc(*s, fd);
20325 if (c == EOF)
20326 {
20327 ret = FAIL;
20328 break;
20329 }
20330 }
20331 if (!binary || li->li_next != NULL)
20332 if (putc('\n', fd) == EOF)
20333 {
20334 ret = FAIL;
20335 break;
20336 }
20337 if (ret == FAIL)
20338 {
20339 EMSG(_(e_write));
20340 break;
20341 }
20342 }
20343 return ret;
20344}
20345
20346/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020347 * "writefile()" function
20348 */
20349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020350f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020351{
20352 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020353 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020354 char_u *fname;
20355 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020356 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020357
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020358 if (check_restricted() || check_secure())
20359 return;
20360
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020361 if (argvars[0].v_type != VAR_LIST)
20362 {
20363 EMSG2(_(e_listarg), "writefile()");
20364 return;
20365 }
20366 if (argvars[0].vval.v_list == NULL)
20367 return;
20368
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020369 if (argvars[2].v_type != VAR_UNKNOWN)
20370 {
20371 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20372 binary = TRUE;
20373 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20374 append = TRUE;
20375 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020376
20377 /* Always open the file in binary mode, library functions have a mind of
20378 * their own about CR-LF conversion. */
20379 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020380 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20381 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020382 {
20383 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20384 ret = -1;
20385 }
20386 else
20387 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020388 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20389 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020390 fclose(fd);
20391 }
20392
20393 rettv->vval.v_number = ret;
20394}
20395
20396/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020397 * "xor(expr, expr)" function
20398 */
20399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020400f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020401{
20402 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20403 ^ get_tv_number_chk(&argvars[1], NULL);
20404}
20405
20406
20407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020409 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 */
20411 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020412var2fpos(
20413 typval_T *varp,
20414 int dollar_lnum, /* TRUE when $ is last line */
20415 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020417 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020419 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420
Bram Moolenaara5525202006-03-02 22:52:09 +000020421 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020422 if (varp->v_type == VAR_LIST)
20423 {
20424 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020425 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020426 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020427 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020428
20429 l = varp->vval.v_list;
20430 if (l == NULL)
20431 return NULL;
20432
20433 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020434 pos.lnum = list_find_nr(l, 0L, &error);
20435 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020436 return NULL; /* invalid line number */
20437
20438 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020439 pos.col = list_find_nr(l, 1L, &error);
20440 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020441 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020442 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020443
20444 /* We accept "$" for the column number: last column. */
20445 li = list_find(l, 1L);
20446 if (li != NULL && li->li_tv.v_type == VAR_STRING
20447 && li->li_tv.vval.v_string != NULL
20448 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20449 pos.col = len + 1;
20450
Bram Moolenaara5525202006-03-02 22:52:09 +000020451 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020452 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020453 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020454 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020455
Bram Moolenaara5525202006-03-02 22:52:09 +000020456#ifdef FEAT_VIRTUALEDIT
20457 /* Get the virtual offset. Defaults to zero. */
20458 pos.coladd = list_find_nr(l, 2L, &error);
20459 if (error)
20460 pos.coladd = 0;
20461#endif
20462
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020463 return &pos;
20464 }
20465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020466 name = get_tv_string_chk(varp);
20467 if (name == NULL)
20468 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020469 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020471 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20472 {
20473 if (VIsual_active)
20474 return &VIsual;
20475 return &curwin->w_cursor;
20476 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020477 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020479 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20481 return NULL;
20482 return pp;
20483 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020484
20485#ifdef FEAT_VIRTUALEDIT
20486 pos.coladd = 0;
20487#endif
20488
Bram Moolenaar477933c2007-07-17 14:32:23 +000020489 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020490 {
20491 pos.col = 0;
20492 if (name[1] == '0') /* "w0": first visible line */
20493 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020494 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020495 pos.lnum = curwin->w_topline;
20496 return &pos;
20497 }
20498 else if (name[1] == '$') /* "w$": last visible line */
20499 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020500 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020501 pos.lnum = curwin->w_botline - 1;
20502 return &pos;
20503 }
20504 }
20505 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020507 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 {
20509 pos.lnum = curbuf->b_ml.ml_line_count;
20510 pos.col = 0;
20511 }
20512 else
20513 {
20514 pos.lnum = curwin->w_cursor.lnum;
20515 pos.col = (colnr_T)STRLEN(ml_get_curline());
20516 }
20517 return &pos;
20518 }
20519 return NULL;
20520}
20521
20522/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020523 * Convert list in "arg" into a position and optional file number.
20524 * When "fnump" is NULL there is no file number, only 3 items.
20525 * Note that the column is passed on as-is, the caller may want to decrement
20526 * it to use 1 for the first column.
20527 * Return FAIL when conversion is not possible, doesn't check the position for
20528 * validity.
20529 */
20530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020531list2fpos(
20532 typval_T *arg,
20533 pos_T *posp,
20534 int *fnump,
20535 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020536{
20537 list_T *l = arg->vval.v_list;
20538 long i = 0;
20539 long n;
20540
Bram Moolenaar493c1782014-05-28 14:34:46 +020020541 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20542 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020543 if (arg->v_type != VAR_LIST
20544 || l == NULL
20545 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020546 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020547 return FAIL;
20548
20549 if (fnump != NULL)
20550 {
20551 n = list_find_nr(l, i++, NULL); /* fnum */
20552 if (n < 0)
20553 return FAIL;
20554 if (n == 0)
20555 n = curbuf->b_fnum; /* current buffer */
20556 *fnump = n;
20557 }
20558
20559 n = list_find_nr(l, i++, NULL); /* lnum */
20560 if (n < 0)
20561 return FAIL;
20562 posp->lnum = n;
20563
20564 n = list_find_nr(l, i++, NULL); /* col */
20565 if (n < 0)
20566 return FAIL;
20567 posp->col = n;
20568
20569#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020570 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020571 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020572 posp->coladd = 0;
20573 else
20574 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020575#endif
20576
Bram Moolenaar493c1782014-05-28 14:34:46 +020020577 if (curswantp != NULL)
20578 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20579
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020580 return OK;
20581}
20582
20583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 * Get the length of an environment variable name.
20585 * Advance "arg" to the first character after the name.
20586 * Return 0 for error.
20587 */
20588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020589get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590{
20591 char_u *p;
20592 int len;
20593
20594 for (p = *arg; vim_isIDc(*p); ++p)
20595 ;
20596 if (p == *arg) /* no name found */
20597 return 0;
20598
20599 len = (int)(p - *arg);
20600 *arg = p;
20601 return len;
20602}
20603
20604/*
20605 * Get the length of the name of a function or internal variable.
20606 * "arg" is advanced to the first non-white character after the name.
20607 * Return 0 if something is wrong.
20608 */
20609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020610get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611{
20612 char_u *p;
20613 int len;
20614
20615 /* Find the end of the name. */
20616 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020617 {
20618 if (*p == ':')
20619 {
20620 /* "s:" is start of "s:var", but "n:" is not and can be used in
20621 * slice "[n:]". Also "xx:" is not a namespace. */
20622 len = (int)(p - *arg);
20623 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20624 || len > 1)
20625 break;
20626 }
20627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 if (p == *arg) /* no name found */
20629 return 0;
20630
20631 len = (int)(p - *arg);
20632 *arg = skipwhite(p);
20633
20634 return len;
20635}
20636
20637/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020638 * Get the length of the name of a variable or function.
20639 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020641 * Return -1 if curly braces expansion failed.
20642 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 * If the name contains 'magic' {}'s, expand them and return the
20644 * expanded name in an allocated string via 'alias' - caller must free.
20645 */
20646 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020647get_name_len(
20648 char_u **arg,
20649 char_u **alias,
20650 int evaluate,
20651 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652{
20653 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 char_u *p;
20655 char_u *expr_start;
20656 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657
20658 *alias = NULL; /* default to no alias */
20659
20660 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20661 && (*arg)[2] == (int)KE_SNR)
20662 {
20663 /* hard coded <SNR>, already translated */
20664 *arg += 3;
20665 return get_id_len(arg) + 3;
20666 }
20667 len = eval_fname_script(*arg);
20668 if (len > 0)
20669 {
20670 /* literal "<SID>", "s:" or "<SNR>" */
20671 *arg += len;
20672 }
20673
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020675 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020677 p = find_name_end(*arg, &expr_start, &expr_end,
20678 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 if (expr_start != NULL)
20680 {
20681 char_u *temp_string;
20682
20683 if (!evaluate)
20684 {
20685 len += (int)(p - *arg);
20686 *arg = skipwhite(p);
20687 return len;
20688 }
20689
20690 /*
20691 * Include any <SID> etc in the expanded string:
20692 * Thus the -len here.
20693 */
20694 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20695 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020696 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 *alias = temp_string;
20698 *arg = skipwhite(p);
20699 return (int)STRLEN(temp_string);
20700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701
20702 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020703 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 EMSG2(_(e_invexpr2), *arg);
20705
20706 return len;
20707}
20708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020709/*
20710 * Find the end of a variable or function name, taking care of magic braces.
20711 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20712 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020713 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020714 * Return a pointer to just after the name. Equal to "arg" if there is no
20715 * valid name.
20716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020718find_name_end(
20719 char_u *arg,
20720 char_u **expr_start,
20721 char_u **expr_end,
20722 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020724 int mb_nest = 0;
20725 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020727 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020729 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020731 *expr_start = NULL;
20732 *expr_end = NULL;
20733 }
20734
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020735 /* Quick check for valid starting character. */
20736 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20737 return arg;
20738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020739 for (p = arg; *p != NUL
20740 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020741 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020742 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020743 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020744 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020745 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020746 if (*p == '\'')
20747 {
20748 /* skip over 'string' to avoid counting [ and ] inside it. */
20749 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20750 ;
20751 if (*p == NUL)
20752 break;
20753 }
20754 else if (*p == '"')
20755 {
20756 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20757 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20758 if (*p == '\\' && p[1] != NUL)
20759 ++p;
20760 if (*p == NUL)
20761 break;
20762 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020763 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20764 {
20765 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020766 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020767 len = (int)(p - arg);
20768 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020769 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020770 break;
20771 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020773 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020775 if (*p == '[')
20776 ++br_nest;
20777 else if (*p == ']')
20778 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020781 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020783 if (*p == '{')
20784 {
20785 mb_nest++;
20786 if (expr_start != NULL && *expr_start == NULL)
20787 *expr_start = p;
20788 }
20789 else if (*p == '}')
20790 {
20791 mb_nest--;
20792 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20793 *expr_end = p;
20794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 }
20797
20798 return p;
20799}
20800
20801/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020802 * Expands out the 'magic' {}'s in a variable/function name.
20803 * Note that this can call itself recursively, to deal with
20804 * constructs like foo{bar}{baz}{bam}
20805 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20806 * "in_start" ^
20807 * "expr_start" ^
20808 * "expr_end" ^
20809 * "in_end" ^
20810 *
20811 * Returns a new allocated string, which the caller must free.
20812 * Returns NULL for failure.
20813 */
20814 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020815make_expanded_name(
20816 char_u *in_start,
20817 char_u *expr_start,
20818 char_u *expr_end,
20819 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020820{
20821 char_u c1;
20822 char_u *retval = NULL;
20823 char_u *temp_result;
20824 char_u *nextcmd = NULL;
20825
20826 if (expr_end == NULL || in_end == NULL)
20827 return NULL;
20828 *expr_start = NUL;
20829 *expr_end = NUL;
20830 c1 = *in_end;
20831 *in_end = NUL;
20832
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020833 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020834 if (temp_result != NULL && nextcmd == NULL)
20835 {
20836 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20837 + (in_end - expr_end) + 1));
20838 if (retval != NULL)
20839 {
20840 STRCPY(retval, in_start);
20841 STRCAT(retval, temp_result);
20842 STRCAT(retval, expr_end + 1);
20843 }
20844 }
20845 vim_free(temp_result);
20846
20847 *in_end = c1; /* put char back for error messages */
20848 *expr_start = '{';
20849 *expr_end = '}';
20850
20851 if (retval != NULL)
20852 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020853 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020854 if (expr_start != NULL)
20855 {
20856 /* Further expansion! */
20857 temp_result = make_expanded_name(retval, expr_start,
20858 expr_end, temp_result);
20859 vim_free(retval);
20860 retval = temp_result;
20861 }
20862 }
20863
20864 return retval;
20865}
20866
20867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020869 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 */
20871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020872eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020874 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20875}
20876
20877/*
20878 * Return TRUE if character "c" can be used as the first character in a
20879 * variable or function name (excluding '{' and '}').
20880 */
20881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020882eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020883{
20884 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885}
20886
20887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 * Set number v: variable to "val".
20889 */
20890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020891set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020893 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894}
20895
20896/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020897 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 */
20899 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020900get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020902 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903}
20904
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020905/*
20906 * Get string v: variable value. Uses a static buffer, can only be used once.
20907 */
20908 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020909get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020910{
20911 return get_tv_string(&vimvars[idx].vv_tv);
20912}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020913
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020915 * Get List v: variable value. Caller must take care of reference count when
20916 * needed.
20917 */
20918 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020919get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020920{
20921 return vimvars[idx].vv_list;
20922}
20923
20924/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020925 * Set v:char to character "c".
20926 */
20927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020928set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020929{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020930 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020931
20932#ifdef FEAT_MBYTE
20933 if (has_mbyte)
20934 buf[(*mb_char2bytes)(c, buf)] = NUL;
20935 else
20936#endif
20937 {
20938 buf[0] = c;
20939 buf[1] = NUL;
20940 }
20941 set_vim_var_string(VV_CHAR, buf, -1);
20942}
20943
20944/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020945 * Set v:count to "count" and v:count1 to "count1".
20946 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 */
20948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020949set_vcount(
20950 long count,
20951 long count1,
20952 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020954 if (set_prevcount)
20955 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020956 vimvars[VV_COUNT].vv_nr = count;
20957 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958}
20959
20960/*
20961 * Set string v: variable to a copy of "val".
20962 */
20963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020964set_vim_var_string(
20965 int idx,
20966 char_u *val,
20967 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968{
Bram Moolenaara542c682016-01-31 16:28:04 +010020969 clear_tv(&vimvars[idx].vv_di.di_tv);
20970 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020972 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020974 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020976 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977}
20978
20979/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020980 * Set List v: variable to "val".
20981 */
20982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020983set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020984{
Bram Moolenaara542c682016-01-31 16:28:04 +010020985 clear_tv(&vimvars[idx].vv_di.di_tv);
20986 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020987 vimvars[idx].vv_list = val;
20988 if (val != NULL)
20989 ++val->lv_refcount;
20990}
20991
20992/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020993 * Set Dictionary v: variable to "val".
20994 */
20995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020996set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020997{
20998 int todo;
20999 hashitem_T *hi;
21000
Bram Moolenaara542c682016-01-31 16:28:04 +010021001 clear_tv(&vimvars[idx].vv_di.di_tv);
21002 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021003 vimvars[idx].vv_dict = val;
21004 if (val != NULL)
21005 {
21006 ++val->dv_refcount;
21007
21008 /* Set readonly */
21009 todo = (int)val->dv_hashtab.ht_used;
21010 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21011 {
21012 if (HASHITEM_EMPTY(hi))
21013 continue;
21014 --todo;
21015 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21016 }
21017 }
21018}
21019
21020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 * Set v:register if needed.
21022 */
21023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021024set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025{
21026 char_u regname;
21027
21028 if (c == 0 || c == ' ')
21029 regname = '"';
21030 else
21031 regname = c;
21032 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021033 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 set_vim_var_string(VV_REG, &regname, 1);
21035}
21036
21037/*
21038 * Get or set v:exception. If "oldval" == NULL, return the current value.
21039 * Otherwise, restore the value to "oldval" and return NULL.
21040 * Must always be called in pairs to save and restore v:exception! Does not
21041 * take care of memory allocations.
21042 */
21043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021044v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045{
21046 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021047 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048
Bram Moolenaare9a41262005-01-15 22:18:47 +000021049 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 return NULL;
21051}
21052
21053/*
21054 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21055 * Otherwise, restore the value to "oldval" and return NULL.
21056 * Must always be called in pairs to save and restore v:throwpoint! Does not
21057 * take care of memory allocations.
21058 */
21059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021060v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061{
21062 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021063 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064
Bram Moolenaare9a41262005-01-15 22:18:47 +000021065 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 return NULL;
21067}
21068
21069#if defined(FEAT_AUTOCMD) || defined(PROTO)
21070/*
21071 * Set v:cmdarg.
21072 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21073 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21074 * Must always be called in pairs!
21075 */
21076 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021077set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078{
21079 char_u *oldval;
21080 char_u *newval;
21081 unsigned len;
21082
Bram Moolenaare9a41262005-01-15 22:18:47 +000021083 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021084 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021086 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021087 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021088 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 }
21090
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021091 if (eap->force_bin == FORCE_BIN)
21092 len = 6;
21093 else if (eap->force_bin == FORCE_NOBIN)
21094 len = 8;
21095 else
21096 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021097
21098 if (eap->read_edit)
21099 len += 7;
21100
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021101 if (eap->force_ff != 0)
21102 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21103# ifdef FEAT_MBYTE
21104 if (eap->force_enc != 0)
21105 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021106 if (eap->bad_char != 0)
21107 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021108# endif
21109
21110 newval = alloc(len + 1);
21111 if (newval == NULL)
21112 return NULL;
21113
21114 if (eap->force_bin == FORCE_BIN)
21115 sprintf((char *)newval, " ++bin");
21116 else if (eap->force_bin == FORCE_NOBIN)
21117 sprintf((char *)newval, " ++nobin");
21118 else
21119 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021120
21121 if (eap->read_edit)
21122 STRCAT(newval, " ++edit");
21123
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021124 if (eap->force_ff != 0)
21125 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21126 eap->cmd + eap->force_ff);
21127# ifdef FEAT_MBYTE
21128 if (eap->force_enc != 0)
21129 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21130 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021131 if (eap->bad_char == BAD_KEEP)
21132 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21133 else if (eap->bad_char == BAD_DROP)
21134 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21135 else if (eap->bad_char != 0)
21136 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021137# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021138 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021139 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140}
21141#endif
21142
21143/*
21144 * Get the value of internal variable "name".
21145 * Return OK or FAIL.
21146 */
21147 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021148get_var_tv(
21149 char_u *name,
21150 int len, /* length of "name" */
21151 typval_T *rettv, /* NULL when only checking existence */
21152 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21153 int verbose, /* may give error message */
21154 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155{
21156 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021157 typval_T *tv = NULL;
21158 typval_T atv;
21159 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161
21162 /* truncate the name, so that we can use strcmp() */
21163 cc = name[len];
21164 name[len] = NUL;
21165
21166 /*
21167 * Check for "b:changedtick".
21168 */
21169 if (STRCMP(name, "b:changedtick") == 0)
21170 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021171 atv.v_type = VAR_NUMBER;
21172 atv.vval.v_number = curbuf->b_changedtick;
21173 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 }
21175
21176 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 * Check for user-defined variables.
21178 */
21179 else
21180 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021181 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021183 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021184 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021185 if (dip != NULL)
21186 *dip = v;
21187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 }
21189
Bram Moolenaare9a41262005-01-15 22:18:47 +000021190 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021192 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021193 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 ret = FAIL;
21195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021196 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021197 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198
21199 name[len] = cc;
21200
21201 return ret;
21202}
21203
21204/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021205 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21206 * Also handle function call with Funcref variable: func(expr)
21207 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21208 */
21209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210handle_subscript(
21211 char_u **arg,
21212 typval_T *rettv,
21213 int evaluate, /* do more than finding the end */
21214 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021215{
21216 int ret = OK;
21217 dict_T *selfdict = NULL;
21218 char_u *s;
21219 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021220 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021221
21222 while (ret == OK
21223 && (**arg == '['
21224 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021225 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021226 && !vim_iswhite(*(*arg - 1)))
21227 {
21228 if (**arg == '(')
21229 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021230 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021231 if (evaluate)
21232 {
21233 functv = *rettv;
21234 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021235
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021236 /* Invoke the function. Recursive! */
21237 s = functv.vval.v_string;
21238 }
21239 else
21240 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021241 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021242 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21243 &len, evaluate, selfdict);
21244
21245 /* Clear the funcref afterwards, so that deleting it while
21246 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021247 if (evaluate)
21248 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021249
21250 /* Stop the expression evaluation when immediately aborting on
21251 * error, or when an interrupt occurred or an exception was thrown
21252 * but not caught. */
21253 if (aborting())
21254 {
21255 if (ret == OK)
21256 clear_tv(rettv);
21257 ret = FAIL;
21258 }
21259 dict_unref(selfdict);
21260 selfdict = NULL;
21261 }
21262 else /* **arg == '[' || **arg == '.' */
21263 {
21264 dict_unref(selfdict);
21265 if (rettv->v_type == VAR_DICT)
21266 {
21267 selfdict = rettv->vval.v_dict;
21268 if (selfdict != NULL)
21269 ++selfdict->dv_refcount;
21270 }
21271 else
21272 selfdict = NULL;
21273 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21274 {
21275 clear_tv(rettv);
21276 ret = FAIL;
21277 }
21278 }
21279 }
21280 dict_unref(selfdict);
21281 return ret;
21282}
21283
21284/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021285 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021286 * value).
21287 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021288 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021289alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021290{
Bram Moolenaar33570922005-01-25 22:26:29 +000021291 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021292}
21293
21294/*
21295 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 * The string "s" must have been allocated, it is consumed.
21297 * Return NULL for out of memory, the variable otherwise.
21298 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021299 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301{
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021304 rettv = alloc_tv();
21305 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021307 rettv->v_type = VAR_STRING;
21308 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 }
21310 else
21311 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021312 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313}
21314
21315/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021316 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021318 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021319free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320{
21321 if (varp != NULL)
21322 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021323 switch (varp->v_type)
21324 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021325 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021326 func_unref(varp->vval.v_string);
21327 /*FALLTHROUGH*/
21328 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021329 vim_free(varp->vval.v_string);
21330 break;
21331 case VAR_LIST:
21332 list_unref(varp->vval.v_list);
21333 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021334 case VAR_DICT:
21335 dict_unref(varp->vval.v_dict);
21336 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021337 case VAR_JOB:
21338#ifdef FEAT_JOB
21339 job_unref(varp->vval.v_job);
21340 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021341#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021342 case VAR_NUMBER:
21343 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021344 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021345 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021346 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 vim_free(varp);
21349 }
21350}
21351
21352/*
21353 * Free the memory for a variable value and set the value to NULL or 0.
21354 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021356clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357{
21358 if (varp != NULL)
21359 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021360 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021362 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021363 func_unref(varp->vval.v_string);
21364 /*FALLTHROUGH*/
21365 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021366 vim_free(varp->vval.v_string);
21367 varp->vval.v_string = NULL;
21368 break;
21369 case VAR_LIST:
21370 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021371 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021372 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021373 case VAR_DICT:
21374 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021375 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021376 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021378 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021379 varp->vval.v_number = 0;
21380 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021381 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021382#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021383 varp->vval.v_float = 0.0;
21384 break;
21385#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021386 case VAR_JOB:
21387#ifdef FEAT_JOB
21388 job_unref(varp->vval.v_job);
21389 varp->vval.v_job = NULL;
21390#endif
21391 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392 case VAR_UNKNOWN:
21393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021395 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 }
21397}
21398
21399/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021400 * Set the value of a variable to NULL without freeing items.
21401 */
21402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021403init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404{
21405 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021406 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021407}
21408
21409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 * Get the number value of a variable.
21411 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021412 * For incompatible types, return 0.
21413 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21414 * caller of incompatible types: it sets *denote to TRUE if "denote"
21415 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 */
21417 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021418get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021420 int error = FALSE;
21421
21422 return get_tv_number_chk(varp, &error); /* return 0L on error */
21423}
21424
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021425 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021426get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021427{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021428 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021432 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021433 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021434 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021435#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021436 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021437 break;
21438#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021439 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021440 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021441 break;
21442 case VAR_STRING:
21443 if (varp->vval.v_string != NULL)
21444 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021445 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021446 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021447 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021448 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021449 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021450 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021451 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021452 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021453 case VAR_SPECIAL:
21454 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21455 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021456 case VAR_JOB:
21457#ifdef FEAT_JOB
21458 EMSG(_("E910: Using a Job as a Number"));
21459 break;
21460#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021461 case VAR_UNKNOWN:
21462 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021465 if (denote == NULL) /* useful for values that must be unsigned */
21466 n = -1;
21467 else
21468 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021469 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470}
21471
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021472#ifdef FEAT_FLOAT
21473 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021474get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021475{
21476 switch (varp->v_type)
21477 {
21478 case VAR_NUMBER:
21479 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021480 case VAR_FLOAT:
21481 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021482 case VAR_FUNC:
21483 EMSG(_("E891: Using a Funcref as a Float"));
21484 break;
21485 case VAR_STRING:
21486 EMSG(_("E892: Using a String as a Float"));
21487 break;
21488 case VAR_LIST:
21489 EMSG(_("E893: Using a List as a Float"));
21490 break;
21491 case VAR_DICT:
21492 EMSG(_("E894: Using a Dictionary as a Float"));
21493 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021494 case VAR_SPECIAL:
21495 EMSG(_("E907: Using a special value as a Float"));
21496 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021497 case VAR_JOB:
21498# ifdef FEAT_JOB
21499 EMSG(_("E911: Using a Job as a Float"));
21500 break;
21501# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021502 case VAR_UNKNOWN:
21503 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021504 break;
21505 }
21506 return 0;
21507}
21508#endif
21509
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021511 * Get the lnum from the first argument.
21512 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021513 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 */
21515 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021516get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517{
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 linenr_T lnum;
21520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021521 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 if (lnum == 0) /* no valid number, try using line() */
21523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021524 rettv.v_type = VAR_NUMBER;
21525 f_line(argvars, &rettv);
21526 lnum = rettv.vval.v_number;
21527 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 }
21529 return lnum;
21530}
21531
21532/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021533 * Get the lnum from the first argument.
21534 * Also accepts "$", then "buf" is used.
21535 * Returns 0 on error.
21536 */
21537 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021538get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021539{
21540 if (argvars[0].v_type == VAR_STRING
21541 && argvars[0].vval.v_string != NULL
21542 && argvars[0].vval.v_string[0] == '$'
21543 && buf != NULL)
21544 return buf->b_ml.ml_line_count;
21545 return get_tv_number_chk(&argvars[0], NULL);
21546}
21547
21548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 * Get the string value of a variable.
21550 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021551 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21552 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 * If the String variable has never been set, return an empty string.
21554 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021555 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21556 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 */
21558 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021559get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560{
21561 static char_u mybuf[NUMBUFLEN];
21562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021563 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564}
21565
21566 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021567get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021569 char_u *res = get_tv_string_buf_chk(varp, buf);
21570
21571 return res != NULL ? res : (char_u *)"";
21572}
21573
Bram Moolenaar7d647822014-04-05 21:28:56 +020021574/*
21575 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21576 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021577 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021578get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021579{
21580 static char_u mybuf[NUMBUFLEN];
21581
21582 return get_tv_string_buf_chk(varp, mybuf);
21583}
21584
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021585 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021586get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021587{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021588 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 case VAR_NUMBER:
21591 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21592 return buf;
21593 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021594 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021595 break;
21596 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021597 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021598 break;
21599 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021600 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021601 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021602 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021603#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021604 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021605 break;
21606#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607 case VAR_STRING:
21608 if (varp->vval.v_string != NULL)
21609 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021610 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021611 case VAR_SPECIAL:
21612 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21613 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021614 case VAR_JOB:
21615#ifdef FEAT_JOB
21616 {
21617 job_T *job = varp->vval.v_job;
21618 char *status = job->jv_status == JOB_FAILED ? "fail"
21619 : job->jv_status == JOB_ENDED ? "dead"
21620 : "run";
21621# ifdef UNIX
21622 vim_snprintf((char *)buf, NUMBUFLEN,
21623 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021624# elif defined(WIN32)
21625 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar6119e612016-02-11 12:48:36 +010021626 "process %ld %s", (long)job->jv_pi.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021627 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021628# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021629 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021630 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21631# endif
21632 return buf;
21633 }
21634#endif
21635 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021636 case VAR_UNKNOWN:
21637 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021640 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641}
21642
21643/*
21644 * Find variable "name" in the list of variables.
21645 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021646 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021647 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021648 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021650 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021651find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021654 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655
Bram Moolenaara7043832005-01-21 11:56:39 +000021656 ht = find_var_ht(name, &varname);
21657 if (htp != NULL)
21658 *htp = ht;
21659 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021661 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662}
21663
21664/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021665 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021666 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021668 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021669find_var_in_ht(
21670 hashtab_T *ht,
21671 int htname,
21672 char_u *varname,
21673 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021674{
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 hashitem_T *hi;
21676
21677 if (*varname == NUL)
21678 {
21679 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021680 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021681 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021682 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 case 'g': return &globvars_var;
21684 case 'v': return &vimvars_var;
21685 case 'b': return &curbuf->b_bufvar;
21686 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021687#ifdef FEAT_WINDOWS
21688 case 't': return &curtab->tp_winvar;
21689#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021690 case 'l': return current_funccal == NULL
21691 ? NULL : &current_funccal->l_vars_var;
21692 case 'a': return current_funccal == NULL
21693 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021694 }
21695 return NULL;
21696 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021697
21698 hi = hash_find(ht, varname);
21699 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021700 {
21701 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021702 * worked find the variable again. Don't auto-load a script if it was
21703 * loaded already, otherwise it would be loaded every time when
21704 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021705 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021706 {
21707 /* Note: script_autoload() may make "hi" invalid. It must either
21708 * be obtained again or not used. */
21709 if (!script_autoload(varname, FALSE) || aborting())
21710 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021711 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021712 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021713 if (HASHITEM_EMPTY(hi))
21714 return NULL;
21715 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021717}
21718
21719/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021721 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021722 * Set "varname" to the start of name without ':'.
21723 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021724 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021725find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021727 hashitem_T *hi;
21728
Bram Moolenaar73627d02015-08-11 15:46:09 +020021729 if (name[0] == NUL)
21730 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 if (name[1] != ':')
21732 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021733 /* The name must not start with a colon or #. */
21734 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 return NULL;
21736 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021737
21738 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021739 hi = hash_find(&compat_hashtab, name);
21740 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021741 return &compat_hashtab;
21742
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021744 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021745 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 }
21747 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021748 if (*name == 'g') /* global variable */
21749 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021750 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21751 */
21752 if (vim_strchr(name + 2, ':') != NULL
21753 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021754 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021756 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021758 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021759#ifdef FEAT_WINDOWS
21760 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021761 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021762#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021763 if (*name == 'v') /* v: variable */
21764 return &vimvarht;
21765 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021766 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021768 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 if (*name == 's' /* script variable */
21770 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21771 return &SCRIPT_VARS(current_SID);
21772 return NULL;
21773}
21774
21775/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021776 * Get function call environment based on bactrace debug level
21777 */
21778 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021779get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021780{
21781 int i;
21782 funccall_T *funccal;
21783 funccall_T *temp_funccal;
21784
21785 funccal = current_funccal;
21786 if (debug_backtrace_level > 0)
21787 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021788 for (i = 0; i < debug_backtrace_level; i++)
21789 {
21790 temp_funccal = funccal->caller;
21791 if (temp_funccal)
21792 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021793 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021794 /* backtrace level overflow. reset to max */
21795 debug_backtrace_level = i;
21796 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021797 }
21798 return funccal;
21799}
21800
21801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021803 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 * Returns NULL when it doesn't exist.
21805 */
21806 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021807get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808{
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021811 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 if (v == NULL)
21813 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021814 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815}
21816
21817/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021818 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 * sourcing this script and when executing functions defined in the script.
21820 */
21821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021822new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823{
Bram Moolenaara7043832005-01-21 11:56:39 +000021824 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 hashtab_T *ht;
21826 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021827
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21829 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021830 /* Re-allocating ga_data means that an ht_array pointing to
21831 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021833 for (i = 1; i <= ga_scripts.ga_len; ++i)
21834 {
21835 ht = &SCRIPT_VARS(i);
21836 if (ht->ht_mask == HT_INIT_SIZE - 1)
21837 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021838 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021840 }
21841
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 while (ga_scripts.ga_len < id)
21843 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021844 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021845 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021846 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 }
21849 }
21850}
21851
21852/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021853 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21854 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 */
21856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021857init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858{
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021860 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021861 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021862 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021863 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 dict_var->di_tv.vval.v_dict = dict;
21865 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021866 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21868 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869}
21870
21871/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021872 * Unreference a dictionary initialized by init_var_dict().
21873 */
21874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021875unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021876{
21877 /* Now the dict needs to be freed if no one else is using it, go back to
21878 * normal reference counting. */
21879 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21880 dict_unref(dict);
21881}
21882
21883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 * Frees all allocated variables and the value they contain.
21886 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 */
21888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021889vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021890{
21891 vars_clear_ext(ht, TRUE);
21892}
21893
21894/*
21895 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21896 */
21897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021898vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899{
Bram Moolenaara7043832005-01-21 11:56:39 +000021900 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 hashitem_T *hi;
21902 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903
Bram Moolenaar33570922005-01-25 22:26:29 +000021904 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021905 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021906 for (hi = ht->ht_array; todo > 0; ++hi)
21907 {
21908 if (!HASHITEM_EMPTY(hi))
21909 {
21910 --todo;
21911
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021913 * ht_array might change then. hash_clear() takes care of it
21914 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 v = HI2DI(hi);
21916 if (free_val)
21917 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021918 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021919 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021920 }
21921 }
21922 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021923 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924}
21925
Bram Moolenaara7043832005-01-21 11:56:39 +000021926/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 * Delete a variable from hashtab "ht" at item "hi".
21928 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021931delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932{
Bram Moolenaar33570922005-01-25 22:26:29 +000021933 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021934
21935 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021936 clear_tv(&di->di_tv);
21937 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
21941 * List the value of one internal variable.
21942 */
21943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021944list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021946 char_u *tofree;
21947 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021948 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021949
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021950 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021952 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021953 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954}
21955
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021957list_one_var_a(
21958 char_u *prefix,
21959 char_u *name,
21960 int type,
21961 char_u *string,
21962 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963{
Bram Moolenaar31859182007-08-14 20:41:13 +000021964 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21965 msg_start();
21966 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 if (name != NULL) /* "a:" vars don't have a name stored */
21968 msg_puts(name);
21969 msg_putchar(' ');
21970 msg_advance(22);
21971 if (type == VAR_NUMBER)
21972 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021973 else if (type == VAR_FUNC)
21974 msg_putchar('*');
21975 else if (type == VAR_LIST)
21976 {
21977 msg_putchar('[');
21978 if (*string == '[')
21979 ++string;
21980 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021981 else if (type == VAR_DICT)
21982 {
21983 msg_putchar('{');
21984 if (*string == '{')
21985 ++string;
21986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 else
21988 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021989
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021991
21992 if (type == VAR_FUNC)
21993 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021994 if (*first)
21995 {
21996 msg_clr_eos();
21997 *first = FALSE;
21998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999}
22000
22001/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022002 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 * If the variable already exists, the value is updated.
22004 * Otherwise the variable is created.
22005 */
22006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022007set_var(
22008 char_u *name,
22009 typval_T *tv,
22010 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011{
Bram Moolenaar33570922005-01-25 22:26:29 +000022012 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022014 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022016 ht = find_var_ht(name, &varname);
22017 if (ht == NULL || *varname == NUL)
22018 {
22019 EMSG2(_(e_illvar), name);
22020 return;
22021 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022022 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022023
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022024 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22025 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022026
Bram Moolenaar33570922005-01-25 22:26:29 +000022027 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022029 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022030 if (var_check_ro(v->di_flags, name, FALSE)
22031 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022032 return;
22033 if (v->di_tv.v_type != tv->v_type
22034 && !((v->di_tv.v_type == VAR_STRING
22035 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022036 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022037 || tv->v_type == VAR_NUMBER))
22038#ifdef FEAT_FLOAT
22039 && !((v->di_tv.v_type == VAR_NUMBER
22040 || v->di_tv.v_type == VAR_FLOAT)
22041 && (tv->v_type == VAR_NUMBER
22042 || tv->v_type == VAR_FLOAT))
22043#endif
22044 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022045 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022046 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022047 return;
22048 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022049
22050 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022051 * Handle setting internal v: variables separately where needed to
22052 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022053 */
22054 if (ht == &vimvarht)
22055 {
22056 if (v->di_tv.v_type == VAR_STRING)
22057 {
22058 vim_free(v->di_tv.vval.v_string);
22059 if (copy || tv->v_type != VAR_STRING)
22060 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22061 else
22062 {
22063 /* Take over the string to avoid an extra alloc/free. */
22064 v->di_tv.vval.v_string = tv->vval.v_string;
22065 tv->vval.v_string = NULL;
22066 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022067 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022068 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022069 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022070 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022071 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022072 if (STRCMP(varname, "searchforward") == 0)
22073 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022074#ifdef FEAT_SEARCH_EXTRA
22075 else if (STRCMP(varname, "hlsearch") == 0)
22076 {
22077 no_hlsearch = !v->di_tv.vval.v_number;
22078 redraw_all_later(SOME_VALID);
22079 }
22080#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022081 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022082 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022083 else if (v->di_tv.v_type != tv->v_type)
22084 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022085 }
22086
22087 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 }
22089 else /* add a new variable */
22090 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022091 /* Can't add "v:" variable. */
22092 if (ht == &vimvarht)
22093 {
22094 EMSG2(_(e_illvar), name);
22095 return;
22096 }
22097
Bram Moolenaar92124a32005-06-17 22:03:40 +000022098 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022099 if (!valid_varname(varname))
22100 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022101
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022102 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22103 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022104 if (v == NULL)
22105 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022106 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022107 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022109 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 return;
22111 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022112 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022114
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022115 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022116 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022117 else
22118 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022120 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022121 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123}
22124
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022125/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022126 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022127 * Also give an error message.
22128 */
22129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022130var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022131{
22132 if (flags & DI_FLAGS_RO)
22133 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022134 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022135 return TRUE;
22136 }
22137 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22138 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022139 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022140 return TRUE;
22141 }
22142 return FALSE;
22143}
22144
22145/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022146 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22147 * Also give an error message.
22148 */
22149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022150var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022151{
22152 if (flags & DI_FLAGS_FIX)
22153 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022154 EMSG2(_("E795: Cannot delete variable %s"),
22155 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022156 return TRUE;
22157 }
22158 return FALSE;
22159}
22160
22161/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022162 * Check if a funcref is assigned to a valid variable name.
22163 * Return TRUE and give an error if not.
22164 */
22165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022166var_check_func_name(
22167 char_u *name, /* points to start of variable name */
22168 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022169{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022170 /* Allow for w: b: s: and t:. */
22171 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022172 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22173 ? name[2] : name[0]))
22174 {
22175 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22176 name);
22177 return TRUE;
22178 }
22179 /* Don't allow hiding a function. When "v" is not NULL we might be
22180 * assigning another function to the same var, the type is checked
22181 * below. */
22182 if (new_var && function_exists(name))
22183 {
22184 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22185 name);
22186 return TRUE;
22187 }
22188 return FALSE;
22189}
22190
22191/*
22192 * Check if a variable name is valid.
22193 * Return FALSE and give an error if not.
22194 */
22195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022196valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022197{
22198 char_u *p;
22199
22200 for (p = varname; *p != NUL; ++p)
22201 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22202 && *p != AUTOLOAD_CHAR)
22203 {
22204 EMSG2(_(e_illvar), varname);
22205 return FALSE;
22206 }
22207 return TRUE;
22208}
22209
22210/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022211 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022212 * Also give an error message, using "name" or _("name") when use_gettext is
22213 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022214 */
22215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022216tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022217{
22218 if (lock & VAR_LOCKED)
22219 {
22220 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022221 name == NULL ? (char_u *)_("Unknown")
22222 : use_gettext ? (char_u *)_(name)
22223 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022224 return TRUE;
22225 }
22226 if (lock & VAR_FIXED)
22227 {
22228 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022229 name == NULL ? (char_u *)_("Unknown")
22230 : use_gettext ? (char_u *)_(name)
22231 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022232 return TRUE;
22233 }
22234 return FALSE;
22235}
22236
22237/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022239 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022240 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022241 * It is OK for "from" and "to" to point to the same item. This is used to
22242 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022243 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022245copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022247 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022248 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022249 switch (from->v_type)
22250 {
22251 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022252 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022253 to->vval.v_number = from->vval.v_number;
22254 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022255 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022256#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022257 to->vval.v_float = from->vval.v_float;
22258 break;
22259#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022260 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022261#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022262 to->vval.v_job = from->vval.v_job;
22263 ++to->vval.v_job->jv_refcount;
22264 break;
22265#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022266 case VAR_STRING:
22267 case VAR_FUNC:
22268 if (from->vval.v_string == NULL)
22269 to->vval.v_string = NULL;
22270 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022271 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022272 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022273 if (from->v_type == VAR_FUNC)
22274 func_ref(to->vval.v_string);
22275 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022276 break;
22277 case VAR_LIST:
22278 if (from->vval.v_list == NULL)
22279 to->vval.v_list = NULL;
22280 else
22281 {
22282 to->vval.v_list = from->vval.v_list;
22283 ++to->vval.v_list->lv_refcount;
22284 }
22285 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022286 case VAR_DICT:
22287 if (from->vval.v_dict == NULL)
22288 to->vval.v_dict = NULL;
22289 else
22290 {
22291 to->vval.v_dict = from->vval.v_dict;
22292 ++to->vval.v_dict->dv_refcount;
22293 }
22294 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022295 case VAR_UNKNOWN:
22296 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022297 break;
22298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299}
22300
22301/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022302 * Make a copy of an item.
22303 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022304 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22305 * reference to an already copied list/dict can be used.
22306 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022307 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022309item_copy(
22310 typval_T *from,
22311 typval_T *to,
22312 int deep,
22313 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022314{
22315 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022316 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022317
Bram Moolenaar33570922005-01-25 22:26:29 +000022318 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022319 {
22320 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022321 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022322 }
22323 ++recurse;
22324
22325 switch (from->v_type)
22326 {
22327 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022328 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022329 case VAR_STRING:
22330 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022331 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022332 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022333 copy_tv(from, to);
22334 break;
22335 case VAR_LIST:
22336 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022337 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022338 if (from->vval.v_list == NULL)
22339 to->vval.v_list = NULL;
22340 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22341 {
22342 /* use the copy made earlier */
22343 to->vval.v_list = from->vval.v_list->lv_copylist;
22344 ++to->vval.v_list->lv_refcount;
22345 }
22346 else
22347 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22348 if (to->vval.v_list == NULL)
22349 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022350 break;
22351 case VAR_DICT:
22352 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022353 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022354 if (from->vval.v_dict == NULL)
22355 to->vval.v_dict = NULL;
22356 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22357 {
22358 /* use the copy made earlier */
22359 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22360 ++to->vval.v_dict->dv_refcount;
22361 }
22362 else
22363 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22364 if (to->vval.v_dict == NULL)
22365 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022366 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022367 case VAR_UNKNOWN:
22368 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022369 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022370 }
22371 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022372 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022373}
22374
22375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 * ":echo expr1 ..." print each argument separated with a space, add a
22377 * newline at the end.
22378 * ":echon expr1 ..." print each argument plain.
22379 */
22380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022381ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382{
22383 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022384 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022385 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 char_u *p;
22387 int needclr = TRUE;
22388 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022389 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390
22391 if (eap->skip)
22392 ++emsg_skip;
22393 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22394 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022395 /* If eval1() causes an error message the text from the command may
22396 * still need to be cleared. E.g., "echo 22,44". */
22397 need_clr_eos = needclr;
22398
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022400 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 {
22402 /*
22403 * Report the invalid expression unless the expression evaluation
22404 * has been cancelled due to an aborting error, an interrupt, or an
22405 * exception.
22406 */
22407 if (!aborting())
22408 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022409 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 break;
22411 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022412 need_clr_eos = FALSE;
22413
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 if (!eap->skip)
22415 {
22416 if (atstart)
22417 {
22418 atstart = FALSE;
22419 /* Call msg_start() after eval1(), evaluating the expression
22420 * may cause a message to appear. */
22421 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022422 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022423 /* Mark the saved text as finishing the line, so that what
22424 * follows is displayed on a new line when scrolling back
22425 * at the more prompt. */
22426 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 }
22430 else if (eap->cmdidx == CMD_echo)
22431 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022432 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022433 if (p != NULL)
22434 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022436 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022438 if (*p != TAB && needclr)
22439 {
22440 /* remove any text still there from the command */
22441 msg_clr_eos();
22442 needclr = FALSE;
22443 }
22444 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 }
22446 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022447 {
22448#ifdef FEAT_MBYTE
22449 if (has_mbyte)
22450 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022451 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022452
22453 (void)msg_outtrans_len_attr(p, i, echo_attr);
22454 p += i - 1;
22455 }
22456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022458 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022461 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022463 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 arg = skipwhite(arg);
22465 }
22466 eap->nextcmd = check_nextcmd(arg);
22467
22468 if (eap->skip)
22469 --emsg_skip;
22470 else
22471 {
22472 /* remove text that may still be there from the command */
22473 if (needclr)
22474 msg_clr_eos();
22475 if (eap->cmdidx == CMD_echo)
22476 msg_end();
22477 }
22478}
22479
22480/*
22481 * ":echohl {name}".
22482 */
22483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022484ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485{
22486 int id;
22487
22488 id = syn_name2id(eap->arg);
22489 if (id == 0)
22490 echo_attr = 0;
22491 else
22492 echo_attr = syn_id2attr(id);
22493}
22494
22495/*
22496 * ":execute expr1 ..." execute the result of an expression.
22497 * ":echomsg expr1 ..." Print a message
22498 * ":echoerr expr1 ..." Print an error
22499 * Each gets spaces around each argument and a newline at the end for
22500 * echo commands
22501 */
22502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022503ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504{
22505 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 int ret = OK;
22508 char_u *p;
22509 garray_T ga;
22510 int len;
22511 int save_did_emsg;
22512
22513 ga_init2(&ga, 1, 80);
22514
22515 if (eap->skip)
22516 ++emsg_skip;
22517 while (*arg != NUL && *arg != '|' && *arg != '\n')
22518 {
22519 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022520 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 {
22522 /*
22523 * Report the invalid expression unless the expression evaluation
22524 * has been cancelled due to an aborting error, an interrupt, or an
22525 * exception.
22526 */
22527 if (!aborting())
22528 EMSG2(_(e_invexpr2), p);
22529 ret = FAIL;
22530 break;
22531 }
22532
22533 if (!eap->skip)
22534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022535 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 len = (int)STRLEN(p);
22537 if (ga_grow(&ga, len + 2) == FAIL)
22538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022539 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 ret = FAIL;
22541 break;
22542 }
22543 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 ga.ga_len += len;
22547 }
22548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022549 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550 arg = skipwhite(arg);
22551 }
22552
22553 if (ret != FAIL && ga.ga_data != NULL)
22554 {
22555 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022558 out_flush();
22559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 else if (eap->cmdidx == CMD_echoerr)
22561 {
22562 /* We don't want to abort following commands, restore did_emsg. */
22563 save_did_emsg = did_emsg;
22564 EMSG((char_u *)ga.ga_data);
22565 if (!force_abort)
22566 did_emsg = save_did_emsg;
22567 }
22568 else if (eap->cmdidx == CMD_execute)
22569 do_cmdline((char_u *)ga.ga_data,
22570 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22571 }
22572
22573 ga_clear(&ga);
22574
22575 if (eap->skip)
22576 --emsg_skip;
22577
22578 eap->nextcmd = check_nextcmd(arg);
22579}
22580
22581/*
22582 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22583 * "arg" points to the "&" or '+' when called, to "option" when returning.
22584 * Returns NULL when no option name found. Otherwise pointer to the char
22585 * after the option name.
22586 */
22587 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022588find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589{
22590 char_u *p = *arg;
22591
22592 ++p;
22593 if (*p == 'g' && p[1] == ':')
22594 {
22595 *opt_flags = OPT_GLOBAL;
22596 p += 2;
22597 }
22598 else if (*p == 'l' && p[1] == ':')
22599 {
22600 *opt_flags = OPT_LOCAL;
22601 p += 2;
22602 }
22603 else
22604 *opt_flags = 0;
22605
22606 if (!ASCII_ISALPHA(*p))
22607 return NULL;
22608 *arg = p;
22609
22610 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22611 p += 4; /* termcap option */
22612 else
22613 while (ASCII_ISALPHA(*p))
22614 ++p;
22615 return p;
22616}
22617
22618/*
22619 * ":function"
22620 */
22621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022622ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623{
22624 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022625 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 int j;
22627 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022629 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 char_u *name = NULL;
22631 char_u *p;
22632 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022633 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 garray_T newargs;
22635 garray_T newlines;
22636 int varargs = FALSE;
22637 int mustend = FALSE;
22638 int flags = 0;
22639 ufunc_T *fp;
22640 int indent;
22641 int nesting;
22642 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022643 dictitem_T *v;
22644 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022645 static int func_nr = 0; /* number for nameless function */
22646 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022647 hashtab_T *ht;
22648 int todo;
22649 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022650 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651
22652 /*
22653 * ":function" without argument: list functions.
22654 */
22655 if (ends_excmd(*eap->arg))
22656 {
22657 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022658 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022659 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022660 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022661 {
22662 if (!HASHITEM_EMPTY(hi))
22663 {
22664 --todo;
22665 fp = HI2UF(hi);
22666 if (!isdigit(*fp->uf_name))
22667 list_func_head(fp, FALSE);
22668 }
22669 }
22670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 eap->nextcmd = check_nextcmd(eap->arg);
22672 return;
22673 }
22674
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022675 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022676 * ":function /pat": list functions matching pattern.
22677 */
22678 if (*eap->arg == '/')
22679 {
22680 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22681 if (!eap->skip)
22682 {
22683 regmatch_T regmatch;
22684
22685 c = *p;
22686 *p = NUL;
22687 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22688 *p = c;
22689 if (regmatch.regprog != NULL)
22690 {
22691 regmatch.rm_ic = p_ic;
22692
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022693 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022694 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22695 {
22696 if (!HASHITEM_EMPTY(hi))
22697 {
22698 --todo;
22699 fp = HI2UF(hi);
22700 if (!isdigit(*fp->uf_name)
22701 && vim_regexec(&regmatch, fp->uf_name, 0))
22702 list_func_head(fp, FALSE);
22703 }
22704 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022705 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022706 }
22707 }
22708 if (*p == '/')
22709 ++p;
22710 eap->nextcmd = check_nextcmd(p);
22711 return;
22712 }
22713
22714 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022715 * Get the function name. There are these situations:
22716 * func normal function name
22717 * "name" == func, "fudi.fd_dict" == NULL
22718 * dict.func new dictionary entry
22719 * "name" == NULL, "fudi.fd_dict" set,
22720 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22721 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022722 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022723 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22724 * dict.func existing dict entry that's not a Funcref
22725 * "name" == NULL, "fudi.fd_dict" set,
22726 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022727 * s:func script-local function name
22728 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022731 name = trans_function_name(&p, eap->skip, 0, &fudi);
22732 paren = (vim_strchr(p, '(') != NULL);
22733 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 {
22735 /*
22736 * Return on an invalid expression in braces, unless the expression
22737 * evaluation has been cancelled due to an aborting error, an
22738 * interrupt, or an exception.
22739 */
22740 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022742 if (!eap->skip && fudi.fd_newkey != NULL)
22743 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022744 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 else
22748 eap->skip = TRUE;
22749 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022750
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 /* An error in a function call during evaluation of an expression in magic
22752 * braces should not cause the function not to be defined. */
22753 saved_did_emsg = did_emsg;
22754 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
22756 /*
22757 * ":function func" with only function name: list function.
22758 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022759 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 {
22761 if (!ends_excmd(*skipwhite(p)))
22762 {
22763 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022764 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 }
22766 eap->nextcmd = check_nextcmd(p);
22767 if (eap->nextcmd != NULL)
22768 *p = NUL;
22769 if (!eap->skip && !got_int)
22770 {
22771 fp = find_func(name);
22772 if (fp != NULL)
22773 {
22774 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022775 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022777 if (FUNCLINE(fp, j) == NULL)
22778 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 msg_putchar('\n');
22780 msg_outnum((long)(j + 1));
22781 if (j < 9)
22782 msg_putchar(' ');
22783 if (j < 99)
22784 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022785 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 out_flush(); /* show a line at a time */
22787 ui_breakcheck();
22788 }
22789 if (!got_int)
22790 {
22791 msg_putchar('\n');
22792 msg_puts((char_u *)" endfunction");
22793 }
22794 }
22795 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022796 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022798 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 }
22800
22801 /*
22802 * ":function name(arg1, arg2)" Define function.
22803 */
22804 p = skipwhite(p);
22805 if (*p != '(')
22806 {
22807 if (!eap->skip)
22808 {
22809 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022810 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 }
22812 /* attempt to continue by skipping some text */
22813 if (vim_strchr(p, '(') != NULL)
22814 p = vim_strchr(p, '(');
22815 }
22816 p = skipwhite(p + 1);
22817
22818 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22819 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22820
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022821 if (!eap->skip)
22822 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022823 /* Check the name of the function. Unless it's a dictionary function
22824 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022825 if (name != NULL)
22826 arg = name;
22827 else
22828 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022829 if (arg != NULL && (fudi.fd_di == NULL
22830 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022831 {
22832 if (*arg == K_SPECIAL)
22833 j = 3;
22834 else
22835 j = 0;
22836 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22837 : eval_isnamec(arg[j])))
22838 ++j;
22839 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022840 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022841 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022842 /* Disallow using the g: dict. */
22843 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22844 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022845 }
22846
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 /*
22848 * Isolate the arguments: "arg1, arg2, ...)"
22849 */
22850 while (*p != ')')
22851 {
22852 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22853 {
22854 varargs = TRUE;
22855 p += 3;
22856 mustend = TRUE;
22857 }
22858 else
22859 {
22860 arg = p;
22861 while (ASCII_ISALNUM(*p) || *p == '_')
22862 ++p;
22863 if (arg == p || isdigit(*arg)
22864 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22865 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22866 {
22867 if (!eap->skip)
22868 EMSG2(_("E125: Illegal argument: %s"), arg);
22869 break;
22870 }
22871 if (ga_grow(&newargs, 1) == FAIL)
22872 goto erret;
22873 c = *p;
22874 *p = NUL;
22875 arg = vim_strsave(arg);
22876 if (arg == NULL)
22877 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022878
22879 /* Check for duplicate argument name. */
22880 for (i = 0; i < newargs.ga_len; ++i)
22881 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22882 {
22883 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022884 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022885 goto erret;
22886 }
22887
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22889 *p = c;
22890 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 if (*p == ',')
22892 ++p;
22893 else
22894 mustend = TRUE;
22895 }
22896 p = skipwhite(p);
22897 if (mustend && *p != ')')
22898 {
22899 if (!eap->skip)
22900 EMSG2(_(e_invarg2), eap->arg);
22901 break;
22902 }
22903 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022904 if (*p != ')')
22905 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 ++p; /* skip the ')' */
22907
Bram Moolenaare9a41262005-01-15 22:18:47 +000022908 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 for (;;)
22910 {
22911 p = skipwhite(p);
22912 if (STRNCMP(p, "range", 5) == 0)
22913 {
22914 flags |= FC_RANGE;
22915 p += 5;
22916 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022917 else if (STRNCMP(p, "dict", 4) == 0)
22918 {
22919 flags |= FC_DICT;
22920 p += 4;
22921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 else if (STRNCMP(p, "abort", 5) == 0)
22923 {
22924 flags |= FC_ABORT;
22925 p += 5;
22926 }
22927 else
22928 break;
22929 }
22930
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022931 /* When there is a line break use what follows for the function body.
22932 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22933 if (*p == '\n')
22934 line_arg = p + 1;
22935 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 EMSG(_(e_trailing));
22937
22938 /*
22939 * Read the body of the function, until ":endfunction" is found.
22940 */
22941 if (KeyTyped)
22942 {
22943 /* Check if the function already exists, don't let the user type the
22944 * whole function before telling him it doesn't work! For a script we
22945 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 if (!eap->skip && !eap->forceit)
22947 {
22948 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22949 EMSG(_(e_funcdict));
22950 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022951 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022954 if (!eap->skip && did_emsg)
22955 goto erret;
22956
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 msg_putchar('\n'); /* don't overwrite the function name */
22958 cmdline_row = msg_row;
22959 }
22960
22961 indent = 2;
22962 nesting = 0;
22963 for (;;)
22964 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022965 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022966 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022967 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022968 saved_wait_return = FALSE;
22969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022971 sourcing_lnum_off = sourcing_lnum;
22972
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022973 if (line_arg != NULL)
22974 {
22975 /* Use eap->arg, split up in parts by line breaks. */
22976 theline = line_arg;
22977 p = vim_strchr(theline, '\n');
22978 if (p == NULL)
22979 line_arg += STRLEN(line_arg);
22980 else
22981 {
22982 *p = NUL;
22983 line_arg = p + 1;
22984 }
22985 }
22986 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 theline = getcmdline(':', 0L, indent);
22988 else
22989 theline = eap->getline(':', eap->cookie, indent);
22990 if (KeyTyped)
22991 lines_left = Rows - 1;
22992 if (theline == NULL)
22993 {
22994 EMSG(_("E126: Missing :endfunction"));
22995 goto erret;
22996 }
22997
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022998 /* Detect line continuation: sourcing_lnum increased more than one. */
22999 if (sourcing_lnum > sourcing_lnum_off + 1)
23000 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23001 else
23002 sourcing_lnum_off = 0;
23003
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 if (skip_until != NULL)
23005 {
23006 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23007 * don't check for ":endfunc". */
23008 if (STRCMP(theline, skip_until) == 0)
23009 {
23010 vim_free(skip_until);
23011 skip_until = NULL;
23012 }
23013 }
23014 else
23015 {
23016 /* skip ':' and blanks*/
23017 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23018 ;
23019
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023020 /* Check for "endfunction". */
23021 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023023 if (line_arg == NULL)
23024 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 break;
23026 }
23027
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023028 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 * at "end". */
23030 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23031 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023032 else if (STRNCMP(p, "if", 2) == 0
23033 || STRNCMP(p, "wh", 2) == 0
23034 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 || STRNCMP(p, "try", 3) == 0)
23036 indent += 2;
23037
23038 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023039 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023041 if (*p == '!')
23042 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023044 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23045 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023047 ++nesting;
23048 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 }
23050 }
23051
23052 /* Check for ":append" or ":insert". */
23053 p = skip_range(p, NULL);
23054 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23055 || (p[0] == 'i'
23056 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23057 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23058 skip_until = vim_strsave((char_u *)".");
23059
23060 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23061 arg = skipwhite(skiptowhite(p));
23062 if (arg[0] == '<' && arg[1] =='<'
23063 && ((p[0] == 'p' && p[1] == 'y'
23064 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23065 || (p[0] == 'p' && p[1] == 'e'
23066 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23067 || (p[0] == 't' && p[1] == 'c'
23068 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023069 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23070 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23072 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023073 || (p[0] == 'm' && p[1] == 'z'
23074 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 ))
23076 {
23077 /* ":python <<" continues until a dot, like ":append" */
23078 p = skipwhite(arg + 2);
23079 if (*p == NUL)
23080 skip_until = vim_strsave((char_u *)".");
23081 else
23082 skip_until = vim_strsave(p);
23083 }
23084 }
23085
23086 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023087 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023088 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023089 if (line_arg == NULL)
23090 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023092 }
23093
23094 /* Copy the line to newly allocated memory. get_one_sourceline()
23095 * allocates 250 bytes per line, this saves 80% on average. The cost
23096 * is an extra alloc/free. */
23097 p = vim_strsave(theline);
23098 if (p != NULL)
23099 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023100 if (line_arg == NULL)
23101 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023102 theline = p;
23103 }
23104
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023105 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23106
23107 /* Add NULL lines for continuation lines, so that the line count is
23108 * equal to the index in the growarray. */
23109 while (sourcing_lnum_off-- > 0)
23110 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023111
23112 /* Check for end of eap->arg. */
23113 if (line_arg != NULL && *line_arg == NUL)
23114 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 }
23116
23117 /* Don't define the function when skipping commands or when an error was
23118 * detected. */
23119 if (eap->skip || did_emsg)
23120 goto erret;
23121
23122 /*
23123 * If there are no errors, add the function
23124 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023125 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023126 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023127 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023129 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023130 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023131 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023132 goto erret;
23133 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023135 fp = find_func(name);
23136 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023138 if (!eap->forceit)
23139 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023140 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023141 goto erret;
23142 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023143 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023144 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023145 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023146 name);
23147 goto erret;
23148 }
23149 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023150 ga_clear_strings(&(fp->uf_args));
23151 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023152 vim_free(name);
23153 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 }
23156 else
23157 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023158 char numbuf[20];
23159
23160 fp = NULL;
23161 if (fudi.fd_newkey == NULL && !eap->forceit)
23162 {
23163 EMSG(_(e_funcdict));
23164 goto erret;
23165 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023166 if (fudi.fd_di == NULL)
23167 {
23168 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023169 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023170 goto erret;
23171 }
23172 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023173 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023174 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023175
23176 /* Give the function a sequential number. Can only be used with a
23177 * Funcref! */
23178 vim_free(name);
23179 sprintf(numbuf, "%d", ++func_nr);
23180 name = vim_strsave((char_u *)numbuf);
23181 if (name == NULL)
23182 goto erret;
23183 }
23184
23185 if (fp == NULL)
23186 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023187 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023188 {
23189 int slen, plen;
23190 char_u *scriptname;
23191
23192 /* Check that the autoload name matches the script name. */
23193 j = FAIL;
23194 if (sourcing_name != NULL)
23195 {
23196 scriptname = autoload_name(name);
23197 if (scriptname != NULL)
23198 {
23199 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023200 plen = (int)STRLEN(p);
23201 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023202 if (slen > plen && fnamecmp(p,
23203 sourcing_name + slen - plen) == 0)
23204 j = OK;
23205 vim_free(scriptname);
23206 }
23207 }
23208 if (j == FAIL)
23209 {
23210 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23211 goto erret;
23212 }
23213 }
23214
23215 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 if (fp == NULL)
23217 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023218
23219 if (fudi.fd_dict != NULL)
23220 {
23221 if (fudi.fd_di == NULL)
23222 {
23223 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023224 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023225 if (fudi.fd_di == NULL)
23226 {
23227 vim_free(fp);
23228 goto erret;
23229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023230 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23231 {
23232 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023233 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023234 goto erret;
23235 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023236 }
23237 else
23238 /* overwrite existing dict entry */
23239 clear_tv(&fudi.fd_di->di_tv);
23240 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023241 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023242 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023243 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023244
23245 /* behave like "dict" was used */
23246 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023247 }
23248
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023250 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023251 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23252 {
23253 vim_free(fp);
23254 goto erret;
23255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023257 fp->uf_args = newargs;
23258 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259#ifdef FEAT_PROFILE
23260 fp->uf_tml_count = NULL;
23261 fp->uf_tml_total = NULL;
23262 fp->uf_tml_self = NULL;
23263 fp->uf_profiling = FALSE;
23264 if (prof_def_func())
23265 func_do_profile(fp);
23266#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023267 fp->uf_varargs = varargs;
23268 fp->uf_flags = flags;
23269 fp->uf_calls = 0;
23270 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023271 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272
23273erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 ga_clear_strings(&newargs);
23275 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023276ret_free:
23277 vim_free(skip_until);
23278 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023281 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282}
23283
23284/*
23285 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023286 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023288 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023289 * TFN_INT: internal function name OK
23290 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023291 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 * Advances "pp" to just after the function name (if no error).
23293 */
23294 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023295trans_function_name(
23296 char_u **pp,
23297 int skip, /* only find the end, don't evaluate */
23298 int flags,
23299 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023301 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 char_u *start;
23303 char_u *end;
23304 int lead;
23305 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023308
23309 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023312
23313 /* Check for hard coded <SNR>: already translated function ID (from a user
23314 * command). */
23315 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23316 && (*pp)[2] == (int)KE_SNR)
23317 {
23318 *pp += 3;
23319 len = get_id_len(pp) + 3;
23320 return vim_strnsave(start, len);
23321 }
23322
23323 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23324 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023326 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023328
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023329 /* Note that TFN_ flags use the same values as GLV_ flags. */
23330 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023331 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023332 if (end == start)
23333 {
23334 if (!skip)
23335 EMSG(_("E129: Function name required"));
23336 goto theend;
23337 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023338 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023339 {
23340 /*
23341 * Report an invalid expression in braces, unless the expression
23342 * evaluation has been cancelled due to an aborting error, an
23343 * interrupt, or an exception.
23344 */
23345 if (!aborting())
23346 {
23347 if (end != NULL)
23348 EMSG2(_(e_invarg2), start);
23349 }
23350 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023351 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023352 goto theend;
23353 }
23354
23355 if (lv.ll_tv != NULL)
23356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023357 if (fdp != NULL)
23358 {
23359 fdp->fd_dict = lv.ll_dict;
23360 fdp->fd_newkey = lv.ll_newkey;
23361 lv.ll_newkey = NULL;
23362 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023363 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023364 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23365 {
23366 name = vim_strsave(lv.ll_tv->vval.v_string);
23367 *pp = end;
23368 }
23369 else
23370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023371 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23372 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023373 EMSG(_(e_funcref));
23374 else
23375 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023376 name = NULL;
23377 }
23378 goto theend;
23379 }
23380
23381 if (lv.ll_name == NULL)
23382 {
23383 /* Error found, but continue after the function name. */
23384 *pp = end;
23385 goto theend;
23386 }
23387
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023388 /* Check if the name is a Funcref. If so, use the value. */
23389 if (lv.ll_exp_name != NULL)
23390 {
23391 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023392 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023393 if (name == lv.ll_exp_name)
23394 name = NULL;
23395 }
23396 else
23397 {
23398 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023399 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023400 if (name == *pp)
23401 name = NULL;
23402 }
23403 if (name != NULL)
23404 {
23405 name = vim_strsave(name);
23406 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023407 if (STRNCMP(name, "<SNR>", 5) == 0)
23408 {
23409 /* Change "<SNR>" to the byte sequence. */
23410 name[0] = K_SPECIAL;
23411 name[1] = KS_EXTRA;
23412 name[2] = (int)KE_SNR;
23413 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23414 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023415 goto theend;
23416 }
23417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023418 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023419 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023420 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023421 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23422 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23423 {
23424 /* When there was "s:" already or the name expanded to get a
23425 * leading "s:" then remove it. */
23426 lv.ll_name += 2;
23427 len -= 2;
23428 lead = 2;
23429 }
23430 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023431 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023432 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023433 /* skip over "s:" and "g:" */
23434 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023435 lv.ll_name += 2;
23436 len = (int)(end - lv.ll_name);
23437 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023438
23439 /*
23440 * Copy the function name to allocated memory.
23441 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23442 * Accept <SNR>123_name() outside a script.
23443 */
23444 if (skip)
23445 lead = 0; /* do nothing */
23446 else if (lead > 0)
23447 {
23448 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023449 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23450 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023451 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023452 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023453 if (current_SID <= 0)
23454 {
23455 EMSG(_(e_usingsid));
23456 goto theend;
23457 }
23458 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23459 lead += (int)STRLEN(sid_buf);
23460 }
23461 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023462 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023463 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023464 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023465 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023466 goto theend;
23467 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023468 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023469 {
23470 char_u *cp = vim_strchr(lv.ll_name, ':');
23471
23472 if (cp != NULL && cp < end)
23473 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023474 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023475 goto theend;
23476 }
23477 }
23478
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023479 name = alloc((unsigned)(len + lead + 1));
23480 if (name != NULL)
23481 {
23482 if (lead > 0)
23483 {
23484 name[0] = K_SPECIAL;
23485 name[1] = KS_EXTRA;
23486 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023487 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023488 STRCPY(name + 3, sid_buf);
23489 }
23490 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023491 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023492 }
23493 *pp = end;
23494
23495theend:
23496 clear_lval(&lv);
23497 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498}
23499
23500/*
23501 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23502 * Return 2 if "p" starts with "s:".
23503 * Return 0 otherwise.
23504 */
23505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023506eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023508 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23509 * the standard library function. */
23510 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23511 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 return 5;
23513 if (p[0] == 's' && p[1] == ':')
23514 return 2;
23515 return 0;
23516}
23517
23518/*
23519 * Return TRUE if "p" starts with "<SID>" or "s:".
23520 * Only works if eval_fname_script() returned non-zero for "p"!
23521 */
23522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023523eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524{
23525 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23526}
23527
23528/*
23529 * List the head of the function: "name(arg1, arg2)".
23530 */
23531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023532list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533{
23534 int j;
23535
23536 msg_start();
23537 if (indent)
23538 MSG_PUTS(" ");
23539 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023540 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 {
23542 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023543 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 }
23545 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023546 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023548 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 {
23550 if (j)
23551 MSG_PUTS(", ");
23552 msg_puts(FUNCARG(fp, j));
23553 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023554 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 {
23556 if (j)
23557 MSG_PUTS(", ");
23558 MSG_PUTS("...");
23559 }
23560 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023561 if (fp->uf_flags & FC_ABORT)
23562 MSG_PUTS(" abort");
23563 if (fp->uf_flags & FC_RANGE)
23564 MSG_PUTS(" range");
23565 if (fp->uf_flags & FC_DICT)
23566 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023567 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023568 if (p_verbose > 0)
23569 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570}
23571
23572/*
23573 * Find a function by name, return pointer to it in ufuncs.
23574 * Return NULL for unknown function.
23575 */
23576 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023577find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023579 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023581 hi = hash_find(&func_hashtab, name);
23582 if (!HASHITEM_EMPTY(hi))
23583 return HI2UF(hi);
23584 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585}
23586
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023587#if defined(EXITFREE) || defined(PROTO)
23588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023589free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023590{
23591 hashitem_T *hi;
23592
23593 /* Need to start all over every time, because func_free() may change the
23594 * hash table. */
23595 while (func_hashtab.ht_used > 0)
23596 for (hi = func_hashtab.ht_array; ; ++hi)
23597 if (!HASHITEM_EMPTY(hi))
23598 {
23599 func_free(HI2UF(hi));
23600 break;
23601 }
23602}
23603#endif
23604
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023606translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023607{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023608 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023609 return find_internal_func(name) >= 0;
23610 return find_func(name) != NULL;
23611}
23612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023613/*
23614 * Return TRUE if a function "name" exists.
23615 */
23616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023617function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023618{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023619 char_u *nm = name;
23620 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023621 int n = FALSE;
23622
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023623 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23624 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023625 nm = skipwhite(nm);
23626
23627 /* Only accept "funcname", "funcname ", "funcname (..." and
23628 * "funcname(...", not "funcname!...". */
23629 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023630 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023631 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023632 return n;
23633}
23634
Bram Moolenaara1544c02013-05-30 12:35:52 +020023635 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023636get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023637{
23638 char_u *nm = name;
23639 char_u *p;
23640
23641 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23642
23643 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023644 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023645 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023646
Bram Moolenaara1544c02013-05-30 12:35:52 +020023647 vim_free(p);
23648 return NULL;
23649}
23650
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023651/*
23652 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023653 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23654 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023655 */
23656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023657builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023658{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023659 char_u *p;
23660
23661 if (!ASCII_ISLOWER(name[0]))
23662 return FALSE;
23663 p = vim_strchr(name, AUTOLOAD_CHAR);
23664 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023665}
23666
Bram Moolenaar05159a02005-02-26 23:04:13 +000023667#if defined(FEAT_PROFILE) || defined(PROTO)
23668/*
23669 * Start profiling function "fp".
23670 */
23671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023672func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023673{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023674 int len = fp->uf_lines.ga_len;
23675
23676 if (len == 0)
23677 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023678 fp->uf_tm_count = 0;
23679 profile_zero(&fp->uf_tm_self);
23680 profile_zero(&fp->uf_tm_total);
23681 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023682 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023683 if (fp->uf_tml_total == NULL)
23684 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023685 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023686 if (fp->uf_tml_self == NULL)
23687 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023688 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023689 fp->uf_tml_idx = -1;
23690 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23691 || fp->uf_tml_self == NULL)
23692 return; /* out of memory */
23693
23694 fp->uf_profiling = TRUE;
23695}
23696
23697/*
23698 * Dump the profiling results for all functions in file "fd".
23699 */
23700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023701func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023702{
23703 hashitem_T *hi;
23704 int todo;
23705 ufunc_T *fp;
23706 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023707 ufunc_T **sorttab;
23708 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023709
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023710 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023711 if (todo == 0)
23712 return; /* nothing to dump */
23713
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023714 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023715
Bram Moolenaar05159a02005-02-26 23:04:13 +000023716 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23717 {
23718 if (!HASHITEM_EMPTY(hi))
23719 {
23720 --todo;
23721 fp = HI2UF(hi);
23722 if (fp->uf_profiling)
23723 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023724 if (sorttab != NULL)
23725 sorttab[st_len++] = fp;
23726
Bram Moolenaar05159a02005-02-26 23:04:13 +000023727 if (fp->uf_name[0] == K_SPECIAL)
23728 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23729 else
23730 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23731 if (fp->uf_tm_count == 1)
23732 fprintf(fd, "Called 1 time\n");
23733 else
23734 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23735 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23736 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23737 fprintf(fd, "\n");
23738 fprintf(fd, "count total (s) self (s)\n");
23739
23740 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23741 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023742 if (FUNCLINE(fp, i) == NULL)
23743 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023744 prof_func_line(fd, fp->uf_tml_count[i],
23745 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023746 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23747 }
23748 fprintf(fd, "\n");
23749 }
23750 }
23751 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023752
23753 if (sorttab != NULL && st_len > 0)
23754 {
23755 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23756 prof_total_cmp);
23757 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23758 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23759 prof_self_cmp);
23760 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23761 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023762
23763 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023764}
Bram Moolenaar73830342005-02-28 22:48:19 +000023765
23766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023767prof_sort_list(
23768 FILE *fd,
23769 ufunc_T **sorttab,
23770 int st_len,
23771 char *title,
23772 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023773{
23774 int i;
23775 ufunc_T *fp;
23776
23777 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23778 fprintf(fd, "count total (s) self (s) function\n");
23779 for (i = 0; i < 20 && i < st_len; ++i)
23780 {
23781 fp = sorttab[i];
23782 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23783 prefer_self);
23784 if (fp->uf_name[0] == K_SPECIAL)
23785 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23786 else
23787 fprintf(fd, " %s()\n", fp->uf_name);
23788 }
23789 fprintf(fd, "\n");
23790}
23791
23792/*
23793 * Print the count and times for one function or function line.
23794 */
23795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023796prof_func_line(
23797 FILE *fd,
23798 int count,
23799 proftime_T *total,
23800 proftime_T *self,
23801 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023802{
23803 if (count > 0)
23804 {
23805 fprintf(fd, "%5d ", count);
23806 if (prefer_self && profile_equal(total, self))
23807 fprintf(fd, " ");
23808 else
23809 fprintf(fd, "%s ", profile_msg(total));
23810 if (!prefer_self && profile_equal(total, self))
23811 fprintf(fd, " ");
23812 else
23813 fprintf(fd, "%s ", profile_msg(self));
23814 }
23815 else
23816 fprintf(fd, " ");
23817}
23818
23819/*
23820 * Compare function for total time sorting.
23821 */
23822 static int
23823#ifdef __BORLANDC__
23824_RTLENTRYF
23825#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023826prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023827{
23828 ufunc_T *p1, *p2;
23829
23830 p1 = *(ufunc_T **)s1;
23831 p2 = *(ufunc_T **)s2;
23832 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23833}
23834
23835/*
23836 * Compare function for self time sorting.
23837 */
23838 static int
23839#ifdef __BORLANDC__
23840_RTLENTRYF
23841#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023842prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023843{
23844 ufunc_T *p1, *p2;
23845
23846 p1 = *(ufunc_T **)s1;
23847 p2 = *(ufunc_T **)s2;
23848 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23849}
23850
Bram Moolenaar05159a02005-02-26 23:04:13 +000023851#endif
23852
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023853/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023854 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023855 * Return TRUE if a package was loaded.
23856 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023858script_autoload(
23859 char_u *name,
23860 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023861{
23862 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023863 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023864 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023865 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023866
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023867 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023868 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023869 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023870 return FALSE;
23871
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023872 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023873
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023874 /* Find the name in the list of previously loaded package names. Skip
23875 * "autoload/", it's always the same. */
23876 for (i = 0; i < ga_loaded.ga_len; ++i)
23877 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23878 break;
23879 if (!reload && i < ga_loaded.ga_len)
23880 ret = FALSE; /* was loaded already */
23881 else
23882 {
23883 /* Remember the name if it wasn't loaded already. */
23884 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23885 {
23886 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23887 tofree = NULL;
23888 }
23889
23890 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023891 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023892 ret = TRUE;
23893 }
23894
23895 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023896 return ret;
23897}
23898
23899/*
23900 * Return the autoload script name for a function or variable name.
23901 * Returns NULL when out of memory.
23902 */
23903 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023904autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023905{
23906 char_u *p;
23907 char_u *scriptname;
23908
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023909 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023910 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23911 if (scriptname == NULL)
23912 return FALSE;
23913 STRCPY(scriptname, "autoload/");
23914 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023915 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023916 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023917 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023918 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023919 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023920}
23921
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23923
23924/*
23925 * Function given to ExpandGeneric() to obtain the list of user defined
23926 * function names.
23927 */
23928 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023929get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023931 static long_u done;
23932 static hashitem_T *hi;
23933 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934
23935 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023937 done = 0;
23938 hi = func_hashtab.ht_array;
23939 }
23940 if (done < func_hashtab.ht_used)
23941 {
23942 if (done++ > 0)
23943 ++hi;
23944 while (HASHITEM_EMPTY(hi))
23945 ++hi;
23946 fp = HI2UF(hi);
23947
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023948 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023949 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023950
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023951 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23952 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953
23954 cat_func_name(IObuff, fp);
23955 if (xp->xp_context != EXPAND_USER_FUNC)
23956 {
23957 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023958 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 STRCAT(IObuff, ")");
23960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 return IObuff;
23962 }
23963 return NULL;
23964}
23965
23966#endif /* FEAT_CMDL_COMPL */
23967
23968/*
23969 * Copy the function name of "fp" to buffer "buf".
23970 * "buf" must be able to hold the function name plus three bytes.
23971 * Takes care of script-local function names.
23972 */
23973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023974cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023976 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 {
23978 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023979 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 }
23981 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023982 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983}
23984
23985/*
23986 * ":delfunction {name}"
23987 */
23988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023989ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023991 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 char_u *p;
23993 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023994 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995
23996 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023997 name = trans_function_name(&p, eap->skip, 0, &fudi);
23998 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024000 {
24001 if (fudi.fd_dict != NULL && !eap->skip)
24002 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 if (!ends_excmd(*skipwhite(p)))
24006 {
24007 vim_free(name);
24008 EMSG(_(e_trailing));
24009 return;
24010 }
24011 eap->nextcmd = check_nextcmd(p);
24012 if (eap->nextcmd != NULL)
24013 *p = NUL;
24014
24015 if (!eap->skip)
24016 fp = find_func(name);
24017 vim_free(name);
24018
24019 if (!eap->skip)
24020 {
24021 if (fp == NULL)
24022 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024023 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 return;
24025 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024026 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 {
24028 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24029 return;
24030 }
24031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024032 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024034 /* Delete the dict item that refers to the function, it will
24035 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024036 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024038 else
24039 func_free(fp);
24040 }
24041}
24042
24043/*
24044 * Free a function and remove it from the list of functions.
24045 */
24046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024047func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024048{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024049 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024050
24051 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024052 ga_clear_strings(&(fp->uf_args));
24053 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024054#ifdef FEAT_PROFILE
24055 vim_free(fp->uf_tml_count);
24056 vim_free(fp->uf_tml_total);
24057 vim_free(fp->uf_tml_self);
24058#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024060 /* remove the function from the function hashtable */
24061 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24062 if (HASHITEM_EMPTY(hi))
24063 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024064 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024065 hash_remove(&func_hashtab, hi);
24066
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024067 vim_free(fp);
24068}
24069
24070/*
24071 * Unreference a Function: decrement the reference count and free it when it
24072 * becomes zero. Only for numbered functions.
24073 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024075func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024076{
24077 ufunc_T *fp;
24078
24079 if (name != NULL && isdigit(*name))
24080 {
24081 fp = find_func(name);
24082 if (fp == NULL)
24083 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024084 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024085 {
24086 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024087 * when "uf_calls" becomes zero. */
24088 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024089 func_free(fp);
24090 }
24091 }
24092}
24093
24094/*
24095 * Count a reference to a Function.
24096 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024098func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024099{
24100 ufunc_T *fp;
24101
24102 if (name != NULL && isdigit(*name))
24103 {
24104 fp = find_func(name);
24105 if (fp == NULL)
24106 EMSG2(_(e_intern2), "func_ref()");
24107 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024108 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 }
24110}
24111
24112/*
24113 * Call a user function.
24114 */
24115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024116call_user_func(
24117 ufunc_T *fp, /* pointer to function */
24118 int argcount, /* nr of args */
24119 typval_T *argvars, /* arguments */
24120 typval_T *rettv, /* return value */
24121 linenr_T firstline, /* first line of range */
24122 linenr_T lastline, /* last line of range */
24123 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124{
Bram Moolenaar33570922005-01-25 22:26:29 +000024125 char_u *save_sourcing_name;
24126 linenr_T save_sourcing_lnum;
24127 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024128 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024129 int save_did_emsg;
24130 static int depth = 0;
24131 dictitem_T *v;
24132 int fixvar_idx = 0; /* index in fixvar[] */
24133 int i;
24134 int ai;
24135 char_u numbuf[NUMBUFLEN];
24136 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024137 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024138#ifdef FEAT_PROFILE
24139 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024140 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142
24143 /* If depth of calling is getting too high, don't execute the function */
24144 if (depth >= p_mfd)
24145 {
24146 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024147 rettv->v_type = VAR_NUMBER;
24148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 return;
24150 }
24151 ++depth;
24152
24153 line_breakcheck(); /* check for CTRL-C hit */
24154
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024155 fc = (funccall_T *)alloc(sizeof(funccall_T));
24156 fc->caller = current_funccal;
24157 current_funccal = fc;
24158 fc->func = fp;
24159 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024160 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024161 fc->linenr = 0;
24162 fc->returned = FALSE;
24163 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024165 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24166 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167
Bram Moolenaar33570922005-01-25 22:26:29 +000024168 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024169 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024170 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24171 * each argument variable and saves a lot of time.
24172 */
24173 /*
24174 * Init l: variables.
24175 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024176 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024177 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024178 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024179 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24180 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024181 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024182 name = v->di_key;
24183 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024184 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024185 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024186 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024187 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024188 v->di_tv.vval.v_dict = selfdict;
24189 ++selfdict->dv_refcount;
24190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024191
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 /*
24193 * Init a: variables.
24194 * Set a:0 to "argcount".
24195 * Set a:000 to a list with room for the "..." arguments.
24196 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024197 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024198 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024199 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024200 /* Use "name" to avoid a warning from some compiler that checks the
24201 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024202 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024203 name = v->di_key;
24204 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024205 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024206 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024207 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024208 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024209 v->di_tv.vval.v_list = &fc->l_varlist;
24210 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24211 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24212 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024213
24214 /*
24215 * Set a:firstline to "firstline" and a:lastline to "lastline".
24216 * Set a:name to named arguments.
24217 * Set a:N to the "..." arguments.
24218 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024219 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024220 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024221 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024222 (varnumber_T)lastline);
24223 for (i = 0; i < argcount; ++i)
24224 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024225 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024226 if (ai < 0)
24227 /* named argument a:name */
24228 name = FUNCARG(fp, i);
24229 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024231 /* "..." argument a:1, a:2, etc. */
24232 sprintf((char *)numbuf, "%d", ai + 1);
24233 name = numbuf;
24234 }
24235 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24236 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024237 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024238 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24239 }
24240 else
24241 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024242 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24243 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024244 if (v == NULL)
24245 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024246 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024247 }
24248 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024249 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024250
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024251 /* Note: the values are copied directly to avoid alloc/free.
24252 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024253 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024254 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024255
24256 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24257 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024258 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24259 fc->l_listitems[ai].li_tv = argvars[i];
24260 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024261 }
24262 }
24263
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 /* Don't redraw while executing the function. */
24265 ++RedrawingDisabled;
24266 save_sourcing_name = sourcing_name;
24267 save_sourcing_lnum = sourcing_lnum;
24268 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024269 /* need space for function name + ("function " + 3) or "[number]" */
24270 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24271 + STRLEN(fp->uf_name) + 20;
24272 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 if (sourcing_name != NULL)
24274 {
24275 if (save_sourcing_name != NULL
24276 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024277 sprintf((char *)sourcing_name, "%s[%d]..",
24278 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 else
24280 STRCPY(sourcing_name, "function ");
24281 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24282
24283 if (p_verbose >= 12)
24284 {
24285 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024286 verbose_enter_scroll();
24287
Bram Moolenaar555b2802005-05-19 21:08:39 +000024288 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 if (p_verbose >= 14)
24290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024292 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024293 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024294 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295
24296 msg_puts((char_u *)"(");
24297 for (i = 0; i < argcount; ++i)
24298 {
24299 if (i > 0)
24300 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024301 if (argvars[i].v_type == VAR_NUMBER)
24302 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 else
24304 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024305 /* Do not want errors such as E724 here. */
24306 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024307 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024308 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024309 if (s != NULL)
24310 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024311 if (vim_strsize(s) > MSG_BUF_CLEN)
24312 {
24313 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24314 s = buf;
24315 }
24316 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024317 vim_free(tofree);
24318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 }
24320 }
24321 msg_puts((char_u *)")");
24322 }
24323 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024324
24325 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 --no_wait_return;
24327 }
24328 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024329#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024330 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024331 {
24332 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24333 func_do_profile(fp);
24334 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024335 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024336 {
24337 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024338 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024339 profile_zero(&fp->uf_tm_children);
24340 }
24341 script_prof_save(&wait_start);
24342 }
24343#endif
24344
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024346 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 save_did_emsg = did_emsg;
24348 did_emsg = FALSE;
24349
24350 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024351 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24353
24354 --RedrawingDisabled;
24355
24356 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024359 clear_tv(rettv);
24360 rettv->v_type = VAR_NUMBER;
24361 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 }
24363
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024365 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024366 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024368 profile_end(&call_start);
24369 profile_sub_wait(&wait_start, &call_start);
24370 profile_add(&fp->uf_tm_total, &call_start);
24371 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024372 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024373 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024374 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24375 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024376 }
24377 }
24378#endif
24379
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 /* when being verbose, mention the return value */
24381 if (p_verbose >= 12)
24382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024384 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024387 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024388 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024389 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024390 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024391 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024393 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024394 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024395 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024396 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024397
Bram Moolenaar555b2802005-05-19 21:08:39 +000024398 /* The value may be very long. Skip the middle part, so that we
24399 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024400 * truncate it at the end. Don't want errors such as E724 here. */
24401 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024402 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024403 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024404 if (s != NULL)
24405 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024406 if (vim_strsize(s) > MSG_BUF_CLEN)
24407 {
24408 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24409 s = buf;
24410 }
24411 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024412 vim_free(tofree);
24413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 }
24415 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024416
24417 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 --no_wait_return;
24419 }
24420
24421 vim_free(sourcing_name);
24422 sourcing_name = save_sourcing_name;
24423 sourcing_lnum = save_sourcing_lnum;
24424 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024425#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024426 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024427 script_prof_restore(&wait_start);
24428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429
24430 if (p_verbose >= 12 && sourcing_name != NULL)
24431 {
24432 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024433 verbose_enter_scroll();
24434
Bram Moolenaar555b2802005-05-19 21:08:39 +000024435 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024437
24438 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 --no_wait_return;
24440 }
24441
24442 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024443 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024445
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024446 /* If the a:000 list and the l: and a: dicts are not referenced we can
24447 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024448 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24449 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24450 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24451 {
24452 free_funccal(fc, FALSE);
24453 }
24454 else
24455 {
24456 hashitem_T *hi;
24457 listitem_T *li;
24458 int todo;
24459
24460 /* "fc" is still in use. This can happen when returning "a:000" or
24461 * assigning "l:" to a global variable.
24462 * Link "fc" in the list for garbage collection later. */
24463 fc->caller = previous_funccal;
24464 previous_funccal = fc;
24465
24466 /* Make a copy of the a: variables, since we didn't do that above. */
24467 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24468 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24469 {
24470 if (!HASHITEM_EMPTY(hi))
24471 {
24472 --todo;
24473 v = HI2DI(hi);
24474 copy_tv(&v->di_tv, &v->di_tv);
24475 }
24476 }
24477
24478 /* Make a copy of the a:000 items, since we didn't do that above. */
24479 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24480 copy_tv(&li->li_tv, &li->li_tv);
24481 }
24482}
24483
24484/*
24485 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024486 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024487 */
24488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024489can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024490{
24491 return (fc->l_varlist.lv_copyID != copyID
24492 && fc->l_vars.dv_copyID != copyID
24493 && fc->l_avars.dv_copyID != copyID);
24494}
24495
24496/*
24497 * Free "fc" and what it contains.
24498 */
24499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024500free_funccal(
24501 funccall_T *fc,
24502 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024503{
24504 listitem_T *li;
24505
24506 /* The a: variables typevals may not have been allocated, only free the
24507 * allocated variables. */
24508 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24509
24510 /* free all l: variables */
24511 vars_clear(&fc->l_vars.dv_hashtab);
24512
24513 /* Free the a:000 variables if they were allocated. */
24514 if (free_val)
24515 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24516 clear_tv(&li->li_tv);
24517
24518 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519}
24520
24521/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024522 * Add a number variable "name" to dict "dp" with value "nr".
24523 */
24524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024525add_nr_var(
24526 dict_T *dp,
24527 dictitem_T *v,
24528 char *name,
24529 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024530{
24531 STRCPY(v->di_key, name);
24532 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24533 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24534 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024535 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024536 v->di_tv.vval.v_number = nr;
24537}
24538
24539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 * ":return [expr]"
24541 */
24542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024543ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024544{
24545 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024546 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547 int returning = FALSE;
24548
24549 if (current_funccal == NULL)
24550 {
24551 EMSG(_("E133: :return not inside a function"));
24552 return;
24553 }
24554
24555 if (eap->skip)
24556 ++emsg_skip;
24557
24558 eap->nextcmd = NULL;
24559 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024560 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 {
24562 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024563 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024565 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 }
24567 /* It's safer to return also on error. */
24568 else if (!eap->skip)
24569 {
24570 /*
24571 * Return unless the expression evaluation has been cancelled due to an
24572 * aborting error, an interrupt, or an exception.
24573 */
24574 if (!aborting())
24575 returning = do_return(eap, FALSE, TRUE, NULL);
24576 }
24577
24578 /* When skipping or the return gets pending, advance to the next command
24579 * in this line (!returning). Otherwise, ignore the rest of the line.
24580 * Following lines will be ignored by get_func_line(). */
24581 if (returning)
24582 eap->nextcmd = NULL;
24583 else if (eap->nextcmd == NULL) /* no argument */
24584 eap->nextcmd = check_nextcmd(arg);
24585
24586 if (eap->skip)
24587 --emsg_skip;
24588}
24589
24590/*
24591 * Return from a function. Possibly makes the return pending. Also called
24592 * for a pending return at the ":endtry" or after returning from an extra
24593 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024594 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024595 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596 * FALSE when the return gets pending.
24597 */
24598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024599do_return(
24600 exarg_T *eap,
24601 int reanimate,
24602 int is_cmd,
24603 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604{
24605 int idx;
24606 struct condstack *cstack = eap->cstack;
24607
24608 if (reanimate)
24609 /* Undo the return. */
24610 current_funccal->returned = FALSE;
24611
24612 /*
24613 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24614 * not in its finally clause (which then is to be executed next) is found.
24615 * In this case, make the ":return" pending for execution at the ":endtry".
24616 * Otherwise, return normally.
24617 */
24618 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24619 if (idx >= 0)
24620 {
24621 cstack->cs_pending[idx] = CSTP_RETURN;
24622
24623 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024624 /* A pending return again gets pending. "rettv" points to an
24625 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024627 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628 else
24629 {
24630 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024631 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024633 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024635 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 {
24637 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024638 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024639 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640 else
24641 EMSG(_(e_outofmem));
24642 }
24643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024644 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645
24646 if (reanimate)
24647 {
24648 /* The pending return value could be overwritten by a ":return"
24649 * without argument in a finally clause; reset the default
24650 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024651 current_funccal->rettv->v_type = VAR_NUMBER;
24652 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 }
24654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024655 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 }
24657 else
24658 {
24659 current_funccal->returned = TRUE;
24660
24661 /* If the return is carried out now, store the return value. For
24662 * a return immediately after reanimation, the value is already
24663 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024664 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024666 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024667 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024669 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 }
24671 }
24672
24673 return idx < 0;
24674}
24675
24676/*
24677 * Free the variable with a pending return value.
24678 */
24679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024680discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681{
Bram Moolenaar33570922005-01-25 22:26:29 +000024682 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024683}
24684
24685/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024686 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687 * is an allocated string. Used by report_pending() for verbose messages.
24688 */
24689 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024690get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024692 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024693 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024694 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024696 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024697 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024698 if (s == NULL)
24699 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024700
24701 STRCPY(IObuff, ":return ");
24702 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24703 if (STRLEN(s) + 8 >= IOSIZE)
24704 STRCPY(IObuff + IOSIZE - 4, "...");
24705 vim_free(tofree);
24706 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707}
24708
24709/*
24710 * Get next function line.
24711 * Called by do_cmdline() to get the next line.
24712 * Returns allocated string, or NULL for end of function.
24713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024715get_func_line(
24716 int c UNUSED,
24717 void *cookie,
24718 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719{
Bram Moolenaar33570922005-01-25 22:26:29 +000024720 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024721 ufunc_T *fp = fcp->func;
24722 char_u *retval;
24723 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724
24725 /* If breakpoints have been added/deleted need to check for it. */
24726 if (fcp->dbg_tick != debug_tick)
24727 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024728 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 sourcing_lnum);
24730 fcp->dbg_tick = debug_tick;
24731 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024733 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024734 func_line_end(cookie);
24735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736
Bram Moolenaar05159a02005-02-26 23:04:13 +000024737 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024738 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24739 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 retval = NULL;
24741 else
24742 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024743 /* Skip NULL lines (continuation lines). */
24744 while (fcp->linenr < gap->ga_len
24745 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24746 ++fcp->linenr;
24747 if (fcp->linenr >= gap->ga_len)
24748 retval = NULL;
24749 else
24750 {
24751 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24752 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024753#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024754 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024755 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024756#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 }
24759
24760 /* Did we encounter a breakpoint? */
24761 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24762 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024763 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024765 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 sourcing_lnum);
24767 fcp->dbg_tick = debug_tick;
24768 }
24769
24770 return retval;
24771}
24772
Bram Moolenaar05159a02005-02-26 23:04:13 +000024773#if defined(FEAT_PROFILE) || defined(PROTO)
24774/*
24775 * Called when starting to read a function line.
24776 * "sourcing_lnum" must be correct!
24777 * When skipping lines it may not actually be executed, but we won't find out
24778 * until later and we need to store the time now.
24779 */
24780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024781func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024782{
24783 funccall_T *fcp = (funccall_T *)cookie;
24784 ufunc_T *fp = fcp->func;
24785
24786 if (fp->uf_profiling && sourcing_lnum >= 1
24787 && sourcing_lnum <= fp->uf_lines.ga_len)
24788 {
24789 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024790 /* Skip continuation lines. */
24791 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24792 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024793 fp->uf_tml_execed = FALSE;
24794 profile_start(&fp->uf_tml_start);
24795 profile_zero(&fp->uf_tml_children);
24796 profile_get_wait(&fp->uf_tml_wait);
24797 }
24798}
24799
24800/*
24801 * Called when actually executing a function line.
24802 */
24803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024804func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024805{
24806 funccall_T *fcp = (funccall_T *)cookie;
24807 ufunc_T *fp = fcp->func;
24808
24809 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24810 fp->uf_tml_execed = TRUE;
24811}
24812
24813/*
24814 * Called when done with a function line.
24815 */
24816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024817func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024818{
24819 funccall_T *fcp = (funccall_T *)cookie;
24820 ufunc_T *fp = fcp->func;
24821
24822 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24823 {
24824 if (fp->uf_tml_execed)
24825 {
24826 ++fp->uf_tml_count[fp->uf_tml_idx];
24827 profile_end(&fp->uf_tml_start);
24828 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024829 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024830 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24831 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024832 }
24833 fp->uf_tml_idx = -1;
24834 }
24835}
24836#endif
24837
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838/*
24839 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024840 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 */
24842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024843func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844{
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846
24847 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24848 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024849 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 || fcp->returned);
24851}
24852
24853/*
24854 * return TRUE if cookie indicates a function which "abort"s on errors.
24855 */
24856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024857func_has_abort(
24858 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024860 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861}
24862
24863#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24864typedef enum
24865{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024866 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24867 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24868 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869} var_flavour_T;
24870
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024871static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872
24873 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024874var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875{
24876 char_u *p = varname;
24877
24878 if (ASCII_ISUPPER(*p))
24879 {
24880 while (*(++p))
24881 if (ASCII_ISLOWER(*p))
24882 return VAR_FLAVOUR_SESSION;
24883 return VAR_FLAVOUR_VIMINFO;
24884 }
24885 else
24886 return VAR_FLAVOUR_DEFAULT;
24887}
24888#endif
24889
24890#if defined(FEAT_VIMINFO) || defined(PROTO)
24891/*
24892 * Restore global vars that start with a capital from the viminfo file
24893 */
24894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024895read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896{
24897 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024898 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024899 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024900 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901
24902 if (!writing && (find_viminfo_parameter('!') != NULL))
24903 {
24904 tab = vim_strchr(virp->vir_line + 1, '\t');
24905 if (tab != NULL)
24906 {
24907 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024908 switch (*tab)
24909 {
24910 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024911#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024912 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024913#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024914 case 'D': type = VAR_DICT; break;
24915 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024916 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918
24919 tab = vim_strchr(tab, '\t');
24920 if (tab != NULL)
24921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024922 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024923 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024924 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024926#ifdef FEAT_FLOAT
24927 else if (type == VAR_FLOAT)
24928 (void)string2float(tab + 1, &tv.vval.v_float);
24929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024931 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024932 if (type == VAR_DICT || type == VAR_LIST)
24933 {
24934 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24935
24936 if (etv == NULL)
24937 /* Failed to parse back the dict or list, use it as a
24938 * string. */
24939 tv.v_type = VAR_STRING;
24940 else
24941 {
24942 vim_free(tv.vval.v_string);
24943 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024944 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024945 }
24946 }
24947
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024948 /* when in a function use global variables */
24949 save_funccal = current_funccal;
24950 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024951 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024952 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024953
24954 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024955 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024956 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24957 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958 }
24959 }
24960 }
24961
24962 return viminfo_readline(virp);
24963}
24964
24965/*
24966 * Write global vars that start with a capital to the viminfo file
24967 */
24968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024969write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970{
Bram Moolenaar33570922005-01-25 22:26:29 +000024971 hashitem_T *hi;
24972 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024973 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010024974 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024975 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024976 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024977 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978
24979 if (find_viminfo_parameter('!') == NULL)
24980 return;
24981
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024982 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024983
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024984 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024985 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024987 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024989 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024990 this_var = HI2DI(hi);
24991 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024992 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024993 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024994 {
24995 case VAR_STRING: s = "STR"; break;
24996 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024997 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024998 case VAR_DICT: s = "DIC"; break;
24999 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025000 case VAR_SPECIAL: s = "XPL"; break;
25001
25002 case VAR_UNKNOWN:
25003 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025004 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025005 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025006 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025007 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025008 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025009 if (p != NULL)
25010 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025011 vim_free(tofree);
25012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 }
25014 }
25015}
25016#endif
25017
25018#if defined(FEAT_SESSION) || defined(PROTO)
25019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025020store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021{
Bram Moolenaar33570922005-01-25 22:26:29 +000025022 hashitem_T *hi;
25023 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025024 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025 char_u *p, *t;
25026
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025027 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025028 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025030 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025032 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025033 this_var = HI2DI(hi);
25034 if ((this_var->di_tv.v_type == VAR_NUMBER
25035 || this_var->di_tv.v_type == VAR_STRING)
25036 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025037 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025038 /* Escape special characters with a backslash. Turn a LF and
25039 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025040 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025041 (char_u *)"\\\"\n\r");
25042 if (p == NULL) /* out of memory */
25043 break;
25044 for (t = p; *t != NUL; ++t)
25045 if (*t == '\n')
25046 *t = 'n';
25047 else if (*t == '\r')
25048 *t = 'r';
25049 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025050 this_var->di_key,
25051 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25052 : ' ',
25053 p,
25054 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25055 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025056 || put_eol(fd) == FAIL)
25057 {
25058 vim_free(p);
25059 return FAIL;
25060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 vim_free(p);
25062 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025063#ifdef FEAT_FLOAT
25064 else if (this_var->di_tv.v_type == VAR_FLOAT
25065 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25066 {
25067 float_T f = this_var->di_tv.vval.v_float;
25068 int sign = ' ';
25069
25070 if (f < 0)
25071 {
25072 f = -f;
25073 sign = '-';
25074 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025075 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025076 this_var->di_key, sign, f) < 0)
25077 || put_eol(fd) == FAIL)
25078 return FAIL;
25079 }
25080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081 }
25082 }
25083 return OK;
25084}
25085#endif
25086
Bram Moolenaar661b1822005-07-28 22:36:45 +000025087/*
25088 * Display script name where an item was last set.
25089 * Should only be invoked when 'verbose' is non-zero.
25090 */
25091 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025092last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025093{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025094 char_u *p;
25095
Bram Moolenaar661b1822005-07-28 22:36:45 +000025096 if (scriptID != 0)
25097 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025098 p = home_replace_save(NULL, get_scriptname(scriptID));
25099 if (p != NULL)
25100 {
25101 verbose_enter();
25102 MSG_PUTS(_("\n\tLast set from "));
25103 MSG_PUTS(p);
25104 vim_free(p);
25105 verbose_leave();
25106 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025107 }
25108}
25109
Bram Moolenaard812df62008-11-09 12:46:09 +000025110/*
25111 * List v:oldfiles in a nice way.
25112 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025114ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025115{
25116 list_T *l = vimvars[VV_OLDFILES].vv_list;
25117 listitem_T *li;
25118 int nr = 0;
25119
25120 if (l == NULL)
25121 msg((char_u *)_("No old files"));
25122 else
25123 {
25124 msg_start();
25125 msg_scroll = TRUE;
25126 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25127 {
25128 msg_outnum((long)++nr);
25129 MSG_PUTS(": ");
25130 msg_outtrans(get_tv_string(&li->li_tv));
25131 msg_putchar('\n');
25132 out_flush(); /* output one line at a time */
25133 ui_breakcheck();
25134 }
25135 /* Assume "got_int" was set to truncate the listing. */
25136 got_int = FALSE;
25137
25138#ifdef FEAT_BROWSE_CMD
25139 if (cmdmod.browse)
25140 {
25141 quit_more = FALSE;
25142 nr = prompt_for_number(FALSE);
25143 msg_starthere();
25144 if (nr > 0)
25145 {
25146 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25147 (long)nr);
25148
25149 if (p != NULL)
25150 {
25151 p = expand_env_save(p);
25152 eap->arg = p;
25153 eap->cmdidx = CMD_edit;
25154 cmdmod.browse = FALSE;
25155 do_exedit(eap, NULL);
25156 vim_free(p);
25157 }
25158 }
25159 }
25160#endif
25161 }
25162}
25163
Bram Moolenaar53744302015-07-17 17:38:22 +020025164/* reset v:option_new, v:option_old and v:option_type */
25165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025166reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025167{
25168 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25169 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25170 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25171}
25172
25173
Bram Moolenaar071d4272004-06-13 20:20:40 +000025174#endif /* FEAT_EVAL */
25175
Bram Moolenaar071d4272004-06-13 20:20:40 +000025176
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025177#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178
25179#ifdef WIN3264
25180/*
25181 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25182 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025183static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25184static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25185static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186
25187/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025188 * Get the short path (8.3) for the filename in "fnamep".
25189 * Only works for a valid file name.
25190 * When the path gets longer "fnamep" is changed and the allocated buffer
25191 * is put in "bufp".
25192 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25193 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194 */
25195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025196get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025198 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 char_u *newbuf;
25200
25201 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202 l = GetShortPathName(*fnamep, *fnamep, len);
25203 if (l > len - 1)
25204 {
25205 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025206 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 newbuf = vim_strnsave(*fnamep, l);
25208 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025209 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210
25211 vim_free(*bufp);
25212 *fnamep = *bufp = newbuf;
25213
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025214 /* Really should always succeed, as the buffer is big enough. */
25215 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025216 }
25217
25218 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025219 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220}
25221
25222/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025223 * Get the short path (8.3) for the filename in "fname". The converted
25224 * path is returned in "bufp".
25225 *
25226 * Some of the directories specified in "fname" may not exist. This function
25227 * will shorten the existing directories at the beginning of the path and then
25228 * append the remaining non-existing path.
25229 *
25230 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025231 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025232 * bufp - Pointer to an allocated buffer for the filename.
25233 * fnamelen - Length of the filename pointed to by fname
25234 *
25235 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 */
25237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025238shortpath_for_invalid_fname(
25239 char_u **fname,
25240 char_u **bufp,
25241 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025243 char_u *short_fname, *save_fname, *pbuf_unused;
25244 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025246 int old_len, len;
25247 int new_len, sfx_len;
25248 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249
25250 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025251 old_len = *fnamelen;
25252 save_fname = vim_strnsave(*fname, old_len);
25253 pbuf_unused = NULL;
25254 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025256 endp = save_fname + old_len - 1; /* Find the end of the copy */
25257 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025259 /*
25260 * Try shortening the supplied path till it succeeds by removing one
25261 * directory at a time from the tail of the path.
25262 */
25263 len = 0;
25264 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025266 /* go back one path-separator */
25267 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25268 --endp;
25269 if (endp <= save_fname)
25270 break; /* processed the complete path */
25271
25272 /*
25273 * Replace the path separator with a NUL and try to shorten the
25274 * resulting path.
25275 */
25276 ch = *endp;
25277 *endp = 0;
25278 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025279 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025280 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25281 {
25282 retval = FAIL;
25283 goto theend;
25284 }
25285 *endp = ch; /* preserve the string */
25286
25287 if (len > 0)
25288 break; /* successfully shortened the path */
25289
25290 /* failed to shorten the path. Skip the path separator */
25291 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 }
25293
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025294 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025296 /*
25297 * Succeeded in shortening the path. Now concatenate the shortened
25298 * path with the remaining path at the tail.
25299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025300
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025301 /* Compute the length of the new path. */
25302 sfx_len = (int)(save_endp - endp) + 1;
25303 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025304
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025305 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025307 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025309 /* There is not enough space in the currently allocated string,
25310 * copy it to a buffer big enough. */
25311 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025313 {
25314 retval = FAIL;
25315 goto theend;
25316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 }
25318 else
25319 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025320 /* Transfer short_fname to the main buffer (it's big enough),
25321 * unless get_short_pathname() did its work in-place. */
25322 *fname = *bufp = save_fname;
25323 if (short_fname != save_fname)
25324 vim_strncpy(save_fname, short_fname, len);
25325 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025327
25328 /* concat the not-shortened part of the path */
25329 vim_strncpy(*fname + len, endp, sfx_len);
25330 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025332
25333theend:
25334 vim_free(pbuf_unused);
25335 vim_free(save_fname);
25336
25337 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338}
25339
25340/*
25341 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025342 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 */
25344 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025345shortpath_for_partial(
25346 char_u **fnamep,
25347 char_u **bufp,
25348 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025349{
25350 int sepcount, len, tflen;
25351 char_u *p;
25352 char_u *pbuf, *tfname;
25353 int hasTilde;
25354
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025355 /* Count up the path separators from the RHS.. so we know which part
25356 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025358 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359 if (vim_ispathsep(*p))
25360 ++sepcount;
25361
25362 /* Need full path first (use expand_env() to remove a "~/") */
25363 hasTilde = (**fnamep == '~');
25364 if (hasTilde)
25365 pbuf = tfname = expand_env_save(*fnamep);
25366 else
25367 pbuf = tfname = FullName_save(*fnamep, FALSE);
25368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025369 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025371 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373
25374 if (len == 0)
25375 {
25376 /* Don't have a valid filename, so shorten the rest of the
25377 * path if we can. This CAN give us invalid 8.3 filenames, but
25378 * there's not a lot of point in guessing what it might be.
25379 */
25380 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025381 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 }
25384
25385 /* Count the paths backward to find the beginning of the desired string. */
25386 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025387 {
25388#ifdef FEAT_MBYTE
25389 if (has_mbyte)
25390 p -= mb_head_off(tfname, p);
25391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 if (vim_ispathsep(*p))
25393 {
25394 if (sepcount == 0 || (hasTilde && sepcount == 1))
25395 break;
25396 else
25397 sepcount --;
25398 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400 if (hasTilde)
25401 {
25402 --p;
25403 if (p >= tfname)
25404 *p = '~';
25405 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025406 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 }
25408 else
25409 ++p;
25410
25411 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25412 vim_free(*bufp);
25413 *fnamelen = (int)STRLEN(p);
25414 *bufp = pbuf;
25415 *fnamep = p;
25416
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025417 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418}
25419#endif /* WIN3264 */
25420
25421/*
25422 * Adjust a filename, according to a string of modifiers.
25423 * *fnamep must be NUL terminated when called. When returning, the length is
25424 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025425 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 * When there is an error, *fnamep is set to NULL.
25427 */
25428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025429modify_fname(
25430 char_u *src, /* string with modifiers */
25431 int *usedlen, /* characters after src that are used */
25432 char_u **fnamep, /* file name so far */
25433 char_u **bufp, /* buffer for allocated file name or NULL */
25434 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435{
25436 int valid = 0;
25437 char_u *tail;
25438 char_u *s, *p, *pbuf;
25439 char_u dirname[MAXPATHL];
25440 int c;
25441 int has_fullname = 0;
25442#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025443 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444 int has_shortname = 0;
25445#endif
25446
25447repeat:
25448 /* ":p" - full path/file_name */
25449 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25450 {
25451 has_fullname = 1;
25452
25453 valid |= VALID_PATH;
25454 *usedlen += 2;
25455
25456 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25457 if ((*fnamep)[0] == '~'
25458#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25459 && ((*fnamep)[1] == '/'
25460# ifdef BACKSLASH_IN_FILENAME
25461 || (*fnamep)[1] == '\\'
25462# endif
25463 || (*fnamep)[1] == NUL)
25464
25465#endif
25466 )
25467 {
25468 *fnamep = expand_env_save(*fnamep);
25469 vim_free(*bufp); /* free any allocated file name */
25470 *bufp = *fnamep;
25471 if (*fnamep == NULL)
25472 return -1;
25473 }
25474
25475 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025476 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477 {
25478 if (vim_ispathsep(*p)
25479 && p[1] == '.'
25480 && (p[2] == NUL
25481 || vim_ispathsep(p[2])
25482 || (p[2] == '.'
25483 && (p[3] == NUL || vim_ispathsep(p[3])))))
25484 break;
25485 }
25486
25487 /* FullName_save() is slow, don't use it when not needed. */
25488 if (*p != NUL || !vim_isAbsName(*fnamep))
25489 {
25490 *fnamep = FullName_save(*fnamep, *p != NUL);
25491 vim_free(*bufp); /* free any allocated file name */
25492 *bufp = *fnamep;
25493 if (*fnamep == NULL)
25494 return -1;
25495 }
25496
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025497#ifdef WIN3264
25498# if _WIN32_WINNT >= 0x0500
25499 if (vim_strchr(*fnamep, '~') != NULL)
25500 {
25501 /* Expand 8.3 filename to full path. Needed to make sure the same
25502 * file does not have two different names.
25503 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25504 p = alloc(_MAX_PATH + 1);
25505 if (p != NULL)
25506 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025507 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025508 {
25509 vim_free(*bufp);
25510 *bufp = *fnamep = p;
25511 }
25512 else
25513 vim_free(p);
25514 }
25515 }
25516# endif
25517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 /* Append a path separator to a directory. */
25519 if (mch_isdir(*fnamep))
25520 {
25521 /* Make room for one or two extra characters. */
25522 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25523 vim_free(*bufp); /* free any allocated file name */
25524 *bufp = *fnamep;
25525 if (*fnamep == NULL)
25526 return -1;
25527 add_pathsep(*fnamep);
25528 }
25529 }
25530
25531 /* ":." - path relative to the current directory */
25532 /* ":~" - path relative to the home directory */
25533 /* ":8" - shortname path - postponed till after */
25534 while (src[*usedlen] == ':'
25535 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25536 {
25537 *usedlen += 2;
25538 if (c == '8')
25539 {
25540#ifdef WIN3264
25541 has_shortname = 1; /* Postpone this. */
25542#endif
25543 continue;
25544 }
25545 pbuf = NULL;
25546 /* Need full path first (use expand_env() to remove a "~/") */
25547 if (!has_fullname)
25548 {
25549 if (c == '.' && **fnamep == '~')
25550 p = pbuf = expand_env_save(*fnamep);
25551 else
25552 p = pbuf = FullName_save(*fnamep, FALSE);
25553 }
25554 else
25555 p = *fnamep;
25556
25557 has_fullname = 0;
25558
25559 if (p != NULL)
25560 {
25561 if (c == '.')
25562 {
25563 mch_dirname(dirname, MAXPATHL);
25564 s = shorten_fname(p, dirname);
25565 if (s != NULL)
25566 {
25567 *fnamep = s;
25568 if (pbuf != NULL)
25569 {
25570 vim_free(*bufp); /* free any allocated file name */
25571 *bufp = pbuf;
25572 pbuf = NULL;
25573 }
25574 }
25575 }
25576 else
25577 {
25578 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25579 /* Only replace it when it starts with '~' */
25580 if (*dirname == '~')
25581 {
25582 s = vim_strsave(dirname);
25583 if (s != NULL)
25584 {
25585 *fnamep = s;
25586 vim_free(*bufp);
25587 *bufp = s;
25588 }
25589 }
25590 }
25591 vim_free(pbuf);
25592 }
25593 }
25594
25595 tail = gettail(*fnamep);
25596 *fnamelen = (int)STRLEN(*fnamep);
25597
25598 /* ":h" - head, remove "/file_name", can be repeated */
25599 /* Don't remove the first "/" or "c:\" */
25600 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25601 {
25602 valid |= VALID_HEAD;
25603 *usedlen += 2;
25604 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025605 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025606 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 *fnamelen = (int)(tail - *fnamep);
25608#ifdef VMS
25609 if (*fnamelen > 0)
25610 *fnamelen += 1; /* the path separator is part of the path */
25611#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025612 if (*fnamelen == 0)
25613 {
25614 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25615 p = vim_strsave((char_u *)".");
25616 if (p == NULL)
25617 return -1;
25618 vim_free(*bufp);
25619 *bufp = *fnamep = tail = p;
25620 *fnamelen = 1;
25621 }
25622 else
25623 {
25624 while (tail > s && !after_pathsep(s, tail))
25625 mb_ptr_back(*fnamep, tail);
25626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627 }
25628
25629 /* ":8" - shortname */
25630 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25631 {
25632 *usedlen += 2;
25633#ifdef WIN3264
25634 has_shortname = 1;
25635#endif
25636 }
25637
25638#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025639 /*
25640 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 */
25642 if (has_shortname)
25643 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025644 /* Copy the string if it is shortened by :h and when it wasn't copied
25645 * yet, because we are going to change it in place. Avoids changing
25646 * the buffer name for "%:8". */
25647 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 {
25649 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025650 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651 return -1;
25652 vim_free(*bufp);
25653 *bufp = *fnamep = p;
25654 }
25655
25656 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025657 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 if (!has_fullname && !vim_isAbsName(*fnamep))
25659 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025660 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 return -1;
25662 }
25663 else
25664 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025665 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666
Bram Moolenaardc935552011-08-17 15:23:23 +020025667 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025669 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 return -1;
25671
25672 if (l == 0)
25673 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025674 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025676 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 return -1;
25678 }
25679 *fnamelen = l;
25680 }
25681 }
25682#endif /* WIN3264 */
25683
25684 /* ":t" - tail, just the basename */
25685 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25686 {
25687 *usedlen += 2;
25688 *fnamelen -= (int)(tail - *fnamep);
25689 *fnamep = tail;
25690 }
25691
25692 /* ":e" - extension, can be repeated */
25693 /* ":r" - root, without extension, can be repeated */
25694 while (src[*usedlen] == ':'
25695 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25696 {
25697 /* find a '.' in the tail:
25698 * - for second :e: before the current fname
25699 * - otherwise: The last '.'
25700 */
25701 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25702 s = *fnamep - 2;
25703 else
25704 s = *fnamep + *fnamelen - 1;
25705 for ( ; s > tail; --s)
25706 if (s[0] == '.')
25707 break;
25708 if (src[*usedlen + 1] == 'e') /* :e */
25709 {
25710 if (s > tail)
25711 {
25712 *fnamelen += (int)(*fnamep - (s + 1));
25713 *fnamep = s + 1;
25714#ifdef VMS
25715 /* cut version from the extension */
25716 s = *fnamep + *fnamelen - 1;
25717 for ( ; s > *fnamep; --s)
25718 if (s[0] == ';')
25719 break;
25720 if (s > *fnamep)
25721 *fnamelen = s - *fnamep;
25722#endif
25723 }
25724 else if (*fnamep <= tail)
25725 *fnamelen = 0;
25726 }
25727 else /* :r */
25728 {
25729 if (s > tail) /* remove one extension */
25730 *fnamelen = (int)(s - *fnamep);
25731 }
25732 *usedlen += 2;
25733 }
25734
25735 /* ":s?pat?foo?" - substitute */
25736 /* ":gs?pat?foo?" - global substitute */
25737 if (src[*usedlen] == ':'
25738 && (src[*usedlen + 1] == 's'
25739 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25740 {
25741 char_u *str;
25742 char_u *pat;
25743 char_u *sub;
25744 int sep;
25745 char_u *flags;
25746 int didit = FALSE;
25747
25748 flags = (char_u *)"";
25749 s = src + *usedlen + 2;
25750 if (src[*usedlen + 1] == 'g')
25751 {
25752 flags = (char_u *)"g";
25753 ++s;
25754 }
25755
25756 sep = *s++;
25757 if (sep)
25758 {
25759 /* find end of pattern */
25760 p = vim_strchr(s, sep);
25761 if (p != NULL)
25762 {
25763 pat = vim_strnsave(s, (int)(p - s));
25764 if (pat != NULL)
25765 {
25766 s = p + 1;
25767 /* find end of substitution */
25768 p = vim_strchr(s, sep);
25769 if (p != NULL)
25770 {
25771 sub = vim_strnsave(s, (int)(p - s));
25772 str = vim_strnsave(*fnamep, *fnamelen);
25773 if (sub != NULL && str != NULL)
25774 {
25775 *usedlen = (int)(p + 1 - src);
25776 s = do_string_sub(str, pat, sub, flags);
25777 if (s != NULL)
25778 {
25779 *fnamep = s;
25780 *fnamelen = (int)STRLEN(s);
25781 vim_free(*bufp);
25782 *bufp = s;
25783 didit = TRUE;
25784 }
25785 }
25786 vim_free(sub);
25787 vim_free(str);
25788 }
25789 vim_free(pat);
25790 }
25791 }
25792 /* after using ":s", repeat all the modifiers */
25793 if (didit)
25794 goto repeat;
25795 }
25796 }
25797
Bram Moolenaar26df0922014-02-23 23:39:13 +010025798 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25799 {
25800 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25801 if (p == NULL)
25802 return -1;
25803 vim_free(*bufp);
25804 *bufp = *fnamep = p;
25805 *fnamelen = (int)STRLEN(p);
25806 *usedlen += 2;
25807 }
25808
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 return valid;
25810}
25811
25812/*
25813 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25814 * "flags" can be "g" to do a global substitute.
25815 * Returns an allocated string, NULL for error.
25816 */
25817 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025818do_string_sub(
25819 char_u *str,
25820 char_u *pat,
25821 char_u *sub,
25822 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025823{
25824 int sublen;
25825 regmatch_T regmatch;
25826 int i;
25827 int do_all;
25828 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025829 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 garray_T ga;
25831 char_u *ret;
25832 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025833 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834
25835 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25836 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025837 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838
25839 ga_init2(&ga, 1, 200);
25840
25841 do_all = (flags[0] == 'g');
25842
25843 regmatch.rm_ic = p_ic;
25844 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25845 if (regmatch.regprog != NULL)
25846 {
25847 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025848 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25850 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025851 /* Skip empty match except for first match. */
25852 if (regmatch.startp[0] == regmatch.endp[0])
25853 {
25854 if (zero_width == regmatch.startp[0])
25855 {
25856 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025857 i = MB_PTR2LEN(tail);
25858 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25859 (size_t)i);
25860 ga.ga_len += i;
25861 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025862 continue;
25863 }
25864 zero_width = regmatch.startp[0];
25865 }
25866
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867 /*
25868 * Get some space for a temporary buffer to do the substitution
25869 * into. It will contain:
25870 * - The text up to where the match is.
25871 * - The substituted text.
25872 * - The text after the match.
25873 */
25874 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025875 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025876 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25877 {
25878 ga_clear(&ga);
25879 break;
25880 }
25881
25882 /* copy the text up to where the match is */
25883 i = (int)(regmatch.startp[0] - tail);
25884 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25885 /* add the substituted text */
25886 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25887 + ga.ga_len + i, TRUE, TRUE, FALSE);
25888 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025889 tail = regmatch.endp[0];
25890 if (*tail == NUL)
25891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 if (!do_all)
25893 break;
25894 }
25895
25896 if (ga.ga_data != NULL)
25897 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25898
Bram Moolenaar473de612013-06-08 18:19:48 +020025899 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 }
25901
25902 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25903 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025904 if (p_cpo == empty_option)
25905 p_cpo = save_cpo;
25906 else
25907 /* Darn, evaluating {sub} expression changed the value. */
25908 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909
25910 return ret;
25911}
25912
25913#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */