blob: a0cc6d0170e982ee5b495f4ce7be1f50c228bcbd [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 Moolenaara40058a2005-07-11 22:42:07 +0000391static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
392static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
394static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
395static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
397static void list_glob_vars __ARGS((int *first));
398static void list_buf_vars __ARGS((int *first));
399static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000401static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000403static void list_vim_vars __ARGS((int *first));
404static void list_script_vars __ARGS((int *first));
405static void list_func_vars __ARGS((int *first));
406static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
408static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100409static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000410static void clear_lval __ARGS((lval_T *lp));
411static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
412static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000413static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
414static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
415static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
416static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
417static void item_lock __ARGS((typval_T *tv, int deep, int lock));
418static int tv_islocked __ARGS((typval_T *tv));
419
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
421static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000426static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
427static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000429static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
431static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
432static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
433static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100435static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
436static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
437static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000438static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000440static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
442static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100445static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
449static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000450static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000452static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000454static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
455static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static char_u *string_quote __ARGS((char_u *str, int function));
457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_func_tv __ARGS((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 Moolenaarf506c5b2010-06-22 06:28:58 +0200461static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100470static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100471static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200475static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100477static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara803c7f2016-01-15 15:31:39 +0100478static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara260b872016-01-15 20:48:22 +0100479static void f_assert_fails __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100480static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200485static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100496static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100498static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000503static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000506static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000508#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000509static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000510static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
512#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#ifdef FEAT_FLOAT
516static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000518#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
522static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200532static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200565static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000568static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200569static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000577static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000578static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200579static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000580static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000581static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200584static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000585static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100591static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000594static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000608static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100613static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000615static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100618static void f_jsondecode __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_jsonencode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000629#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200630static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200633#ifdef FEAT_LUA
634static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000640static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200641static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000643static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000649#ifdef vim_mkdir
650static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
651#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100653#ifdef FEAT_MZSCHEME
654static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
655#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100658static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000659static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100660#ifdef FEAT_PERL
661static void f_perleval __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000667static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000668static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200669#ifdef FEAT_PYTHON3
670static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
671#endif
672#ifdef FEAT_PYTHON
673static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
674#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000676static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000677static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
690static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
691#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200692static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100694static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000697static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000699static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200704static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000707static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000708static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000709static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000710static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200712static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000713static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100715#ifdef FEAT_CRYPT
716static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
717#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000718static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200719static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
722static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200723static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000725static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000726static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
731static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000734static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200735static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736#ifdef HAVE_STRFTIME
737static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
739static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200745static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200746static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000752static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200753static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200755static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000756static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000757static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000758static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000759static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000760static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000762static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200763#ifdef FEAT_FLOAT
764static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
766#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770#ifdef FEAT_FLOAT
771static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
772#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200774static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200775static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100776static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100780static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
782static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
783static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
784static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
785static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
786static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000787static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
788static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000790static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100791static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100792static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793
Bram Moolenaar493c1782014-05-28 14:34:46 +0200794static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000795static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static int get_env_len __ARGS((char_u **arg));
797static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000798static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
800#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
801#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
802 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000803static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000805static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200806static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000807static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static typval_T *alloc_tv __ARGS((void));
809static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static void init_tv __ARGS((typval_T *varp));
811static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100812#ifdef FEAT_FLOAT
813static float_T get_tv_float(typval_T *varp);
814#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000816static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000817static char_u *get_tv_string __ARGS((typval_T *varp));
818static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100819static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
820static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100822static funccall_T *get_funccal __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
824static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000825static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
826static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200828static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
829static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200830static int var_check_func_name __ARGS((char_u *name, int new_var));
831static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200832static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000833static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
835static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
836static int eval_fname_script __ARGS((char_u *p));
837static int eval_fname_sid __ARGS((char_u *p));
838static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000839static ufunc_T *find_func __ARGS((char_u *name));
840static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200841static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842#ifdef FEAT_PROFILE
843static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000844static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
845static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
846static int
847# ifdef __BORLANDC__
848 _RTLENTRYF
849# endif
850 prof_total_cmp __ARGS((const void *s1, const void *s2));
851static int
852# ifdef __BORLANDC__
853 _RTLENTRYF
854# endif
855 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200857static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000859static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000860static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000861static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000862static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
863static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000864static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000865static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
Bram Moolenaarc9703302016-01-17 21:49:33 +0100866static win_T *find_tabwin __ARGS((typval_T *wvp, typval_T *tvp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000867static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000868static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000869static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000870static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200871static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200872static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000873
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200874
875#ifdef EBCDIC
876static int compare_func_name __ARGS((const void *s1, const void *s2));
877static void sortFunctions __ARGS(());
878#endif
879
Bram Moolenaar33570922005-01-25 22:26:29 +0000880/*
881 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000882 */
883 void
884eval_init()
885{
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 int i;
887 struct vimvar *p;
888
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200889 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
890 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200891 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000892 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000893 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 STRCPY(p->vv_di.di_key, p->vv_name);
899 if (p->vv_flags & VV_RO)
900 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
901 else if (p->vv_flags & VV_RO_SBX)
902 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
903 else
904 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000905
906 /* add to v: scope dict, unless the value is not always available */
907 if (p->vv_type != VAR_UNKNOWN)
908 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000909 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000910 /* add to compat scope dict */
911 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000913 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100914 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200915 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100916 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100917
918 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
919 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
920 set_vim_var_nr(VV_NONE, VVAL_NONE);
921 set_vim_var_nr(VV_NULL, VVAL_NULL);
922
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200923 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200924
925#ifdef EBCDIC
926 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100927 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200928 */
929 sortFunctions();
930#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000931}
932
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000933#if defined(EXITFREE) || defined(PROTO)
934 void
935eval_clear()
936{
937 int i;
938 struct vimvar *p;
939
940 for (i = 0; i < VV_LEN; ++i)
941 {
942 p = &vimvars[i];
943 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000944 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000945 vim_free(p->vv_str);
946 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000947 }
948 else if (p->vv_di.di_tv.v_type == VAR_LIST)
949 {
950 list_unref(p->vv_list);
951 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000952 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000953 }
954 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000955 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000956 hash_clear(&compat_hashtab);
957
Bram Moolenaard9fba312005-06-26 22:34:35 +0000958 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100959# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200960 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100961# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962
963 /* global variables */
964 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000965
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000966 /* autoloaded script names */
967 ga_clear_strings(&ga_loaded);
968
Bram Moolenaarcca74132013-09-25 21:00:28 +0200969 /* Script-local variables. First clear all the variables and in a second
970 * loop free the scriptvar_T, because a variable in one script might hold
971 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200972 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200973 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200974 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200975 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200976 ga_clear(&ga_scripts);
977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000978 /* unreferenced lists and dicts */
979 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000980
981 /* functions */
982 free_all_functions();
983 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984}
985#endif
986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 * Return the name of the executed function.
989 */
990 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100991func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000993 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994}
995
996/*
997 * Return the address holding the next breakpoint line for a funccall cookie.
998 */
999 linenr_T *
1000func_breakpoint(cookie)
1001 void *cookie;
1002{
Bram Moolenaar33570922005-01-25 22:26:29 +00001003 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004}
1005
1006/*
1007 * Return the address holding the debug tick for a funccall cookie.
1008 */
1009 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001010func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015/*
1016 * Return the nesting level for a funccall cookie.
1017 */
1018 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001019func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001025funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001027/* pointer to list of previously used funccal, still around because some
1028 * item in it is still being used. */
1029funccall_T *previous_funccal = NULL;
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031/*
1032 * Return TRUE when a function was ended by a ":return" command.
1033 */
1034 int
1035current_func_returned()
1036{
1037 return current_funccal->returned;
1038}
1039
1040
1041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 * Set an internal variable to a string value. Creates the variable if it does
1043 * not already exist.
1044 */
1045 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001048 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001049 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 val = vim_strsave(value);
1052 if (val != NULL)
1053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001054 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001055 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001057 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001058 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060 }
1061}
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001064static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065static char_u *redir_endp = NULL;
1066static char_u *redir_varname = NULL;
1067
1068/*
1069 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001070 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 * Returns OK if successfully completed the setup. FAIL otherwise.
1072 */
1073 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001074var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075{
1076 int save_emsg;
1077 int err;
1078 typval_T tv;
1079
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001081 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 {
1083 EMSG(_(e_invarg));
1084 return FAIL;
1085 }
1086
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 redir_varname = vim_strsave(name);
1089 if (redir_varname == NULL)
1090 return FAIL;
1091
1092 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1093 if (redir_lval == NULL)
1094 {
1095 var_redir_stop();
1096 return FAIL;
1097 }
1098
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 /* The output is stored in growarray "redir_ga" until redirection ends. */
1100 ga_init2(&redir_ga, (int)sizeof(char), 500);
1101
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001103 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001104 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1106 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001107 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 if (redir_endp != NULL && *redir_endp != NUL)
1109 /* Trailing characters are present after the variable name */
1110 EMSG(_(e_trailing));
1111 else
1112 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001113 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 var_redir_stop();
1115 return FAIL;
1116 }
1117
1118 /* check if we can write to the variable: set it to or append an empty
1119 * string */
1120 save_emsg = did_emsg;
1121 did_emsg = FALSE;
1122 tv.v_type = VAR_STRING;
1123 tv.vval.v_string = (char_u *)"";
1124 if (append)
1125 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1126 else
1127 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001128 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001130 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 if (err)
1132 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 var_redir_stop();
1135 return FAIL;
1136 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137
1138 return OK;
1139}
1140
1141/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 * Append "value[value_len]" to the variable set by var_redir_start().
1143 * The actual appending is postponed until redirection ends, because the value
1144 * appended may in fact be the string we write to, changing it may cause freed
1145 * memory to be used:
1146 * :redir => foo
1147 * :let foo
1148 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 */
1150 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001151var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154
1155 if (redir_lval == NULL)
1156 return;
1157
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001159 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001161 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001163 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164 {
1165 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001166 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 }
1168 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170}
1171
1172/*
1173 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 */
1176 void
1177var_redir_stop()
1178{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001179 typval_T tv;
1180
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 if (redir_lval != NULL)
1182 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001183 /* If there was no error: assign the text to the variable. */
1184 if (redir_endp != NULL)
1185 {
1186 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1187 tv.v_type = VAR_STRING;
1188 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001189 /* Call get_lval() again, if it's inside a Dict or List it may
1190 * have changed. */
1191 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001192 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001193 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1194 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1195 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001196 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001198 /* free the collected output */
1199 vim_free(redir_ga.ga_data);
1200 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001201
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 vim_free(redir_lval);
1203 redir_lval = NULL;
1204 }
1205 vim_free(redir_varname);
1206 redir_varname = NULL;
1207}
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209# if defined(FEAT_MBYTE) || defined(PROTO)
1210 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001211eval_charconvert(
1212 char_u *enc_from,
1213 char_u *enc_to,
1214 char_u *fname_from,
1215 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
1217 int err = FALSE;
1218
1219 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1220 set_vim_var_string(VV_CC_TO, enc_to, -1);
1221 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1222 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1223 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1224 err = TRUE;
1225 set_vim_var_string(VV_CC_FROM, NULL, -1);
1226 set_vim_var_string(VV_CC_TO, NULL, -1);
1227 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1228 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1229
1230 if (err)
1231 return FAIL;
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1237 int
1238eval_printexpr(fname, args)
1239 char_u *fname;
1240 char_u *args;
1241{
1242 int err = FALSE;
1243
1244 set_vim_var_string(VV_FNAME_IN, fname, -1);
1245 set_vim_var_string(VV_CMDARG, args, -1);
1246 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1247 err = TRUE;
1248 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1249 set_vim_var_string(VV_CMDARG, NULL, -1);
1250
1251 if (err)
1252 {
1253 mch_remove(fname);
1254 return FAIL;
1255 }
1256 return OK;
1257}
1258# endif
1259
1260# if defined(FEAT_DIFF) || defined(PROTO)
1261 void
1262eval_diff(origfile, newfile, outfile)
1263 char_u *origfile;
1264 char_u *newfile;
1265 char_u *outfile;
1266{
1267 int err = FALSE;
1268
1269 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1270 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1271 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1272 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1273 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1274 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1275 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1276}
1277
1278 void
1279eval_patch(origfile, difffile, outfile)
1280 char_u *origfile;
1281 char_u *difffile;
1282 char_u *outfile;
1283{
1284 int err;
1285
1286 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1287 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1288 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1289 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1290 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1291 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1292 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1293}
1294# endif
1295
1296/*
1297 * Top level evaluation function, returning a boolean.
1298 * Sets "error" to TRUE if there was an error.
1299 * Return TRUE or FALSE.
1300 */
1301 int
1302eval_to_bool(arg, error, nextcmd, skip)
1303 char_u *arg;
1304 int *error;
1305 char_u **nextcmd;
1306 int skip; /* only parse, don't execute */
1307{
Bram Moolenaar33570922005-01-25 22:26:29 +00001308 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 int retval = FALSE;
1310
1311 if (skip)
1312 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 else
1316 {
1317 *error = FALSE;
1318 if (!skip)
1319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 }
1324 if (skip)
1325 --emsg_skip;
1326
1327 return retval;
1328}
1329
1330/*
1331 * Top level evaluation function, returning a string. If "skip" is TRUE,
1332 * only parsing to "nextcmd" is done, without reporting errors. Return
1333 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1334 */
1335 char_u *
1336eval_to_string_skip(arg, nextcmd, skip)
1337 char_u *arg;
1338 char_u **nextcmd;
1339 int skip; /* only parse, don't execute */
1340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
1343
1344 if (skip)
1345 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 retval = NULL;
1348 else
1349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 retval = vim_strsave(get_tv_string(&tv));
1351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 if (skip)
1354 --emsg_skip;
1355
1356 return retval;
1357}
1358
1359/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360 * Skip over an expression at "*pp".
1361 * Return FAIL for an error, OK otherwise.
1362 */
1363 int
1364skip_expr(pp)
1365 char_u **pp;
1366{
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368
1369 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001371}
1372
1373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375 * When "convert" is TRUE convert a List into a sequence of lines and convert
1376 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 * Return pointer to allocated memory, or NULL for failure.
1378 */
1379 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 char_u *arg;
1382 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001383 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
Bram Moolenaar33570922005-01-25 22:26:29 +00001385 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001388#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001389 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 retval = NULL;
1394 else
1395 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001396 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 {
1398 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001399 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001400 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001401 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001402 if (tv.vval.v_list->lv_len > 0)
1403 ga_append(&ga, NL);
1404 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001405 ga_append(&ga, NUL);
1406 retval = (char_u *)ga.ga_data;
1407 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001408#ifdef FEAT_FLOAT
1409 else if (convert && tv.v_type == VAR_FLOAT)
1410 {
1411 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1412 retval = vim_strsave(numbuf);
1413 }
1414#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001415 else
1416 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419
1420 return retval;
1421}
1422
1423/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001424 * Call eval_to_string() without using current local variables and using
1425 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 */
1427 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001428eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 char_u *arg;
1430 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001431 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 char_u *retval;
1434 void *save_funccalp;
1435
1436 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001437 if (use_sandbox)
1438 ++sandbox;
1439 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001440 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001441 if (use_sandbox)
1442 --sandbox;
1443 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 restore_funccal(save_funccalp);
1445 return retval;
1446}
1447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448/*
1449 * Top level evaluation function, returning a number.
1450 * Evaluates "expr" silently.
1451 * Returns -1 for an error.
1452 */
1453 int
1454eval_to_number(expr)
1455 char_u *expr;
1456{
Bram Moolenaar33570922005-01-25 22:26:29 +00001457 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001459 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
1461 ++emsg_off;
1462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001463 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 retval = -1;
1465 else
1466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001467 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001468 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 --emsg_off;
1471
1472 return retval;
1473}
1474
Bram Moolenaara40058a2005-07-11 22:42:07 +00001475/*
1476 * Prepare v: variable "idx" to be used.
1477 * Save the current typeval in "save_tv".
1478 * When not used yet add the variable to the v: hashtable.
1479 */
1480 static void
1481prepare_vimvar(idx, save_tv)
1482 int idx;
1483 typval_T *save_tv;
1484{
1485 *save_tv = vimvars[idx].vv_tv;
1486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1488}
1489
1490/*
1491 * Restore v: variable "idx" to typeval "save_tv".
1492 * When no longer defined, remove the variable from the v: hashtable.
1493 */
1494 static void
1495restore_vimvar(idx, save_tv)
1496 int idx;
1497 typval_T *save_tv;
1498{
1499 hashitem_T *hi;
1500
Bram Moolenaara40058a2005-07-11 22:42:07 +00001501 vimvars[idx].vv_tv = *save_tv;
1502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1503 {
1504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1505 if (HASHITEM_EMPTY(hi))
1506 EMSG2(_(e_intern2), "restore_vimvar()");
1507 else
1508 hash_remove(&vimvarht, hi);
1509 }
1510}
1511
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001512#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001513/*
1514 * Evaluate an expression to a list with suggestions.
1515 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001516 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517 */
1518 list_T *
1519eval_spell_expr(badword, expr)
1520 char_u *badword;
1521 char_u *expr;
1522{
1523 typval_T save_val;
1524 typval_T rettv;
1525 list_T *list = NULL;
1526 char_u *p = skipwhite(expr);
1527
1528 /* Set "v:val" to the bad word. */
1529 prepare_vimvar(VV_VAL, &save_val);
1530 vimvars[VV_VAL].vv_type = VAR_STRING;
1531 vimvars[VV_VAL].vv_str = badword;
1532 if (p_verbose == 0)
1533 ++emsg_off;
1534
1535 if (eval1(&p, &rettv, TRUE) == OK)
1536 {
1537 if (rettv.v_type != VAR_LIST)
1538 clear_tv(&rettv);
1539 else
1540 list = rettv.vval.v_list;
1541 }
1542
1543 if (p_verbose == 0)
1544 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545 restore_vimvar(VV_VAL, &save_val);
1546
1547 return list;
1548}
1549
1550/*
1551 * "list" is supposed to contain two items: a word and a number. Return the
1552 * word in "pp" and the number as the return value.
1553 * Return -1 if anything isn't right.
1554 * Used to get the good word and score from the eval_spell_expr() result.
1555 */
1556 int
1557get_spellword(list, pp)
1558 list_T *list;
1559 char_u **pp;
1560{
1561 listitem_T *li;
1562
1563 li = list->lv_first;
1564 if (li == NULL)
1565 return -1;
1566 *pp = get_tv_string(&li->li_tv);
1567
1568 li = li->li_next;
1569 if (li == NULL)
1570 return -1;
1571 return get_tv_number(&li->li_tv);
1572}
1573#endif
1574
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001576 * Top level evaluation function.
1577 * Returns an allocated typval_T with the result.
1578 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001579 */
1580 typval_T *
1581eval_expr(arg, nextcmd)
1582 char_u *arg;
1583 char_u **nextcmd;
1584{
1585 typval_T *tv;
1586
1587 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001588 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001589 {
1590 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001591 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592 }
1593
1594 return tv;
1595}
1596
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600 * Uses argv[argc] for the function arguments. Only Number and String
1601 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001602 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001604 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001605call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 char_u *func;
1607 int argc;
1608 char_u **argv;
1609 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001610 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 long n;
1615 int len;
1616 int i;
1617 int doesrange;
1618 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001621 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
1625 for (i = 0; i < argc; i++)
1626 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001627 /* Pass a NULL or empty argument as an empty string */
1628 if (argv[i] == NULL || *argv[i] == NUL)
1629 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001630 argvars[i].v_type = VAR_STRING;
1631 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001632 continue;
1633 }
1634
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001635 if (str_arg_only)
1636 len = 0;
1637 else
1638 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001639 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (len != 0 && len == (int)STRLEN(argv[i]))
1641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001642 argvars[i].v_type = VAR_NUMBER;
1643 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 else
1646 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001647 argvars[i].v_type = VAR_STRING;
1648 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
1650 }
1651
1652 if (safe)
1653 {
1654 save_funccalp = save_funccal();
1655 ++sandbox;
1656 }
1657
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1659 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (safe)
1663 {
1664 --sandbox;
1665 restore_funccal(save_funccalp);
1666 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 vim_free(argvars);
1668
1669 if (ret == FAIL)
1670 clear_tv(rettv);
1671
1672 return ret;
1673}
1674
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001675/*
1676 * Call vimL function "func" and return the result as a number.
1677 * Returns -1 when calling the function fails.
1678 * Uses argv[argc] for the function arguments.
1679 */
1680 long
1681call_func_retnr(func, argc, argv, safe)
1682 char_u *func;
1683 int argc;
1684 char_u **argv;
1685 int safe; /* use the sandbox */
1686{
1687 typval_T rettv;
1688 long retval;
1689
1690 /* All arguments are passed as strings, no conversion to number. */
1691 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1692 return -1;
1693
1694 retval = get_tv_number_chk(&rettv, NULL);
1695 clear_tv(&rettv);
1696 return retval;
1697}
1698
1699#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1700 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1701
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704 * Call vimL function "func" and return the result as a string.
1705 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
1707 */
1708 void *
1709call_func_retstr(func, argc, argv, safe)
1710 char_u *func;
1711 int argc;
1712 char_u **argv;
1713 int safe; /* use the sandbox */
1714{
1715 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001716 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 return retval;
1725}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001726# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001728/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001729 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001731 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 */
1733 void *
1734call_func_retlist(func, argc, argv, safe)
1735 char_u *func;
1736 int argc;
1737 char_u **argv;
1738 int safe; /* use the sandbox */
1739{
1740 typval_T rettv;
1741
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001742 /* All arguments are passed as strings, no conversion to number. */
1743 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 return NULL;
1745
1746 if (rettv.v_type != VAR_LIST)
1747 {
1748 clear_tv(&rettv);
1749 return NULL;
1750 }
1751
1752 return rettv.vval.v_list;
1753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#endif
1755
1756/*
1757 * Save the current function call pointer, and set it to NULL.
1758 * Used when executing autocommands and for ":source".
1759 */
1760 void *
1761save_funccal()
1762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 current_funccal = NULL;
1766 return (void *)fc;
1767}
1768
1769 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001770restore_funccal(vfc)
1771 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001773 funccall_T *fc = (funccall_T *)vfc;
1774
1775 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776}
1777
Bram Moolenaar05159a02005-02-26 23:04:13 +00001778#if defined(FEAT_PROFILE) || defined(PROTO)
1779/*
1780 * Prepare profiling for entering a child or something else that is not
1781 * counted for the script/function itself.
1782 * Should always be called in pair with prof_child_exit().
1783 */
1784 void
1785prof_child_enter(tm)
1786 proftime_T *tm; /* place to store waittime */
1787{
1788 funccall_T *fc = current_funccal;
1789
1790 if (fc != NULL && fc->func->uf_profiling)
1791 profile_start(&fc->prof_child);
1792 script_prof_save(tm);
1793}
1794
1795/*
1796 * Take care of time spent in a child.
1797 * Should always be called after prof_child_enter().
1798 */
1799 void
1800prof_child_exit(tm)
1801 proftime_T *tm; /* where waittime was stored */
1802{
1803 funccall_T *fc = current_funccal;
1804
1805 if (fc != NULL && fc->func->uf_profiling)
1806 {
1807 profile_end(&fc->prof_child);
1808 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1809 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1810 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1811 }
1812 script_prof_restore(tm);
1813}
1814#endif
1815
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#ifdef FEAT_FOLDING
1818/*
1819 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1820 * it in "*cp". Doesn't give error messages.
1821 */
1822 int
1823eval_foldexpr(arg, cp)
1824 char_u *arg;
1825 int *cp;
1826{
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 int retval;
1829 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001830 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1831 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001834 if (use_sandbox)
1835 ++sandbox;
1836 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 retval = 0;
1840 else
1841 {
1842 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 if (tv.v_type == VAR_NUMBER)
1844 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001845 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 retval = 0;
1847 else
1848 {
1849 /* If the result is a string, check if there is a non-digit before
1850 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (!VIM_ISDIGIT(*s) && *s != '-')
1853 *cp = *s++;
1854 retval = atol((char *)s);
1855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001859 if (use_sandbox)
1860 --sandbox;
1861 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863 return retval;
1864}
1865#endif
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 * ":let" list all variable values
1869 * ":let var1 var2" list variable values
1870 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 * ":let var += expr" assignment command.
1872 * ":let var -= expr" assignment command.
1873 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 */
1876 void
1877ex_let(eap)
1878 exarg_T *eap;
1879{
1880 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 int var_count = 0;
1885 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001887 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001888 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaardb552d602006-03-23 22:59:57 +00001890 argend = skip_var_list(arg, &var_count, &semicolon);
1891 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001893 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1894 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 expr = skipwhite(argend);
1896 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1897 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001899 /*
1900 * ":let" without "=": list variables
1901 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 if (*arg == '[')
1903 EMSG(_(e_invarg));
1904 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001910 list_glob_vars(&first);
1911 list_buf_vars(&first);
1912 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001913#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001914 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001915#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001916 list_script_vars(&first);
1917 list_func_vars(&first);
1918 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 eap->nextcmd = check_nextcmd(arg);
1921 }
1922 else
1923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 op[0] = '=';
1925 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001926 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001928 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1929 op[0] = *expr; /* +=, -= or .= */
1930 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001932 else
1933 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (eap->skip)
1936 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (eap->skip)
1939 {
1940 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 --emsg_skip;
1943 }
1944 else if (i != FAIL)
1945 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
1951}
1952
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953/*
1954 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1955 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 * When "nextchars" is not NULL it points to a string with characters that
1957 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1958 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 * Returns OK or FAIL;
1960 */
1961 static int
1962ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1963 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 int copy; /* copy values from "tv", don't move */
1966 int semicolon; /* from skip_var_list() */
1967 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969{
1970 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001971 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001973 listitem_T *item;
1974 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975
1976 if (*arg != '[')
1977 {
1978 /*
1979 * ":let var = expr" or ":for var in list"
1980 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 return FAIL;
1983 return OK;
1984 }
1985
1986 /*
1987 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1988 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001989 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
1991 EMSG(_(e_listreq));
1992 return FAIL;
1993 }
1994
1995 i = list_len(l);
1996 if (semicolon == 0 && var_count < i)
1997 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001998 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 }
2001 if (var_count - semicolon > i)
2002 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002003 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 return FAIL;
2005 }
2006
2007 item = l->lv_first;
2008 while (*arg != ']')
2009 {
2010 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 item = item->li_next;
2013 if (arg == NULL)
2014 return FAIL;
2015
2016 arg = skipwhite(arg);
2017 if (*arg == ';')
2018 {
2019 /* Put the rest of the list (may be empty) in the var after ';'.
2020 * Create a new list for this. */
2021 l = list_alloc();
2022 if (l == NULL)
2023 return FAIL;
2024 while (item != NULL)
2025 {
2026 list_append_tv(l, &item->li_tv);
2027 item = item->li_next;
2028 }
2029
2030 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002031 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 ltv.vval.v_list = l;
2033 l->lv_refcount = 1;
2034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2036 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 clear_tv(&ltv);
2038 if (arg == NULL)
2039 return FAIL;
2040 break;
2041 }
2042 else if (*arg != ',' && *arg != ']')
2043 {
2044 EMSG2(_(e_intern2), "ex_let_vars()");
2045 return FAIL;
2046 }
2047 }
2048
2049 return OK;
2050}
2051
2052/*
2053 * Skip over assignable variable "var" or list of variables "[var, var]".
2054 * Used for ":let varvar = expr" and ":for varvar in expr".
2055 * For "[var, var]" increment "*var_count" for each variable.
2056 * for "[var, var; var]" set "semicolon".
2057 * Return NULL for an error.
2058 */
2059 static char_u *
2060skip_var_list(arg, var_count, semicolon)
2061 char_u *arg;
2062 int *var_count;
2063 int *semicolon;
2064{
2065 char_u *p, *s;
2066
2067 if (*arg == '[')
2068 {
2069 /* "[var, var]": find the matching ']'. */
2070 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002071 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 {
2073 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2074 s = skip_var_one(p);
2075 if (s == p)
2076 {
2077 EMSG2(_(e_invarg2), p);
2078 return NULL;
2079 }
2080 ++*var_count;
2081
2082 p = skipwhite(s);
2083 if (*p == ']')
2084 break;
2085 else if (*p == ';')
2086 {
2087 if (*semicolon == 1)
2088 {
2089 EMSG(_("Double ; in list of variables"));
2090 return NULL;
2091 }
2092 *semicolon = 1;
2093 }
2094 else if (*p != ',')
2095 {
2096 EMSG2(_(e_invarg2), p);
2097 return NULL;
2098 }
2099 }
2100 return p + 1;
2101 }
2102 else
2103 return skip_var_one(arg);
2104}
2105
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002107 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002108 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110 static char_u *
2111skip_var_one(arg)
2112 char_u *arg;
2113{
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 Moolenaar7d61a922007-08-30 09:12:23 +00002125list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002128 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 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 Moolenaar7d61a922007-08-30 09:12:23 +00002153list_glob_vars(first)
2154 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002155{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002157}
2158
2159/*
2160 * List buffer variables.
2161 */
2162 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163list_buf_vars(first)
2164 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002166 char_u numbuf[NUMBUFLEN];
2167
Bram Moolenaar429fa852013-04-15 12:27:36 +02002168 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170
2171 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2173 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002174}
2175
2176/*
2177 * List window variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_win_vars(first)
2181 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002183 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187#ifdef FEAT_WINDOWS
2188/*
2189 * List tab page variables.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_tab_vars(first)
2193 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002194{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002195 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197}
2198#endif
2199
Bram Moolenaara7043832005-01-21 11:56:39 +00002200/*
2201 * List Vim variables.
2202 */
2203 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_vim_vars(first)
2205 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208}
2209
2210/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211 * List script-local variables, if there is a script.
2212 */
2213 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_script_vars(first)
2215 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216{
2217 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2219 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220}
2221
2222/*
2223 * List function variables, if there is a function.
2224 */
2225 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226list_func_vars(first)
2227 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228{
2229 if (current_funccal != NULL)
2230 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002232}
2233
2234/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 * List variables in "arg".
2236 */
2237 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 exarg_T *eap;
2240 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242{
2243 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 char_u *name_start;
2247 char_u *arg_subsc;
2248 char_u *tofree;
2249 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250
2251 while (!ends_excmd(*arg) && !got_int)
2252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2257 {
2258 emsg_severe = TRUE;
2259 EMSG(_(e_trailing));
2260 break;
2261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 /* get_name_len() takes care of expanding curly braces */
2266 name_start = name = arg;
2267 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2268 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 /* This is mainly to keep test 49 working: when expanding
2271 * curly braces fails overrule the exception error message. */
2272 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 emsg_severe = TRUE;
2275 EMSG2(_(e_invarg2), arg);
2276 break;
2277 }
2278 error = TRUE;
2279 }
2280 else
2281 {
2282 if (tofree != NULL)
2283 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002284 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 else
2287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* handle d.key, l[idx], f(expr) */
2289 arg_subsc = arg;
2290 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 'g': list_glob_vars(first); break;
2299 case 'b': list_buf_vars(first); break;
2300 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002303#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'v': list_vim_vars(first); break;
2305 case 's': list_script_vars(first); break;
2306 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 default:
2308 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
2311 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 {
2313 char_u numbuf[NUMBUFLEN];
2314 char_u *tf;
2315 int c;
2316 char_u *s;
2317
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002318 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 c = *arg;
2320 *arg = NUL;
2321 list_one_var_a((char_u *)"",
2322 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002323 tv.v_type,
2324 s == NULL ? (char_u *)"" : s,
2325 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 *arg = c;
2327 vim_free(tf);
2328 }
2329 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333
2334 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
2339
2340 return arg;
2341}
2342
2343/*
2344 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2345 * Returns a pointer to the char just after the var name.
2346 * Returns NULL if there is an error.
2347 */
2348 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002351 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002353 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355{
2356 int c1;
2357 char_u *name;
2358 char_u *p;
2359 char_u *arg_end = NULL;
2360 int len;
2361 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
2364 /*
2365 * ":let $VAR = expr": Set environment variable.
2366 */
2367 if (*arg == '$')
2368 {
2369 /* Find the end of the name. */
2370 ++arg;
2371 name = arg;
2372 len = get_env_len(&arg);
2373 if (len == 0)
2374 EMSG2(_(e_invarg2), name - 1);
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 if (op != NULL && (*op == '+' || *op == '-'))
2378 EMSG2(_(e_letwrong), op);
2379 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002382 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 {
2384 c1 = name[len];
2385 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 p = get_tv_string_chk(tv);
2387 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 {
2389 int mustfree = FALSE;
2390 char_u *s = vim_getenv(name, &mustfree);
2391
2392 if (s != NULL)
2393 {
2394 p = tofree = concat_str(s, p);
2395 if (mustfree)
2396 vim_free(s);
2397 }
2398 }
2399 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 if (STRICMP(name, "HOME") == 0)
2403 init_homedir();
2404 else if (didset_vim && STRICMP(name, "VIM") == 0)
2405 didset_vim = FALSE;
2406 else if (didset_vimruntime
2407 && STRICMP(name, "VIMRUNTIME") == 0)
2408 didset_vimruntime = FALSE;
2409 arg_end = arg;
2410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 }
2414 }
2415 }
2416
2417 /*
2418 * ":let &option = expr": Set option value.
2419 * ":let &l:option = expr": Set local option value.
2420 * ":let &g:option = expr": Set global option value.
2421 */
2422 else if (*arg == '&')
2423 {
2424 /* Find the end of the name. */
2425 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 if (p == NULL || (endchars != NULL
2427 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 EMSG(_(e_letunexp));
2429 else
2430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 long n;
2432 int opt_type;
2433 long numval;
2434 char_u *stringval = NULL;
2435 char_u *s;
2436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 c1 = *p;
2438 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439
2440 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 s = get_tv_string_chk(tv); /* != NULL if number or string */
2442 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 opt_type = get_option_value(arg, &numval,
2445 &stringval, opt_flags);
2446 if ((opt_type == 1 && *op == '.')
2447 || (opt_type == 0 && *op != '.'))
2448 EMSG2(_(e_letwrong), op);
2449 else
2450 {
2451 if (opt_type == 1) /* number */
2452 {
2453 if (*op == '+')
2454 n = numval + n;
2455 else
2456 n = numval - n;
2457 }
2458 else if (opt_type == 0 && stringval != NULL) /* string */
2459 {
2460 s = concat_str(stringval, s);
2461 vim_free(stringval);
2462 stringval = s;
2463 }
2464 }
2465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (s != NULL)
2467 {
2468 set_option_value(arg, n, s, opt_flags);
2469 arg_end = p;
2470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474 }
2475
2476 /*
2477 * ":let @r = expr": Set register contents.
2478 */
2479 else if (*arg == '@')
2480 {
2481 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 if (op != NULL && (*op == '+' || *op == '-'))
2483 EMSG2(_(e_letwrong), op);
2484 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 EMSG(_(e_letunexp));
2487 else
2488 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 char_u *s;
2491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 p = get_tv_string_chk(tv);
2493 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002495 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 if (s != NULL)
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 vim_free(s);
2500 }
2501 }
2502 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002504 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 arg_end = arg + 1;
2506 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
2509 }
2510
2511 /*
2512 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002519 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2523 EMSG(_(e_letunexp));
2524 else
2525 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 arg_end = p;
2528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
2532
2533 else
2534 EMSG2(_(e_invarg2), arg);
2535
2536 return arg_end;
2537}
2538
2539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2541 */
2542 static int
2543check_changedtick(arg)
2544 char_u *arg;
2545{
2546 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2547 {
2548 EMSG2(_(e_readonlyvar), arg);
2549 return TRUE;
2550 }
2551 return FALSE;
2552}
2553
2554/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 * Get an lval: variable, Dict item or List item that can be assigned a value
2556 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2557 * "name.key", "name.key[expr]" etc.
2558 * Indexing only works if "name" is an existing List or Dictionary.
2559 * "name" points to the start of the name.
2560 * If "rettv" is not NULL it points to the value to be assigned.
2561 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2562 * wrong; must end in space or cmd separator.
2563 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002564 * flags:
2565 * GLV_QUIET: do not give error messages
2566 * GLV_NO_AUTOLOAD: do not use script autoloading
2567 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002569 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 */
2572 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002573get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 typval_T *rettv;
2576 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 int unlet;
2578 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002579 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002580 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 char_u *expr_start, *expr_end;
2584 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 dictitem_T *v;
2586 typval_T var1;
2587 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002593 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002596 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597
2598 if (skip)
2599 {
2600 /* When skipping just find the end of the name. */
2601 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002602 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 }
2604
2605 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002606 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (expr_start != NULL)
2608 {
2609 /* Don't expand the name when we already know there is an error. */
2610 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2611 && *p != '[' && *p != '.')
2612 {
2613 EMSG(_(e_trailing));
2614 return NULL;
2615 }
2616
2617 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2618 if (lp->ll_exp_name == NULL)
2619 {
2620 /* Report an invalid expression in braces, unless the
2621 * expression evaluation has been cancelled due to an
2622 * aborting error, an interrupt, or an exception. */
2623 if (!aborting() && !quiet)
2624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002625 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 EMSG2(_(e_invarg2), name);
2627 return NULL;
2628 }
2629 }
2630 lp->ll_name = lp->ll_exp_name;
2631 }
2632 else
2633 lp->ll_name = name;
2634
2635 /* Without [idx] or .key we are done. */
2636 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2637 return p;
2638
2639 cc = *p;
2640 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002641 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (v == NULL && !quiet)
2643 EMSG2(_(e_undefvar), lp->ll_name);
2644 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 if (v == NULL)
2646 return NULL;
2647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /*
2649 * Loop until no more [idx] or .key is following.
2650 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002651 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2655 && !(lp->ll_tv->v_type == VAR_DICT
2656 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E689: Can only index a List or Dictionary"));
2660 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E708: [:] must come last"));
2666 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 len = -1;
2670 if (*p == '.')
2671 {
2672 key = p + 1;
2673 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2674 ;
2675 if (len == 0)
2676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_(e_emptykey));
2679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
2681 p = key + len;
2682 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 else
2684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (*p == ':')
2688 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 else
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 empty1 = FALSE;
2692 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var1) == NULL)
2695 {
2696 /* not a number or string */
2697 clear_tv(&var1);
2698 return NULL;
2699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Optionally get the second index [ :expr]. */
2703 if (*p == ':')
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (rettv != NULL && (rettv->v_type != VAR_LIST
2714 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
2722 p = skipwhite(p + 1);
2723 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (!empty1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 if (get_tv_string_chk(&var2) == NULL)
2735 {
2736 /* not a number or string */
2737 if (!empty1)
2738 clear_tv(&var1);
2739 clear_tv(&var2);
2740 return NULL;
2741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (!quiet)
2751 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 if (!empty1)
2753 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758
2759 /* Skip to past ']'. */
2760 ++p;
2761 }
2762
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 {
2765 if (len == -1)
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002768 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (*key == NUL)
2770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (!quiet)
2772 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 lp->ll_list = NULL;
2778 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002779 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 /* When assigning to a scope dictionary check that a function and
2782 * variable name is valid (only variable name unless it is l: or
2783 * g: dictionary). Disallow overwriting a builtin function. */
2784 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 int prevval;
2787 int wrong;
2788
2789 if (len != -1)
2790 {
2791 prevval = key[len];
2792 key[len] = NUL;
2793 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002794 else
2795 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2797 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002799 || !valid_varname(key);
2800 if (len != -1)
2801 key[len] = prevval;
2802 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 return NULL;
2804 }
2805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 /* Can't add "v:" variable. */
2809 if (lp->ll_dict == &vimvardict)
2810 {
2811 EMSG2(_(e_illvar), name);
2812 return NULL;
2813 }
2814
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002815 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 p = NULL;
2832 break;
2833 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002835 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 return NULL;
2837
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 if (len == -1)
2839 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842 else
2843 {
2844 /*
2845 * Get the number and item for the only or first index of the List.
2846 */
2847 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 else
2850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var1);
2853 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_list = lp->ll_tv->vval.v_list;
2856 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2857 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002859 if (lp->ll_n1 < 0)
2860 {
2861 lp->ll_n1 = 0;
2862 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2863 }
2864 }
2865 if (lp->ll_li == NULL)
2866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 if (!quiet)
2870 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 }
2873
2874 /*
2875 * May need to find the item or absolute index for the second
2876 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 * When no index given: "lp->ll_empty2" is TRUE.
2878 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002882 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2897 if (lp->ll_n1 < 0)
2898 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2899 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 {
2901 if (!quiet)
2902 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 }
2906
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return p;
2912}
2913
2914/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 */
2917 static void
2918clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920{
2921 vim_free(lp->ll_exp_name);
2922 vim_free(lp->ll_newkey);
2923}
2924
2925/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 */
2930 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937{
2938 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 listitem_T *ri;
2940 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941
2942 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 cc = *endp;
2947 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 if (op != NULL && *op != '=')
2949 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002954 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 if ((di == NULL
2958 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2959 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2960 FALSE)))
2961 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 set_var(lp->ll_name, &tv, FALSE);
2963 clear_tv(&tv);
2964 }
2965 }
2966 else
2967 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002971 else if (tv_check_lock(lp->ll_newkey == NULL
2972 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002973 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002974 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 else if (lp->ll_range)
2976 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002977 listitem_T *ll_li = lp->ll_li;
2978 int ll_n1 = lp->ll_n1;
2979
2980 /*
2981 * Check whether any of the list items is locked
2982 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002983 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002985 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 return;
2987 ri = ri->li_next;
2988 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2989 break;
2990 ll_li = ll_li->li_next;
2991 ++ll_n1;
2992 }
2993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 /*
2995 * Assign the List values to the list items.
2996 */
2997 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 if (op != NULL && *op != '=')
3000 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3001 else
3002 {
3003 clear_tv(&lp->ll_li->li_tv);
3004 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = ri->li_next;
3007 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3008 break;
3009 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003012 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 ri = NULL;
3015 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003016 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 lp->ll_li = lp->ll_li->li_next;
3019 ++lp->ll_n1;
3020 }
3021 if (ri != NULL)
3022 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (lp->ll_empty2
3024 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 : lp->ll_n1 != lp->ll_n2)
3026 EMSG(_("E711: List value has not enough items"));
3027 }
3028 else
3029 {
3030 /*
3031 * Assign to a List or Dictionary item.
3032 */
3033 if (lp->ll_newkey != NULL)
3034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 if (op != NULL && *op != '=')
3036 {
3037 EMSG2(_(e_letwrong), op);
3038 return;
3039 }
3040
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003042 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 if (di == NULL)
3044 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3046 {
3047 vim_free(di);
3048 return;
3049 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else if (op != NULL && *op != '=')
3053 {
3054 tv_op(lp->ll_tv, rettv, op);
3055 return;
3056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /*
3061 * Assign the value to the variable or list item.
3062 */
3063 if (copy)
3064 copy_tv(rettv, lp->ll_tv);
3065 else
3066 {
3067 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003068 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 }
3071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072}
3073
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3076 * Returns OK or FAIL.
3077 */
3078 static int
3079tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003080 typval_T *tv1;
3081 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 char_u *op;
3083{
3084 long n;
3085 char_u numbuf[NUMBUFLEN];
3086 char_u *s;
3087
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003088 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3089 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3090 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 {
3092 switch (tv1->v_type)
3093 {
3094 case VAR_DICT:
3095 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003096 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 break;
3098
3099 case VAR_LIST:
3100 if (*op != '+' || tv2->v_type != VAR_LIST)
3101 break;
3102 /* List += List */
3103 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3104 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3105 return OK;
3106
3107 case VAR_NUMBER:
3108 case VAR_STRING:
3109 if (tv2->v_type == VAR_LIST)
3110 break;
3111 if (*op == '+' || *op == '-')
3112 {
3113 /* nr += nr or nr -= nr*/
3114 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#ifdef FEAT_FLOAT
3116 if (tv2->v_type == VAR_FLOAT)
3117 {
3118 float_T f = n;
3119
3120 if (*op == '+')
3121 f += tv2->vval.v_float;
3122 else
3123 f -= tv2->vval.v_float;
3124 clear_tv(tv1);
3125 tv1->v_type = VAR_FLOAT;
3126 tv1->vval.v_float = f;
3127 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129#endif
3130 {
3131 if (*op == '+')
3132 n += get_tv_number(tv2);
3133 else
3134 n -= get_tv_number(tv2);
3135 clear_tv(tv1);
3136 tv1->v_type = VAR_NUMBER;
3137 tv1->vval.v_number = n;
3138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 }
3140 else
3141 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142 if (tv2->v_type == VAR_FLOAT)
3143 break;
3144
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 /* str .= str */
3146 s = get_tv_string(tv1);
3147 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3148 clear_tv(tv1);
3149 tv1->v_type = VAR_STRING;
3150 tv1->vval.v_string = s;
3151 }
3152 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153
3154#ifdef FEAT_FLOAT
3155 case VAR_FLOAT:
3156 {
3157 float_T f;
3158
3159 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3160 && tv2->v_type != VAR_NUMBER
3161 && tv2->v_type != VAR_STRING))
3162 break;
3163 if (tv2->v_type == VAR_FLOAT)
3164 f = tv2->vval.v_float;
3165 else
3166 f = get_tv_number(tv2);
3167 if (*op == '+')
3168 tv1->vval.v_float += f;
3169 else
3170 tv1->vval.v_float -= f;
3171 }
3172 return OK;
3173#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 }
3176
3177 EMSG2(_(e_letwrong), op);
3178 return FAIL;
3179}
3180
3181/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 * Add a watcher to a list.
3183 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003184 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 list_T *l;
3187 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
3189 lw->lw_next = l->lv_watch;
3190 l->lv_watch = lw;
3191}
3192
3193/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003194 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 * No warning when it isn't found...
3196 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003197 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 list_T *l;
3200 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201{
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203
3204 lwp = &l->lv_watch;
3205 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3206 {
3207 if (lw == lwrem)
3208 {
3209 *lwp = lw->lw_next;
3210 break;
3211 }
3212 lwp = &lw->lw_next;
3213 }
3214}
3215
3216/*
3217 * Just before removing an item from a list: advance watchers to the next
3218 * item.
3219 */
3220 static void
3221list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 list_T *l;
3223 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226
3227 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3228 if (lw->lw_item == item)
3229 lw->lw_item = item->li_next;
3230}
3231
3232/*
3233 * Evaluate the expression used in a ":for var in expr" command.
3234 * "arg" points to "var".
3235 * Set "*errp" to TRUE for an error, FALSE otherwise;
3236 * Return a pointer that holds the info. Null when there is an error.
3237 */
3238 void *
3239eval_for_line(arg, errp, nextcmdp, skip)
3240 char_u *arg;
3241 int *errp;
3242 char_u **nextcmdp;
3243 int skip;
3244{
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 typval_T tv;
3248 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249
3250 *errp = TRUE; /* default: there is an error */
3251
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 if (fi == NULL)
3254 return NULL;
3255
3256 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3257 if (expr == NULL)
3258 return fi;
3259
3260 expr = skipwhite(expr);
3261 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3262 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003263 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 return fi;
3265 }
3266
3267 if (skip)
3268 ++emsg_skip;
3269 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3270 {
3271 *errp = FALSE;
3272 if (!skip)
3273 {
3274 l = tv.vval.v_list;
3275 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 clear_tv(&tv);
3279 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 else
3281 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003282 /* No need to increment the refcount, it's already set for the
3283 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 fi->fi_list = l;
3285 list_add_watch(l, &fi->fi_lw);
3286 fi->fi_lw.lw_item = l->lv_first;
3287 }
3288 }
3289 }
3290 if (skip)
3291 --emsg_skip;
3292
3293 return fi;
3294}
3295
3296/*
3297 * Use the first item in a ":for" list. Advance to the next.
3298 * Assign the values to the variable (list). "arg" points to the first one.
3299 * Return TRUE when a valid item was found, FALSE when at end of list or
3300 * something wrong.
3301 */
3302 int
3303next_for_item(fi_void, arg)
3304 void *fi_void;
3305 char_u *arg;
3306{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003307 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310
3311 item = fi->fi_lw.lw_item;
3312 if (item == NULL)
3313 result = FALSE;
3314 else
3315 {
3316 fi->fi_lw.lw_item = item->li_next;
3317 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3318 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3319 }
3320 return result;
3321}
3322
3323/*
3324 * Free the structure used to store info used by ":for".
3325 */
3326 void
3327free_for_info(fi_void)
3328 void *fi_void;
3329{
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003332 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003333 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003335 list_unref(fi->fi_list);
3336 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 vim_free(fi);
3338}
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3341
3342 void
3343set_context_for_expression(xp, arg, cmdidx)
3344 expand_T *xp;
3345 char_u *arg;
3346 cmdidx_T cmdidx;
3347{
3348 int got_eq = FALSE;
3349 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (cmdidx == CMD_let)
3353 {
3354 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003355 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 {
3357 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003358 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 {
3360 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 if (vim_iswhite(*p))
3363 break;
3364 }
3365 return;
3366 }
3367 }
3368 else
3369 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3370 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 while ((xp->xp_pattern = vim_strpbrk(arg,
3372 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3373 {
3374 c = *xp->xp_pattern;
3375 if (c == '&')
3376 {
3377 c = xp->xp_pattern[1];
3378 if (c == '&')
3379 {
3380 ++xp->xp_pattern;
3381 xp->xp_context = cmdidx != CMD_let || got_eq
3382 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3383 }
3384 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003387 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3388 xp->xp_pattern += 2;
3389
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 else if (c == '$')
3393 {
3394 /* environment variable */
3395 xp->xp_context = EXPAND_ENV_VARS;
3396 }
3397 else if (c == '=')
3398 {
3399 got_eq = TRUE;
3400 xp->xp_context = EXPAND_EXPRESSION;
3401 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003402 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 && xp->xp_context == EXPAND_FUNCTIONS
3404 && vim_strchr(xp->xp_pattern, '(') == NULL)
3405 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003406 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 break;
3408 }
3409 else if (cmdidx != CMD_let || got_eq)
3410 {
3411 if (c == '"') /* string */
3412 {
3413 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3414 if (c == '\\' && xp->xp_pattern[1] != NUL)
3415 ++xp->xp_pattern;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '\'') /* literal string */
3419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003420 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3422 /* skip */ ;
3423 xp->xp_context = EXPAND_NOTHING;
3424 }
3425 else if (c == '|')
3426 {
3427 if (xp->xp_pattern[1] == '|')
3428 {
3429 ++xp->xp_pattern;
3430 xp->xp_context = EXPAND_EXPRESSION;
3431 }
3432 else
3433 xp->xp_context = EXPAND_COMMANDS;
3434 }
3435 else
3436 xp->xp_context = EXPAND_EXPRESSION;
3437 }
3438 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003439 /* Doesn't look like something valid, expand as an expression
3440 * anyway. */
3441 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 arg = xp->xp_pattern;
3443 if (*arg != NUL)
3444 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3445 /* skip */ ;
3446 }
3447 xp->xp_pattern = arg;
3448}
3449
3450#endif /* FEAT_CMDL_COMPL */
3451
3452/*
3453 * ":1,25call func(arg1, arg2)" function call.
3454 */
3455 void
3456ex_call(eap)
3457 exarg_T *eap;
3458{
3459 char_u *arg = eap->arg;
3460 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003462 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 linenr_T lnum;
3466 int doesrange;
3467 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003468 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003470 if (eap->skip)
3471 {
3472 /* trans_function_name() doesn't work well when skipping, use eval0()
3473 * instead to skip to any following command, e.g. for:
3474 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003475 ++emsg_skip;
3476 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3477 clear_tv(&rettv);
3478 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003479 return;
3480 }
3481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003483 if (fudi.fd_newkey != NULL)
3484 {
3485 /* Still need to give an error message for missing key. */
3486 EMSG2(_(e_dictkey), fudi.fd_newkey);
3487 vim_free(fudi.fd_newkey);
3488 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 if (tofree == NULL)
3490 return;
3491
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003492 /* Increase refcount on dictionary, it could get deleted when evaluating
3493 * the arguments. */
3494 if (fudi.fd_dict != NULL)
3495 ++fudi.fd_dict->dv_refcount;
3496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003497 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003499 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaar532c7802005-01-27 14:44:31 +00003501 /* Skip white space to allow ":call func ()". Not good, but required for
3502 * backward compatibility. */
3503 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 if (*startarg != '(')
3507 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003508 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 goto end;
3510 }
3511
3512 /*
3513 * When skipping, evaluate the function once, to find the end of the
3514 * arguments.
3515 * When the function takes a range, this is discovered after the first
3516 * call, and the loop is broken.
3517 */
3518 if (eap->skip)
3519 {
3520 ++emsg_skip;
3521 lnum = eap->line2; /* do it once, also with an invalid range */
3522 }
3523 else
3524 lnum = eap->line1;
3525 for ( ; lnum <= eap->line2; ++lnum)
3526 {
3527 if (!eap->skip && eap->addr_count > 0)
3528 {
3529 curwin->w_cursor.lnum = lnum;
3530 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003531#ifdef FEAT_VIRTUALEDIT
3532 curwin->w_cursor.coladd = 0;
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003536 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003537 eap->line1, eap->line2, &doesrange,
3538 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 failed = TRUE;
3541 break;
3542 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003543
3544 /* Handle a function returning a Funcref, Dictionary or List. */
3545 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3546 {
3547 failed = TRUE;
3548 break;
3549 }
3550
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (doesrange || eap->skip)
3553 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003556 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (aborting())
3560 break;
3561 }
3562 if (eap->skip)
3563 --emsg_skip;
3564
3565 if (!failed)
3566 {
3567 /* Check for trailing illegal characters and a following command. */
3568 if (!ends_excmd(*arg))
3569 {
3570 emsg_severe = TRUE;
3571 EMSG(_(e_trailing));
3572 }
3573 else
3574 eap->nextcmd = check_nextcmd(arg);
3575 }
3576
3577end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003578 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003579 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580}
3581
3582/*
3583 * ":unlet[!] var1 ... " command.
3584 */
3585 void
3586ex_unlet(eap)
3587 exarg_T *eap;
3588{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 ex_unletlock(eap, eap->arg, 0);
3590}
3591
3592/*
3593 * ":lockvar" and ":unlockvar" commands
3594 */
3595 void
3596ex_lockvar(eap)
3597 exarg_T *eap;
3598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600 int deep = 2;
3601
3602 if (eap->forceit)
3603 deep = -1;
3604 else if (vim_isdigit(*arg))
3605 {
3606 deep = getdigits(&arg);
3607 arg = skipwhite(arg);
3608 }
3609
3610 ex_unletlock(eap, arg, deep);
3611}
3612
3613/*
3614 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3615 */
3616 static void
3617ex_unletlock(eap, argstart, deep)
3618 exarg_T *eap;
3619 char_u *argstart;
3620 int deep;
3621{
3622 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 do
3628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003630 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003631 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (lv.ll_name == NULL)
3633 error = TRUE; /* error but continue parsing */
3634 if (name_end == NULL || (!vim_iswhite(*name_end)
3635 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 if (name_end != NULL)
3638 {
3639 emsg_severe = TRUE;
3640 EMSG(_(e_trailing));
3641 }
3642 if (!(eap->skip || error))
3643 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 break;
3645 }
3646
3647 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 {
3649 if (eap->cmdidx == CMD_unlet)
3650 {
3651 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3652 error = TRUE;
3653 }
3654 else
3655 {
3656 if (do_lock_var(&lv, name_end, deep,
3657 eap->cmdidx == CMD_lockvar) == FAIL)
3658 error = TRUE;
3659 }
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 if (!eap->skip)
3663 clear_lval(&lv);
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 arg = skipwhite(name_end);
3666 } while (!ends_excmd(*arg));
3667
3668 eap->nextcmd = check_nextcmd(arg);
3669}
3670
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675 int forceit;
3676{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 int ret = OK;
3678 int cc;
3679
3680 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 cc = *name_end;
3683 *name_end = NUL;
3684
3685 /* Normal name or expanded name. */
3686 if (check_changedtick(lp->ll_name))
3687 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003692 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003694 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 else if (lp->ll_range)
3698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003700 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003701 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003702
3703 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3704 {
3705 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003706 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003707 return FAIL;
3708 ll_li = li;
3709 ++ll_n1;
3710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711
3712 /* Delete a range of List items. */
3713 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3714 {
3715 li = lp->ll_li->li_next;
3716 listitem_remove(lp->ll_list, lp->ll_li);
3717 lp->ll_li = li;
3718 ++lp->ll_n1;
3719 }
3720 }
3721 else
3722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 /* unlet a List item. */
3725 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003728 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 }
3730
3731 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732}
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734/*
3735 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 */
3738 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003741 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
Bram Moolenaar33570922005-01-25 22:26:29 +00003743 hashtab_T *ht;
3744 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003745 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003746 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003747 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 ht = find_var_ht(name, &varname);
3750 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003752 if (ht == &globvarht)
3753 d = &globvardict;
3754 else if (current_funccal != NULL
3755 && ht == &current_funccal->l_vars.dv_hashtab)
3756 d = &current_funccal->l_vars;
3757 else if (ht == &compat_hashtab)
3758 d = &vimvardict;
3759 else
3760 {
3761 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3762 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3763 }
3764 if (d == NULL)
3765 {
3766 EMSG2(_(e_intern2), "do_unlet()");
3767 return FAIL;
3768 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 hi = hash_find(ht, varname);
3770 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003772 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003773 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003774 || var_check_ro(di->di_flags, name, FALSE)
3775 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003776 return FAIL;
3777
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778 delete_var(ht, hi);
3779 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003782 if (forceit)
3783 return OK;
3784 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 return FAIL;
3786}
3787
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003788/*
3789 * Lock or unlock variable indicated by "lp".
3790 * "deep" is the levels to go (-1 for unlimited);
3791 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3792 */
3793 static int
3794do_lock_var(lp, name_end, deep, lock)
3795 lval_T *lp;
3796 char_u *name_end;
3797 int deep;
3798 int lock;
3799{
3800 int ret = OK;
3801 int cc;
3802 dictitem_T *di;
3803
3804 if (deep == 0) /* nothing to do */
3805 return OK;
3806
3807 if (lp->ll_tv == NULL)
3808 {
3809 cc = *name_end;
3810 *name_end = NUL;
3811
3812 /* Normal name or expanded name. */
3813 if (check_changedtick(lp->ll_name))
3814 ret = FAIL;
3815 else
3816 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003817 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 if (di == NULL)
3819 ret = FAIL;
3820 else
3821 {
3822 if (lock)
3823 di->di_flags |= DI_FLAGS_LOCK;
3824 else
3825 di->di_flags &= ~DI_FLAGS_LOCK;
3826 item_lock(&di->di_tv, deep, lock);
3827 }
3828 }
3829 *name_end = cc;
3830 }
3831 else if (lp->ll_range)
3832 {
3833 listitem_T *li = lp->ll_li;
3834
3835 /* (un)lock a range of List items. */
3836 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3837 {
3838 item_lock(&li->li_tv, deep, lock);
3839 li = li->li_next;
3840 ++lp->ll_n1;
3841 }
3842 }
3843 else if (lp->ll_list != NULL)
3844 /* (un)lock a List item. */
3845 item_lock(&lp->ll_li->li_tv, deep, lock);
3846 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003847 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003848 item_lock(&lp->ll_di->di_tv, deep, lock);
3849
3850 return ret;
3851}
3852
3853/*
3854 * Lock or unlock an item. "deep" is nr of levels to go.
3855 */
3856 static void
3857item_lock(tv, deep, lock)
3858 typval_T *tv;
3859 int deep;
3860 int lock;
3861{
3862 static int recurse = 0;
3863 list_T *l;
3864 listitem_T *li;
3865 dict_T *d;
3866 hashitem_T *hi;
3867 int todo;
3868
3869 if (recurse >= DICT_MAXNEST)
3870 {
3871 EMSG(_("E743: variable nested too deep for (un)lock"));
3872 return;
3873 }
3874 if (deep == 0)
3875 return;
3876 ++recurse;
3877
3878 /* lock/unlock the item itself */
3879 if (lock)
3880 tv->v_lock |= VAR_LOCKED;
3881 else
3882 tv->v_lock &= ~VAR_LOCKED;
3883
3884 switch (tv->v_type)
3885 {
3886 case VAR_LIST:
3887 if ((l = tv->vval.v_list) != NULL)
3888 {
3889 if (lock)
3890 l->lv_lock |= VAR_LOCKED;
3891 else
3892 l->lv_lock &= ~VAR_LOCKED;
3893 if (deep < 0 || deep > 1)
3894 /* recursive: lock/unlock the items the List contains */
3895 for (li = l->lv_first; li != NULL; li = li->li_next)
3896 item_lock(&li->li_tv, deep - 1, lock);
3897 }
3898 break;
3899 case VAR_DICT:
3900 if ((d = tv->vval.v_dict) != NULL)
3901 {
3902 if (lock)
3903 d->dv_lock |= VAR_LOCKED;
3904 else
3905 d->dv_lock &= ~VAR_LOCKED;
3906 if (deep < 0 || deep > 1)
3907 {
3908 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003909 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003910 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3911 {
3912 if (!HASHITEM_EMPTY(hi))
3913 {
3914 --todo;
3915 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3916 }
3917 }
3918 }
3919 }
3920 }
3921 --recurse;
3922}
3923
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003925 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3926 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003927 */
3928 static int
3929tv_islocked(tv)
3930 typval_T *tv;
3931{
3932 return (tv->v_lock & VAR_LOCKED)
3933 || (tv->v_type == VAR_LIST
3934 && tv->vval.v_list != NULL
3935 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3936 || (tv->v_type == VAR_DICT
3937 && tv->vval.v_dict != NULL
3938 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3939}
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3942/*
3943 * Delete all "menutrans_" variables.
3944 */
3945 void
3946del_menutrans_vars()
3947{
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003952 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 {
3955 if (!HASHITEM_EMPTY(hi))
3956 {
3957 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3959 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 }
3961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963}
3964#endif
3965
3966#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3967
3968/*
3969 * Local string buffer for the next two functions to store a variable name
3970 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3971 * get_user_var_name().
3972 */
3973
3974static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3975
3976static char_u *varnamebuf = NULL;
3977static int varnamebuflen = 0;
3978
3979/*
3980 * Function to concatenate a prefix and a variable name.
3981 */
3982 static char_u *
3983cat_prefix_varname(prefix, name)
3984 int prefix;
3985 char_u *name;
3986{
3987 int len;
3988
3989 len = (int)STRLEN(name) + 3;
3990 if (len > varnamebuflen)
3991 {
3992 vim_free(varnamebuf);
3993 len += 10; /* some additional space */
3994 varnamebuf = alloc(len);
3995 if (varnamebuf == NULL)
3996 {
3997 varnamebuflen = 0;
3998 return NULL;
3999 }
4000 varnamebuflen = len;
4001 }
4002 *varnamebuf = prefix;
4003 varnamebuf[1] = ':';
4004 STRCPY(varnamebuf + 2, name);
4005 return varnamebuf;
4006}
4007
4008/*
4009 * Function given to ExpandGeneric() to obtain the list of user defined
4010 * (global/buffer/window/built-in) variable names.
4011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 char_u *
4013get_user_var_name(xp, idx)
4014 expand_T *xp;
4015 int idx;
4016{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004017 static long_u gdone;
4018 static long_u bdone;
4019 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 static long_u tdone;
4022#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004023 static int vidx;
4024 static hashitem_T *hi;
4025 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004030#ifdef FEAT_WINDOWS
4031 tdone = 0;
4032#endif
4033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* Global variables */
4036 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004038 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004040 else
4041 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 while (HASHITEM_EMPTY(hi))
4043 ++hi;
4044 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4045 return cat_prefix_varname('g', hi->hi_key);
4046 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004050 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004055 else
4056 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 while (HASHITEM_EMPTY(hi))
4058 ++hi;
4059 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004063 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return (char_u *)"b:changedtick";
4065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004066
4067 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004068 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004071 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004073 else
4074 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004075 while (HASHITEM_EMPTY(hi))
4076 ++hi;
4077 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004079
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004080#ifdef FEAT_WINDOWS
4081 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004082 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004083 if (tdone < ht->ht_used)
4084 {
4085 if (tdone++ == 0)
4086 hi = ht->ht_array;
4087 else
4088 ++hi;
4089 while (HASHITEM_EMPTY(hi))
4090 ++hi;
4091 return cat_prefix_varname('t', hi->hi_key);
4092 }
4093#endif
4094
Bram Moolenaar33570922005-01-25 22:26:29 +00004095 /* v: variables */
4096 if (vidx < VV_LEN)
4097 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 vim_free(varnamebuf);
4100 varnamebuf = NULL;
4101 varnamebuflen = 0;
4102 return NULL;
4103}
4104
4105#endif /* FEAT_CMDL_COMPL */
4106
4107/*
4108 * types for expressions.
4109 */
4110typedef enum
4111{
4112 TYPE_UNKNOWN = 0
4113 , TYPE_EQUAL /* == */
4114 , TYPE_NEQUAL /* != */
4115 , TYPE_GREATER /* > */
4116 , TYPE_GEQUAL /* >= */
4117 , TYPE_SMALLER /* < */
4118 , TYPE_SEQUAL /* <= */
4119 , TYPE_MATCH /* =~ */
4120 , TYPE_NOMATCH /* !~ */
4121} exptype_T;
4122
4123/*
4124 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4127 */
4128
4129/*
4130 * Handle zero level expression.
4131 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004133 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **nextcmd;
4141 int evaluate;
4142{
4143 int ret;
4144 char_u *p;
4145
4146 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (ret == FAIL || !ends_excmd(*p))
4149 {
4150 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /*
4153 * Report the invalid expression unless the expression evaluation has
4154 * been cancelled due to an aborting error, an interrupt, or an
4155 * exception.
4156 */
4157 if (!aborting())
4158 EMSG2(_(e_invexpr2), arg);
4159 ret = FAIL;
4160 }
4161 if (nextcmd != NULL)
4162 *nextcmd = check_nextcmd(p);
4163
4164 return ret;
4165}
4166
4167/*
4168 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004169 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 *
4171 * "arg" must point to the first non-white of the expression.
4172 * "arg" is advanced to the next non-white after the recognized expression.
4173 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004174 * Note: "rettv.v_lock" is not set.
4175 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * Return OK or FAIL.
4177 */
4178 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 int evaluate;
4183{
4184 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004185 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
4187 /*
4188 * Get the first variable.
4189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 if ((*arg)[0] == '?')
4194 {
4195 result = FALSE;
4196 if (evaluate)
4197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 int error = FALSE;
4199
4200 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 if (error)
4204 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206
4207 /*
4208 * Get the second variable.
4209 */
4210 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Check for the ":".
4216 */
4217 if ((*arg)[0] != ':')
4218 {
4219 EMSG(_("E109: Missing ':' after '?'"));
4220 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return FAIL;
4223 }
4224
4225 /*
4226 * Get the third variable.
4227 */
4228 *arg = skipwhite(*arg + 1);
4229 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4230 {
4231 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return FAIL;
4234 }
4235 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238
4239 return OK;
4240}
4241
4242/*
4243 * Handle first level expression:
4244 * expr2 || expr2 || expr2 logical OR
4245 *
4246 * "arg" must point to the first non-white of the expression.
4247 * "arg" is advanced to the next non-white after the recognized expression.
4248 *
4249 * Return OK or FAIL.
4250 */
4251 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 int evaluate;
4256{
Bram Moolenaar33570922005-01-25 22:26:29 +00004257 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 long result;
4259 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 /*
4263 * Get the first variable.
4264 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return FAIL;
4267
4268 /*
4269 * Repeat until there is no following "||".
4270 */
4271 first = TRUE;
4272 result = FALSE;
4273 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4274 {
4275 if (evaluate && first)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (error)
4281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 first = FALSE;
4283 }
4284
4285 /*
4286 * Get the second variable.
4287 */
4288 *arg = skipwhite(*arg + 2);
4289 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4290 return FAIL;
4291
4292 /*
4293 * Compute the result.
4294 */
4295 if (evaluate && !result)
4296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 if (error)
4301 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 if (evaluate)
4304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 rettv->v_type = VAR_NUMBER;
4306 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 }
4309
4310 return OK;
4311}
4312
4313/*
4314 * Handle second level expression:
4315 * expr3 && expr3 && expr3 logical AND
4316 *
4317 * "arg" must point to the first non-white of the expression.
4318 * "arg" is advanced to the next non-white after the recognized expression.
4319 *
4320 * Return OK or FAIL.
4321 */
4322 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int evaluate;
4327{
Bram Moolenaar33570922005-01-25 22:26:29 +00004328 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 long result;
4330 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
4333 /*
4334 * Get the first variable.
4335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return FAIL;
4338
4339 /*
4340 * Repeat until there is no following "&&".
4341 */
4342 first = TRUE;
4343 result = TRUE;
4344 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4345 {
4346 if (evaluate && first)
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 first = FALSE;
4354 }
4355
4356 /*
4357 * Get the second variable.
4358 */
4359 *arg = skipwhite(*arg + 2);
4360 if (eval4(arg, &var2, evaluate && result) == FAIL)
4361 return FAIL;
4362
4363 /*
4364 * Compute the result.
4365 */
4366 if (evaluate && result)
4367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004368 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004371 if (error)
4372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 if (evaluate)
4375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 rettv->v_type = VAR_NUMBER;
4377 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 }
4380
4381 return OK;
4382}
4383
4384/*
4385 * Handle third level expression:
4386 * var1 == var2
4387 * var1 =~ var2
4388 * var1 != var2
4389 * var1 !~ var2
4390 * var1 > var2
4391 * var1 >= var2
4392 * var1 < var2
4393 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004394 * var1 is var2
4395 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 *
4397 * "arg" must point to the first non-white of the expression.
4398 * "arg" is advanced to the next non-white after the recognized expression.
4399 *
4400 * Return OK or FAIL.
4401 */
4402 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 int evaluate;
4407{
Bram Moolenaar33570922005-01-25 22:26:29 +00004408 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 char_u *p;
4410 int i;
4411 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 int len = 2;
4414 long n1, n2;
4415 char_u *s1, *s2;
4416 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4417 regmatch_T regmatch;
4418 int ic;
4419 char_u *save_cpo;
4420
4421 /*
4422 * Get the first variable.
4423 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 return FAIL;
4426
4427 p = *arg;
4428 switch (p[0])
4429 {
4430 case '=': if (p[1] == '=')
4431 type = TYPE_EQUAL;
4432 else if (p[1] == '~')
4433 type = TYPE_MATCH;
4434 break;
4435 case '!': if (p[1] == '=')
4436 type = TYPE_NEQUAL;
4437 else if (p[1] == '~')
4438 type = TYPE_NOMATCH;
4439 break;
4440 case '>': if (p[1] != '=')
4441 {
4442 type = TYPE_GREATER;
4443 len = 1;
4444 }
4445 else
4446 type = TYPE_GEQUAL;
4447 break;
4448 case '<': if (p[1] != '=')
4449 {
4450 type = TYPE_SMALLER;
4451 len = 1;
4452 }
4453 else
4454 type = TYPE_SEQUAL;
4455 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004456 case 'i': if (p[1] == 's')
4457 {
4458 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4459 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004460 i = p[len];
4461 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 {
4463 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4464 type_is = TRUE;
4465 }
4466 }
4467 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469
4470 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 */
4473 if (type != TYPE_UNKNOWN)
4474 {
4475 /* extra question mark appended: ignore case */
4476 if (p[len] == '?')
4477 {
4478 ic = TRUE;
4479 ++len;
4480 }
4481 /* extra '#' appended: match case */
4482 else if (p[len] == '#')
4483 {
4484 ic = FALSE;
4485 ++len;
4486 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004487 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
4489 ic = p_ic;
4490
4491 /*
4492 * Get the second variable.
4493 */
4494 *arg = skipwhite(p + len);
4495 if (eval5(arg, &var2, evaluate) == FAIL)
4496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004497 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 return FAIL;
4499 }
4500
4501 if (evaluate)
4502 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 if (type_is && rettv->v_type != var2.v_type)
4504 {
4505 /* For "is" a different type always means FALSE, for "notis"
4506 * it means TRUE. */
4507 n1 = (type == TYPE_NEQUAL);
4508 }
4509 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4510 {
4511 if (type_is)
4512 {
4513 n1 = (rettv->v_type == var2.v_type
4514 && rettv->vval.v_list == var2.vval.v_list);
4515 if (type == TYPE_NEQUAL)
4516 n1 = !n1;
4517 }
4518 else if (rettv->v_type != var2.v_type
4519 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4520 {
4521 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004522 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004524 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 clear_tv(rettv);
4526 clear_tv(&var2);
4527 return FAIL;
4528 }
4529 else
4530 {
4531 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004532 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4533 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 }
4538
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004539 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4540 {
4541 if (type_is)
4542 {
4543 n1 = (rettv->v_type == var2.v_type
4544 && rettv->vval.v_dict == var2.vval.v_dict);
4545 if (type == TYPE_NEQUAL)
4546 n1 = !n1;
4547 }
4548 else if (rettv->v_type != var2.v_type
4549 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4550 {
4551 if (rettv->v_type != var2.v_type)
4552 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4553 else
4554 EMSG(_("E736: Invalid operation for Dictionary"));
4555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 else
4560 {
4561 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004562 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4563 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004564 if (type == TYPE_NEQUAL)
4565 n1 = !n1;
4566 }
4567 }
4568
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4570 {
4571 if (rettv->v_type != var2.v_type
4572 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4573 {
4574 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004575 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004577 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 clear_tv(rettv);
4579 clear_tv(&var2);
4580 return FAIL;
4581 }
4582 else
4583 {
4584 /* Compare two Funcrefs for being equal or unequal. */
4585 if (rettv->vval.v_string == NULL
4586 || var2.vval.v_string == NULL)
4587 n1 = FALSE;
4588 else
4589 n1 = STRCMP(rettv->vval.v_string,
4590 var2.vval.v_string) == 0;
4591 if (type == TYPE_NEQUAL)
4592 n1 = !n1;
4593 }
4594 }
4595
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004596#ifdef FEAT_FLOAT
4597 /*
4598 * If one of the two variables is a float, compare as a float.
4599 * When using "=~" or "!~", always compare as string.
4600 */
4601 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4602 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4603 {
4604 float_T f1, f2;
4605
4606 if (rettv->v_type == VAR_FLOAT)
4607 f1 = rettv->vval.v_float;
4608 else
4609 f1 = get_tv_number(rettv);
4610 if (var2.v_type == VAR_FLOAT)
4611 f2 = var2.vval.v_float;
4612 else
4613 f2 = get_tv_number(&var2);
4614 n1 = FALSE;
4615 switch (type)
4616 {
4617 case TYPE_EQUAL: n1 = (f1 == f2); break;
4618 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4619 case TYPE_GREATER: n1 = (f1 > f2); break;
4620 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4621 case TYPE_SMALLER: n1 = (f1 < f2); break;
4622 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4623 case TYPE_UNKNOWN:
4624 case TYPE_MATCH:
4625 case TYPE_NOMATCH: break; /* avoid gcc warning */
4626 }
4627 }
4628#endif
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 /*
4631 * If one of the two variables is a number, compare as a number.
4632 * When using "=~" or "!~", always compare as string.
4633 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004634 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 n1 = get_tv_number(rettv);
4638 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (n1 == n2); break;
4642 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4643 case TYPE_GREATER: n1 = (n1 > n2); break;
4644 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4645 case TYPE_SMALLER: n1 = (n1 < n2); break;
4646 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4647 case TYPE_UNKNOWN:
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH: break; /* avoid gcc warning */
4650 }
4651 }
4652 else
4653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 s1 = get_tv_string_buf(rettv, buf1);
4655 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4657 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4658 else
4659 i = 0;
4660 n1 = FALSE;
4661 switch (type)
4662 {
4663 case TYPE_EQUAL: n1 = (i == 0); break;
4664 case TYPE_NEQUAL: n1 = (i != 0); break;
4665 case TYPE_GREATER: n1 = (i > 0); break;
4666 case TYPE_GEQUAL: n1 = (i >= 0); break;
4667 case TYPE_SMALLER: n1 = (i < 0); break;
4668 case TYPE_SEQUAL: n1 = (i <= 0); break;
4669
4670 case TYPE_MATCH:
4671 case TYPE_NOMATCH:
4672 /* avoid 'l' flag in 'cpoptions' */
4673 save_cpo = p_cpo;
4674 p_cpo = (char_u *)"";
4675 regmatch.regprog = vim_regcomp(s2,
4676 RE_MAGIC + RE_STRING);
4677 regmatch.rm_ic = ic;
4678 if (regmatch.regprog != NULL)
4679 {
4680 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004681 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (type == TYPE_NOMATCH)
4683 n1 = !n1;
4684 }
4685 p_cpo = save_cpo;
4686 break;
4687
4688 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4689 }
4690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 clear_tv(&var2);
4693 rettv->v_type = VAR_NUMBER;
4694 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 }
4697
4698 return OK;
4699}
4700
4701/*
4702 * Handle fourth level expression:
4703 * + number addition
4704 * - number subtraction
4705 * . string concatenation
4706 *
4707 * "arg" must point to the first non-white of the expression.
4708 * "arg" is advanced to the next non-white after the recognized expression.
4709 *
4710 * Return OK or FAIL.
4711 */
4712 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 int evaluate;
4717{
Bram Moolenaar33570922005-01-25 22:26:29 +00004718 typval_T var2;
4719 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 int op;
4721 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#ifdef FEAT_FLOAT
4723 float_T f1 = 0, f2 = 0;
4724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 char_u *s1, *s2;
4726 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4727 char_u *p;
4728
4729 /*
4730 * Get the first variable.
4731 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004732 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return FAIL;
4734
4735 /*
4736 * Repeat computing, until no '+', '-' or '.' is following.
4737 */
4738 for (;;)
4739 {
4740 op = **arg;
4741 if (op != '+' && op != '-' && op != '.')
4742 break;
4743
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744 if ((op != '+' || rettv->v_type != VAR_LIST)
4745#ifdef FEAT_FLOAT
4746 && (op == '.' || rettv->v_type != VAR_FLOAT)
4747#endif
4748 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 {
4750 /* For "list + ...", an illegal use of the first operand as
4751 * a number cannot be determined before evaluating the 2nd
4752 * operand: if this is also a list, all is ok.
4753 * For "something . ...", "something - ..." or "non-list + ...",
4754 * we know that the first operand needs to be a string or number
4755 * without evaluating the 2nd operand. So check before to avoid
4756 * side effects after an error. */
4757 if (evaluate && get_tv_string_chk(rettv) == NULL)
4758 {
4759 clear_tv(rettv);
4760 return FAIL;
4761 }
4762 }
4763
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 /*
4765 * Get the second variable.
4766 */
4767 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004768 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 return FAIL;
4772 }
4773
4774 if (evaluate)
4775 {
4776 /*
4777 * Compute the result.
4778 */
4779 if (op == '.')
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4782 s2 = get_tv_string_buf_chk(&var2, buf2);
4783 if (s2 == NULL) /* type error ? */
4784 {
4785 clear_tv(rettv);
4786 clear_tv(&var2);
4787 return FAIL;
4788 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004789 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(rettv);
4791 rettv->v_type = VAR_STRING;
4792 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004794 else if (op == '+' && rettv->v_type == VAR_LIST
4795 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004796 {
4797 /* concatenate Lists */
4798 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4799 &var3) == FAIL)
4800 {
4801 clear_tv(rettv);
4802 clear_tv(&var2);
4803 return FAIL;
4804 }
4805 clear_tv(rettv);
4806 *rettv = var3;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else
4809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 int error = FALSE;
4811
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812#ifdef FEAT_FLOAT
4813 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815 f1 = rettv->vval.v_float;
4816 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818 else
4819#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004821 n1 = get_tv_number_chk(rettv, &error);
4822 if (error)
4823 {
4824 /* This can only happen for "list + non-list". For
4825 * "non-list + ..." or "something - ...", we returned
4826 * before evaluating the 2nd operand. */
4827 clear_tv(rettv);
4828 return FAIL;
4829 }
4830#ifdef FEAT_FLOAT
4831 if (var2.v_type == VAR_FLOAT)
4832 f1 = n1;
4833#endif
4834 }
4835#ifdef FEAT_FLOAT
4836 if (var2.v_type == VAR_FLOAT)
4837 {
4838 f2 = var2.vval.v_float;
4839 n2 = 0;
4840 }
4841 else
4842#endif
4843 {
4844 n2 = get_tv_number_chk(&var2, &error);
4845 if (error)
4846 {
4847 clear_tv(rettv);
4848 clear_tv(&var2);
4849 return FAIL;
4850 }
4851#ifdef FEAT_FLOAT
4852 if (rettv->v_type == VAR_FLOAT)
4853 f2 = n2;
4854#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857
4858#ifdef FEAT_FLOAT
4859 /* If there is a float on either side the result is a float. */
4860 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4861 {
4862 if (op == '+')
4863 f1 = f1 + f2;
4864 else
4865 f1 = f1 - f2;
4866 rettv->v_type = VAR_FLOAT;
4867 rettv->vval.v_float = f1;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#endif
4871 {
4872 if (op == '+')
4873 n1 = n1 + n2;
4874 else
4875 n1 = n1 - n2;
4876 rettv->v_type = VAR_NUMBER;
4877 rettv->vval.v_number = n1;
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 }
4883 return OK;
4884}
4885
4886/*
4887 * Handle fifth level expression:
4888 * * number multiplication
4889 * / number division
4890 * % number modulo
4891 *
4892 * "arg" must point to the first non-white of the expression.
4893 * "arg" is advanced to the next non-white after the recognized expression.
4894 *
4895 * Return OK or FAIL.
4896 */
4897 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004898eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004902 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
Bram Moolenaar33570922005-01-25 22:26:29 +00004904 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 int op;
4906 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907#ifdef FEAT_FLOAT
4908 int use_float = FALSE;
4909 float_T f1 = 0, f2;
4910#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004911 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
4913 /*
4914 * Get the first variable.
4915 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004916 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return FAIL;
4918
4919 /*
4920 * Repeat computing, until no '*', '/' or '%' is following.
4921 */
4922 for (;;)
4923 {
4924 op = **arg;
4925 if (op != '*' && op != '/' && op != '%')
4926 break;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (rettv->v_type == VAR_FLOAT)
4932 {
4933 f1 = rettv->vval.v_float;
4934 use_float = TRUE;
4935 n1 = 0;
4936 }
4937 else
4938#endif
4939 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004940 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004941 if (error)
4942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 else
4945 n1 = 0;
4946
4947 /*
4948 * Get the second variable.
4949 */
4950 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004951 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 return FAIL;
4953
4954 if (evaluate)
4955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956#ifdef FEAT_FLOAT
4957 if (var2.v_type == VAR_FLOAT)
4958 {
4959 if (!use_float)
4960 {
4961 f1 = n1;
4962 use_float = TRUE;
4963 }
4964 f2 = var2.vval.v_float;
4965 n2 = 0;
4966 }
4967 else
4968#endif
4969 {
4970 n2 = get_tv_number_chk(&var2, &error);
4971 clear_tv(&var2);
4972 if (error)
4973 return FAIL;
4974#ifdef FEAT_FLOAT
4975 if (use_float)
4976 f2 = n2;
4977#endif
4978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980 /*
4981 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984#ifdef FEAT_FLOAT
4985 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 if (op == '*')
4988 f1 = f1 * f2;
4989 else if (op == '/')
4990 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991# ifdef VMS
4992 /* VMS crashes on divide by zero, work around it */
4993 if (f2 == 0.0)
4994 {
4995 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004996 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004997 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004998 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004999 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005000 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005001 }
5002 else
5003 f1 = f1 / f2;
5004# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 /* We rely on the floating point library to handle divide
5006 * by zero to result in "inf" and not a crash. */
5007 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005008# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005012 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 return FAIL;
5014 }
5015 rettv->v_type = VAR_FLOAT;
5016 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 if (op == '*')
5022 n1 = n1 * n2;
5023 else if (op == '/')
5024 {
5025 if (n2 == 0) /* give an error message? */
5026 {
5027 if (n1 == 0)
5028 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5029 else if (n1 < 0)
5030 n1 = -0x7fffffffL;
5031 else
5032 n1 = 0x7fffffffL;
5033 }
5034 else
5035 n1 = n1 / n2;
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005038 {
5039 if (n2 == 0) /* give an error message? */
5040 n1 = 0;
5041 else
5042 n1 = n1 % n2;
5043 }
5044 rettv->v_type = VAR_NUMBER;
5045 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048 }
5049
5050 return OK;
5051}
5052
5053/*
5054 * Handle sixth level expression:
5055 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005056 * "string" string constant
5057 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 * &option-name option value
5059 * @r register contents
5060 * identifier variable value
5061 * function() function call
5062 * $VAR environment variable
5063 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005064 * [expr, expr] List
5065 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 *
5067 * Also handle:
5068 * ! in front logical NOT
5069 * - in front unary minus
5070 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 * trailing [] subscript in String or List
5072 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 *
5074 * "arg" must point to the first non-white of the expression.
5075 * "arg" is advanced to the next non-white after the recognized expression.
5076 *
5077 * Return OK or FAIL.
5078 */
5079 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005080eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005084 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 long n;
5087 int len;
5088 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 char_u *start_leader, *end_leader;
5090 int ret = OK;
5091 char_u *alias;
5092
5093 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005095 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
5099 /*
5100 * Skip '!' and '-' characters. They are handled later.
5101 */
5102 start_leader = *arg;
5103 while (**arg == '!' || **arg == '-' || **arg == '+')
5104 *arg = skipwhite(*arg + 1);
5105 end_leader = *arg;
5106
5107 switch (**arg)
5108 {
5109 /*
5110 * Number constant.
5111 */
5112 case '0':
5113 case '1':
5114 case '2':
5115 case '3':
5116 case '4':
5117 case '5':
5118 case '6':
5119 case '7':
5120 case '8':
5121 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 {
5123#ifdef FEAT_FLOAT
5124 char_u *p = skipdigits(*arg + 1);
5125 int get_float = FALSE;
5126
5127 /* We accept a float when the format matches
5128 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005129 * strict to avoid backwards compatibility problems.
5130 * Don't look for a float after the "." operator, so that
5131 * ":let vers = 1.2.3" doesn't fail. */
5132 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005134 get_float = TRUE;
5135 p = skipdigits(p + 2);
5136 if (*p == 'e' || *p == 'E')
5137 {
5138 ++p;
5139 if (*p == '-' || *p == '+')
5140 ++p;
5141 if (!vim_isdigit(*p))
5142 get_float = FALSE;
5143 else
5144 p = skipdigits(p + 1);
5145 }
5146 if (ASCII_ISALPHA(*p) || *p == '.')
5147 get_float = FALSE;
5148 }
5149 if (get_float)
5150 {
5151 float_T f;
5152
5153 *arg += string2float(*arg, &f);
5154 if (evaluate)
5155 {
5156 rettv->v_type = VAR_FLOAT;
5157 rettv->vval.v_float = f;
5158 }
5159 }
5160 else
5161#endif
5162 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005163 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005164 *arg += len;
5165 if (evaluate)
5166 {
5167 rettv->v_type = VAR_NUMBER;
5168 rettv->vval.v_number = n;
5169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 /*
5175 * String constant: "string".
5176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 break;
5179
5180 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 break;
5185
5186 /*
5187 * List: [expr, expr]
5188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191
5192 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 * Dictionary: {key: val, key: val}
5194 */
5195 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5196 break;
5197
5198 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005199 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005201 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /*
5205 * Environment variable: $VAR.
5206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 break;
5209
5210 /*
5211 * Register contents: @r.
5212 */
5213 case '@': ++*arg;
5214 if (evaluate)
5215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005216 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005217 rettv->vval.v_string = get_reg_contents(**arg,
5218 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220 if (**arg != NUL)
5221 ++*arg;
5222 break;
5223
5224 /*
5225 * nested expression: (expression).
5226 */
5227 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (**arg == ')')
5230 ++*arg;
5231 else if (ret == OK)
5232 {
5233 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 ret = FAIL;
5236 }
5237 break;
5238
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 break;
5241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242
5243 if (ret == NOTDONE)
5244 {
5245 /*
5246 * Must be a variable or function name.
5247 * Can also be a curly-braces kind of name: {expr}.
5248 */
5249 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005250 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 if (alias != NULL)
5252 s = alias;
5253
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005254 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 ret = FAIL;
5256 else
5257 {
5258 if (**arg == '(') /* recursive! */
5259 {
5260 /* If "s" is the name of a variable of type VAR_FUNC
5261 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005262 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263
5264 /* Invoke the function. */
5265 ret = get_func_tv(s, len, rettv, arg,
5266 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005267 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005268
5269 /* If evaluate is FALSE rettv->v_type was not set in
5270 * get_func_tv, but it's needed in handle_subscript() to parse
5271 * what follows. So set it here. */
5272 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5273 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005274 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005275 rettv->v_type = VAR_FUNC;
5276 }
5277
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 /* Stop the expression evaluation when immediately
5279 * aborting on error, or when an interrupt occurred or
5280 * an exception was thrown but not caught. */
5281 if (aborting())
5282 {
5283 if (ret == OK)
5284 clear_tv(rettv);
5285 ret = FAIL;
5286 }
5287 }
5288 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005289 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005290 else
5291 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005293 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 }
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 *arg = skipwhite(*arg);
5297
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5299 * expr(expr). */
5300 if (ret == OK)
5301 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 /*
5304 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5305 */
5306 if (ret == OK && evaluate && end_leader > start_leader)
5307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 int val = 0;
5310#ifdef FEAT_FLOAT
5311 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 if (rettv->v_type == VAR_FLOAT)
5314 f = rettv->vval.v_float;
5315 else
5316#endif
5317 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 clear_tv(rettv);
5321 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005323 else
5324 {
5325 while (end_leader > start_leader)
5326 {
5327 --end_leader;
5328 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005329 {
5330#ifdef FEAT_FLOAT
5331 if (rettv->v_type == VAR_FLOAT)
5332 f = !f;
5333 else
5334#endif
5335 val = !val;
5336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005337 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005338 {
5339#ifdef FEAT_FLOAT
5340 if (rettv->v_type == VAR_FLOAT)
5341 f = -f;
5342 else
5343#endif
5344 val = -val;
5345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005347#ifdef FEAT_FLOAT
5348 if (rettv->v_type == VAR_FLOAT)
5349 {
5350 clear_tv(rettv);
5351 rettv->vval.v_float = f;
5352 }
5353 else
5354#endif
5355 {
5356 clear_tv(rettv);
5357 rettv->v_type = VAR_NUMBER;
5358 rettv->vval.v_number = val;
5359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362
5363 return ret;
5364}
5365
5366/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005367 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5368 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5370 */
5371 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005372eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005376 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377{
5378 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005379 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005381 long len = -1;
5382 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005386 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005388 if (verbose)
5389 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 return FAIL;
5391 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005392#ifdef FEAT_FLOAT
5393 else if (rettv->v_type == VAR_FLOAT)
5394 {
5395 if (verbose)
5396 EMSG(_(e_float_as_string));
5397 return FAIL;
5398 }
5399#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005400 else if (rettv->v_type == VAR_SPECIAL)
5401 {
5402 return FAIL;
5403 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005405 init_tv(&var1);
5406 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 /*
5410 * dict.name
5411 */
5412 key = *arg + 1;
5413 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5414 ;
5415 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 }
5419 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421 /*
5422 * something[idx]
5423 *
5424 * Get the (first) variable from inside the [].
5425 */
5426 *arg = skipwhite(*arg + 1);
5427 if (**arg == ':')
5428 empty1 = TRUE;
5429 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5430 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005431 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5432 {
5433 /* not a number or string */
5434 clear_tv(&var1);
5435 return FAIL;
5436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437
5438 /*
5439 * Get the second variable from inside the [:].
5440 */
5441 if (**arg == ':')
5442 {
5443 range = TRUE;
5444 *arg = skipwhite(*arg + 1);
5445 if (**arg == ']')
5446 empty2 = TRUE;
5447 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005449 if (!empty1)
5450 clear_tv(&var1);
5451 return FAIL;
5452 }
5453 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5454 {
5455 /* not a number or string */
5456 if (!empty1)
5457 clear_tv(&var1);
5458 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 return FAIL;
5460 }
5461 }
5462
5463 /* Check for the ']'. */
5464 if (**arg != ']')
5465 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 if (verbose)
5467 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 clear_tv(&var1);
5469 if (range)
5470 clear_tv(&var2);
5471 return FAIL;
5472 }
5473 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475
5476 if (evaluate)
5477 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 n1 = 0;
5479 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 n1 = get_tv_number(&var1);
5482 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 if (range)
5485 {
5486 if (empty2)
5487 n2 = -1;
5488 else
5489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 n2 = get_tv_number(&var2);
5491 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 }
5493 }
5494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 {
5497 case VAR_NUMBER:
5498 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 len = (long)STRLEN(s);
5501 if (range)
5502 {
5503 /* The resulting variable is a substring. If the indexes
5504 * are out of range the result is empty. */
5505 if (n1 < 0)
5506 {
5507 n1 = len + n1;
5508 if (n1 < 0)
5509 n1 = 0;
5510 }
5511 if (n2 < 0)
5512 n2 = len + n2;
5513 else if (n2 >= len)
5514 n2 = len;
5515 if (n1 >= len || n2 < 0 || n1 > n2)
5516 s = NULL;
5517 else
5518 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5519 }
5520 else
5521 {
5522 /* The resulting variable is a string of a single
5523 * character. If the index is too big or negative the
5524 * result is empty. */
5525 if (n1 >= len || n1 < 0)
5526 s = NULL;
5527 else
5528 s = vim_strnsave(s + n1, 1);
5529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 clear_tv(rettv);
5531 rettv->v_type = VAR_STRING;
5532 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 break;
5534
5535 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 if (n1 < 0)
5538 n1 = len + n1;
5539 if (!empty1 && (n1 < 0 || n1 >= len))
5540 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005541 /* For a range we allow invalid values and return an empty
5542 * list. A list index out of range is an error. */
5543 if (!range)
5544 {
5545 if (verbose)
5546 EMSGN(_(e_listidx), n1);
5547 return FAIL;
5548 }
5549 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 }
5551 if (range)
5552 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005553 list_T *l;
5554 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555
5556 if (n2 < 0)
5557 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005558 else if (n2 >= len)
5559 n2 = len - 1;
5560 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005561 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 l = list_alloc();
5563 if (l == NULL)
5564 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 n1 <= n2; ++n1)
5567 {
5568 if (list_append_tv(l, &item->li_tv) == FAIL)
5569 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005570 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 return FAIL;
5572 }
5573 item = item->li_next;
5574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 clear_tv(rettv);
5576 rettv->v_type = VAR_LIST;
5577 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005578 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 }
5580 else
5581 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005582 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 clear_tv(rettv);
5584 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 }
5586 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587
5588 case VAR_DICT:
5589 if (range)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 if (len == -1)
5594 clear_tv(&var1);
5595 return FAIL;
5596 }
5597 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005598 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
5600 if (len == -1)
5601 {
5602 key = get_tv_string(&var1);
5603 if (*key == NUL)
5604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005605 if (verbose)
5606 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 clear_tv(&var1);
5608 return FAIL;
5609 }
5610 }
5611
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005612 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005615 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 if (len == -1)
5617 clear_tv(&var1);
5618 if (item == NULL)
5619 return FAIL;
5620
5621 copy_tv(&item->di_tv, &var1);
5622 clear_tv(rettv);
5623 *rettv = var1;
5624 }
5625 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 }
5627 }
5628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 return OK;
5630}
5631
5632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 * Get an option value.
5634 * "arg" points to the '&' or '+' before the option name.
5635 * "arg" is advanced to character after the option name.
5636 * Return OK or FAIL.
5637 */
5638 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005641 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 int evaluate;
5643{
5644 char_u *option_end;
5645 long numval;
5646 char_u *stringval;
5647 int opt_type;
5648 int c;
5649 int working = (**arg == '+'); /* has("+option") */
5650 int ret = OK;
5651 int opt_flags;
5652
5653 /*
5654 * Isolate the option name and find its value.
5655 */
5656 option_end = find_option_end(arg, &opt_flags);
5657 if (option_end == NULL)
5658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 EMSG2(_("E112: Option name missing: %s"), *arg);
5661 return FAIL;
5662 }
5663
5664 if (!evaluate)
5665 {
5666 *arg = option_end;
5667 return OK;
5668 }
5669
5670 c = *option_end;
5671 *option_end = NUL;
5672 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675 if (opt_type == -3) /* invalid name */
5676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 EMSG2(_("E113: Unknown option: %s"), *arg);
5679 ret = FAIL;
5680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
5683 if (opt_type == -2) /* hidden string option */
5684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 rettv->v_type = VAR_STRING;
5686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 else if (opt_type == -1) /* hidden number option */
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 rettv->v_type = VAR_NUMBER;
5691 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 else if (opt_type == 1) /* number option */
5694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 rettv->v_type = VAR_NUMBER;
5696 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698 else /* string option */
5699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 rettv->v_type = VAR_STRING;
5701 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
5703 }
5704 else if (working && (opt_type == -2 || opt_type == -1))
5705 ret = FAIL;
5706
5707 *option_end = c; /* put back for error messages */
5708 *arg = option_end;
5709
5710 return ret;
5711}
5712
5713/*
5714 * Allocate a variable for a string constant.
5715 * Return OK or FAIL.
5716 */
5717 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 int evaluate;
5722{
5723 char_u *p;
5724 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 int extra = 0;
5726
5727 /*
5728 * Find the end of the string, skipping backslashed characters.
5729 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
5732 if (*p == '\\' && p[1] != NUL)
5733 {
5734 ++p;
5735 /* A "\<x>" form occupies at least 4 characters, and produces up
5736 * to 6 characters: reserve space for 2 extra */
5737 if (*p == '<')
5738 extra += 2;
5739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
5741
5742 if (*p != '"')
5743 {
5744 EMSG2(_("E114: Missing quote: %s"), *arg);
5745 return FAIL;
5746 }
5747
5748 /* If only parsing, set *arg and return here */
5749 if (!evaluate)
5750 {
5751 *arg = p + 1;
5752 return OK;
5753 }
5754
5755 /*
5756 * Copy the string into allocated memory, handling backslashed
5757 * characters.
5758 */
5759 name = alloc((unsigned)(p - *arg + extra));
5760 if (name == NULL)
5761 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 rettv->v_type = VAR_STRING;
5763 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 if (*p == '\\')
5768 {
5769 switch (*++p)
5770 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 case 'b': *name++ = BS; ++p; break;
5772 case 'e': *name++ = ESC; ++p; break;
5773 case 'f': *name++ = FF; ++p; break;
5774 case 'n': *name++ = NL; ++p; break;
5775 case 'r': *name++ = CAR; ++p; break;
5776 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
5778 case 'X': /* hex: "\x1", "\x12" */
5779 case 'x':
5780 case 'u': /* Unicode: "\u0023" */
5781 case 'U':
5782 if (vim_isxdigit(p[1]))
5783 {
5784 int n, nr;
5785 int c = toupper(*p);
5786
5787 if (c == 'X')
5788 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005789 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005791 else
5792 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 nr = 0;
5794 while (--n >= 0 && vim_isxdigit(p[1]))
5795 {
5796 ++p;
5797 nr = (nr << 4) + hex2nr(*p);
5798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800#ifdef FEAT_MBYTE
5801 /* For "\u" store the number according to
5802 * 'encoding'. */
5803 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 else
5806#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 break;
5810
5811 /* octal: "\1", "\12", "\123" */
5812 case '0':
5813 case '1':
5814 case '2':
5815 case '3':
5816 case '4':
5817 case '5':
5818 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 case '7': *name = *p++ - '0';
5820 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 *name = (*name << 3) + *p++ - '0';
5823 if (*p >= '0' && *p <= '7')
5824 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 break;
5828
5829 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (extra != 0)
5832 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 break;
5835 }
5836 /* FALLTHROUGH */
5837
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 break;
5840 }
5841 }
5842 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 *arg = p + 1;
5848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 return OK;
5850}
5851
5852/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 * Return OK or FAIL.
5855 */
5856 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005857get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 int evaluate;
5861{
5862 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 int reduce = 0;
5865
5866 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 */
5869 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 break;
5875 ++reduce;
5876 ++p;
5877 }
5878 }
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 return FAIL;
5884 }
5885
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 if (!evaluate)
5888 {
5889 *arg = p + 1;
5890 return OK;
5891 }
5892
5893 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 */
5896 str = alloc((unsigned)((p - *arg) - reduce));
5897 if (str == NULL)
5898 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 rettv->v_type = VAR_STRING;
5900 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 break;
5908 ++p;
5909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 *arg = p + 1;
5914
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915 return OK;
5916}
5917
5918/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 * Allocate a variable for a List and fill it from "*arg".
5920 * Return OK or FAIL.
5921 */
5922 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005923get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 int evaluate;
5927{
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 list_T *l = NULL;
5929 typval_T tv;
5930 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931
5932 if (evaluate)
5933 {
5934 l = list_alloc();
5935 if (l == NULL)
5936 return FAIL;
5937 }
5938
5939 *arg = skipwhite(*arg + 1);
5940 while (**arg != ']' && **arg != NUL)
5941 {
5942 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5943 goto failret;
5944 if (evaluate)
5945 {
5946 item = listitem_alloc();
5947 if (item != NULL)
5948 {
5949 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005950 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 list_append(l, item);
5952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005953 else
5954 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 }
5956
5957 if (**arg == ']')
5958 break;
5959 if (**arg != ',')
5960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 goto failret;
5963 }
5964 *arg = skipwhite(*arg + 1);
5965 }
5966
5967 if (**arg != ']')
5968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005969 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970failret:
5971 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005972 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 return FAIL;
5974 }
5975
5976 *arg = skipwhite(*arg + 1);
5977 if (evaluate)
5978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 rettv->v_type = VAR_LIST;
5980 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 ++l->lv_refcount;
5982 }
5983
5984 return OK;
5985}
5986
5987/*
5988 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005989 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005991 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992list_alloc()
5993{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005994 list_T *l;
5995
5996 l = (list_T *)alloc_clear(sizeof(list_T));
5997 if (l != NULL)
5998 {
5999 /* Prepend the list to the list of lists for garbage collection. */
6000 if (first_list != NULL)
6001 first_list->lv_used_prev = l;
6002 l->lv_used_prev = NULL;
6003 l->lv_used_next = first_list;
6004 first_list = l;
6005 }
6006 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007}
6008
6009/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006010 * Allocate an empty list for a return value.
6011 * Returns OK or FAIL.
6012 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006013 int
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006014rettv_list_alloc(rettv)
6015 typval_T *rettv;
6016{
6017 list_T *l = list_alloc();
6018
6019 if (l == NULL)
6020 return FAIL;
6021
6022 rettv->vval.v_list = l;
6023 rettv->v_type = VAR_LIST;
6024 ++l->lv_refcount;
6025 return OK;
6026}
6027
6028/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 * Unreference a list: decrement the reference count and free it when it
6030 * becomes zero.
6031 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006032 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006036 if (l != NULL && --l->lv_refcount <= 0)
6037 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038}
6039
6040/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006041 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 * Ignores the reference count.
6043 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006044 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045list_free(l, recurse)
6046 list_T *l;
6047 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048{
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006051 /* Remove the list from the list of lists for garbage collection. */
6052 if (l->lv_used_prev == NULL)
6053 first_list = l->lv_used_next;
6054 else
6055 l->lv_used_prev->lv_used_next = l->lv_used_next;
6056 if (l->lv_used_next != NULL)
6057 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6058
Bram Moolenaard9fba312005-06-26 22:34:35 +00006059 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006061 /* Remove the item before deleting it. */
6062 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006063 if (recurse || (item->li_tv.v_type != VAR_LIST
6064 && item->li_tv.v_type != VAR_DICT))
6065 clear_tv(&item->li_tv);
6066 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 }
6068 vim_free(l);
6069}
6070
6071/*
6072 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006073 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006075 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076listitem_alloc()
6077{
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079}
6080
6081/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006082 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006084 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006088 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 vim_free(item);
6090}
6091
6092/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006093 * Remove a list item from a List and free it. Also clears the value.
6094 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006095 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006096listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 list_T *l;
6098 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006099{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006100 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006101 listitem_free(item);
6102}
6103
6104/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006105 * Get the number of items in a list.
6106 */
6107 static long
6108list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111 if (l == NULL)
6112 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006113 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114}
6115
6116/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 * Return TRUE when two lists have exactly the same values.
6118 */
6119 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006120list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 list_T *l1;
6122 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125{
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006128 if (l1 == NULL || l2 == NULL)
6129 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006130 if (l1 == l2)
6131 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 if (list_len(l1) != list_len(l2))
6133 return FALSE;
6134
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 for (item1 = l1->lv_first, item2 = l2->lv_first;
6136 item1 != NULL && item2 != NULL;
6137 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006138 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 return FALSE;
6140 return item1 == NULL && item2 == NULL;
6141}
6142
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006143#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6144 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006145/*
6146 * Return the dictitem that an entry in a hashtable points to.
6147 */
6148 dictitem_T *
6149dict_lookup(hi)
6150 hashitem_T *hi;
6151{
6152 return HI2DI(hi);
6153}
6154#endif
6155
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157 * Return TRUE when two dictionaries have exactly the same key/values.
6158 */
6159 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 dict_T *d1;
6162 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165{
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 hashitem_T *hi;
6167 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006170 if (d1 == NULL || d2 == NULL)
6171 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006172 if (d1 == d2)
6173 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006174 if (dict_len(d1) != dict_len(d2))
6175 return FALSE;
6176
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006177 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006178 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006180 if (!HASHITEM_EMPTY(hi))
6181 {
6182 item2 = dict_find(d2, hi->hi_key, -1);
6183 if (item2 == NULL)
6184 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186 return FALSE;
6187 --todo;
6188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006189 }
6190 return TRUE;
6191}
6192
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006193static int tv_equal_recurse_limit;
6194
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006195/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006198 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 */
6200 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006202 typval_T *tv1;
6203 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006204 int ic; /* ignore case */
6205 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206{
6207 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006208 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006211
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006215 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006216 * recursiveness to a limit. We guess they are equal then.
6217 * A fixed limit has the problem of still taking an awful long time.
6218 * Reduce the limit every time running into it. That should work fine for
6219 * deeply linked structures that are not recursively linked and catch
6220 * recursiveness quickly. */
6221 if (!recursive)
6222 tv_equal_recurse_limit = 1000;
6223 if (recursive_cnt >= tv_equal_recurse_limit)
6224 {
6225 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006226 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006227 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006232 ++recursive_cnt;
6233 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6234 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006235 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006236
6237 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006238 ++recursive_cnt;
6239 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6240 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006241 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242
6243 case VAR_FUNC:
6244 return (tv1->vval.v_string != NULL
6245 && tv2->vval.v_string != NULL
6246 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6247
6248 case VAR_NUMBER:
6249 return tv1->vval.v_number == tv2->vval.v_number;
6250
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006251#ifdef FEAT_FLOAT
6252 case VAR_FLOAT:
6253 return tv1->vval.v_float == tv2->vval.v_float;
6254#endif
6255
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006256 case VAR_STRING:
6257 s1 = get_tv_string_buf(tv1, buf1);
6258 s2 = get_tv_string_buf(tv2, buf2);
6259 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006260
6261 case VAR_SPECIAL:
6262 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006263 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006264
6265 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266 return TRUE;
6267}
6268
6269/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 * Locate item with index "n" in list "l" and return it.
6271 * A negative index is counted from the end; -1 is the last item.
6272 * Returns NULL when "n" is out of range.
6273 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006274 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 long n;
6278{
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006280 long idx;
6281
6282 if (l == NULL)
6283 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284
6285 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006287 n = l->lv_len + n;
6288
6289 /* Check for index out of range. */
6290 if (n < 0 || n >= l->lv_len)
6291 return NULL;
6292
6293 /* When there is a cached index may start search from there. */
6294 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006296 if (n < l->lv_idx / 2)
6297 {
6298 /* closest to the start of the list */
6299 item = l->lv_first;
6300 idx = 0;
6301 }
6302 else if (n > (l->lv_idx + l->lv_len) / 2)
6303 {
6304 /* closest to the end of the list */
6305 item = l->lv_last;
6306 idx = l->lv_len - 1;
6307 }
6308 else
6309 {
6310 /* closest to the cached index */
6311 item = l->lv_idx_item;
6312 idx = l->lv_idx;
6313 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 }
6315 else
6316 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 if (n < l->lv_len / 2)
6318 {
6319 /* closest to the start of the list */
6320 item = l->lv_first;
6321 idx = 0;
6322 }
6323 else
6324 {
6325 /* closest to the end of the list */
6326 item = l->lv_last;
6327 idx = l->lv_len - 1;
6328 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330
6331 while (n > idx)
6332 {
6333 /* search forward */
6334 item = item->li_next;
6335 ++idx;
6336 }
6337 while (n < idx)
6338 {
6339 /* search backward */
6340 item = item->li_prev;
6341 --idx;
6342 }
6343
6344 /* cache the used index */
6345 l->lv_idx = idx;
6346 l->lv_idx_item = item;
6347
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return item;
6349}
6350
6351/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006352 * Get list item "l[idx]" as a number.
6353 */
6354 static long
6355list_find_nr(l, idx, errorp)
6356 list_T *l;
6357 long idx;
6358 int *errorp; /* set to TRUE when something wrong */
6359{
6360 listitem_T *li;
6361
6362 li = list_find(l, idx);
6363 if (li == NULL)
6364 {
6365 if (errorp != NULL)
6366 *errorp = TRUE;
6367 return -1L;
6368 }
6369 return get_tv_number_chk(&li->li_tv, errorp);
6370}
6371
6372/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006373 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6374 */
6375 char_u *
6376list_find_str(l, idx)
6377 list_T *l;
6378 long idx;
6379{
6380 listitem_T *li;
6381
6382 li = list_find(l, idx - 1);
6383 if (li == NULL)
6384 {
6385 EMSGN(_(e_listidx), idx);
6386 return NULL;
6387 }
6388 return get_tv_string(&li->li_tv);
6389}
6390
6391/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006392 * Locate "item" list "l" and return its index.
6393 * Returns -1 when "item" is not in the list.
6394 */
6395 static long
6396list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 list_T *l;
6398 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006399{
6400 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006402
6403 if (l == NULL)
6404 return -1;
6405 idx = 0;
6406 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6407 ++idx;
6408 if (li == NULL)
6409 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006410 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006411}
6412
6413/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 * Append item "item" to the end of list "l".
6415 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006416 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 list_T *l;
6419 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420{
6421 if (l->lv_last == NULL)
6422 {
6423 /* empty list */
6424 l->lv_first = item;
6425 l->lv_last = item;
6426 item->li_prev = NULL;
6427 }
6428 else
6429 {
6430 l->lv_last->li_next = item;
6431 item->li_prev = l->lv_last;
6432 l->lv_last = item;
6433 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006434 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 item->li_next = NULL;
6436}
6437
6438/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006442 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 list_T *l;
6445 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006447 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448
Bram Moolenaar05159a02005-02-26 23:04:13 +00006449 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006451 copy_tv(tv, &li->li_tv);
6452 list_append(l, li);
6453 return OK;
6454}
6455
6456/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006457 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006458 * Return FAIL when out of memory.
6459 */
6460 int
6461list_append_dict(list, dict)
6462 list_T *list;
6463 dict_T *dict;
6464{
6465 listitem_T *li = listitem_alloc();
6466
6467 if (li == NULL)
6468 return FAIL;
6469 li->li_tv.v_type = VAR_DICT;
6470 li->li_tv.v_lock = 0;
6471 li->li_tv.vval.v_dict = dict;
6472 list_append(list, li);
6473 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474 return OK;
6475}
6476
6477/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006478 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006479 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006480 * Returns FAIL when out of memory.
6481 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006482 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006483list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006484 list_T *l;
6485 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006486 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006487{
6488 listitem_T *li = listitem_alloc();
6489
6490 if (li == NULL)
6491 return FAIL;
6492 list_append(l, li);
6493 li->li_tv.v_type = VAR_STRING;
6494 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006495 if (str == NULL)
6496 li->li_tv.vval.v_string = NULL;
6497 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006498 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006499 return FAIL;
6500 return OK;
6501}
6502
6503/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006504 * Append "n" to list "l".
6505 * Returns FAIL when out of memory.
6506 */
6507 static int
6508list_append_number(l, n)
6509 list_T *l;
6510 varnumber_T n;
6511{
6512 listitem_T *li;
6513
6514 li = listitem_alloc();
6515 if (li == NULL)
6516 return FAIL;
6517 li->li_tv.v_type = VAR_NUMBER;
6518 li->li_tv.v_lock = 0;
6519 li->li_tv.vval.v_number = n;
6520 list_append(l, li);
6521 return OK;
6522}
6523
6524/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 * If "item" is NULL append at the end.
6527 * Return FAIL when out of memory.
6528 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006529 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 list_T *l;
6532 typval_T *tv;
6533 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534{
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536
6537 if (ni == NULL)
6538 return FAIL;
6539 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006540 list_insert(l, ni, item);
6541 return OK;
6542}
6543
6544 void
6545list_insert(l, ni, item)
6546 list_T *l;
6547 listitem_T *ni;
6548 listitem_T *item;
6549{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 if (item == NULL)
6551 /* Append new item at end of list. */
6552 list_append(l, ni);
6553 else
6554 {
6555 /* Insert new item before existing item. */
6556 ni->li_prev = item->li_prev;
6557 ni->li_next = item;
6558 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006559 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 ++l->lv_idx;
6562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006564 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 l->lv_idx_item = NULL;
6567 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006569 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571}
6572
6573/*
6574 * Extend "l1" with "l2".
6575 * If "bef" is NULL append at the end, otherwise insert before this item.
6576 * Returns FAIL when out of memory.
6577 */
6578 static int
6579list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 list_T *l1;
6581 list_T *l2;
6582 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583{
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006585 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006587 /* We also quit the loop when we have inserted the original item count of
6588 * the list, avoid a hang when we extend a list with itself. */
6589 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6591 return FAIL;
6592 return OK;
6593}
6594
6595/*
6596 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6597 * Return FAIL when out of memory.
6598 */
6599 static int
6600list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 list_T *l1;
6602 list_T *l2;
6603 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006604{
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006607 if (l1 == NULL || l2 == NULL)
6608 return FAIL;
6609
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006610 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 if (l == NULL)
6613 return FAIL;
6614 tv->v_type = VAR_LIST;
6615 tv->vval.v_list = l;
6616
6617 /* append all items from the second list */
6618 return list_extend(l, l2, NULL);
6619}
6620
6621/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006622 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006624 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 * Returns NULL when out of memory.
6626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006627 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006631 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632{
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 list_T *copy;
6634 listitem_T *item;
6635 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636
6637 if (orig == NULL)
6638 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639
6640 copy = list_alloc();
6641 if (copy != NULL)
6642 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006643 if (copyID != 0)
6644 {
6645 /* Do this before adding the items, because one of the items may
6646 * refer back to this list. */
6647 orig->lv_copyID = copyID;
6648 orig->lv_copylist = copy;
6649 }
6650 for (item = orig->lv_first; item != NULL && !got_int;
6651 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652 {
6653 ni = listitem_alloc();
6654 if (ni == NULL)
6655 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006656 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006657 {
6658 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6659 {
6660 vim_free(ni);
6661 break;
6662 }
6663 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006665 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 list_append(copy, ni);
6667 }
6668 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006669 if (item != NULL)
6670 {
6671 list_unref(copy);
6672 copy = NULL;
6673 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674 }
6675
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 return copy;
6677}
6678
6679/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006681 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006682 * This used to be called list_remove, but that conflicts with a Sun header
6683 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006685 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006686vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *l;
6688 listitem_T *item;
6689 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006690{
Bram Moolenaar33570922005-01-25 22:26:29 +00006691 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006693 /* notify watchers */
6694 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006696 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006697 list_fix_watch(l, ip);
6698 if (ip == item2)
6699 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006701
6702 if (item2->li_next == NULL)
6703 l->lv_last = item->li_prev;
6704 else
6705 item2->li_next->li_prev = item->li_prev;
6706 if (item->li_prev == NULL)
6707 l->lv_first = item2->li_next;
6708 else
6709 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006710 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006711}
6712
6713/*
6714 * Return an allocated string with the string representation of a list.
6715 * May return NULL.
6716 */
6717 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006718list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006719 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006720 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721{
6722 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006723
6724 if (tv->vval.v_list == NULL)
6725 return NULL;
6726 ga_init2(&ga, (int)sizeof(char), 80);
6727 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006728 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006729 {
6730 vim_free(ga.ga_data);
6731 return NULL;
6732 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 ga_append(&ga, ']');
6734 ga_append(&ga, NUL);
6735 return (char_u *)ga.ga_data;
6736}
6737
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006738typedef struct join_S {
6739 char_u *s;
6740 char_u *tofree;
6741} join_T;
6742
6743 static int
6744list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6745 garray_T *gap; /* to store the result in */
6746 list_T *l;
6747 char_u *sep;
6748 int echo_style;
6749 int copyID;
6750 garray_T *join_gap; /* to keep each list item string */
6751{
6752 int i;
6753 join_T *p;
6754 int len;
6755 int sumlen = 0;
6756 int first = TRUE;
6757 char_u *tofree;
6758 char_u numbuf[NUMBUFLEN];
6759 listitem_T *item;
6760 char_u *s;
6761
6762 /* Stringify each item in the list. */
6763 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6764 {
6765 if (echo_style)
6766 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6767 else
6768 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6769 if (s == NULL)
6770 return FAIL;
6771
6772 len = (int)STRLEN(s);
6773 sumlen += len;
6774
Bram Moolenaarcde88542015-08-11 19:14:00 +02006775 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006776 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6777 if (tofree != NULL || s != numbuf)
6778 {
6779 p->s = s;
6780 p->tofree = tofree;
6781 }
6782 else
6783 {
6784 p->s = vim_strnsave(s, len);
6785 p->tofree = p->s;
6786 }
6787
6788 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006789 if (did_echo_string_emsg) /* recursion error, bail out */
6790 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 }
6792
6793 /* Allocate result buffer with its total size, avoid re-allocation and
6794 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6795 if (join_gap->ga_len >= 2)
6796 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6797 if (ga_grow(gap, sumlen + 2) == FAIL)
6798 return FAIL;
6799
6800 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6801 {
6802 if (first)
6803 first = FALSE;
6804 else
6805 ga_concat(gap, sep);
6806 p = ((join_T *)join_gap->ga_data) + i;
6807
6808 if (p->s != NULL)
6809 ga_concat(gap, p->s);
6810 line_breakcheck();
6811 }
6812
6813 return OK;
6814}
6815
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006816/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006818 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006819 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006821 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006822list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006826 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006827 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006829 garray_T join_ga;
6830 int retval;
6831 join_T *p;
6832 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006833
Bram Moolenaard39a7512015-04-16 22:51:22 +02006834 if (l->lv_len < 1)
6835 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006836 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6837 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6838
6839 /* Dispose each item in join_ga. */
6840 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006841 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006842 p = (join_T *)join_ga.ga_data;
6843 for (i = 0; i < join_ga.ga_len; ++i)
6844 {
6845 vim_free(p->tofree);
6846 ++p;
6847 }
6848 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006849 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006850
6851 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006852}
6853
6854/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006855 * Return the next (unique) copy ID.
6856 * Used for serializing nested structures.
6857 */
6858 int
6859get_copyID()
6860{
6861 current_copyID += COPYID_INC;
6862 return current_copyID;
6863}
6864
6865/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 * Garbage collection for lists and dictionaries.
6867 *
6868 * We use reference counts to be able to free most items right away when they
6869 * are no longer used. But for composite items it's possible that it becomes
6870 * unused while the reference count is > 0: When there is a recursive
6871 * reference. Example:
6872 * :let l = [1, 2, 3]
6873 * :let d = {9: l}
6874 * :let l[1] = d
6875 *
6876 * Since this is quite unusual we handle this with garbage collection: every
6877 * once in a while find out which lists and dicts are not referenced from any
6878 * variable.
6879 *
6880 * Here is a good reference text about garbage collection (refers to Python
6881 * but it applies to all reference-counting mechanisms):
6882 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006883 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006884
6885/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 * Do garbage collection for lists and dicts.
6887 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006888 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 int
6890garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006891{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006892 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894 buf_T *buf;
6895 win_T *wp;
6896 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006897 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006898 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900#ifdef FEAT_WINDOWS
6901 tabpage_T *tp;
6902#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006904 /* Only do this once. */
6905 want_garbage_collect = FALSE;
6906 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006907 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006908
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 /* We advance by two because we add one for items referenced through
6910 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006911 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006912
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913 /*
6914 * 1. Go through all accessible variables and mark all lists and dicts
6915 * with copyID.
6916 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006917
6918 /* Don't free variables in the previous_funccal list unless they are only
6919 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006920 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006921 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6922 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6924 NULL);
6925 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6926 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006927 }
6928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 /* script-local variables */
6930 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006932
6933 /* buffer-local variables */
6934 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6936 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937
6938 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006939 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6941 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006942#ifdef FEAT_AUTOCMD
6943 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6945 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006946#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006948#ifdef FEAT_WINDOWS
6949 /* tabpage-local variables */
6950 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6952 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006953#endif
6954
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957
6958 /* function-local variables */
6959 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6960 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6962 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 }
6964
Bram Moolenaard812df62008-11-09 12:46:09 +00006965 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006967
Bram Moolenaar1dced572012-04-05 16:54:08 +02006968#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006970#endif
6971
Bram Moolenaardb913952012-06-29 12:54:53 +02006972#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006973 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006974#endif
6975
6976#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006978#endif
6979
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 /*
6983 * 2. Free lists and dictionaries that are not referenced.
6984 */
6985 did_free = free_unref_items(copyID);
6986
6987 /*
6988 * 3. Check if any funccal can be freed now.
6989 */
6990 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006992 if (can_free_funccal(*pfc, copyID))
6993 {
6994 fc = *pfc;
6995 *pfc = fc->caller;
6996 free_funccal(fc, TRUE);
6997 did_free = TRUE;
6998 did_free_funccal = TRUE;
6999 }
7000 else
7001 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 if (did_free_funccal)
7004 /* When a funccal was freed some more items might be garbage
7005 * collected, so run again. */
7006 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007007 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007008 else if (p_verbose > 0)
7009 {
7010 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7011 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007012
7013 return did_free;
7014}
7015
7016/*
7017 * Free lists and dictionaries that are no longer referenced.
7018 */
7019 static int
7020free_unref_items(copyID)
7021 int copyID;
7022{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 dict_T *dd, *dd_next;
7024 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 int did_free = FALSE;
7026
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007028 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 */
7030 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007031 {
7032 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007033 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007035 /* Free the Dictionary and ordinary items it contains, but don't
7036 * recurse into Lists and Dictionaries, they will be in the list
7037 * of dicts or list of lists. */
7038 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007041 dd = dd_next;
7042 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043
7044 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007045 * Go through the list of lists and free items without the copyID.
7046 * But don't free a list that has a watcher (used in a for loop), these
7047 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 */
7049 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007050 {
7051 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007052 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7053 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007054 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007055 /* Free the List and ordinary items it contains, but don't recurse
7056 * into Lists and Dictionaries, they will be in the list of dicts
7057 * or list of lists. */
7058 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007059 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007061 ll = ll_next;
7062 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 return did_free;
7064}
7065
7066/*
7067 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 * "list_stack" is used to add lists to be marked. Can be NULL.
7069 *
7070 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 int
7073set_ref_in_ht(ht, copyID, list_stack)
7074 hashtab_T *ht;
7075 int copyID;
7076 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077{
7078 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 hashtab_T *cur_ht;
7082 ht_stack_T *ht_stack = NULL;
7083 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 cur_ht = ht;
7086 for (;;)
7087 {
7088 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007090 /* Mark each item in the hashtab. If the item contains a hashtab
7091 * it is added to ht_stack, if it contains a list it is added to
7092 * list_stack. */
7093 todo = (int)cur_ht->ht_used;
7094 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7095 if (!HASHITEM_EMPTY(hi))
7096 {
7097 --todo;
7098 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7099 &ht_stack, list_stack);
7100 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102
7103 if (ht_stack == NULL)
7104 break;
7105
7106 /* take an item from the stack */
7107 cur_ht = ht_stack->ht;
7108 tempitem = ht_stack;
7109 ht_stack = ht_stack->prev;
7110 free(tempitem);
7111 }
7112
7113 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114}
7115
7116/*
7117 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7119 *
7120 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 int
7123set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124 list_T *l;
7125 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007126 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 listitem_T *li;
7129 int abort = FALSE;
7130 list_T *cur_l;
7131 list_stack_T *list_stack = NULL;
7132 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007133
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 cur_l = l;
7135 for (;;)
7136 {
7137 if (!abort)
7138 /* Mark each item in the list. If the item contains a hashtab
7139 * it is added to ht_stack, if it contains a list it is added to
7140 * list_stack. */
7141 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7142 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7143 ht_stack, &list_stack);
7144 if (list_stack == NULL)
7145 break;
7146
7147 /* take an item from the stack */
7148 cur_l = list_stack->list;
7149 tempitem = list_stack;
7150 list_stack = list_stack->prev;
7151 free(tempitem);
7152 }
7153
7154 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007155}
7156
7157/*
7158 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007159 * "list_stack" is used to add lists to be marked. Can be NULL.
7160 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7161 *
7162 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007163 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007164 int
7165set_ref_in_item(tv, copyID, ht_stack, list_stack)
7166 typval_T *tv;
7167 int copyID;
7168 ht_stack_T **ht_stack;
7169 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170{
7171 dict_T *dd;
7172 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174
7175 switch (tv->v_type)
7176 {
7177 case VAR_DICT:
7178 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007179 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007181 /* Didn't see this dict yet. */
7182 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 if (ht_stack == NULL)
7184 {
7185 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7186 }
7187 else
7188 {
7189 ht_stack_T *newitem = (ht_stack_T*)malloc(
7190 sizeof(ht_stack_T));
7191 if (newitem == NULL)
7192 abort = TRUE;
7193 else
7194 {
7195 newitem->ht = &dd->dv_hashtab;
7196 newitem->prev = *ht_stack;
7197 *ht_stack = newitem;
7198 }
7199 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007200 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007202
7203 case VAR_LIST:
7204 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007205 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007206 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007207 /* Didn't see this list yet. */
7208 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007209 if (list_stack == NULL)
7210 {
7211 abort = set_ref_in_list(ll, copyID, ht_stack);
7212 }
7213 else
7214 {
7215 list_stack_T *newitem = (list_stack_T*)malloc(
7216 sizeof(list_stack_T));
7217 if (newitem == NULL)
7218 abort = TRUE;
7219 else
7220 {
7221 newitem->list = ll;
7222 newitem->prev = *list_stack;
7223 *list_stack = newitem;
7224 }
7225 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007226 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007227 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007228 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007229 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007230}
7231
7232/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233 * Allocate an empty header for a dictionary.
7234 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007235 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236dict_alloc()
7237{
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239
Bram Moolenaar33570922005-01-25 22:26:29 +00007240 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007242 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007243 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007244 if (first_dict != NULL)
7245 first_dict->dv_used_prev = d;
7246 d->dv_used_next = first_dict;
7247 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007248 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007249
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007251 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007252 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007253 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007254 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007255 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257}
7258
7259/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007260 * Allocate an empty dict for a return value.
7261 * Returns OK or FAIL.
7262 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007263 int
Bram Moolenaara800b422010-06-27 01:15:55 +02007264rettv_dict_alloc(rettv)
7265 typval_T *rettv;
7266{
7267 dict_T *d = dict_alloc();
7268
7269 if (d == NULL)
7270 return FAIL;
7271
7272 rettv->vval.v_dict = d;
7273 rettv->v_type = VAR_DICT;
7274 ++d->dv_refcount;
7275 return OK;
7276}
7277
7278
7279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 * Unreference a Dictionary: decrement the reference count and free it when it
7281 * becomes zero.
7282 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007283 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007287 if (d != NULL && --d->dv_refcount <= 0)
7288 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289}
7290
7291/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007292 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 * Ignores the reference count.
7294 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007295 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007296dict_free(d, recurse)
7297 dict_T *d;
7298 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007302 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007304 /* Remove the dict from the list of dicts for garbage collection. */
7305 if (d->dv_used_prev == NULL)
7306 first_dict = d->dv_used_next;
7307 else
7308 d->dv_used_prev->dv_used_next = d->dv_used_next;
7309 if (d->dv_used_next != NULL)
7310 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7311
7312 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007313 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007314 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 if (!HASHITEM_EMPTY(hi))
7318 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007319 /* Remove the item before deleting it, just in case there is
7320 * something recursive causing trouble. */
7321 di = HI2DI(hi);
7322 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007323 if (recurse || (di->di_tv.v_type != VAR_LIST
7324 && di->di_tv.v_type != VAR_DICT))
7325 clear_tv(&di->di_tv);
7326 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 --todo;
7328 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 vim_free(d);
7332}
7333
7334/*
7335 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 * The "key" is copied to the new item.
7337 * Note that the value of the item "di_tv" still needs to be initialized!
7338 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007340 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341dictitem_alloc(key)
7342 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007346 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007350 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007351 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353}
7354
7355/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007356 * Make a copy of a Dictionary item.
7357 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007359dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361{
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007364 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7365 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 if (di != NULL)
7367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007369 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 copy_tv(&org->di_tv, &di->di_tv);
7371 }
7372 return di;
7373}
7374
7375/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 * Remove item "item" from Dictionary "dict" and free it.
7377 */
7378 static void
7379dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dict_T *dict;
7381 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382{
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 if (HASHITEM_EMPTY(hi))
7387 EMSG2(_(e_intern2), "dictitem_remove()");
7388 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 dictitem_free(item);
7391}
7392
7393/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 * Free a dict item. Also clears the value.
7395 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007396 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007401 if (item->di_flags & DI_FLAGS_ALLOC)
7402 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403}
7404
7405/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7407 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007408 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 * Returns NULL when out of memory.
7410 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007412dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007415 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416{
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 dict_T *copy;
7418 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007419 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007420 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421
7422 if (orig == NULL)
7423 return NULL;
7424
7425 copy = dict_alloc();
7426 if (copy != NULL)
7427 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007428 if (copyID != 0)
7429 {
7430 orig->dv_copyID = copyID;
7431 orig->dv_copydict = copy;
7432 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007433 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007434 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 --todo;
7439
7440 di = dictitem_alloc(hi->hi_key);
7441 if (di == NULL)
7442 break;
7443 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007444 {
7445 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7446 copyID) == FAIL)
7447 {
7448 vim_free(di);
7449 break;
7450 }
7451 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 else
7453 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7454 if (dict_add(copy, di) == FAIL)
7455 {
7456 dictitem_free(di);
7457 break;
7458 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007461
Bram Moolenaare9a41262005-01-15 22:18:47 +00007462 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007463 if (todo > 0)
7464 {
7465 dict_unref(copy);
7466 copy = NULL;
7467 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 }
7469
7470 return copy;
7471}
7472
7473/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007475 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007477 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 dict_T *d;
7480 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481{
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483}
7484
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007486 * Add a number or string entry to dictionary "d".
7487 * When "str" is NULL use number "nr", otherwise use "str".
7488 * Returns FAIL when out of memory and when key already exists.
7489 */
7490 int
7491dict_add_nr_str(d, key, nr, str)
7492 dict_T *d;
7493 char *key;
7494 long nr;
7495 char_u *str;
7496{
7497 dictitem_T *item;
7498
7499 item = dictitem_alloc((char_u *)key);
7500 if (item == NULL)
7501 return FAIL;
7502 item->di_tv.v_lock = 0;
7503 if (str == NULL)
7504 {
7505 item->di_tv.v_type = VAR_NUMBER;
7506 item->di_tv.vval.v_number = nr;
7507 }
7508 else
7509 {
7510 item->di_tv.v_type = VAR_STRING;
7511 item->di_tv.vval.v_string = vim_strsave(str);
7512 }
7513 if (dict_add(d, item) == FAIL)
7514 {
7515 dictitem_free(item);
7516 return FAIL;
7517 }
7518 return OK;
7519}
7520
7521/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007522 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007523 * Returns FAIL when out of memory and when key already exists.
7524 */
7525 int
7526dict_add_list(d, key, list)
7527 dict_T *d;
7528 char *key;
7529 list_T *list;
7530{
7531 dictitem_T *item;
7532
7533 item = dictitem_alloc((char_u *)key);
7534 if (item == NULL)
7535 return FAIL;
7536 item->di_tv.v_lock = 0;
7537 item->di_tv.v_type = VAR_LIST;
7538 item->di_tv.vval.v_list = list;
7539 if (dict_add(d, item) == FAIL)
7540 {
7541 dictitem_free(item);
7542 return FAIL;
7543 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007544 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007545 return OK;
7546}
7547
7548/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549 * Get the number of items in a Dictionary.
7550 */
7551 static long
7552dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007554{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007555 if (d == NULL)
7556 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007557 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007558}
7559
7560/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 * Find item "key[len]" in Dictionary "d".
7562 * If "len" is negative use strlen(key).
7563 * Returns NULL when not found.
7564 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007565 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 char_u *key;
7569 int len;
7570{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571#define AKEYLEN 200
7572 char_u buf[AKEYLEN];
7573 char_u *akey;
7574 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 if (len < 0)
7578 akey = key;
7579 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 tofree = akey = vim_strnsave(key, len);
7582 if (akey == NULL)
7583 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007584 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007585 else
7586 {
7587 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007588 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007589 akey = buf;
7590 }
7591
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593 vim_free(tofree);
7594 if (HASHITEM_EMPTY(hi))
7595 return NULL;
7596 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597}
7598
7599/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007600 * Get a string item from a dictionary.
7601 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007602 * Returns NULL if the entry doesn't exist or out of memory.
7603 */
7604 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007605get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007606 dict_T *d;
7607 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007608 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007609{
7610 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007611 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007612
7613 di = dict_find(d, key, -1);
7614 if (di == NULL)
7615 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007616 s = get_tv_string(&di->di_tv);
7617 if (save && s != NULL)
7618 s = vim_strsave(s);
7619 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007620}
7621
7622/*
7623 * Get a number item from a dictionary.
7624 * Returns 0 if the entry doesn't exist or out of memory.
7625 */
7626 long
7627get_dict_number(d, key)
7628 dict_T *d;
7629 char_u *key;
7630{
7631 dictitem_T *di;
7632
7633 di = dict_find(d, key, -1);
7634 if (di == NULL)
7635 return 0;
7636 return get_tv_number(&di->di_tv);
7637}
7638
7639/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 * Return an allocated string with the string representation of a Dictionary.
7641 * May return NULL.
7642 */
7643 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007644dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007645 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007646 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647{
7648 garray_T ga;
7649 int first = TRUE;
7650 char_u *tofree;
7651 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007654 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 return NULL;
7659 ga_init2(&ga, (int)sizeof(char), 80);
7660 ga_append(&ga, '{');
7661
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007662 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007663 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007665 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 --todo;
7668
7669 if (first)
7670 first = FALSE;
7671 else
7672 ga_concat(&ga, (char_u *)", ");
7673
7674 tofree = string_quote(hi->hi_key, FALSE);
7675 if (tofree != NULL)
7676 {
7677 ga_concat(&ga, tofree);
7678 vim_free(tofree);
7679 }
7680 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007681 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007682 if (s != NULL)
7683 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007685 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007686 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007687 line_breakcheck();
7688
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007691 if (todo > 0)
7692 {
7693 vim_free(ga.ga_data);
7694 return NULL;
7695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696
7697 ga_append(&ga, '}');
7698 ga_append(&ga, NUL);
7699 return (char_u *)ga.ga_data;
7700}
7701
7702/*
7703 * Allocate a variable for a Dictionary and fill it from "*arg".
7704 * Return OK or FAIL. Returns NOTDONE for {expr}.
7705 */
7706 static int
7707get_dict_tv(arg, rettv, evaluate)
7708 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 int evaluate;
7711{
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 dict_T *d = NULL;
7713 typval_T tvkey;
7714 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007715 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007718 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719
7720 /*
7721 * First check if it's not a curly-braces thing: {expr}.
7722 * Must do this without evaluating, otherwise a function may be called
7723 * twice. Unfortunately this means we need to call eval1() twice for the
7724 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007727 if (*start != '}')
7728 {
7729 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7730 return FAIL;
7731 if (*start == '}')
7732 return NOTDONE;
7733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734
7735 if (evaluate)
7736 {
7737 d = dict_alloc();
7738 if (d == NULL)
7739 return FAIL;
7740 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007741 tvkey.v_type = VAR_UNKNOWN;
7742 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743
7744 *arg = skipwhite(*arg + 1);
7745 while (**arg != '}' && **arg != NUL)
7746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748 goto failret;
7749 if (**arg != ':')
7750 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007751 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007752 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 goto failret;
7754 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007755 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007757 key = get_tv_string_buf_chk(&tvkey, buf);
7758 if (key == NULL || *key == NUL)
7759 {
7760 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7761 if (key != NULL)
7762 EMSG(_(e_emptykey));
7763 clear_tv(&tvkey);
7764 goto failret;
7765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767
7768 *arg = skipwhite(*arg + 1);
7769 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7770 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007771 if (evaluate)
7772 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007773 goto failret;
7774 }
7775 if (evaluate)
7776 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007777 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778 if (item != NULL)
7779 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007780 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007781 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782 clear_tv(&tv);
7783 goto failret;
7784 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007785 item = dictitem_alloc(key);
7786 clear_tv(&tvkey);
7787 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007789 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007790 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007791 if (dict_add(d, item) == FAIL)
7792 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007793 }
7794 }
7795
7796 if (**arg == '}')
7797 break;
7798 if (**arg != ',')
7799 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007800 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007801 goto failret;
7802 }
7803 *arg = skipwhite(*arg + 1);
7804 }
7805
7806 if (**arg != '}')
7807 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007808 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007809failret:
7810 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007811 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007812 return FAIL;
7813 }
7814
7815 *arg = skipwhite(*arg + 1);
7816 if (evaluate)
7817 {
7818 rettv->v_type = VAR_DICT;
7819 rettv->vval.v_dict = d;
7820 ++d->dv_refcount;
7821 }
7822
7823 return OK;
7824}
7825
Bram Moolenaar8c711452005-01-14 21:53:12 +00007826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 * Return a string with the string representation of a variable.
7828 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007829 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007830 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007831 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007832 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 */
7834 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007835echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007837 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007838 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007839 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 static int recurse = 0;
7842 char_u *r = NULL;
7843
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007846 if (!did_echo_string_emsg)
7847 {
7848 /* Only give this message once for a recursive call to avoid
7849 * flooding the user with errors. And stop iterating over lists
7850 * and dicts. */
7851 did_echo_string_emsg = TRUE;
7852 EMSG(_("E724: variable nested too deep for displaying"));
7853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007855 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 }
7857 ++recurse;
7858
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 switch (tv->v_type)
7860 {
7861 case VAR_FUNC:
7862 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 r = tv->vval.v_string;
7864 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007866 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007867 if (tv->vval.v_list == NULL)
7868 {
7869 *tofree = NULL;
7870 r = NULL;
7871 }
7872 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7873 {
7874 *tofree = NULL;
7875 r = (char_u *)"[...]";
7876 }
7877 else
7878 {
7879 tv->vval.v_list->lv_copyID = copyID;
7880 *tofree = list2string(tv, copyID);
7881 r = *tofree;
7882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007884
Bram Moolenaar8c711452005-01-14 21:53:12 +00007885 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007886 if (tv->vval.v_dict == NULL)
7887 {
7888 *tofree = NULL;
7889 r = NULL;
7890 }
7891 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7892 {
7893 *tofree = NULL;
7894 r = (char_u *)"{...}";
7895 }
7896 else
7897 {
7898 tv->vval.v_dict->dv_copyID = copyID;
7899 *tofree = dict2string(tv, copyID);
7900 r = *tofree;
7901 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007903
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007904 case VAR_STRING:
7905 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 *tofree = NULL;
7907 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007910#ifdef FEAT_FLOAT
7911 case VAR_FLOAT:
7912 *tofree = NULL;
7913 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7914 r = numbuf;
7915 break;
7916#endif
7917
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007918 case VAR_SPECIAL:
7919 *tofree = NULL;
7920 switch (tv->vval.v_number)
7921 {
7922 case VVAL_FALSE: r = (char_u *)"false"; break;
7923 case VVAL_TRUE: r = (char_u *)"true"; break;
7924 case VVAL_NONE: r = (char_u *)"none"; break;
7925 case VVAL_NULL: r = (char_u *)"null"; break;
7926 default: EMSG2(_(e_intern2), "echo_string(special)");
7927 }
7928 break;
7929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007934
Bram Moolenaar8502c702014-06-17 12:51:16 +02007935 if (--recurse == 0)
7936 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007937 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007938}
7939
7940/*
7941 * Return a string with the string representation of a variable.
7942 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7943 * "numbuf" is used for a number.
7944 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007945 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 */
7947 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007948tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 char_u **tofree;
7951 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007952 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007953{
7954 switch (tv->v_type)
7955 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007956 case VAR_FUNC:
7957 *tofree = string_quote(tv->vval.v_string, TRUE);
7958 return *tofree;
7959 case VAR_STRING:
7960 *tofree = string_quote(tv->vval.v_string, FALSE);
7961 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007962#ifdef FEAT_FLOAT
7963 case VAR_FLOAT:
7964 *tofree = NULL;
7965 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7966 return numbuf;
7967#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007968 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007971 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007972 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007976 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007977}
7978
7979/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 * Return string "str" in ' quotes, doubling ' characters.
7981 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007983 */
7984 static char_u *
7985string_quote(str, function)
7986 char_u *str;
7987 int function;
7988{
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 char_u *p, *r, *s;
7991
Bram Moolenaar33570922005-01-25 22:26:29 +00007992 len = (function ? 13 : 3);
7993 if (str != NULL)
7994 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007995 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 for (p = str; *p != NUL; mb_ptr_adv(p))
7997 if (*p == '\'')
7998 ++len;
7999 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000 s = r = alloc(len);
8001 if (r != NULL)
8002 {
8003 if (function)
8004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008005 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006 r += 10;
8007 }
8008 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008009 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 if (str != NULL)
8011 for (p = str; *p != NUL; )
8012 {
8013 if (*p == '\'')
8014 *r++ = '\'';
8015 MB_COPY_CHAR(p, r);
8016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008018 if (function)
8019 *r++ = ')';
8020 *r++ = NUL;
8021 }
8022 return s;
8023}
8024
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008025#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026/*
8027 * Convert the string "text" to a floating point number.
8028 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8029 * this always uses a decimal point.
8030 * Returns the length of the text that was consumed.
8031 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008032 int
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008033string2float(text, value)
8034 char_u *text;
8035 float_T *value; /* result stored here */
8036{
8037 char *s = (char *)text;
8038 float_T f;
8039
8040 f = strtod(s, &s);
8041 *value = f;
8042 return (int)((char_u *)s - text);
8043}
8044#endif
8045
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 * Get the value of an environment variable.
8048 * "arg" is pointing to the '$'. It is advanced to after the name.
8049 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008050 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 */
8052 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008053get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 int evaluate;
8057{
8058 char_u *string = NULL;
8059 int len;
8060 int cc;
8061 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008062 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 ++*arg;
8065 name = *arg;
8066 len = get_env_len(arg);
8067 if (evaluate)
8068 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008069 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008070 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008071
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008072 cc = name[len];
8073 name[len] = NUL;
8074 /* first try vim_getenv(), fast for normal environment vars */
8075 string = vim_getenv(name, &mustfree);
8076 if (string != NULL && *string != NUL)
8077 {
8078 if (!mustfree)
8079 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008081 else
8082 {
8083 if (mustfree)
8084 vim_free(string);
8085
8086 /* next try expanding things like $VIM and ${HOME} */
8087 string = expand_env_save(name - 1);
8088 if (string != NULL && *string == '$')
8089 {
8090 vim_free(string);
8091 string = NULL;
8092 }
8093 }
8094 name[len] = cc;
8095
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008096 rettv->v_type = VAR_STRING;
8097 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 }
8099
8100 return OK;
8101}
8102
8103/*
8104 * Array with names and number of arguments of all internal functions
8105 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8106 */
8107static struct fst
8108{
8109 char *f_name; /* function name */
8110 char f_min_argc; /* minimal number of arguments */
8111 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008113 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114} functions[] =
8115{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008118 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008120 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008121 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008122 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"append", 2, 2, f_append},
8124 {"argc", 0, 0, f_argc},
8125 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008126 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008127 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008128#ifdef FEAT_FLOAT
8129 {"asin", 1, 1, f_asin}, /* WJMc */
8130#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008131 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008132 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008133 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008134 {"assert_false", 1, 2, f_assert_false},
8135 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#ifdef FEAT_FLOAT
8137 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008141 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"bufexists", 1, 1, f_bufexists},
8143 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8144 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8145 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8146 {"buflisted", 1, 1, f_buflisted},
8147 {"bufloaded", 1, 1, f_bufloaded},
8148 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008149 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"bufwinnr", 1, 1, f_bufwinnr},
8151 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008152 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008153 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008155#ifdef FEAT_FLOAT
8156 {"ceil", 1, 1, f_ceil},
8157#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008158 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008159 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008161 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008163#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008164 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008165 {"complete_add", 1, 1, f_complete_add},
8166 {"complete_check", 0, 0, f_complete_check},
8167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008169 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008170#ifdef FEAT_FLOAT
8171 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008172 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008174 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008176 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008177 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008178 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008180 {"diff_filler", 1, 1, f_diff_filler},
8181 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008182 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008184 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"eventhandler", 0, 0, f_eventhandler},
8186 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008187 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008189#ifdef FEAT_FLOAT
8190 {"exp", 1, 1, f_exp},
8191#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008192 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008193 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008194 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8196 {"filereadable", 1, 1, f_filereadable},
8197 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008199 {"finddir", 1, 3, f_finddir},
8200 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201#ifdef FEAT_FLOAT
8202 {"float2nr", 1, 1, f_float2nr},
8203 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008204 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008205#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008206 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"fnamemodify", 2, 2, f_fnamemodify},
8208 {"foldclosed", 1, 1, f_foldclosed},
8209 {"foldclosedend", 1, 1, f_foldclosedend},
8210 {"foldlevel", 1, 1, f_foldlevel},
8211 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008212 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008214 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008215 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008216 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008217 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008218 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"getchar", 0, 1, f_getchar},
8220 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008221 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"getcmdline", 0, 0, f_getcmdline},
8223 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008224 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008225 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008226 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008227 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008228 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008229 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"getfsize", 1, 1, f_getfsize},
8231 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008232 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008233 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008234 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008235 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008236 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008237 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008238 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008239 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008241 {"gettabvar", 2, 3, f_gettabvar},
8242 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"getwinposx", 0, 0, f_getwinposx},
8244 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008245 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008246 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008247 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008248 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008250 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008251 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008252 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8254 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8255 {"histadd", 2, 2, f_histadd},
8256 {"histdel", 1, 2, f_histdel},
8257 {"histget", 1, 2, f_histget},
8258 {"histnr", 1, 1, f_histnr},
8259 {"hlID", 1, 1, f_hlID},
8260 {"hlexists", 1, 1, f_hlexists},
8261 {"hostname", 0, 0, f_hostname},
8262 {"iconv", 3, 3, f_iconv},
8263 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008264 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008265 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008267 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 {"inputrestore", 0, 0, f_inputrestore},
8269 {"inputsave", 0, 0, f_inputsave},
8270 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008272 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008274 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008275 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008276 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008277 {"jsondecode", 1, 1, f_jsondecode},
8278 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008279 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"libcall", 3, 3, f_libcall},
8283 {"libcallnr", 3, 3, f_libcallnr},
8284 {"line", 1, 1, f_line},
8285 {"line2byte", 1, 1, f_line2byte},
8286 {"lispindent", 1, 1, f_lispindent},
8287 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008288#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008289 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008290 {"log10", 1, 1, f_log10},
8291#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008292#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008293 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008294#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008295 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008296 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008297 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008298 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008299 {"matchadd", 2, 5, f_matchadd},
8300 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008301 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008302 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008303 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008304 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008305 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008306 {"max", 1, 1, f_max},
8307 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008308#ifdef vim_mkdir
8309 {"mkdir", 1, 3, f_mkdir},
8310#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008312#ifdef FEAT_MZSCHEME
8313 {"mzeval", 1, 1, f_mzeval},
8314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008316 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008317 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008318 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008319#ifdef FEAT_PERL
8320 {"perleval", 1, 1, f_perleval},
8321#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008322#ifdef FEAT_FLOAT
8323 {"pow", 2, 2, f_pow},
8324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008326 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008327 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008328#ifdef FEAT_PYTHON3
8329 {"py3eval", 1, 1, f_py3eval},
8330#endif
8331#ifdef FEAT_PYTHON
8332 {"pyeval", 1, 1, f_pyeval},
8333#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008334 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008335 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008336 {"reltime", 0, 2, f_reltime},
8337 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"remote_expr", 2, 3, f_remote_expr},
8339 {"remote_foreground", 1, 1, f_remote_foreground},
8340 {"remote_peek", 1, 2, f_remote_peek},
8341 {"remote_read", 1, 1, f_remote_read},
8342 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008343 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008345 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008347 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008348#ifdef FEAT_FLOAT
8349 {"round", 1, 1, f_round},
8350#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008351 {"screenattr", 2, 2, f_screenattr},
8352 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008353 {"screencol", 0, 0, f_screencol},
8354 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008355 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008356 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008357 {"searchpair", 3, 7, f_searchpair},
8358 {"searchpairpos", 3, 7, f_searchpairpos},
8359 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"server2client", 2, 2, f_server2client},
8361 {"serverlist", 0, 0, f_serverlist},
8362 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008363 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"setcmdpos", 1, 1, f_setcmdpos},
8365 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008366 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008367 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008368 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008369 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008371 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008372 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008374#ifdef FEAT_CRYPT
8375 {"sha256", 1, 1, f_sha256},
8376#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008377 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008378 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008380#ifdef FEAT_FLOAT
8381 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008382 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008384 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008385 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008386 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008387 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008388 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008389#ifdef FEAT_FLOAT
8390 {"sqrt", 1, 1, f_sqrt},
8391 {"str2float", 1, 1, f_str2float},
8392#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008393 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008394 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008395 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396#ifdef HAVE_STRFTIME
8397 {"strftime", 1, 2, f_strftime},
8398#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008399 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008400 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {"strlen", 1, 1, f_strlen},
8402 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008403 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008405 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008406 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"substitute", 4, 4, f_substitute},
8408 {"synID", 3, 3, f_synID},
8409 {"synIDattr", 2, 3, f_synIDattr},
8410 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008411 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008412 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008413 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008414 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008415 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008416 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008417 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008418 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008419 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008420#ifdef FEAT_FLOAT
8421 {"tan", 1, 1, f_tan},
8422 {"tanh", 1, 1, f_tanh},
8423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008425 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"tolower", 1, 1, f_tolower},
8427 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008428 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008429#ifdef FEAT_FLOAT
8430 {"trunc", 1, 1, f_trunc},
8431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008433 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008434 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008435 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008436 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {"virtcol", 1, 1, f_virtcol},
8438 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008439 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"winbufnr", 1, 1, f_winbufnr},
8441 {"wincol", 0, 0, f_wincol},
8442 {"winheight", 1, 1, f_winheight},
8443 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008444 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008446 {"winrestview", 1, 1, f_winrestview},
8447 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008449 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008450 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008451 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452};
8453
8454#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8455
8456/*
8457 * Function given to ExpandGeneric() to obtain the list of internal
8458 * or user defined function names.
8459 */
8460 char_u *
8461get_function_name(xp, idx)
8462 expand_T *xp;
8463 int idx;
8464{
8465 static int intidx = -1;
8466 char_u *name;
8467
8468 if (idx == 0)
8469 intidx = -1;
8470 if (intidx < 0)
8471 {
8472 name = get_user_func_name(xp, idx);
8473 if (name != NULL)
8474 return name;
8475 }
8476 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8477 {
8478 STRCPY(IObuff, functions[intidx].f_name);
8479 STRCAT(IObuff, "(");
8480 if (functions[intidx].f_max_argc == 0)
8481 STRCAT(IObuff, ")");
8482 return IObuff;
8483 }
8484
8485 return NULL;
8486}
8487
8488/*
8489 * Function given to ExpandGeneric() to obtain the list of internal or
8490 * user defined variable or function names.
8491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 char_u *
8493get_expr_name(xp, idx)
8494 expand_T *xp;
8495 int idx;
8496{
8497 static int intidx = -1;
8498 char_u *name;
8499
8500 if (idx == 0)
8501 intidx = -1;
8502 if (intidx < 0)
8503 {
8504 name = get_function_name(xp, idx);
8505 if (name != NULL)
8506 return name;
8507 }
8508 return get_user_var_name(xp, ++intidx);
8509}
8510
8511#endif /* FEAT_CMDL_COMPL */
8512
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008513#if defined(EBCDIC) || defined(PROTO)
8514/*
8515 * Compare struct fst by function name.
8516 */
8517 static int
8518compare_func_name(s1, s2)
8519 const void *s1;
8520 const void *s2;
8521{
8522 struct fst *p1 = (struct fst *)s1;
8523 struct fst *p2 = (struct fst *)s2;
8524
8525 return STRCMP(p1->f_name, p2->f_name);
8526}
8527
8528/*
8529 * Sort the function table by function name.
8530 * The sorting of the table above is ASCII dependant.
8531 * On machines using EBCDIC we have to sort it.
8532 */
8533 static void
8534sortFunctions()
8535{
8536 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8537
8538 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8539}
8540#endif
8541
8542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543/*
8544 * Find internal function in table above.
8545 * Return index, or -1 if not found
8546 */
8547 static int
8548find_internal_func(name)
8549 char_u *name; /* name of the function */
8550{
8551 int first = 0;
8552 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8553 int cmp;
8554 int x;
8555
8556 /*
8557 * Find the function name in the table. Binary search.
8558 */
8559 while (first <= last)
8560 {
8561 x = first + ((unsigned)(last - first) >> 1);
8562 cmp = STRCMP(name, functions[x].f_name);
8563 if (cmp < 0)
8564 last = x - 1;
8565 else if (cmp > 0)
8566 first = x + 1;
8567 else
8568 return x;
8569 }
8570 return -1;
8571}
8572
8573/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8575 * name it contains, otherwise return "name".
8576 */
8577 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008578deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008579 char_u *name;
8580 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008581 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008582{
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008584 int cc;
8585
8586 cc = name[*lenp];
8587 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008588 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008589 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008591 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 {
8594 *lenp = 0;
8595 return (char_u *)""; /* just in case */
8596 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008597 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008599 }
8600
8601 return name;
8602}
8603
8604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 * Allocate a variable for the result of a function.
8606 * Return OK or FAIL.
8607 */
8608 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008609get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8610 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 char_u *name; /* name of the function */
8612 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 char_u **arg; /* argument, pointing to the '(' */
8615 linenr_T firstline; /* first line of range */
8616 linenr_T lastline; /* last line of range */
8617 int *doesrange; /* return: function handled range */
8618 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 char_u *argp;
8622 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008623 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 int argcount = 0; /* number of arguments found */
8625
8626 /*
8627 * Get the arguments.
8628 */
8629 argp = *arg;
8630 while (argcount < MAX_FUNC_ARGS)
8631 {
8632 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8633 if (*argp == ')' || *argp == ',' || *argp == NUL)
8634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8636 {
8637 ret = FAIL;
8638 break;
8639 }
8640 ++argcount;
8641 if (*argp != ',')
8642 break;
8643 }
8644 if (*argp == ')')
8645 ++argp;
8646 else
8647 ret = FAIL;
8648
8649 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008651 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 {
8654 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008655 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008657 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
8660 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 *arg = skipwhite(argp);
8664 return ret;
8665}
8666
8667
8668/*
8669 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008670 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008671 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 */
8673 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008674call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008675 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008676 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008680 typval_T *argvars; /* vars for arguments, must have "argcount"
8681 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 linenr_T firstline; /* first line of range */
8683 linenr_T lastline; /* last line of range */
8684 int *doesrange; /* return: function handled range */
8685 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008686 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689#define ERROR_UNKNOWN 0
8690#define ERROR_TOOMANY 1
8691#define ERROR_TOOFEW 2
8692#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008693#define ERROR_DICT 4
8694#define ERROR_NONE 5
8695#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 int error = ERROR_NONE;
8697 int i;
8698 int llen;
8699 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#define FLEN_FIXED 40
8701 char_u fname_buf[FLEN_FIXED + 1];
8702 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008703 char_u *name;
8704
8705 /* Make a copy of the name, if it comes from a funcref variable it could
8706 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008707 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008708 if (name == NULL)
8709 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
8711 /*
8712 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8713 * Change <SNR>123_name() to K_SNR 123_name().
8714 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 llen = eval_fname_script(name);
8717 if (llen > 0)
8718 {
8719 fname_buf[0] = K_SPECIAL;
8720 fname_buf[1] = KS_EXTRA;
8721 fname_buf[2] = (int)KE_SNR;
8722 i = 3;
8723 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8724 {
8725 if (current_SID <= 0)
8726 error = ERROR_SCRIPT;
8727 else
8728 {
8729 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8730 i = (int)STRLEN(fname_buf);
8731 }
8732 }
8733 if (i + STRLEN(name + llen) < FLEN_FIXED)
8734 {
8735 STRCPY(fname_buf + i, name + llen);
8736 fname = fname_buf;
8737 }
8738 else
8739 {
8740 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8741 if (fname == NULL)
8742 error = ERROR_OTHER;
8743 else
8744 {
8745 mch_memmove(fname, fname_buf, (size_t)i);
8746 STRCPY(fname + i, name + llen);
8747 }
8748 }
8749 }
8750 else
8751 fname = name;
8752
8753 *doesrange = FALSE;
8754
8755
8756 /* execute the function if no errors detected and executing */
8757 if (evaluate && error == ERROR_NONE)
8758 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008759 char_u *rfname = fname;
8760
8761 /* Ignore "g:" before a function name. */
8762 if (fname[0] == 'g' && fname[1] == ':')
8763 rfname = fname + 2;
8764
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008765 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8766 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 error = ERROR_UNKNOWN;
8768
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008769 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 {
8771 /*
8772 * User defined function.
8773 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008774 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008777 /* Trigger FuncUndefined event, may load the function. */
8778 if (fp == NULL
8779 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008780 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008781 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008783 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008784 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 }
8786#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008787 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008788 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008789 {
8790 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008791 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008792 }
8793
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 if (fp != NULL)
8795 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008796 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008798 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008800 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008802 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008803 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 else
8805 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008806 int did_save_redo = FALSE;
8807
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 /*
8809 * Call the user function.
8810 * Save and restore search patterns, script variables and
8811 * redo buffer.
8812 */
8813 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008814#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008815 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008816#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008817 {
8818 saveRedobuff();
8819 did_save_redo = TRUE;
8820 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008821 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008823 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008824 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8825 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8826 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008827 /* Function was unreferenced while being used, free it
8828 * now. */
8829 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008830 if (did_save_redo)
8831 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 restore_search_patterns();
8833 error = ERROR_NONE;
8834 }
8835 }
8836 }
8837 else
8838 {
8839 /*
8840 * Find the function name in the table, call its implementation.
8841 */
8842 i = find_internal_func(fname);
8843 if (i >= 0)
8844 {
8845 if (argcount < functions[i].f_min_argc)
8846 error = ERROR_TOOFEW;
8847 else if (argcount > functions[i].f_max_argc)
8848 error = ERROR_TOOMANY;
8849 else
8850 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008851 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 error = ERROR_NONE;
8854 }
8855 }
8856 }
8857 /*
8858 * The function call (or "FuncUndefined" autocommand sequence) might
8859 * have been aborted by an error, an interrupt, or an explicitly thrown
8860 * exception that has not been caught so far. This situation can be
8861 * tested for by calling aborting(). For an error in an internal
8862 * function or for the "E132" error in call_user_func(), however, the
8863 * throw point at which the "force_abort" flag (temporarily reset by
8864 * emsg()) is normally updated has not been reached yet. We need to
8865 * update that flag first to make aborting() reliable.
8866 */
8867 update_force_abort();
8868 }
8869 if (error == ERROR_NONE)
8870 ret = OK;
8871
8872 /*
8873 * Report an error unless the argument evaluation or function call has been
8874 * cancelled due to an aborting error, an interrupt, or an exception.
8875 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008876 if (!aborting())
8877 {
8878 switch (error)
8879 {
8880 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008881 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008882 break;
8883 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008884 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008885 break;
8886 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008887 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008888 name);
8889 break;
8890 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008891 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008892 name);
8893 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008894 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008895 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008896 name);
8897 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008898 }
8899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 if (fname != name && fname != fname_buf)
8902 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008903 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
8905 return ret;
8906}
8907
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008908/*
8909 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008910 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008911 */
8912 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008913emsg_funcname(ermsg, name)
8914 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008915 char_u *name;
8916{
8917 char_u *p;
8918
8919 if (*name == K_SPECIAL)
8920 p = concat_str((char_u *)"<SNR>", name + 3);
8921 else
8922 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008923 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008924 if (p != name)
8925 vim_free(p);
8926}
8927
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008928/*
8929 * Return TRUE for a non-zero Number and a non-empty String.
8930 */
8931 static int
8932non_zero_arg(argvars)
8933 typval_T *argvars;
8934{
8935 return ((argvars[0].v_type == VAR_NUMBER
8936 && argvars[0].vval.v_number != 0)
8937 || (argvars[0].v_type == VAR_STRING
8938 && argvars[0].vval.v_string != NULL
8939 && *argvars[0].vval.v_string != NUL));
8940}
8941
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942/*********************************************
8943 * Implementation of the built-in functions
8944 */
8945
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008946#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008947static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8948
8949/*
8950 * Get the float value of "argvars[0]" into "f".
8951 * Returns FAIL when the argument is not a Number or Float.
8952 */
8953 static int
8954get_float_arg(argvars, f)
8955 typval_T *argvars;
8956 float_T *f;
8957{
8958 if (argvars[0].v_type == VAR_FLOAT)
8959 {
8960 *f = argvars[0].vval.v_float;
8961 return OK;
8962 }
8963 if (argvars[0].v_type == VAR_NUMBER)
8964 {
8965 *f = (float_T)argvars[0].vval.v_number;
8966 return OK;
8967 }
8968 EMSG(_("E808: Number or Float required"));
8969 return FAIL;
8970}
8971
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008972/*
8973 * "abs(expr)" function
8974 */
8975 static void
8976f_abs(argvars, rettv)
8977 typval_T *argvars;
8978 typval_T *rettv;
8979{
8980 if (argvars[0].v_type == VAR_FLOAT)
8981 {
8982 rettv->v_type = VAR_FLOAT;
8983 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8984 }
8985 else
8986 {
8987 varnumber_T n;
8988 int error = FALSE;
8989
8990 n = get_tv_number_chk(&argvars[0], &error);
8991 if (error)
8992 rettv->vval.v_number = -1;
8993 else if (n > 0)
8994 rettv->vval.v_number = n;
8995 else
8996 rettv->vval.v_number = -n;
8997 }
8998}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008999
9000/*
9001 * "acos()" function
9002 */
9003 static void
9004f_acos(argvars, rettv)
9005 typval_T *argvars;
9006 typval_T *rettv;
9007{
9008 float_T f;
9009
9010 rettv->v_type = VAR_FLOAT;
9011 if (get_float_arg(argvars, &f) == OK)
9012 rettv->vval.v_float = acos(f);
9013 else
9014 rettv->vval.v_float = 0.0;
9015}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009016#endif
9017
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009019 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 */
9021 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009022f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009023 typval_T *argvars;
9024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009029 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009031 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009032 && !tv_check_lock(l->lv_lock,
9033 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009034 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009036 }
9037 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009038 EMSG(_(e_listreq));
9039}
9040
9041/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009042 * "alloc_fail(id, countdown, repeat)" function
9043 */
9044 static void
9045f_alloc_fail(argvars, rettv)
9046 typval_T *argvars;
9047 typval_T *rettv UNUSED;
9048{
9049 if (argvars[0].v_type != VAR_NUMBER
9050 || argvars[0].vval.v_number <= 0
9051 || argvars[1].v_type != VAR_NUMBER
9052 || argvars[1].vval.v_number < 0
9053 || argvars[2].v_type != VAR_NUMBER)
9054 EMSG(_(e_invarg));
9055 else
9056 {
9057 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009058 if (alloc_fail_id >= aid_last)
9059 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009060 alloc_fail_countdown = argvars[1].vval.v_number;
9061 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009062 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009063 }
9064}
9065
9066/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009067 * "and(expr, expr)" function
9068 */
9069 static void
9070f_and(argvars, rettv)
9071 typval_T *argvars;
9072 typval_T *rettv;
9073{
9074 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9075 & get_tv_number_chk(&argvars[1], NULL);
9076}
9077
9078/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079 * "append(lnum, string/list)" function
9080 */
9081 static void
9082f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 typval_T *argvars;
9084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009085{
9086 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 list_T *l = NULL;
9089 listitem_T *li = NULL;
9090 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009091 long added = 0;
9092
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009093 /* When coming here from Insert mode, sync undo, so that this can be
9094 * undone separately from what was previously inserted. */
9095 if (u_sync_once == 2)
9096 {
9097 u_sync_once = 1; /* notify that u_sync() was called */
9098 u_sync(TRUE);
9099 }
9100
Bram Moolenaar0d660222005-01-07 21:51:51 +00009101 lnum = get_tv_lnum(argvars);
9102 if (lnum >= 0
9103 && lnum <= curbuf->b_ml.ml_line_count
9104 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009105 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009106 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009107 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009108 l = argvars[1].vval.v_list;
9109 if (l == NULL)
9110 return;
9111 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009112 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009113 for (;;)
9114 {
9115 if (l == NULL)
9116 tv = &argvars[1]; /* append a string */
9117 else if (li == NULL)
9118 break; /* end of list */
9119 else
9120 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 line = get_tv_string_chk(tv);
9122 if (line == NULL) /* type error */
9123 {
9124 rettv->vval.v_number = 1; /* Failed */
9125 break;
9126 }
9127 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009128 ++added;
9129 if (l == NULL)
9130 break;
9131 li = li->li_next;
9132 }
9133
9134 appended_lines_mark(lnum, added);
9135 if (curwin->w_cursor.lnum > lnum)
9136 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 else
9139 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140}
9141
9142/*
9143 * "argc()" function
9144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151}
9152
9153/*
9154 * "argidx()" function
9155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009158 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162}
9163
9164/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009165 * "arglistid()" function
9166 */
9167 static void
9168f_arglistid(argvars, rettv)
9169 typval_T *argvars UNUSED;
9170 typval_T *rettv;
9171{
9172 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009173
9174 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009175 wp = find_tabwin(&argvars[0], &argvars[1]);
9176 if (wp != NULL)
9177 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009178}
9179
9180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 * "argv(nr)" function
9182 */
9183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 typval_T *argvars;
9186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188 int idx;
9189
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009190 if (argvars[0].v_type != VAR_UNKNOWN)
9191 {
9192 idx = get_tv_number_chk(&argvars[0], NULL);
9193 if (idx >= 0 && idx < ARGCOUNT)
9194 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9195 else
9196 rettv->vval.v_string = NULL;
9197 rettv->v_type = VAR_STRING;
9198 }
9199 else if (rettv_list_alloc(rettv) == OK)
9200 for (idx = 0; idx < ARGCOUNT; ++idx)
9201 list_append_string(rettv->vval.v_list,
9202 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203}
9204
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009205static void prepare_assert_error __ARGS((garray_T*gap));
9206static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9207static void assert_error __ARGS((garray_T *gap));
9208static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9209
9210/*
9211 * Prepare "gap" for an assert error and add the sourcing position.
9212 */
9213 static void
9214prepare_assert_error(gap)
9215 garray_T *gap;
9216{
9217 char buf[NUMBUFLEN];
9218
9219 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009220 if (sourcing_name != NULL)
9221 {
9222 ga_concat(gap, sourcing_name);
9223 if (sourcing_lnum > 0)
9224 ga_concat(gap, (char_u *)" ");
9225 }
9226 if (sourcing_lnum > 0)
9227 {
9228 sprintf(buf, "line %ld", (long)sourcing_lnum);
9229 ga_concat(gap, (char_u *)buf);
9230 }
9231 if (sourcing_name != NULL || sourcing_lnum > 0)
9232 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009233}
9234
9235/*
9236 * Fill "gap" with information about an assert error.
9237 */
9238 static void
9239fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9240 garray_T *gap;
9241 typval_T *opt_msg_tv;
9242 char_u *exp_str;
9243 typval_T *exp_tv;
9244 typval_T *got_tv;
9245{
9246 char_u numbuf[NUMBUFLEN];
9247 char_u *tofree;
9248
9249 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9250 {
9251 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9252 vim_free(tofree);
9253 }
9254 else
9255 {
9256 ga_concat(gap, (char_u *)"Expected ");
9257 if (exp_str == NULL)
9258 {
9259 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9260 vim_free(tofree);
9261 }
9262 else
9263 ga_concat(gap, exp_str);
9264 ga_concat(gap, (char_u *)" but got ");
9265 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9266 vim_free(tofree);
9267 }
9268}
Bram Moolenaar43345542015-11-29 17:35:35 +01009269
9270/*
9271 * Add an assert error to v:errors.
9272 */
9273 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009274assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009275 garray_T *gap;
9276{
9277 struct vimvar *vp = &vimvars[VV_ERRORS];
9278
9279 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9280 /* Make sure v:errors is a list. */
9281 set_vim_var_list(VV_ERRORS, list_alloc());
9282 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9283}
9284
Bram Moolenaar43345542015-11-29 17:35:35 +01009285/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009286 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009287 */
9288 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009289f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009290 typval_T *argvars;
9291 typval_T *rettv UNUSED;
9292{
9293 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009294
9295 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9296 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009297 prepare_assert_error(&ga);
9298 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9299 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009300 ga_clear(&ga);
9301 }
9302}
9303
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009304/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009305 * "assert_exception(string[, msg])" function
9306 */
9307 static void
9308f_assert_exception(argvars, rettv)
9309 typval_T *argvars;
9310 typval_T *rettv UNUSED;
9311{
9312 garray_T ga;
9313 char *error;
9314
9315 error = (char *)get_tv_string_chk(&argvars[0]);
9316 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9317 {
9318 prepare_assert_error(&ga);
9319 ga_concat(&ga, (char_u *)"v:exception is not set");
9320 assert_error(&ga);
9321 ga_clear(&ga);
9322 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009323 else if (error != NULL
9324 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009325 {
9326 prepare_assert_error(&ga);
9327 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9328 &vimvars[VV_EXCEPTION].vv_tv);
9329 assert_error(&ga);
9330 ga_clear(&ga);
9331 }
9332}
9333
9334/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009335 * "assert_fails(cmd [, error])" function
9336 */
9337 static void
9338f_assert_fails(argvars, rettv)
9339 typval_T *argvars;
9340 typval_T *rettv UNUSED;
9341{
9342 char_u *cmd = get_tv_string_chk(&argvars[0]);
9343 garray_T ga;
9344
9345 called_emsg = FALSE;
9346 suppress_errthrow = TRUE;
9347 emsg_silent = TRUE;
9348 do_cmdline_cmd(cmd);
9349 if (!called_emsg)
9350 {
9351 prepare_assert_error(&ga);
9352 ga_concat(&ga, (char_u *)"command did not fail: ");
9353 ga_concat(&ga, cmd);
9354 assert_error(&ga);
9355 ga_clear(&ga);
9356 }
9357 else if (argvars[1].v_type != VAR_UNKNOWN)
9358 {
9359 char_u buf[NUMBUFLEN];
9360 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9361
9362 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9363 {
9364 prepare_assert_error(&ga);
9365 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9366 &vimvars[VV_ERRMSG].vv_tv);
9367 assert_error(&ga);
9368 ga_clear(&ga);
9369 }
9370 }
9371
9372 called_emsg = FALSE;
9373 suppress_errthrow = FALSE;
9374 emsg_silent = FALSE;
9375 emsg_on_display = FALSE;
9376 set_vim_var_string(VV_ERRMSG, NULL, 0);
9377}
9378
9379/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009380 * Common for assert_true() and assert_false().
9381 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009382 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009383assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009384 typval_T *argvars;
9385 int isTrue;
9386{
9387 int error = FALSE;
9388 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009389
9390 if (argvars[0].v_type != VAR_NUMBER
9391 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9392 || error)
9393 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009394 prepare_assert_error(&ga);
9395 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009396 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009397 NULL, &argvars[0]);
9398 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009399 ga_clear(&ga);
9400 }
9401}
9402
9403/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009404 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009405 */
9406 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009407f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009408 typval_T *argvars;
9409 typval_T *rettv UNUSED;
9410{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009411 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009412}
9413
9414/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009415 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009416 */
9417 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009418f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009419 typval_T *argvars;
9420 typval_T *rettv UNUSED;
9421{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009422 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009423}
9424
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009425#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009426/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009427 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009428 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009429 static void
9430f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009431 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009432 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009433{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009434 float_T f;
9435
9436 rettv->v_type = VAR_FLOAT;
9437 if (get_float_arg(argvars, &f) == OK)
9438 rettv->vval.v_float = asin(f);
9439 else
9440 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009441}
9442
9443/*
9444 * "atan()" function
9445 */
9446 static void
9447f_atan(argvars, rettv)
9448 typval_T *argvars;
9449 typval_T *rettv;
9450{
9451 float_T f;
9452
9453 rettv->v_type = VAR_FLOAT;
9454 if (get_float_arg(argvars, &f) == OK)
9455 rettv->vval.v_float = atan(f);
9456 else
9457 rettv->vval.v_float = 0.0;
9458}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009459
9460/*
9461 * "atan2()" function
9462 */
9463 static void
9464f_atan2(argvars, rettv)
9465 typval_T *argvars;
9466 typval_T *rettv;
9467{
9468 float_T fx, fy;
9469
9470 rettv->v_type = VAR_FLOAT;
9471 if (get_float_arg(argvars, &fx) == OK
9472 && get_float_arg(&argvars[1], &fy) == OK)
9473 rettv->vval.v_float = atan2(fx, fy);
9474 else
9475 rettv->vval.v_float = 0.0;
9476}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009477#endif
9478
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479/*
9480 * "browse(save, title, initdir, default)" function
9481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009484 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
9487#ifdef FEAT_BROWSE
9488 int save;
9489 char_u *title;
9490 char_u *initdir;
9491 char_u *defname;
9492 char_u buf[NUMBUFLEN];
9493 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496 save = get_tv_number_chk(&argvars[0], &error);
9497 title = get_tv_string_chk(&argvars[1]);
9498 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9499 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009501 if (error || title == NULL || initdir == NULL || defname == NULL)
9502 rettv->vval.v_string = NULL;
9503 else
9504 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009505 do_browse(save ? BROWSE_SAVE : 0,
9506 title, defname, NULL, initdir, NULL, curbuf);
9507#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009509#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009511}
9512
9513/*
9514 * "browsedir(title, initdir)" function
9515 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009518 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009519 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009520{
9521#ifdef FEAT_BROWSE
9522 char_u *title;
9523 char_u *initdir;
9524 char_u buf[NUMBUFLEN];
9525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009526 title = get_tv_string_chk(&argvars[0]);
9527 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009529 if (title == NULL || initdir == NULL)
9530 rettv->vval.v_string = NULL;
9531 else
9532 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009533 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538}
9539
Bram Moolenaar33570922005-01-25 22:26:29 +00009540static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009541
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542/*
9543 * Find a buffer by number or exact name.
9544 */
9545 static buf_T *
9546find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009551 if (avar->v_type == VAR_NUMBER)
9552 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009553 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009555 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009556 if (buf == NULL)
9557 {
9558 /* No full path name match, try a match with a URL or a "nofile"
9559 * buffer, these don't use the full path. */
9560 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9561 if (buf->b_fname != NULL
9562 && (path_with_url(buf->b_fname)
9563#ifdef FEAT_QUICKFIX
9564 || bt_nofile(buf)
9565#endif
9566 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009568 break;
9569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 }
9571 return buf;
9572}
9573
9574/*
9575 * "bufexists(expr)" function
9576 */
9577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009579 typval_T *argvars;
9580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583}
9584
9585/*
9586 * "buflisted(expr)" function
9587 */
9588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009590 typval_T *argvars;
9591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
9593 buf_T *buf;
9594
9595 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597}
9598
9599/*
9600 * "bufloaded(expr)" function
9601 */
9602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009604 typval_T *argvars;
9605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606{
9607 buf_T *buf;
9608
9609 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009613static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009614
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615/*
9616 * Get buffer by number or pattern.
9617 */
9618 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009619get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009620 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009621 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 int save_magic;
9625 char_u *save_cpo;
9626 buf_T *buf;
9627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 if (tv->v_type == VAR_NUMBER)
9629 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009630 if (tv->v_type != VAR_STRING)
9631 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 if (name == NULL || *name == NUL)
9633 return curbuf;
9634 if (name[0] == '$' && name[1] == NUL)
9635 return lastbuf;
9636
9637 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9638 save_magic = p_magic;
9639 p_magic = TRUE;
9640 save_cpo = p_cpo;
9641 p_cpo = (char_u *)"";
9642
9643 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009644 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645
9646 p_magic = save_magic;
9647 p_cpo = save_cpo;
9648
9649 /* If not found, try expanding the name, like done for bufexists(). */
9650 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652
9653 return buf;
9654}
9655
9656/*
9657 * "bufname(expr)" function
9658 */
9659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 typval_T *argvars;
9662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 buf_T *buf;
9665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009666 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009668 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 --emsg_off;
9675}
9676
9677/*
9678 * "bufnr(expr)" function
9679 */
9680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 typval_T *argvars;
9683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009686 int error = FALSE;
9687 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009689 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009691 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009692 --emsg_off;
9693
9694 /* If the buffer isn't found and the second argument is not zero create a
9695 * new buffer. */
9696 if (buf == NULL
9697 && argvars[1].v_type != VAR_UNKNOWN
9698 && get_tv_number_chk(&argvars[1], &error) != 0
9699 && !error
9700 && (name = get_tv_string_chk(&argvars[0])) != NULL
9701 && !error)
9702 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9703
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708}
9709
9710/*
9711 * "bufwinnr(nr)" function
9712 */
9713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 typval_T *argvars;
9716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718#ifdef FEAT_WINDOWS
9719 win_T *wp;
9720 int winnr = 0;
9721#endif
9722 buf_T *buf;
9723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009724 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009726 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727#ifdef FEAT_WINDOWS
9728 for (wp = firstwin; wp; wp = wp->w_next)
9729 {
9730 ++winnr;
9731 if (wp->w_buffer == buf)
9732 break;
9733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737#endif
9738 --emsg_off;
9739}
9740
9741/*
9742 * "byte2line(byte)" function
9743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009746 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751#else
9752 long boff = 0;
9753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009754 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 (linenr_T)0, &boff);
9760#endif
9761}
9762
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009763 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009764byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009767 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009768{
9769#ifdef FEAT_MBYTE
9770 char_u *t;
9771#endif
9772 char_u *str;
9773 long idx;
9774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 str = get_tv_string_chk(&argvars[0]);
9776 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009779 return;
9780
9781#ifdef FEAT_MBYTE
9782 t = str;
9783 for ( ; idx > 0; idx--)
9784 {
9785 if (*t == NUL) /* EOL reached */
9786 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009787 if (enc_utf8 && comp)
9788 t += utf_ptr2len(t);
9789 else
9790 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009791 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009792 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009793#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009794 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009796#endif
9797}
9798
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009799/*
9800 * "byteidx()" function
9801 */
9802 static void
9803f_byteidx(argvars, rettv)
9804 typval_T *argvars;
9805 typval_T *rettv;
9806{
9807 byteidx(argvars, rettv, FALSE);
9808}
9809
9810/*
9811 * "byteidxcomp()" function
9812 */
9813 static void
9814f_byteidxcomp(argvars, rettv)
9815 typval_T *argvars;
9816 typval_T *rettv;
9817{
9818 byteidx(argvars, rettv, TRUE);
9819}
9820
Bram Moolenaardb913952012-06-29 12:54:53 +02009821 int
9822func_call(name, args, selfdict, rettv)
9823 char_u *name;
9824 typval_T *args;
9825 dict_T *selfdict;
9826 typval_T *rettv;
9827{
9828 listitem_T *item;
9829 typval_T argv[MAX_FUNC_ARGS + 1];
9830 int argc = 0;
9831 int dummy;
9832 int r = 0;
9833
9834 for (item = args->vval.v_list->lv_first; item != NULL;
9835 item = item->li_next)
9836 {
9837 if (argc == MAX_FUNC_ARGS)
9838 {
9839 EMSG(_("E699: Too many arguments"));
9840 break;
9841 }
9842 /* Make a copy of each argument. This is needed to be able to set
9843 * v_lock to VAR_FIXED in the copy without changing the original list.
9844 */
9845 copy_tv(&item->li_tv, &argv[argc++]);
9846 }
9847
9848 if (item == NULL)
9849 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9850 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9851 &dummy, TRUE, selfdict);
9852
9853 /* Free the arguments. */
9854 while (argc > 0)
9855 clear_tv(&argv[--argc]);
9856
9857 return r;
9858}
9859
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009860/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009861 * "call(func, arglist)" function
9862 */
9863 static void
9864f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009865 typval_T *argvars;
9866 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009867{
9868 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009870
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009871 if (argvars[1].v_type != VAR_LIST)
9872 {
9873 EMSG(_(e_listreq));
9874 return;
9875 }
9876 if (argvars[1].vval.v_list == NULL)
9877 return;
9878
9879 if (argvars[0].v_type == VAR_FUNC)
9880 func = argvars[0].vval.v_string;
9881 else
9882 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009883 if (*func == NUL)
9884 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009885
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886 if (argvars[2].v_type != VAR_UNKNOWN)
9887 {
9888 if (argvars[2].v_type != VAR_DICT)
9889 {
9890 EMSG(_(e_dictreq));
9891 return;
9892 }
9893 selfdict = argvars[2].vval.v_dict;
9894 }
9895
Bram Moolenaardb913952012-06-29 12:54:53 +02009896 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009897}
9898
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009899#ifdef FEAT_FLOAT
9900/*
9901 * "ceil({float})" function
9902 */
9903 static void
9904f_ceil(argvars, rettv)
9905 typval_T *argvars;
9906 typval_T *rettv;
9907{
9908 float_T f;
9909
9910 rettv->v_type = VAR_FLOAT;
9911 if (get_float_arg(argvars, &f) == OK)
9912 rettv->vval.v_float = ceil(f);
9913 else
9914 rettv->vval.v_float = 0.0;
9915}
9916#endif
9917
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009919 * "changenr()" function
9920 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009921 static void
9922f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009923 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009924 typval_T *rettv;
9925{
9926 rettv->vval.v_number = curbuf->b_u_seq_cur;
9927}
9928
9929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 * "char2nr(string)" function
9931 */
9932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009934 typval_T *argvars;
9935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936{
9937#ifdef FEAT_MBYTE
9938 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009939 {
9940 int utf8 = 0;
9941
9942 if (argvars[1].v_type != VAR_UNKNOWN)
9943 utf8 = get_tv_number_chk(&argvars[1], NULL);
9944
9945 if (utf8)
9946 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9947 else
9948 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 else
9951#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953}
9954
9955/*
9956 * "cindent(lnum)" function
9957 */
9958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009960 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962{
9963#ifdef FEAT_CINDENT
9964 pos_T pos;
9965 linenr_T lnum;
9966
9967 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9970 {
9971 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 curwin->w_cursor = pos;
9974 }
9975 else
9976#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978}
9979
9980/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009981 * "clearmatches()" function
9982 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009983 static void
9984f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009985 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009986 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009987{
9988#ifdef FEAT_SEARCH_EXTRA
9989 clear_matches(curwin);
9990#endif
9991}
9992
9993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 * "col(string)" function
9995 */
9996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009998 typval_T *argvars;
9999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
10001 colnr_T col = 0;
10002 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010003 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010005 fp = var2fpos(&argvars[0], FALSE, &fnum);
10006 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 {
10008 if (fp->col == MAXCOL)
10009 {
10010 /* '> can be MAXCOL, get the length of the line then */
10011 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010012 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 else
10014 col = MAXCOL;
10015 }
10016 else
10017 {
10018 col = fp->col + 1;
10019#ifdef FEAT_VIRTUALEDIT
10020 /* col(".") when the cursor is on the NUL at the end of the line
10021 * because of "coladd" can be seen as an extra column. */
10022 if (virtual_active() && fp == &curwin->w_cursor)
10023 {
10024 char_u *p = ml_get_cursor();
10025
10026 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10027 curwin->w_virtcol - curwin->w_cursor.coladd))
10028 {
10029# ifdef FEAT_MBYTE
10030 int l;
10031
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010032 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 col += l;
10034# else
10035 if (*p != NUL && p[1] == NUL)
10036 ++col;
10037# endif
10038 }
10039 }
10040#endif
10041 }
10042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044}
10045
Bram Moolenaar572cb562005-08-05 21:35:02 +000010046#if defined(FEAT_INS_EXPAND)
10047/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010048 * "complete()" function
10049 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010050 static void
10051f_complete(argvars, rettv)
10052 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010053 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010054{
10055 int startcol;
10056
10057 if ((State & INSERT) == 0)
10058 {
10059 EMSG(_("E785: complete() can only be used in Insert mode"));
10060 return;
10061 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010062
10063 /* Check for undo allowed here, because if something was already inserted
10064 * the line was already saved for undo and this check isn't done. */
10065 if (!undo_allowed())
10066 return;
10067
Bram Moolenaarade00832006-03-10 21:46:58 +000010068 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10069 {
10070 EMSG(_(e_invarg));
10071 return;
10072 }
10073
10074 startcol = get_tv_number_chk(&argvars[0], NULL);
10075 if (startcol <= 0)
10076 return;
10077
10078 set_completion(startcol - 1, argvars[1].vval.v_list);
10079}
10080
10081/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010082 * "complete_add()" function
10083 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010084 static void
10085f_complete_add(argvars, rettv)
10086 typval_T *argvars;
10087 typval_T *rettv;
10088{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010089 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010090}
10091
10092/*
10093 * "complete_check()" function
10094 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010095 static void
10096f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010097 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010098 typval_T *rettv;
10099{
10100 int saved = RedrawingDisabled;
10101
10102 RedrawingDisabled = 0;
10103 ins_compl_check_keys(0);
10104 rettv->vval.v_number = compl_interrupted;
10105 RedrawingDisabled = saved;
10106}
10107#endif
10108
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109/*
10110 * "confirm(message, buttons[, default [, type]])" function
10111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010114 typval_T *argvars UNUSED;
10115 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10118 char_u *message;
10119 char_u *buttons = NULL;
10120 char_u buf[NUMBUFLEN];
10121 char_u buf2[NUMBUFLEN];
10122 int def = 1;
10123 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 char_u *typestr;
10125 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 message = get_tv_string_chk(&argvars[0]);
10128 if (message == NULL)
10129 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010130 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10133 if (buttons == NULL)
10134 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010135 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010138 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10141 if (typestr == NULL)
10142 error = TRUE;
10143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 switch (TOUPPER_ASC(*typestr))
10146 {
10147 case 'E': type = VIM_ERROR; break;
10148 case 'Q': type = VIM_QUESTION; break;
10149 case 'I': type = VIM_INFO; break;
10150 case 'W': type = VIM_WARNING; break;
10151 case 'G': type = VIM_GENERIC; break;
10152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 }
10154 }
10155 }
10156 }
10157
10158 if (buttons == NULL || *buttons == NUL)
10159 buttons = (char_u *)_("&Ok");
10160
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010161 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010163 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164#endif
10165}
10166
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010167/*
10168 * "copy()" function
10169 */
10170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010172 typval_T *argvars;
10173 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010174{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010175 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010176}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010178#ifdef FEAT_FLOAT
10179/*
10180 * "cos()" function
10181 */
10182 static void
10183f_cos(argvars, rettv)
10184 typval_T *argvars;
10185 typval_T *rettv;
10186{
10187 float_T f;
10188
10189 rettv->v_type = VAR_FLOAT;
10190 if (get_float_arg(argvars, &f) == OK)
10191 rettv->vval.v_float = cos(f);
10192 else
10193 rettv->vval.v_float = 0.0;
10194}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010195
10196/*
10197 * "cosh()" function
10198 */
10199 static void
10200f_cosh(argvars, rettv)
10201 typval_T *argvars;
10202 typval_T *rettv;
10203{
10204 float_T f;
10205
10206 rettv->v_type = VAR_FLOAT;
10207 if (get_float_arg(argvars, &f) == OK)
10208 rettv->vval.v_float = cosh(f);
10209 else
10210 rettv->vval.v_float = 0.0;
10211}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010212#endif
10213
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010215 * "count()" function
10216 */
10217 static void
10218f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 typval_T *argvars;
10220 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010221{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010222 long n = 0;
10223 int ic = FALSE;
10224
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 listitem_T *li;
10228 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010230
Bram Moolenaare9a41262005-01-15 22:18:47 +000010231 if ((l = argvars[0].vval.v_list) != NULL)
10232 {
10233 li = l->lv_first;
10234 if (argvars[2].v_type != VAR_UNKNOWN)
10235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 int error = FALSE;
10237
10238 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010239 if (argvars[3].v_type != VAR_UNKNOWN)
10240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 idx = get_tv_number_chk(&argvars[3], &error);
10242 if (!error)
10243 {
10244 li = list_find(l, idx);
10245 if (li == NULL)
10246 EMSGN(_(e_listidx), idx);
10247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 if (error)
10250 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 }
10252
10253 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010254 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 ++n;
10256 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 else if (argvars[0].v_type == VAR_DICT)
10259 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010260 int todo;
10261 dict_T *d;
10262 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010264 if ((d = argvars[0].vval.v_dict) != NULL)
10265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 int error = FALSE;
10267
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 if (argvars[2].v_type != VAR_UNKNOWN)
10269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 if (argvars[3].v_type != VAR_UNKNOWN)
10272 EMSG(_(e_invarg));
10273 }
10274
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010275 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010276 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010277 {
10278 if (!HASHITEM_EMPTY(hi))
10279 {
10280 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010281 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010282 ++n;
10283 }
10284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 }
10286 }
10287 else
10288 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010289 rettv->vval.v_number = n;
10290}
10291
10292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10294 *
10295 * Checks the existence of a cscope connection.
10296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010299 typval_T *argvars UNUSED;
10300 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
10302#ifdef FEAT_CSCOPE
10303 int num = 0;
10304 char_u *dbpath = NULL;
10305 char_u *prepend = NULL;
10306 char_u buf[NUMBUFLEN];
10307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010308 if (argvars[0].v_type != VAR_UNKNOWN
10309 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 num = (int)get_tv_number(&argvars[0]);
10312 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010313 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010314 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 }
10316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318#endif
10319}
10320
10321/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010322 * "cursor(lnum, col)" function, or
10323 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010325 * Moves the cursor to the specified line and column.
10326 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010330 typval_T *argvars;
10331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
10333 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010334#ifdef FEAT_VIRTUALEDIT
10335 long coladd = 0;
10336#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010337 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010339 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010340 if (argvars[1].v_type == VAR_UNKNOWN)
10341 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010342 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010343 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010344
Bram Moolenaar493c1782014-05-28 14:34:46 +020010345 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010346 {
10347 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010348 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010349 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010350 line = pos.lnum;
10351 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010352#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010353 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010354#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010355 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010356 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010357 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010358 set_curswant = FALSE;
10359 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010360 }
10361 else
10362 {
10363 line = get_tv_lnum(argvars);
10364 col = get_tv_number_chk(&argvars[1], NULL);
10365#ifdef FEAT_VIRTUALEDIT
10366 if (argvars[2].v_type != VAR_UNKNOWN)
10367 coladd = get_tv_number_chk(&argvars[2], NULL);
10368#endif
10369 }
10370 if (line < 0 || col < 0
10371#ifdef FEAT_VIRTUALEDIT
10372 || coladd < 0
10373#endif
10374 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010375 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 if (line > 0)
10377 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 if (col > 0)
10379 curwin->w_cursor.col = col - 1;
10380#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010381 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382#endif
10383
10384 /* Make sure the cursor is in a valid position. */
10385 check_cursor();
10386#ifdef FEAT_MBYTE
10387 /* Correct cursor for multi-byte character. */
10388 if (has_mbyte)
10389 mb_adjust_cursor();
10390#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010391
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010392 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010393 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394}
10395
10396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010397 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 */
10399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010400f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010401 typval_T *argvars;
10402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010404 int noref = 0;
10405
10406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010408 if (noref < 0 || noref > 1)
10409 EMSG(_(e_invarg));
10410 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010411 {
10412 current_copyID += COPYID_INC;
10413 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415}
10416
10417/*
10418 * "delete()" function
10419 */
10420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010421f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010422 typval_T *argvars;
10423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010425 char_u nbuf[NUMBUFLEN];
10426 char_u *name;
10427 char_u *flags;
10428
10429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010431 return;
10432
10433 name = get_tv_string(&argvars[0]);
10434 if (name == NULL || *name == NUL)
10435 {
10436 EMSG(_(e_invarg));
10437 return;
10438 }
10439
10440 if (argvars[1].v_type != VAR_UNKNOWN)
10441 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010443 flags = (char_u *)"";
10444
10445 if (*flags == NUL)
10446 /* delete a file */
10447 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10448 else if (STRCMP(flags, "d") == 0)
10449 /* delete an empty directory */
10450 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10451 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010452 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010453 rettv->vval.v_number = delete_recursive(name);
10454 else
10455 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456}
10457
10458/*
10459 * "did_filetype()" function
10460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010463 typval_T *argvars UNUSED;
10464 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468#endif
10469}
10470
10471/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010472 * "diff_filler()" function
10473 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010476 typval_T *argvars UNUSED;
10477 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010478{
10479#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010480 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010481#endif
10482}
10483
10484/*
10485 * "diff_hlID()" function
10486 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010488f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010489 typval_T *argvars UNUSED;
10490 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010491{
10492#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010494 static linenr_T prev_lnum = 0;
10495 static int changedtick = 0;
10496 static int fnum = 0;
10497 static int change_start = 0;
10498 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010499 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010500 int filler_lines;
10501 int col;
10502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 if (lnum < 0) /* ignore type error in {lnum} arg */
10504 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010505 if (lnum != prev_lnum
10506 || changedtick != curbuf->b_changedtick
10507 || fnum != curbuf->b_fnum)
10508 {
10509 /* New line, buffer, change: need to get the values. */
10510 filler_lines = diff_check(curwin, lnum);
10511 if (filler_lines < 0)
10512 {
10513 if (filler_lines == -1)
10514 {
10515 change_start = MAXCOL;
10516 change_end = -1;
10517 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10518 hlID = HLF_ADD; /* added line */
10519 else
10520 hlID = HLF_CHD; /* changed line */
10521 }
10522 else
10523 hlID = HLF_ADD; /* added line */
10524 }
10525 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010526 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010527 prev_lnum = lnum;
10528 changedtick = curbuf->b_changedtick;
10529 fnum = curbuf->b_fnum;
10530 }
10531
10532 if (hlID == HLF_CHD || hlID == HLF_TXD)
10533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010535 if (col >= change_start && col <= change_end)
10536 hlID = HLF_TXD; /* changed text */
10537 else
10538 hlID = HLF_CHD; /* changed line */
10539 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010540 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010541#endif
10542}
10543
10544/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010545 * "empty({expr})" function
10546 */
10547 static void
10548f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010549 typval_T *argvars;
10550 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010551{
10552 int n;
10553
10554 switch (argvars[0].v_type)
10555 {
10556 case VAR_STRING:
10557 case VAR_FUNC:
10558 n = argvars[0].vval.v_string == NULL
10559 || *argvars[0].vval.v_string == NUL;
10560 break;
10561 case VAR_NUMBER:
10562 n = argvars[0].vval.v_number == 0;
10563 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010564#ifdef FEAT_FLOAT
10565 case VAR_FLOAT:
10566 n = argvars[0].vval.v_float == 0.0;
10567 break;
10568#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010569 case VAR_LIST:
10570 n = argvars[0].vval.v_list == NULL
10571 || argvars[0].vval.v_list->lv_first == NULL;
10572 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 case VAR_DICT:
10574 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010577 default:
10578 EMSG2(_(e_intern2), "f_empty()");
10579 n = 0;
10580 }
10581
10582 rettv->vval.v_number = n;
10583}
10584
10585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 * "escape({string}, {chars})" function
10587 */
10588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010590 typval_T *argvars;
10591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592{
10593 char_u buf[NUMBUFLEN];
10594
Bram Moolenaar758711c2005-02-02 23:11:38 +000010595 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10596 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598}
10599
10600/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010601 * "eval()" function
10602 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010603 static void
10604f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010605 typval_T *argvars;
10606 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010607{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010608 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 s = get_tv_string_chk(&argvars[0]);
10611 if (s != NULL)
10612 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010613
Bram Moolenaar615b9972015-01-14 17:15:05 +010010614 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010615 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10616 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010617 if (p != NULL && !aborting())
10618 EMSG2(_(e_invexpr2), p);
10619 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010621 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010622 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623 else if (*s != NUL)
10624 EMSG(_(e_trailing));
10625}
10626
10627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 * "eventhandler()" function
10629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010632 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636}
10637
10638/*
10639 * "executable()" function
10640 */
10641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010643 typval_T *argvars;
10644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010646 char_u *name = get_tv_string(&argvars[0]);
10647
10648 /* Check in $PATH and also check directly if there is a directory name. */
10649 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10650 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010651}
10652
10653/*
10654 * "exepath()" function
10655 */
10656 static void
10657f_exepath(argvars, rettv)
10658 typval_T *argvars;
10659 typval_T *rettv;
10660{
10661 char_u *p = NULL;
10662
Bram Moolenaarb5971142015-03-21 17:32:19 +010010663 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010664 rettv->v_type = VAR_STRING;
10665 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666}
10667
10668/*
10669 * "exists()" function
10670 */
10671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010672f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010673 typval_T *argvars;
10674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675{
10676 char_u *p;
10677 char_u *name;
10678 int n = FALSE;
10679 int len = 0;
10680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 if (*p == '$') /* environment variable */
10683 {
10684 /* first try "normal" environment variables (fast) */
10685 if (mch_getenv(p + 1) != NULL)
10686 n = TRUE;
10687 else
10688 {
10689 /* try expanding things like $VIM and ${HOME} */
10690 p = expand_env_save(p);
10691 if (p != NULL && *p != '$')
10692 n = TRUE;
10693 vim_free(p);
10694 }
10695 }
10696 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010699 if (*skipwhite(p) != NUL)
10700 n = FALSE; /* trailing garbage */
10701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 else if (*p == '*') /* internal or user defined function */
10703 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010704 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 }
10706 else if (*p == ':')
10707 {
10708 n = cmd_exists(p + 1);
10709 }
10710 else if (*p == '#')
10711 {
10712#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010713 if (p[1] == '#')
10714 n = autocmd_supported(p + 2);
10715 else
10716 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717#endif
10718 }
10719 else /* internal variable */
10720 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010721 char_u *tofree;
10722 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010724 /* get_name_len() takes care of expanding curly braces */
10725 name = p;
10726 len = get_name_len(&p, &tofree, TRUE, FALSE);
10727 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010729 if (tofree != NULL)
10730 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010731 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010732 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010734 /* handle d.key, l[idx], f(expr) */
10735 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10736 if (n)
10737 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 }
10739 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010740 if (*p != NUL)
10741 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010743 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 }
10745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747}
10748
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010749#ifdef FEAT_FLOAT
10750/*
10751 * "exp()" function
10752 */
10753 static void
10754f_exp(argvars, rettv)
10755 typval_T *argvars;
10756 typval_T *rettv;
10757{
10758 float_T f;
10759
10760 rettv->v_type = VAR_FLOAT;
10761 if (get_float_arg(argvars, &f) == OK)
10762 rettv->vval.v_float = exp(f);
10763 else
10764 rettv->vval.v_float = 0.0;
10765}
10766#endif
10767
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768/*
10769 * "expand()" function
10770 */
10771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010773 typval_T *argvars;
10774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
10776 char_u *s;
10777 int len;
10778 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010779 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010781 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010782 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010785 if (argvars[1].v_type != VAR_UNKNOWN
10786 && argvars[2].v_type != VAR_UNKNOWN
10787 && get_tv_number_chk(&argvars[2], &error)
10788 && !error)
10789 {
10790 rettv->v_type = VAR_LIST;
10791 rettv->vval.v_list = NULL;
10792 }
10793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 if (*s == '%' || *s == '#' || *s == '<')
10796 {
10797 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010798 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010800 if (rettv->v_type == VAR_LIST)
10801 {
10802 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10803 list_append_string(rettv->vval.v_list, result, -1);
10804 else
10805 vim_free(result);
10806 }
10807 else
10808 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 }
10810 else
10811 {
10812 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010813 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010814 if (argvars[1].v_type != VAR_UNKNOWN
10815 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010816 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010817 if (!error)
10818 {
10819 ExpandInit(&xpc);
10820 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010821 if (p_wic)
10822 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010823 if (rettv->v_type == VAR_STRING)
10824 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10825 options, WILD_ALL);
10826 else if (rettv_list_alloc(rettv) != FAIL)
10827 {
10828 int i;
10829
10830 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10831 for (i = 0; i < xpc.xp_numfiles; i++)
10832 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10833 ExpandCleanup(&xpc);
10834 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010835 }
10836 else
10837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 }
10839}
10840
10841/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010842 * Go over all entries in "d2" and add them to "d1".
10843 * When "action" is "error" then a duplicate key is an error.
10844 * When "action" is "force" then a duplicate key is overwritten.
10845 * Otherwise duplicate keys are ignored ("action" is "keep").
10846 */
10847 void
10848dict_extend(d1, d2, action)
10849 dict_T *d1;
10850 dict_T *d2;
10851 char_u *action;
10852{
10853 dictitem_T *di1;
10854 hashitem_T *hi2;
10855 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010856 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010857
10858 todo = (int)d2->dv_hashtab.ht_used;
10859 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10860 {
10861 if (!HASHITEM_EMPTY(hi2))
10862 {
10863 --todo;
10864 di1 = dict_find(d1, hi2->hi_key, -1);
10865 if (d1->dv_scope != 0)
10866 {
10867 /* Disallow replacing a builtin function in l: and g:.
10868 * Check the key to be valid when adding to any
10869 * scope. */
10870 if (d1->dv_scope == VAR_DEF_SCOPE
10871 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10872 && var_check_func_name(hi2->hi_key,
10873 di1 == NULL))
10874 break;
10875 if (!valid_varname(hi2->hi_key))
10876 break;
10877 }
10878 if (di1 == NULL)
10879 {
10880 di1 = dictitem_copy(HI2DI(hi2));
10881 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10882 dictitem_free(di1);
10883 }
10884 else if (*action == 'e')
10885 {
10886 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10887 break;
10888 }
10889 else if (*action == 'f' && HI2DI(hi2) != di1)
10890 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010891 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10892 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010893 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010894 clear_tv(&di1->di_tv);
10895 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10896 }
10897 }
10898 }
10899}
10900
10901/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010902 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010904 */
10905 static void
10906f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 typval_T *argvars;
10908 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010909{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010910 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010911
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010913 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010914 list_T *l1, *l2;
10915 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010916 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010918
Bram Moolenaare9a41262005-01-15 22:18:47 +000010919 l1 = argvars[0].vval.v_list;
10920 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010921 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010922 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923 {
10924 if (argvars[2].v_type != VAR_UNKNOWN)
10925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926 before = get_tv_number_chk(&argvars[2], &error);
10927 if (error)
10928 return; /* type error; errmsg already given */
10929
Bram Moolenaar758711c2005-02-02 23:11:38 +000010930 if (before == l1->lv_len)
10931 item = NULL;
10932 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010933 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010934 item = list_find(l1, before);
10935 if (item == NULL)
10936 {
10937 EMSGN(_(e_listidx), before);
10938 return;
10939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 }
10941 }
10942 else
10943 item = NULL;
10944 list_extend(l1, l2, item);
10945
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 copy_tv(&argvars[0], rettv);
10947 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010948 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10950 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010951 dict_T *d1, *d2;
10952 char_u *action;
10953 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954
10955 d1 = argvars[0].vval.v_dict;
10956 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010957 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010958 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010959 {
10960 /* Check the third argument. */
10961 if (argvars[2].v_type != VAR_UNKNOWN)
10962 {
10963 static char *(av[]) = {"keep", "force", "error"};
10964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 action = get_tv_string_chk(&argvars[2]);
10966 if (action == NULL)
10967 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 for (i = 0; i < 3; ++i)
10969 if (STRCMP(action, av[i]) == 0)
10970 break;
10971 if (i == 3)
10972 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010973 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 return;
10975 }
10976 }
10977 else
10978 action = (char_u *)"force";
10979
Bram Moolenaara9922d62013-05-30 13:01:18 +020010980 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 copy_tv(&argvars[0], rettv);
10983 }
10984 }
10985 else
10986 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010987}
10988
10989/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010990 * "feedkeys()" function
10991 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010992 static void
10993f_feedkeys(argvars, rettv)
10994 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010995 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010996{
10997 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010998 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010999 char_u *keys, *flags;
11000 char_u nbuf[NUMBUFLEN];
11001 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011002 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011003 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011004
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011005 /* This is not allowed in the sandbox. If the commands would still be
11006 * executed in the sandbox it would be OK, but it probably happens later,
11007 * when "sandbox" is no longer set. */
11008 if (check_secure())
11009 return;
11010
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011011 keys = get_tv_string(&argvars[0]);
11012 if (*keys != NUL)
11013 {
11014 if (argvars[1].v_type != VAR_UNKNOWN)
11015 {
11016 flags = get_tv_string_buf(&argvars[1], nbuf);
11017 for ( ; *flags != NUL; ++flags)
11018 {
11019 switch (*flags)
11020 {
11021 case 'n': remap = FALSE; break;
11022 case 'm': remap = TRUE; break;
11023 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011024 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011025 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011026 }
11027 }
11028 }
11029
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011030 /* Need to escape K_SPECIAL and CSI before putting the string in the
11031 * typeahead buffer. */
11032 keys_esc = vim_strsave_escape_csi(keys);
11033 if (keys_esc != NULL)
11034 {
11035 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011036 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011037 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011038 if (vgetc_busy)
11039 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011040 if (execute)
11041 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011042 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011043 }
11044}
11045
11046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 * "filereadable()" function
11048 */
11049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011051 typval_T *argvars;
11052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011054 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 char_u *p;
11056 int n;
11057
Bram Moolenaarc236c162008-07-13 17:41:49 +000011058#ifndef O_NONBLOCK
11059# define O_NONBLOCK 0
11060#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011062 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11063 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 {
11065 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011066 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 }
11068 else
11069 n = FALSE;
11070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072}
11073
11074/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011075 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 * rights to write into.
11077 */
11078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 typval_T *argvars;
11081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011083 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084}
11085
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011086static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011087
11088 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011089findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011091 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011092 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011093{
11094#ifdef FEAT_SEARCHPATH
11095 char_u *fname;
11096 char_u *fresult = NULL;
11097 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11098 char_u *p;
11099 char_u pathbuf[NUMBUFLEN];
11100 int count = 1;
11101 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011102 int error = FALSE;
11103#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011104
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011105 rettv->vval.v_string = NULL;
11106 rettv->v_type = VAR_STRING;
11107
11108#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011110
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011111 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11114 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011115 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 else
11117 {
11118 if (*p != NUL)
11119 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011121 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011122 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011124 }
11125
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011126 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11127 error = TRUE;
11128
11129 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 do
11132 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011133 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011134 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 fresult = find_file_in_path_option(first ? fname : NULL,
11136 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011137 0, first, path,
11138 find_what,
11139 curbuf->b_ffname,
11140 find_what == FINDFILE_DIR
11141 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011143
11144 if (fresult != NULL && rettv->v_type == VAR_LIST)
11145 list_append_string(rettv->vval.v_list, fresult, -1);
11146
11147 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011149
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011150 if (rettv->v_type == VAR_STRING)
11151 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011152#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011153}
11154
Bram Moolenaar33570922005-01-25 22:26:29 +000011155static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11156static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011157
11158/*
11159 * Implementation of map() and filter().
11160 */
11161 static void
11162filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 typval_T *argvars;
11164 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011165 int map;
11166{
11167 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011168 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011169 listitem_T *li, *nli;
11170 list_T *l = NULL;
11171 dictitem_T *di;
11172 hashtab_T *ht;
11173 hashitem_T *hi;
11174 dict_T *d = NULL;
11175 typval_T save_val;
11176 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011177 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011178 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011179 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011180 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011181 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011182 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011183 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011184
Bram Moolenaare9a41262005-01-15 22:18:47 +000011185 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011186 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011187 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011188 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011189 return;
11190 }
11191 else if (argvars[0].v_type == VAR_DICT)
11192 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011193 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011194 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011195 return;
11196 }
11197 else
11198 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011199 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011200 return;
11201 }
11202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 expr = get_tv_string_buf_chk(&argvars[1], buf);
11204 /* On type errors, the preceding call has already displayed an error
11205 * message. Avoid a misleading error message for an empty string that
11206 * was not passed as argument. */
11207 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 prepare_vimvar(VV_VAL, &save_val);
11210 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011211
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011212 /* We reset "did_emsg" to be able to detect whether an error
11213 * occurred during evaluation of the expression. */
11214 save_did_emsg = did_emsg;
11215 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011216
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011217 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 vimvars[VV_KEY].vv_type = VAR_STRING;
11221
11222 ht = &d->dv_hashtab;
11223 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011224 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 if (!HASHITEM_EMPTY(hi))
11228 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011229 int r;
11230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231 --todo;
11232 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011233 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011234 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11235 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011236 break;
11237 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011238 r = filter_map_one(&di->di_tv, expr, map, &rem);
11239 clear_tv(&vimvars[VV_KEY].vv_tv);
11240 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 break;
11242 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011243 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011244 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11245 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011246 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 }
11250 }
11251 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011252 }
11253 else
11254 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011255 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 for (li = l->lv_first; li != NULL; li = nli)
11258 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011259 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011260 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011262 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011263 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011264 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011265 break;
11266 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011267 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011268 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011269 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011270 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011271
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011272 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011274
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011275 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011276 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011277
11278 copy_tv(&argvars[0], rettv);
11279}
11280
11281 static int
11282filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011284 char_u *expr;
11285 int map;
11286 int *remp;
11287{
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011289 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011290 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011291
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011293 s = expr;
11294 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011295 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011296 if (*s != NUL) /* check for trailing chars after expr */
11297 {
11298 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011299 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011300 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011301 }
11302 if (map)
11303 {
11304 /* map(): replace the list item value */
11305 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011306 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011307 *tv = rettv;
11308 }
11309 else
11310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 int error = FALSE;
11312
Bram Moolenaare9a41262005-01-15 22:18:47 +000011313 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011314 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011315 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 /* On type error, nothing has been removed; return FAIL to stop the
11317 * loop. The error message was given by get_tv_number_chk(). */
11318 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011319 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011320 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011321 retval = OK;
11322theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011323 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011324 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011325}
11326
11327/*
11328 * "filter()" function
11329 */
11330 static void
11331f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *argvars;
11333 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334{
11335 filter_map(argvars, rettv, FALSE);
11336}
11337
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011338/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011339 * "finddir({fname}[, {path}[, {count}]])" function
11340 */
11341 static void
11342f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *argvars;
11344 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011346 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011347}
11348
11349/*
11350 * "findfile({fname}[, {path}[, {count}]])" function
11351 */
11352 static void
11353f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011354 typval_T *argvars;
11355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011356{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011357 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011358}
11359
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011360#ifdef FEAT_FLOAT
11361/*
11362 * "float2nr({float})" function
11363 */
11364 static void
11365f_float2nr(argvars, rettv)
11366 typval_T *argvars;
11367 typval_T *rettv;
11368{
11369 float_T f;
11370
11371 if (get_float_arg(argvars, &f) == OK)
11372 {
11373 if (f < -0x7fffffff)
11374 rettv->vval.v_number = -0x7fffffff;
11375 else if (f > 0x7fffffff)
11376 rettv->vval.v_number = 0x7fffffff;
11377 else
11378 rettv->vval.v_number = (varnumber_T)f;
11379 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011380}
11381
11382/*
11383 * "floor({float})" function
11384 */
11385 static void
11386f_floor(argvars, rettv)
11387 typval_T *argvars;
11388 typval_T *rettv;
11389{
11390 float_T f;
11391
11392 rettv->v_type = VAR_FLOAT;
11393 if (get_float_arg(argvars, &f) == OK)
11394 rettv->vval.v_float = floor(f);
11395 else
11396 rettv->vval.v_float = 0.0;
11397}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011398
11399/*
11400 * "fmod()" function
11401 */
11402 static void
11403f_fmod(argvars, rettv)
11404 typval_T *argvars;
11405 typval_T *rettv;
11406{
11407 float_T fx, fy;
11408
11409 rettv->v_type = VAR_FLOAT;
11410 if (get_float_arg(argvars, &fx) == OK
11411 && get_float_arg(&argvars[1], &fy) == OK)
11412 rettv->vval.v_float = fmod(fx, fy);
11413 else
11414 rettv->vval.v_float = 0.0;
11415}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011416#endif
11417
Bram Moolenaar0d660222005-01-07 21:51:51 +000011418/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011419 * "fnameescape({string})" function
11420 */
11421 static void
11422f_fnameescape(argvars, rettv)
11423 typval_T *argvars;
11424 typval_T *rettv;
11425{
11426 rettv->vval.v_string = vim_strsave_fnameescape(
11427 get_tv_string(&argvars[0]), FALSE);
11428 rettv->v_type = VAR_STRING;
11429}
11430
11431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 * "fnamemodify({fname}, {mods})" function
11433 */
11434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 typval_T *argvars;
11437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
11439 char_u *fname;
11440 char_u *mods;
11441 int usedlen = 0;
11442 int len;
11443 char_u *fbuf = NULL;
11444 char_u buf[NUMBUFLEN];
11445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 fname = get_tv_string_chk(&argvars[0]);
11447 mods = get_tv_string_buf_chk(&argvars[1], buf);
11448 if (fname == NULL || mods == NULL)
11449 fname = NULL;
11450 else
11451 {
11452 len = (int)STRLEN(fname);
11453 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 vim_free(fbuf);
11462}
11463
Bram Moolenaar33570922005-01-25 22:26:29 +000011464static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465
11466/*
11467 * "foldclosed()" function
11468 */
11469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011473 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
11475#ifdef FEAT_FOLDING
11476 linenr_T lnum;
11477 linenr_T first, last;
11478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11481 {
11482 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11483 {
11484 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 return;
11489 }
11490 }
11491#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493}
11494
11495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011496 * "foldclosed()" function
11497 */
11498 static void
11499f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011500 typval_T *argvars;
11501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011502{
11503 foldclosed_both(argvars, rettv, FALSE);
11504}
11505
11506/*
11507 * "foldclosedend()" function
11508 */
11509 static void
11510f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *argvars;
11512 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513{
11514 foldclosed_both(argvars, rettv, TRUE);
11515}
11516
11517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 * "foldlevel()" function
11519 */
11520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011522 typval_T *argvars UNUSED;
11523 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524{
11525#ifdef FEAT_FOLDING
11526 linenr_T lnum;
11527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011528 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532}
11533
11534/*
11535 * "foldtext()" function
11536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011539 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541{
11542#ifdef FEAT_FOLDING
11543 linenr_T lnum;
11544 char_u *s;
11545 char_u *r;
11546 int len;
11547 char *txt;
11548#endif
11549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 rettv->v_type = VAR_STRING;
11551 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011553 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11554 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11555 <= curbuf->b_ml.ml_line_count
11556 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 {
11558 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011559 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11560 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 {
11562 if (!linewhite(lnum))
11563 break;
11564 ++lnum;
11565 }
11566
11567 /* Find interesting text in this line. */
11568 s = skipwhite(ml_get(lnum));
11569 /* skip C comment-start */
11570 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011573 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011574 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011575 {
11576 s = skipwhite(ml_get(lnum + 1));
11577 if (*s == '*')
11578 s = skipwhite(s + 1);
11579 }
11580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 txt = _("+-%s%3ld lines: ");
11582 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 + 20 /* for %3ld */
11585 + STRLEN(s))); /* concatenated */
11586 if (r != NULL)
11587 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011588 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11589 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11590 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 len = (int)STRLEN(r);
11592 STRCAT(r, s);
11593 /* remove 'foldmarker' and 'commentstring' */
11594 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 }
11597 }
11598#endif
11599}
11600
11601/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011602 * "foldtextresult(lnum)" function
11603 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011606 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011608{
11609#ifdef FEAT_FOLDING
11610 linenr_T lnum;
11611 char_u *text;
11612 char_u buf[51];
11613 foldinfo_T foldinfo;
11614 int fold_count;
11615#endif
11616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->v_type = VAR_STRING;
11618 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011619#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621 /* treat illegal types and illegal string values for {lnum} the same */
11622 if (lnum < 0)
11623 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011624 fold_count = foldedCount(curwin, lnum, &foldinfo);
11625 if (fold_count > 0)
11626 {
11627 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11628 &foldinfo, buf);
11629 if (text == buf)
11630 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011632 }
11633#endif
11634}
11635
11636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 * "foreground()" function
11638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011641 typval_T *argvars UNUSED;
11642 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644#ifdef FEAT_GUI
11645 if (gui.in_use)
11646 gui_mch_set_foreground();
11647#else
11648# ifdef WIN32
11649 win32_set_foreground();
11650# endif
11651#endif
11652}
11653
11654/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011655 * "function()" function
11656 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *argvars;
11660 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011661{
11662 char_u *s;
11663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011664 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011665 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011666 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011667 /* Don't check an autoload name for existence here. */
11668 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011669 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011670 else
11671 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011672 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011673 {
11674 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011675 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011676
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011677 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11678 * also be called from another script. Using trans_function_name()
11679 * would also work, but some plugins depend on the name being
11680 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011681 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011682 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011683 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011684 if (rettv->vval.v_string != NULL)
11685 {
11686 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011687 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011688 }
11689 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011690 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011691 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693 }
11694}
11695
11696/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011697 * "garbagecollect()" function
11698 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011699 static void
11700f_garbagecollect(argvars, rettv)
11701 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011702 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011703{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011704 /* This is postponed until we are back at the toplevel, because we may be
11705 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11706 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011707
11708 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11709 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011710}
11711
11712/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011713 * "get()" function
11714 */
11715 static void
11716f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011717 typval_T *argvars;
11718 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011719{
Bram Moolenaar33570922005-01-25 22:26:29 +000011720 listitem_T *li;
11721 list_T *l;
11722 dictitem_T *di;
11723 dict_T *d;
11724 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011725
Bram Moolenaare9a41262005-01-15 22:18:47 +000011726 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011728 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 int error = FALSE;
11731
11732 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11733 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011735 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011737 else if (argvars[0].v_type == VAR_DICT)
11738 {
11739 if ((d = argvars[0].vval.v_dict) != NULL)
11740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011741 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 if (di != NULL)
11743 tv = &di->di_tv;
11744 }
11745 }
11746 else
11747 EMSG2(_(e_listdictarg), "get()");
11748
11749 if (tv == NULL)
11750 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011751 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011752 copy_tv(&argvars[2], rettv);
11753 }
11754 else
11755 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011756}
11757
Bram Moolenaar342337a2005-07-21 21:11:17 +000011758static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011759
11760/*
11761 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011762 * Return a range (from start to end) of lines in rettv from the specified
11763 * buffer.
11764 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011765 */
11766 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011767get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011768 buf_T *buf;
11769 linenr_T start;
11770 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011771 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011772 typval_T *rettv;
11773{
11774 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011775
Bram Moolenaar959a1432013-12-14 12:17:38 +010011776 rettv->v_type = VAR_STRING;
11777 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011778 if (retlist && rettv_list_alloc(rettv) == FAIL)
11779 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011780
11781 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11782 return;
11783
11784 if (!retlist)
11785 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011786 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11787 p = ml_get_buf(buf, start, FALSE);
11788 else
11789 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011790 rettv->vval.v_string = vim_strsave(p);
11791 }
11792 else
11793 {
11794 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011795 return;
11796
11797 if (start < 1)
11798 start = 1;
11799 if (end > buf->b_ml.ml_line_count)
11800 end = buf->b_ml.ml_line_count;
11801 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011802 if (list_append_string(rettv->vval.v_list,
11803 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011804 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011805 }
11806}
11807
11808/*
11809 * "getbufline()" function
11810 */
11811 static void
11812f_getbufline(argvars, rettv)
11813 typval_T *argvars;
11814 typval_T *rettv;
11815{
11816 linenr_T lnum;
11817 linenr_T end;
11818 buf_T *buf;
11819
11820 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11821 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011822 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011823 --emsg_off;
11824
Bram Moolenaar661b1822005-07-28 22:36:45 +000011825 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011826 if (argvars[2].v_type == VAR_UNKNOWN)
11827 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011828 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011829 end = get_tv_lnum_buf(&argvars[2], buf);
11830
Bram Moolenaar342337a2005-07-21 21:11:17 +000011831 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011832}
11833
Bram Moolenaar0d660222005-01-07 21:51:51 +000011834/*
11835 * "getbufvar()" function
11836 */
11837 static void
11838f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *argvars;
11840 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011841{
11842 buf_T *buf;
11843 buf_T *save_curbuf;
11844 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011845 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011846 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11849 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011851 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011852
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011853 rettv->v_type = VAR_STRING;
11854 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011855
11856 if (buf != NULL && varname != NULL)
11857 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011858 /* set curbuf to be our buf, temporarily */
11859 save_curbuf = curbuf;
11860 curbuf = buf;
11861
Bram Moolenaar0d660222005-01-07 21:51:51 +000011862 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011863 {
11864 if (get_option_tv(&varname, rettv, TRUE) == OK)
11865 done = TRUE;
11866 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011867 else if (STRCMP(varname, "changedtick") == 0)
11868 {
11869 rettv->v_type = VAR_NUMBER;
11870 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011871 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011872 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011873 else
11874 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011875 /* Look up the variable. */
11876 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11877 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11878 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011879 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011880 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011882 done = TRUE;
11883 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011884 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011885
11886 /* restore previous notion of curbuf */
11887 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888 }
11889
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011890 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11891 /* use the default value */
11892 copy_tv(&argvars[2], rettv);
11893
Bram Moolenaar0d660222005-01-07 21:51:51 +000011894 --emsg_off;
11895}
11896
11897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 * "getchar()" function
11899 */
11900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904{
11905 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011906 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011908 /* Position the cursor. Needed after a message that ends in a space. */
11909 windgoto(msg_row, msg_col);
11910
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 ++no_mapping;
11912 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011913 for (;;)
11914 {
11915 if (argvars[0].v_type == VAR_UNKNOWN)
11916 /* getchar(): blocking wait. */
11917 n = safe_vgetc();
11918 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11919 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011920 n = vpeekc_any();
11921 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011922 /* illegal argument or getchar(0) and no char avail: return zero */
11923 n = 0;
11924 else
11925 /* getchar(0) and char avail: return char */
11926 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011927
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011928 if (n == K_IGNORE)
11929 continue;
11930 break;
11931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 --no_mapping;
11933 --allow_keys;
11934
Bram Moolenaar219b8702006-11-01 14:32:36 +000011935 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11936 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11937 vimvars[VV_MOUSE_COL].vv_nr = 0;
11938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 if (IS_SPECIAL(n) || mod_mask != 0)
11941 {
11942 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11943 int i = 0;
11944
11945 /* Turn a special key into three bytes, plus modifier. */
11946 if (mod_mask != 0)
11947 {
11948 temp[i++] = K_SPECIAL;
11949 temp[i++] = KS_MODIFIER;
11950 temp[i++] = mod_mask;
11951 }
11952 if (IS_SPECIAL(n))
11953 {
11954 temp[i++] = K_SPECIAL;
11955 temp[i++] = K_SECOND(n);
11956 temp[i++] = K_THIRD(n);
11957 }
11958#ifdef FEAT_MBYTE
11959 else if (has_mbyte)
11960 i += (*mb_char2bytes)(n, temp + i);
11961#endif
11962 else
11963 temp[i++] = n;
11964 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->v_type = VAR_STRING;
11966 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011967
11968#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011969 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011970 {
11971 int row = mouse_row;
11972 int col = mouse_col;
11973 win_T *win;
11974 linenr_T lnum;
11975# ifdef FEAT_WINDOWS
11976 win_T *wp;
11977# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011978 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011979
11980 if (row >= 0 && col >= 0)
11981 {
11982 /* Find the window at the mouse coordinates and compute the
11983 * text position. */
11984 win = mouse_find_win(&row, &col);
11985 (void)mouse_comp_pos(win, &row, &col, &lnum);
11986# ifdef FEAT_WINDOWS
11987 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011988 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011989# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011990 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011991 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11992 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11993 }
11994 }
11995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 }
11997}
11998
11999/*
12000 * "getcharmod()" function
12001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012004 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008}
12009
12010/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012011 * "getcharsearch()" function
12012 */
12013 static void
12014f_getcharsearch(argvars, rettv)
12015 typval_T *argvars UNUSED;
12016 typval_T *rettv;
12017{
12018 if (rettv_dict_alloc(rettv) != FAIL)
12019 {
12020 dict_T *dict = rettv->vval.v_dict;
12021
12022 dict_add_nr_str(dict, "char", 0L, last_csearch());
12023 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12024 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12025 }
12026}
12027
12028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 * "getcmdline()" function
12030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->v_type = VAR_STRING;
12037 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038}
12039
12040/*
12041 * "getcmdpos()" function
12042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012044f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012045 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012046 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
12055f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012056 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012057 typval_T *rettv;
12058{
12059 rettv->v_type = VAR_STRING;
12060 rettv->vval.v_string = alloc(2);
12061 if (rettv->vval.v_string != NULL)
12062 {
12063 rettv->vval.v_string[0] = get_cmdline_type();
12064 rettv->vval.v_string[1] = NUL;
12065 }
12066}
12067
12068/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012069 * "getcmdwintype()" function
12070 */
12071 static void
12072f_getcmdwintype(argvars, rettv)
12073 typval_T *argvars UNUSED;
12074 typval_T *rettv;
12075{
12076 rettv->v_type = VAR_STRING;
12077 rettv->vval.v_string = NULL;
12078#ifdef FEAT_CMDWIN
12079 rettv->vval.v_string = alloc(2);
12080 if (rettv->vval.v_string != NULL)
12081 {
12082 rettv->vval.v_string[0] = cmdwin_type;
12083 rettv->vval.v_string[1] = NUL;
12084 }
12085#endif
12086}
12087
12088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 * "getcwd()" function
12090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092f_getcwd(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012093 typval_T *argvars;
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012096 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012097 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012100 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012101
12102 wp = find_tabwin(&argvars[0], &argvars[1]);
12103 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012105 if (wp->w_localdir != NULL)
12106 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12107 else if(globaldir != NULL)
12108 rettv->vval.v_string = vim_strsave(globaldir);
12109 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012110 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012111 cwd = alloc(MAXPATHL);
12112 if (cwd != NULL)
12113 {
12114 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12115 rettv->vval.v_string = vim_strsave(cwd);
12116 vim_free(cwd);
12117 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012118 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012119#ifdef BACKSLASH_IN_FILENAME
12120 if (rettv->vval.v_string != NULL)
12121 slash_adjust(rettv->vval.v_string);
12122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 }
12124}
12125
12126/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012127 * "getfontname()" function
12128 */
12129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012133{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->v_type = VAR_STRING;
12135 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012136#ifdef FEAT_GUI
12137 if (gui.in_use)
12138 {
12139 GuiFont font;
12140 char_u *name = NULL;
12141
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012142 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012143 {
12144 /* Get the "Normal" font. Either the name saved by
12145 * hl_set_font_name() or from the font ID. */
12146 font = gui.norm_font;
12147 name = hl_get_font_name();
12148 }
12149 else
12150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012152 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12153 return;
12154 font = gui_mch_get_font(name, FALSE);
12155 if (font == NOFONT)
12156 return; /* Invalid font name, return empty string. */
12157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012159 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012160 gui_mch_free_font(font);
12161 }
12162#endif
12163}
12164
12165/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012166 * "getfperm({fname})" function
12167 */
12168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012170 typval_T *argvars;
12171 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012172{
12173 char_u *fname;
12174 struct stat st;
12175 char_u *perm = NULL;
12176 char_u flags[] = "rwx";
12177 int i;
12178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012182 if (mch_stat((char *)fname, &st) >= 0)
12183 {
12184 perm = vim_strsave((char_u *)"---------");
12185 if (perm != NULL)
12186 {
12187 for (i = 0; i < 9; i++)
12188 {
12189 if (st.st_mode & (1 << (8 - i)))
12190 perm[i] = flags[i % 3];
12191 }
12192 }
12193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012195}
12196
12197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 * "getfsize({fname})" function
12199 */
12200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012202 typval_T *argvars;
12203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
12205 char_u *fname;
12206 struct stat st;
12207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012208 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012210 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211
12212 if (mch_stat((char *)fname, &st) >= 0)
12213 {
12214 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012218 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012219
12220 /* non-perfect check for overflow */
12221 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12222 rettv->vval.v_number = -2;
12223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 }
12225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227}
12228
12229/*
12230 * "getftime({fname})" function
12231 */
12232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012234 typval_T *argvars;
12235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236{
12237 char_u *fname;
12238 struct stat st;
12239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012240 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241
12242 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246}
12247
12248/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012249 * "getftype({fname})" function
12250 */
12251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012253 typval_T *argvars;
12254 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012255{
12256 char_u *fname;
12257 struct stat st;
12258 char_u *type = NULL;
12259 char *t;
12260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012263 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012264 if (mch_lstat((char *)fname, &st) >= 0)
12265 {
12266#ifdef S_ISREG
12267 if (S_ISREG(st.st_mode))
12268 t = "file";
12269 else if (S_ISDIR(st.st_mode))
12270 t = "dir";
12271# ifdef S_ISLNK
12272 else if (S_ISLNK(st.st_mode))
12273 t = "link";
12274# endif
12275# ifdef S_ISBLK
12276 else if (S_ISBLK(st.st_mode))
12277 t = "bdev";
12278# endif
12279# ifdef S_ISCHR
12280 else if (S_ISCHR(st.st_mode))
12281 t = "cdev";
12282# endif
12283# ifdef S_ISFIFO
12284 else if (S_ISFIFO(st.st_mode))
12285 t = "fifo";
12286# endif
12287# ifdef S_ISSOCK
12288 else if (S_ISSOCK(st.st_mode))
12289 t = "fifo";
12290# endif
12291 else
12292 t = "other";
12293#else
12294# ifdef S_IFMT
12295 switch (st.st_mode & S_IFMT)
12296 {
12297 case S_IFREG: t = "file"; break;
12298 case S_IFDIR: t = "dir"; break;
12299# ifdef S_IFLNK
12300 case S_IFLNK: t = "link"; break;
12301# endif
12302# ifdef S_IFBLK
12303 case S_IFBLK: t = "bdev"; break;
12304# endif
12305# ifdef S_IFCHR
12306 case S_IFCHR: t = "cdev"; break;
12307# endif
12308# ifdef S_IFIFO
12309 case S_IFIFO: t = "fifo"; break;
12310# endif
12311# ifdef S_IFSOCK
12312 case S_IFSOCK: t = "socket"; break;
12313# endif
12314 default: t = "other";
12315 }
12316# else
12317 if (mch_isdir(fname))
12318 t = "dir";
12319 else
12320 t = "file";
12321# endif
12322#endif
12323 type = vim_strsave((char_u *)t);
12324 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012326}
12327
12328/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012329 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330 */
12331 static void
12332f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012333 typval_T *argvars;
12334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335{
12336 linenr_T lnum;
12337 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012338 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012339
12340 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012341 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012342 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012343 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012344 retlist = FALSE;
12345 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012346 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012347 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012348 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012349 retlist = TRUE;
12350 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012351
Bram Moolenaar342337a2005-07-21 21:11:17 +000012352 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012353}
12354
12355/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012356 * "getmatches()" function
12357 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012358 static void
12359f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012360 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012361 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012362{
12363#ifdef FEAT_SEARCH_EXTRA
12364 dict_T *dict;
12365 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012366 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012367
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012368 if (rettv_list_alloc(rettv) == OK)
12369 {
12370 while (cur != NULL)
12371 {
12372 dict = dict_alloc();
12373 if (dict == NULL)
12374 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012375 if (cur->match.regprog == NULL)
12376 {
12377 /* match added with matchaddpos() */
12378 for (i = 0; i < MAXPOSMATCH; ++i)
12379 {
12380 llpos_T *llpos;
12381 char buf[6];
12382 list_T *l;
12383
12384 llpos = &cur->pos.pos[i];
12385 if (llpos->lnum == 0)
12386 break;
12387 l = list_alloc();
12388 if (l == NULL)
12389 break;
12390 list_append_number(l, (varnumber_T)llpos->lnum);
12391 if (llpos->col > 0)
12392 {
12393 list_append_number(l, (varnumber_T)llpos->col);
12394 list_append_number(l, (varnumber_T)llpos->len);
12395 }
12396 sprintf(buf, "pos%d", i + 1);
12397 dict_add_list(dict, buf, l);
12398 }
12399 }
12400 else
12401 {
12402 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12403 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012404 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012405 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12406 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012407# ifdef FEAT_CONCEAL
12408 if (cur->conceal_char)
12409 {
12410 char_u buf[MB_MAXBYTES + 1];
12411
12412 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12413 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12414 }
12415# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012416 list_append_dict(rettv->vval.v_list, dict);
12417 cur = cur->next;
12418 }
12419 }
12420#endif
12421}
12422
12423/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012424 * "getpid()" function
12425 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012426 static void
12427f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012428 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012429 typval_T *rettv;
12430{
12431 rettv->vval.v_number = mch_get_pid();
12432}
12433
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012434static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12435
12436/*
12437 * "getcurpos()" function
12438 */
12439 static void
12440f_getcurpos(argvars, rettv)
12441 typval_T *argvars;
12442 typval_T *rettv;
12443{
12444 getpos_both(argvars, rettv, TRUE);
12445}
12446
Bram Moolenaar18081e32008-02-20 19:11:07 +000012447/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012448 * "getpos(string)" function
12449 */
12450 static void
12451f_getpos(argvars, rettv)
12452 typval_T *argvars;
12453 typval_T *rettv;
12454{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012455 getpos_both(argvars, rettv, FALSE);
12456}
12457
12458 static void
12459getpos_both(argvars, rettv, getcurpos)
12460 typval_T *argvars;
12461 typval_T *rettv;
12462 int getcurpos;
12463{
Bram Moolenaara5525202006-03-02 22:52:09 +000012464 pos_T *fp;
12465 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012466 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012467
12468 if (rettv_list_alloc(rettv) == OK)
12469 {
12470 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012471 if (getcurpos)
12472 fp = &curwin->w_cursor;
12473 else
12474 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012475 if (fnum != -1)
12476 list_append_number(l, (varnumber_T)fnum);
12477 else
12478 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012479 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12480 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012481 list_append_number(l, (fp != NULL)
12482 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012483 : (varnumber_T)0);
12484 list_append_number(l,
12485#ifdef FEAT_VIRTUALEDIT
12486 (fp != NULL) ? (varnumber_T)fp->coladd :
12487#endif
12488 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012489 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012490 list_append_number(l, curwin->w_curswant == MAXCOL ?
12491 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012492 }
12493 else
12494 rettv->vval.v_number = FALSE;
12495}
12496
12497/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012498 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012499 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012500 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012501f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012502 typval_T *argvars UNUSED;
12503 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012504{
12505#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012506 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012507#endif
12508
Bram Moolenaar2641f772005-03-25 21:58:17 +000012509#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012510 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012511 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012512 wp = NULL;
12513 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12514 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012515 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012516 if (wp == NULL)
12517 return;
12518 }
12519
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012520 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012521 }
12522#endif
12523}
12524
12525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 * "getreg()" function
12527 */
12528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012530 typval_T *argvars;
12531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532{
12533 char_u *strregname;
12534 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012535 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012536 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012539 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 strregname = get_tv_string_chk(&argvars[0]);
12542 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012543 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012546 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12547 return_list = get_tv_number_chk(&argvars[2], &error);
12548 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012551 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012552
12553 if (error)
12554 return;
12555
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 regname = (strregname == NULL ? '"' : *strregname);
12557 if (regname == 0)
12558 regname = '"';
12559
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012560 if (return_list)
12561 {
12562 rettv->v_type = VAR_LIST;
12563 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12564 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012565 if (rettv->vval.v_list != NULL)
12566 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012567 }
12568 else
12569 {
12570 rettv->v_type = VAR_STRING;
12571 rettv->vval.v_string = get_reg_contents(regname,
12572 arg2 ? GREG_EXPR_SRC : 0);
12573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574}
12575
12576/*
12577 * "getregtype()" function
12578 */
12579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012581 typval_T *argvars;
12582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583{
12584 char_u *strregname;
12585 int regname;
12586 char_u buf[NUMBUFLEN + 2];
12587 long reglen = 0;
12588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012589 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012590 {
12591 strregname = get_tv_string_chk(&argvars[0]);
12592 if (strregname == NULL) /* type error; errmsg already given */
12593 {
12594 rettv->v_type = VAR_STRING;
12595 rettv->vval.v_string = NULL;
12596 return;
12597 }
12598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 else
12600 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012601 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602
12603 regname = (strregname == NULL ? '"' : *strregname);
12604 if (regname == 0)
12605 regname = '"';
12606
12607 buf[0] = NUL;
12608 buf[1] = NUL;
12609 switch (get_reg_type(regname, &reglen))
12610 {
12611 case MLINE: buf[0] = 'V'; break;
12612 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 case MBLOCK:
12614 buf[0] = Ctrl_V;
12615 sprintf((char *)buf + 1, "%ld", reglen + 1);
12616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012618 rettv->v_type = VAR_STRING;
12619 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620}
12621
12622/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012623 * "gettabvar()" function
12624 */
12625 static void
12626f_gettabvar(argvars, rettv)
12627 typval_T *argvars;
12628 typval_T *rettv;
12629{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012630 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012631 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012632 dictitem_T *v;
12633 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012634 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012635
12636 rettv->v_type = VAR_STRING;
12637 rettv->vval.v_string = NULL;
12638
12639 varname = get_tv_string_chk(&argvars[1]);
12640 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12641 if (tp != NULL && varname != NULL)
12642 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012643 /* Set tp to be our tabpage, temporarily. Also set the window to the
12644 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012645 if (switch_win(&oldcurwin, &oldtabpage,
12646 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012647 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012648 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012649 /* look up the variable */
12650 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12651 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12652 if (v != NULL)
12653 {
12654 copy_tv(&v->di_tv, rettv);
12655 done = TRUE;
12656 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012657 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012658
12659 /* restore previous notion of curwin */
12660 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012661 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012662
12663 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012664 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012665}
12666
12667/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012668 * "gettabwinvar()" function
12669 */
12670 static void
12671f_gettabwinvar(argvars, rettv)
12672 typval_T *argvars;
12673 typval_T *rettv;
12674{
12675 getwinvar(argvars, rettv, 1);
12676}
12677
12678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 * "getwinposx()" function
12680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687#ifdef FEAT_GUI
12688 if (gui.in_use)
12689 {
12690 int x, y;
12691
12692 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 }
12695#endif
12696}
12697
12698/*
12699 * "getwinposy()" function
12700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012703 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707#ifdef FEAT_GUI
12708 if (gui.in_use)
12709 {
12710 int x, y;
12711
12712 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714 }
12715#endif
12716}
12717
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012718/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012719 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012720 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012721 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012722find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012723 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012724 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012725{
12726#ifdef FEAT_WINDOWS
12727 win_T *wp;
12728#endif
12729 int nr;
12730
12731 nr = get_tv_number_chk(vp, NULL);
12732
12733#ifdef FEAT_WINDOWS
12734 if (nr < 0)
12735 return NULL;
12736 if (nr == 0)
12737 return curwin;
12738
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012739 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12740 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012741 if (--nr <= 0)
12742 break;
12743 return wp;
12744#else
12745 if (nr == 0 || nr == 1)
12746 return curwin;
12747 return NULL;
12748#endif
12749}
12750
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012752 * Find window specified by "wvp" in tabpage "tvp".
12753 */
12754 static win_T *
12755find_tabwin(wvp, tvp)
12756 typval_T *wvp; /* VAR_UNKNOWN for current window */
12757 typval_T *tvp; /* VAR_UNKNOWN for current tab page */
12758{
12759 win_T *wp = NULL;
12760 tabpage_T *tp = NULL;
12761 long n;
12762
12763 if (wvp->v_type != VAR_UNKNOWN)
12764 {
12765 if (tvp->v_type != VAR_UNKNOWN)
12766 {
12767 n = get_tv_number(tvp);
12768 if (n >= 0)
12769 tp = find_tabpage(n);
12770 }
12771 else
12772 tp = curtab;
12773
12774 if (tp != NULL)
12775 wp = find_win_by_nr(wvp, tp);
12776 }
12777 else
12778 wp = curwin;
12779
12780 return wp;
12781}
12782
12783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 * "getwinvar()" function
12785 */
12786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *argvars;
12789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012791 getwinvar(argvars, rettv, 0);
12792}
12793
12794/*
12795 * getwinvar() and gettabwinvar()
12796 */
12797 static void
12798getwinvar(argvars, rettv, off)
12799 typval_T *argvars;
12800 typval_T *rettv;
12801 int off; /* 1 for gettabwinvar() */
12802{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012803 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012806 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012807 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012808#ifdef FEAT_WINDOWS
12809 win_T *oldcurwin;
12810 tabpage_T *oldtabpage;
12811 int need_switch_win;
12812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012814#ifdef FEAT_WINDOWS
12815 if (off == 1)
12816 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12817 else
12818 tp = curtab;
12819#endif
12820 win = find_win_by_nr(&argvars[off], tp);
12821 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012822 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012824 rettv->v_type = VAR_STRING;
12825 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826
12827 if (win != NULL && varname != NULL)
12828 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012829#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012830 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012831 * otherwise the window is not valid. Only do this when needed,
12832 * autocommands get blocked. */
12833 need_switch_win = !(tp == curtab && win == curwin);
12834 if (!need_switch_win
12835 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12836#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012837 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012838 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012839 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012840 if (get_option_tv(&varname, rettv, 1) == OK)
12841 done = TRUE;
12842 }
12843 else
12844 {
12845 /* Look up the variable. */
12846 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12847 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12848 varname, FALSE);
12849 if (v != NULL)
12850 {
12851 copy_tv(&v->di_tv, rettv);
12852 done = TRUE;
12853 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012856
Bram Moolenaarba117c22015-09-29 16:53:22 +020012857#ifdef FEAT_WINDOWS
12858 if (need_switch_win)
12859 /* restore previous notion of curwin */
12860 restore_win(oldcurwin, oldtabpage, TRUE);
12861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 }
12863
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012864 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12865 /* use the default return value */
12866 copy_tv(&argvars[off + 2], rettv);
12867
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 --emsg_off;
12869}
12870
12871/*
12872 * "glob()" function
12873 */
12874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012876 typval_T *argvars;
12877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012879 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012881 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012883 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012884 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012886 if (argvars[1].v_type != VAR_UNKNOWN)
12887 {
12888 if (get_tv_number_chk(&argvars[1], &error))
12889 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012890 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012891 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012892 if (get_tv_number_chk(&argvars[2], &error))
12893 {
12894 rettv->v_type = VAR_LIST;
12895 rettv->vval.v_list = NULL;
12896 }
12897 if (argvars[3].v_type != VAR_UNKNOWN
12898 && get_tv_number_chk(&argvars[3], &error))
12899 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012900 }
12901 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012902 if (!error)
12903 {
12904 ExpandInit(&xpc);
12905 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012906 if (p_wic)
12907 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012908 if (rettv->v_type == VAR_STRING)
12909 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012910 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012911 else if (rettv_list_alloc(rettv) != FAIL)
12912 {
12913 int i;
12914
12915 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12916 NULL, options, WILD_ALL_KEEP);
12917 for (i = 0; i < xpc.xp_numfiles; i++)
12918 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12919
12920 ExpandCleanup(&xpc);
12921 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012922 }
12923 else
12924 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925}
12926
12927/*
12928 * "globpath()" function
12929 */
12930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012935 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012937 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012938 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012939 garray_T ga;
12940 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012942 /* When the optional second argument is non-zero, don't remove matches
12943 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012945 if (argvars[2].v_type != VAR_UNKNOWN)
12946 {
12947 if (get_tv_number_chk(&argvars[2], &error))
12948 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012949 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012950 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012951 if (get_tv_number_chk(&argvars[3], &error))
12952 {
12953 rettv->v_type = VAR_LIST;
12954 rettv->vval.v_list = NULL;
12955 }
12956 if (argvars[4].v_type != VAR_UNKNOWN
12957 && get_tv_number_chk(&argvars[4], &error))
12958 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012959 }
12960 }
12961 if (file != NULL && !error)
12962 {
12963 ga_init2(&ga, (int)sizeof(char_u *), 10);
12964 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12965 if (rettv->v_type == VAR_STRING)
12966 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12967 else if (rettv_list_alloc(rettv) != FAIL)
12968 for (i = 0; i < ga.ga_len; ++i)
12969 list_append_string(rettv->vval.v_list,
12970 ((char_u **)(ga.ga_data))[i], -1);
12971 ga_clear_strings(&ga);
12972 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012973 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975}
12976
12977/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012978 * "glob2regpat()" function
12979 */
12980 static void
12981f_glob2regpat(argvars, rettv)
12982 typval_T *argvars;
12983 typval_T *rettv;
12984{
12985 char_u *pat = get_tv_string_chk(&argvars[0]);
12986
12987 rettv->v_type = VAR_STRING;
12988 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12989}
12990
12991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 * "has()" function
12993 */
12994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012996 typval_T *argvars;
12997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998{
12999 int i;
13000 char_u *name;
13001 int n = FALSE;
13002 static char *(has_list[]) =
13003 {
13004#ifdef AMIGA
13005 "amiga",
13006# ifdef FEAT_ARP
13007 "arp",
13008# endif
13009#endif
13010#ifdef __BEOS__
13011 "beos",
13012#endif
13013#ifdef MSDOS
13014# ifdef DJGPP
13015 "dos32",
13016# else
13017 "dos16",
13018# endif
13019#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013020#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 "mac",
13022#endif
13023#if defined(MACOS_X_UNIX)
13024 "macunix",
13025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026#ifdef __QNX__
13027 "qnx",
13028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029#ifdef UNIX
13030 "unix",
13031#endif
13032#ifdef VMS
13033 "vms",
13034#endif
13035#ifdef WIN16
13036 "win16",
13037#endif
13038#ifdef WIN32
13039 "win32",
13040#endif
13041#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13042 "win32unix",
13043#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013044#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 "win64",
13046#endif
13047#ifdef EBCDIC
13048 "ebcdic",
13049#endif
13050#ifndef CASE_INSENSITIVE_FILENAME
13051 "fname_case",
13052#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013053#ifdef HAVE_ACL
13054 "acl",
13055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056#ifdef FEAT_ARABIC
13057 "arabic",
13058#endif
13059#ifdef FEAT_AUTOCMD
13060 "autocmd",
13061#endif
13062#ifdef FEAT_BEVAL
13063 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013064# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13065 "balloon_multiline",
13066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067#endif
13068#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13069 "builtin_terms",
13070# ifdef ALL_BUILTIN_TCAPS
13071 "all_builtin_terms",
13072# endif
13073#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013074#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13075 || defined(FEAT_GUI_W32) \
13076 || defined(FEAT_GUI_MOTIF))
13077 "browsefilter",
13078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079#ifdef FEAT_BYTEOFF
13080 "byte_offset",
13081#endif
13082#ifdef FEAT_CINDENT
13083 "cindent",
13084#endif
13085#ifdef FEAT_CLIENTSERVER
13086 "clientserver",
13087#endif
13088#ifdef FEAT_CLIPBOARD
13089 "clipboard",
13090#endif
13091#ifdef FEAT_CMDL_COMPL
13092 "cmdline_compl",
13093#endif
13094#ifdef FEAT_CMDHIST
13095 "cmdline_hist",
13096#endif
13097#ifdef FEAT_COMMENTS
13098 "comments",
13099#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013100#ifdef FEAT_CONCEAL
13101 "conceal",
13102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103#ifdef FEAT_CRYPT
13104 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013105 "crypt-blowfish",
13106 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#endif
13108#ifdef FEAT_CSCOPE
13109 "cscope",
13110#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013111#ifdef FEAT_CURSORBIND
13112 "cursorbind",
13113#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013114#ifdef CURSOR_SHAPE
13115 "cursorshape",
13116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117#ifdef DEBUG
13118 "debug",
13119#endif
13120#ifdef FEAT_CON_DIALOG
13121 "dialog_con",
13122#endif
13123#ifdef FEAT_GUI_DIALOG
13124 "dialog_gui",
13125#endif
13126#ifdef FEAT_DIFF
13127 "diff",
13128#endif
13129#ifdef FEAT_DIGRAPHS
13130 "digraphs",
13131#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013132#ifdef FEAT_DIRECTX
13133 "directx",
13134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135#ifdef FEAT_DND
13136 "dnd",
13137#endif
13138#ifdef FEAT_EMACS_TAGS
13139 "emacs_tags",
13140#endif
13141 "eval", /* always present, of course! */
13142#ifdef FEAT_EX_EXTRA
13143 "ex_extra",
13144#endif
13145#ifdef FEAT_SEARCH_EXTRA
13146 "extra_search",
13147#endif
13148#ifdef FEAT_FKMAP
13149 "farsi",
13150#endif
13151#ifdef FEAT_SEARCHPATH
13152 "file_in_path",
13153#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013154#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013155 "filterpipe",
13156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157#ifdef FEAT_FIND_ID
13158 "find_in_path",
13159#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013160#ifdef FEAT_FLOAT
13161 "float",
13162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163#ifdef FEAT_FOLDING
13164 "folding",
13165#endif
13166#ifdef FEAT_FOOTER
13167 "footer",
13168#endif
13169#if !defined(USE_SYSTEM) && defined(UNIX)
13170 "fork",
13171#endif
13172#ifdef FEAT_GETTEXT
13173 "gettext",
13174#endif
13175#ifdef FEAT_GUI
13176 "gui",
13177#endif
13178#ifdef FEAT_GUI_ATHENA
13179# ifdef FEAT_GUI_NEXTAW
13180 "gui_neXtaw",
13181# else
13182 "gui_athena",
13183# endif
13184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185#ifdef FEAT_GUI_GTK
13186 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013189#ifdef FEAT_GUI_GNOME
13190 "gui_gnome",
13191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192#ifdef FEAT_GUI_MAC
13193 "gui_mac",
13194#endif
13195#ifdef FEAT_GUI_MOTIF
13196 "gui_motif",
13197#endif
13198#ifdef FEAT_GUI_PHOTON
13199 "gui_photon",
13200#endif
13201#ifdef FEAT_GUI_W16
13202 "gui_win16",
13203#endif
13204#ifdef FEAT_GUI_W32
13205 "gui_win32",
13206#endif
13207#ifdef FEAT_HANGULIN
13208 "hangul_input",
13209#endif
13210#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13211 "iconv",
13212#endif
13213#ifdef FEAT_INS_EXPAND
13214 "insert_expand",
13215#endif
13216#ifdef FEAT_JUMPLIST
13217 "jumplist",
13218#endif
13219#ifdef FEAT_KEYMAP
13220 "keymap",
13221#endif
13222#ifdef FEAT_LANGMAP
13223 "langmap",
13224#endif
13225#ifdef FEAT_LIBCALL
13226 "libcall",
13227#endif
13228#ifdef FEAT_LINEBREAK
13229 "linebreak",
13230#endif
13231#ifdef FEAT_LISP
13232 "lispindent",
13233#endif
13234#ifdef FEAT_LISTCMDS
13235 "listcmds",
13236#endif
13237#ifdef FEAT_LOCALMAP
13238 "localmap",
13239#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013240#ifdef FEAT_LUA
13241# ifndef DYNAMIC_LUA
13242 "lua",
13243# endif
13244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245#ifdef FEAT_MENU
13246 "menu",
13247#endif
13248#ifdef FEAT_SESSION
13249 "mksession",
13250#endif
13251#ifdef FEAT_MODIFY_FNAME
13252 "modify_fname",
13253#endif
13254#ifdef FEAT_MOUSE
13255 "mouse",
13256#endif
13257#ifdef FEAT_MOUSESHAPE
13258 "mouseshape",
13259#endif
13260#if defined(UNIX) || defined(VMS)
13261# ifdef FEAT_MOUSE_DEC
13262 "mouse_dec",
13263# endif
13264# ifdef FEAT_MOUSE_GPM
13265 "mouse_gpm",
13266# endif
13267# ifdef FEAT_MOUSE_JSB
13268 "mouse_jsbterm",
13269# endif
13270# ifdef FEAT_MOUSE_NET
13271 "mouse_netterm",
13272# endif
13273# ifdef FEAT_MOUSE_PTERM
13274 "mouse_pterm",
13275# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013276# ifdef FEAT_MOUSE_SGR
13277 "mouse_sgr",
13278# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013279# ifdef FEAT_SYSMOUSE
13280 "mouse_sysmouse",
13281# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013282# ifdef FEAT_MOUSE_URXVT
13283 "mouse_urxvt",
13284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285# ifdef FEAT_MOUSE_XTERM
13286 "mouse_xterm",
13287# endif
13288#endif
13289#ifdef FEAT_MBYTE
13290 "multi_byte",
13291#endif
13292#ifdef FEAT_MBYTE_IME
13293 "multi_byte_ime",
13294#endif
13295#ifdef FEAT_MULTI_LANG
13296 "multi_lang",
13297#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013298#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013299#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013300 "mzscheme",
13301#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303#ifdef FEAT_OLE
13304 "ole",
13305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306#ifdef FEAT_PATH_EXTRA
13307 "path_extra",
13308#endif
13309#ifdef FEAT_PERL
13310#ifndef DYNAMIC_PERL
13311 "perl",
13312#endif
13313#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013314#ifdef FEAT_PERSISTENT_UNDO
13315 "persistent_undo",
13316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317#ifdef FEAT_PYTHON
13318#ifndef DYNAMIC_PYTHON
13319 "python",
13320#endif
13321#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013322#ifdef FEAT_PYTHON3
13323#ifndef DYNAMIC_PYTHON3
13324 "python3",
13325#endif
13326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327#ifdef FEAT_POSTSCRIPT
13328 "postscript",
13329#endif
13330#ifdef FEAT_PRINTER
13331 "printer",
13332#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013333#ifdef FEAT_PROFILE
13334 "profile",
13335#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013336#ifdef FEAT_RELTIME
13337 "reltime",
13338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339#ifdef FEAT_QUICKFIX
13340 "quickfix",
13341#endif
13342#ifdef FEAT_RIGHTLEFT
13343 "rightleft",
13344#endif
13345#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13346 "ruby",
13347#endif
13348#ifdef FEAT_SCROLLBIND
13349 "scrollbind",
13350#endif
13351#ifdef FEAT_CMDL_INFO
13352 "showcmd",
13353 "cmdline_info",
13354#endif
13355#ifdef FEAT_SIGNS
13356 "signs",
13357#endif
13358#ifdef FEAT_SMARTINDENT
13359 "smartindent",
13360#endif
13361#ifdef FEAT_SNIFF
13362 "sniff",
13363#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013364#ifdef STARTUPTIME
13365 "startuptime",
13366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367#ifdef FEAT_STL_OPT
13368 "statusline",
13369#endif
13370#ifdef FEAT_SUN_WORKSHOP
13371 "sun_workshop",
13372#endif
13373#ifdef FEAT_NETBEANS_INTG
13374 "netbeans_intg",
13375#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013376#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013377 "spell",
13378#endif
13379#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 "syntax",
13381#endif
13382#if defined(USE_SYSTEM) || !defined(UNIX)
13383 "system",
13384#endif
13385#ifdef FEAT_TAG_BINS
13386 "tag_binary",
13387#endif
13388#ifdef FEAT_TAG_OLDSTATIC
13389 "tag_old_static",
13390#endif
13391#ifdef FEAT_TAG_ANYWHITE
13392 "tag_any_white",
13393#endif
13394#ifdef FEAT_TCL
13395# ifndef DYNAMIC_TCL
13396 "tcl",
13397# endif
13398#endif
13399#ifdef TERMINFO
13400 "terminfo",
13401#endif
13402#ifdef FEAT_TERMRESPONSE
13403 "termresponse",
13404#endif
13405#ifdef FEAT_TEXTOBJ
13406 "textobjects",
13407#endif
13408#ifdef HAVE_TGETENT
13409 "tgetent",
13410#endif
13411#ifdef FEAT_TITLE
13412 "title",
13413#endif
13414#ifdef FEAT_TOOLBAR
13415 "toolbar",
13416#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013417#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13418 "unnamedplus",
13419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420#ifdef FEAT_USR_CMDS
13421 "user-commands", /* was accidentally included in 5.4 */
13422 "user_commands",
13423#endif
13424#ifdef FEAT_VIMINFO
13425 "viminfo",
13426#endif
13427#ifdef FEAT_VERTSPLIT
13428 "vertsplit",
13429#endif
13430#ifdef FEAT_VIRTUALEDIT
13431 "virtualedit",
13432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434#ifdef FEAT_VISUALEXTRA
13435 "visualextra",
13436#endif
13437#ifdef FEAT_VREPLACE
13438 "vreplace",
13439#endif
13440#ifdef FEAT_WILDIGN
13441 "wildignore",
13442#endif
13443#ifdef FEAT_WILDMENU
13444 "wildmenu",
13445#endif
13446#ifdef FEAT_WINDOWS
13447 "windows",
13448#endif
13449#ifdef FEAT_WAK
13450 "winaltkeys",
13451#endif
13452#ifdef FEAT_WRITEBACKUP
13453 "writebackup",
13454#endif
13455#ifdef FEAT_XIM
13456 "xim",
13457#endif
13458#ifdef FEAT_XFONTSET
13459 "xfontset",
13460#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013461#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013462 "xpm",
13463 "xpm_w32", /* for backward compatibility */
13464#else
13465# if defined(HAVE_XPM)
13466 "xpm",
13467# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469#ifdef USE_XSMP
13470 "xsmp",
13471#endif
13472#ifdef USE_XSMP_INTERACT
13473 "xsmp_interact",
13474#endif
13475#ifdef FEAT_XCLIPBOARD
13476 "xterm_clipboard",
13477#endif
13478#ifdef FEAT_XTERM_SAVE
13479 "xterm_save",
13480#endif
13481#if defined(UNIX) && defined(FEAT_X11)
13482 "X11",
13483#endif
13484 NULL
13485 };
13486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 for (i = 0; has_list[i] != NULL; ++i)
13489 if (STRICMP(name, has_list[i]) == 0)
13490 {
13491 n = TRUE;
13492 break;
13493 }
13494
13495 if (n == FALSE)
13496 {
13497 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013498 {
13499 if (name[5] == '-'
13500 && STRLEN(name) > 11
13501 && vim_isdigit(name[6])
13502 && vim_isdigit(name[8])
13503 && vim_isdigit(name[10]))
13504 {
13505 int major = atoi((char *)name + 6);
13506 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013507
13508 /* Expect "patch-9.9.01234". */
13509 n = (major < VIM_VERSION_MAJOR
13510 || (major == VIM_VERSION_MAJOR
13511 && (minor < VIM_VERSION_MINOR
13512 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013513 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013514 }
13515 else
13516 n = has_patch(atoi((char *)name + 5));
13517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 else if (STRICMP(name, "vim_starting") == 0)
13519 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013520#ifdef FEAT_MBYTE
13521 else if (STRICMP(name, "multi_byte_encoding") == 0)
13522 n = has_mbyte;
13523#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013524#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13525 else if (STRICMP(name, "balloon_multiline") == 0)
13526 n = multiline_balloon_available();
13527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528#ifdef DYNAMIC_TCL
13529 else if (STRICMP(name, "tcl") == 0)
13530 n = tcl_enabled(FALSE);
13531#endif
13532#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13533 else if (STRICMP(name, "iconv") == 0)
13534 n = iconv_enabled(FALSE);
13535#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013536#ifdef DYNAMIC_LUA
13537 else if (STRICMP(name, "lua") == 0)
13538 n = lua_enabled(FALSE);
13539#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013540#ifdef DYNAMIC_MZSCHEME
13541 else if (STRICMP(name, "mzscheme") == 0)
13542 n = mzscheme_enabled(FALSE);
13543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544#ifdef DYNAMIC_RUBY
13545 else if (STRICMP(name, "ruby") == 0)
13546 n = ruby_enabled(FALSE);
13547#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013548#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549#ifdef DYNAMIC_PYTHON
13550 else if (STRICMP(name, "python") == 0)
13551 n = python_enabled(FALSE);
13552#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013553#endif
13554#ifdef FEAT_PYTHON3
13555#ifdef DYNAMIC_PYTHON3
13556 else if (STRICMP(name, "python3") == 0)
13557 n = python3_enabled(FALSE);
13558#endif
13559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560#ifdef DYNAMIC_PERL
13561 else if (STRICMP(name, "perl") == 0)
13562 n = perl_enabled(FALSE);
13563#endif
13564#ifdef FEAT_GUI
13565 else if (STRICMP(name, "gui_running") == 0)
13566 n = (gui.in_use || gui.starting);
13567# ifdef FEAT_GUI_W32
13568 else if (STRICMP(name, "gui_win32s") == 0)
13569 n = gui_is_win32s();
13570# endif
13571# ifdef FEAT_BROWSE
13572 else if (STRICMP(name, "browse") == 0)
13573 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13574# endif
13575#endif
13576#ifdef FEAT_SYN_HL
13577 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013578 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579#endif
13580#if defined(WIN3264)
13581 else if (STRICMP(name, "win95") == 0)
13582 n = mch_windows95();
13583#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013584#ifdef FEAT_NETBEANS_INTG
13585 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013586 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 }
13589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591}
13592
13593/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013594 * "has_key()" function
13595 */
13596 static void
13597f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013598 typval_T *argvars;
13599 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013600{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013601 if (argvars[0].v_type != VAR_DICT)
13602 {
13603 EMSG(_(e_dictreq));
13604 return;
13605 }
13606 if (argvars[0].vval.v_dict == NULL)
13607 return;
13608
13609 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013610 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013611}
13612
13613/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013614 * "haslocaldir()" function
13615 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013616 static void
13617f_haslocaldir(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013618 typval_T *argvars;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013619 typval_T *rettv;
13620{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013621 win_T *wp = NULL;
13622
13623 wp = find_tabwin(&argvars[0], &argvars[1]);
13624 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013625}
13626
13627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 * "hasmapto()" function
13629 */
13630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634{
13635 char_u *name;
13636 char_u *mode;
13637 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013638 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013641 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 mode = (char_u *)"nvo";
13643 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013646 if (argvars[2].v_type != VAR_UNKNOWN)
13647 abbr = get_tv_number(&argvars[2]);
13648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
Bram Moolenaar2c932302006-03-18 21:42:09 +000013650 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654}
13655
13656/*
13657 * "histadd()" function
13658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013661 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663{
13664#ifdef FEAT_CMDHIST
13665 int histype;
13666 char_u *str;
13667 char_u buf[NUMBUFLEN];
13668#endif
13669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 if (check_restricted() || check_secure())
13672 return;
13673#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13675 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 if (histype >= 0)
13677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 if (*str != NUL)
13680 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013681 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013683 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 return;
13685 }
13686 }
13687#endif
13688}
13689
13690/*
13691 * "histdel()" function
13692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013695 typval_T *argvars UNUSED;
13696 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697{
13698#ifdef FEAT_CMDHIST
13699 int n;
13700 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13704 if (str == NULL)
13705 n = 0;
13706 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013709 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013711 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 else
13714 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 get_tv_string_buf(&argvars[1], buf));
13717 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#endif
13719}
13720
13721/*
13722 * "histget()" function
13723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013726 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728{
13729#ifdef FEAT_CMDHIST
13730 int type;
13731 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013732 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13735 if (str == NULL)
13736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 {
13739 type = get_histtype(str);
13740 if (argvars[1].v_type == VAR_UNKNOWN)
13741 idx = get_history_idx(type);
13742 else
13743 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13744 /* -1 on type error */
13745 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751}
13752
13753/*
13754 * "histnr()" function
13755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760{
13761 int i;
13762
13763#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 char_u *history = get_tv_string_chk(&argvars[0]);
13765
13766 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 if (i >= HIST_CMD && i < HIST_COUNT)
13768 i = get_history_idx(i);
13769 else
13770#endif
13771 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013772 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773}
13774
13775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 * "highlightID(name)" function
13777 */
13778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013780 typval_T *argvars;
13781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784}
13785
13786/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787 * "highlight_exists()" function
13788 */
13789 static void
13790f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013791 typval_T *argvars;
13792 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793{
13794 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13795}
13796
13797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 * "hostname()" function
13799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804{
13805 char_u hostname[256];
13806
13807 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808 rettv->v_type = VAR_STRING;
13809 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810}
13811
13812/*
13813 * iconv() function
13814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013817 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819{
13820#ifdef FEAT_MBYTE
13821 char_u buf1[NUMBUFLEN];
13822 char_u buf2[NUMBUFLEN];
13823 char_u *from, *to, *str;
13824 vimconv_T vimconv;
13825#endif
13826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013827 rettv->v_type = VAR_STRING;
13828 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829
13830#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 str = get_tv_string(&argvars[0]);
13832 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13833 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 vimconv.vc_type = CONV_NONE;
13835 convert_setup(&vimconv, from, to);
13836
13837 /* If the encodings are equal, no conversion needed. */
13838 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013839 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842
13843 convert_setup(&vimconv, NULL, NULL);
13844 vim_free(from);
13845 vim_free(to);
13846#endif
13847}
13848
13849/*
13850 * "indent()" function
13851 */
13852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013853f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013854 typval_T *argvars;
13855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856{
13857 linenr_T lnum;
13858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013859 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013861 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013863 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864}
13865
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013866/*
13867 * "index()" function
13868 */
13869 static void
13870f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *argvars;
13872 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013873{
Bram Moolenaar33570922005-01-25 22:26:29 +000013874 list_T *l;
13875 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013876 long idx = 0;
13877 int ic = FALSE;
13878
13879 rettv->vval.v_number = -1;
13880 if (argvars[0].v_type != VAR_LIST)
13881 {
13882 EMSG(_(e_listreq));
13883 return;
13884 }
13885 l = argvars[0].vval.v_list;
13886 if (l != NULL)
13887 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013888 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013889 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013891 int error = FALSE;
13892
Bram Moolenaar758711c2005-02-02 23:11:38 +000013893 /* Start at specified item. Use the cached index that list_find()
13894 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013896 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013897 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 ic = get_tv_number_chk(&argvars[3], &error);
13899 if (error)
13900 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013902
Bram Moolenaar758711c2005-02-02 23:11:38 +000013903 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013904 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013905 {
13906 rettv->vval.v_number = idx;
13907 break;
13908 }
13909 }
13910}
13911
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912static int inputsecret_flag = 0;
13913
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013914static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13915
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013917 * This function is used by f_input() and f_inputdialog() functions. The third
13918 * argument to f_input() specifies the type of completion to use at the
13919 * prompt. The third argument to f_inputdialog() specifies the value to return
13920 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 */
13922 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013923get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013924 typval_T *argvars;
13925 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013926 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013928 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 char_u *p = NULL;
13930 int c;
13931 char_u buf[NUMBUFLEN];
13932 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013934 int xp_type = EXPAND_NOTHING;
13935 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939
13940#ifdef NO_CONSOLE_INPUT
13941 /* While starting up, there is no place to enter text. */
13942 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944#endif
13945
13946 cmd_silent = FALSE; /* Want to see the prompt. */
13947 if (prompt != NULL)
13948 {
13949 /* Only the part of the message after the last NL is considered as
13950 * prompt for the command line */
13951 p = vim_strrchr(prompt, '\n');
13952 if (p == NULL)
13953 p = prompt;
13954 else
13955 {
13956 ++p;
13957 c = *p;
13958 *p = NUL;
13959 msg_start();
13960 msg_clr_eos();
13961 msg_puts_attr(prompt, echo_attr);
13962 msg_didout = FALSE;
13963 msg_starthere();
13964 *p = c;
13965 }
13966 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 if (argvars[1].v_type != VAR_UNKNOWN)
13969 {
13970 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13971 if (defstr != NULL)
13972 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013974 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013975 {
13976 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013977 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013978 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013979
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013980 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013981 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013982
Bram Moolenaar4463f292005-09-25 22:20:24 +000013983 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13984 if (xp_name == NULL)
13985 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013986
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013987 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013988
Bram Moolenaar4463f292005-09-25 22:20:24 +000013989 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13990 &xp_arg) == FAIL)
13991 return;
13992 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013993 }
13994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013996 {
13997# ifdef FEAT_EX_EXTRA
13998 int save_ex_normal_busy = ex_normal_busy;
13999 ex_normal_busy = 0;
14000# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014002 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14003 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014004# ifdef FEAT_EX_EXTRA
14005 ex_normal_busy = save_ex_normal_busy;
14006# endif
14007 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014008 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014009 && argvars[1].v_type != VAR_UNKNOWN
14010 && argvars[2].v_type != VAR_UNKNOWN)
14011 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14012 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014013
14014 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014016 /* since the user typed this, no need to wait for return */
14017 need_wait_return = FALSE;
14018 msg_didout = FALSE;
14019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 cmd_silent = cmd_silent_save;
14021}
14022
14023/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014024 * "input()" function
14025 * Also handles inputsecret() when inputsecret is set.
14026 */
14027 static void
14028f_input(argvars, rettv)
14029 typval_T *argvars;
14030 typval_T *rettv;
14031{
14032 get_user_input(argvars, rettv, FALSE);
14033}
14034
14035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 * "inputdialog()" function
14037 */
14038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014040 typval_T *argvars;
14041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042{
14043#if defined(FEAT_GUI_TEXTDIALOG)
14044 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14045 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14046 {
14047 char_u *message;
14048 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 message = get_tv_string_chk(&argvars[0]);
14052 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014053 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014054 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 else
14056 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057 if (message != NULL && defstr != NULL
14058 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014059 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014060 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 else
14062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063 if (message != NULL && defstr != NULL
14064 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014065 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014066 rettv->vval.v_string = vim_strsave(
14067 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 }
14073 else
14074#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014075 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076}
14077
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014078/*
14079 * "inputlist()" function
14080 */
14081 static void
14082f_inputlist(argvars, rettv)
14083 typval_T *argvars;
14084 typval_T *rettv;
14085{
14086 listitem_T *li;
14087 int selected;
14088 int mouse_used;
14089
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014090#ifdef NO_CONSOLE_INPUT
14091 /* While starting up, there is no place to enter text. */
14092 if (no_console_input())
14093 return;
14094#endif
14095 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14096 {
14097 EMSG2(_(e_listarg), "inputlist()");
14098 return;
14099 }
14100
14101 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014102 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014103 lines_left = Rows; /* avoid more prompt */
14104 msg_scroll = TRUE;
14105 msg_clr_eos();
14106
14107 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14108 {
14109 msg_puts(get_tv_string(&li->li_tv));
14110 msg_putchar('\n');
14111 }
14112
14113 /* Ask for choice. */
14114 selected = prompt_for_number(&mouse_used);
14115 if (mouse_used)
14116 selected -= lines_left;
14117
14118 rettv->vval.v_number = selected;
14119}
14120
14121
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14123
14124/*
14125 * "inputrestore()" function
14126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014128f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014129 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131{
14132 if (ga_userinput.ga_len > 0)
14133 {
14134 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14136 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014137 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 }
14139 else if (p_verbose > 1)
14140 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014141 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 }
14144}
14145
14146/*
14147 * "inputsave()" function
14148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014151 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014154 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 if (ga_grow(&ga_userinput, 1) == OK)
14156 {
14157 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14158 + ga_userinput.ga_len);
14159 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014160 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 }
14162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164}
14165
14166/*
14167 * "inputsecret()" function
14168 */
14169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014170f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014171 typval_T *argvars;
14172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173{
14174 ++cmdline_star;
14175 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 --cmdline_star;
14178 --inputsecret_flag;
14179}
14180
14181/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014182 * "insert()" function
14183 */
14184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014186 typval_T *argvars;
14187 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014188{
14189 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014190 listitem_T *item;
14191 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014193
14194 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014196 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014197 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014198 {
14199 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 before = get_tv_number_chk(&argvars[2], &error);
14201 if (error)
14202 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014203
Bram Moolenaar758711c2005-02-02 23:11:38 +000014204 if (before == l->lv_len)
14205 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014206 else
14207 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014208 item = list_find(l, before);
14209 if (item == NULL)
14210 {
14211 EMSGN(_(e_listidx), before);
14212 l = NULL;
14213 }
14214 }
14215 if (l != NULL)
14216 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014217 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014218 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014219 }
14220 }
14221}
14222
14223/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014224 * "invert(expr)" function
14225 */
14226 static void
14227f_invert(argvars, rettv)
14228 typval_T *argvars;
14229 typval_T *rettv;
14230{
14231 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14232}
14233
14234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 * "isdirectory()" function
14236 */
14237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 typval_T *argvars;
14240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243}
14244
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014245/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014246 * "islocked()" function
14247 */
14248 static void
14249f_islocked(argvars, rettv)
14250 typval_T *argvars;
14251 typval_T *rettv;
14252{
14253 lval_T lv;
14254 char_u *end;
14255 dictitem_T *di;
14256
14257 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014258 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14259 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014260 if (end != NULL && lv.ll_name != NULL)
14261 {
14262 if (*end != NUL)
14263 EMSG(_(e_trailing));
14264 else
14265 {
14266 if (lv.ll_tv == NULL)
14267 {
14268 if (check_changedtick(lv.ll_name))
14269 rettv->vval.v_number = 1; /* always locked */
14270 else
14271 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014272 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014273 if (di != NULL)
14274 {
14275 /* Consider a variable locked when:
14276 * 1. the variable itself is locked
14277 * 2. the value of the variable is locked.
14278 * 3. the List or Dict value is locked.
14279 */
14280 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14281 || tv_islocked(&di->di_tv));
14282 }
14283 }
14284 }
14285 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014286 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014287 else if (lv.ll_newkey != NULL)
14288 EMSG2(_(e_dictkey), lv.ll_newkey);
14289 else if (lv.ll_list != NULL)
14290 /* List item. */
14291 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14292 else
14293 /* Dictionary item. */
14294 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14295 }
14296 }
14297
14298 clear_lval(&lv);
14299}
14300
Bram Moolenaar33570922005-01-25 22:26:29 +000014301static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014302
14303/*
14304 * Turn a dict into a list:
14305 * "what" == 0: list of keys
14306 * "what" == 1: list of values
14307 * "what" == 2: list of items
14308 */
14309 static void
14310dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014311 typval_T *argvars;
14312 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014313 int what;
14314{
Bram Moolenaar33570922005-01-25 22:26:29 +000014315 list_T *l2;
14316 dictitem_T *di;
14317 hashitem_T *hi;
14318 listitem_T *li;
14319 listitem_T *li2;
14320 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014321 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014322
Bram Moolenaar8c711452005-01-14 21:53:12 +000014323 if (argvars[0].v_type != VAR_DICT)
14324 {
14325 EMSG(_(e_dictreq));
14326 return;
14327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014328 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014329 return;
14330
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014331 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014332 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014334 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014337 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014338 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014339 --todo;
14340 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014341
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014342 li = listitem_alloc();
14343 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014344 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014345 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014347 if (what == 0)
14348 {
14349 /* keys() */
14350 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014351 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014352 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14353 }
14354 else if (what == 1)
14355 {
14356 /* values() */
14357 copy_tv(&di->di_tv, &li->li_tv);
14358 }
14359 else
14360 {
14361 /* items() */
14362 l2 = list_alloc();
14363 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014364 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014365 li->li_tv.vval.v_list = l2;
14366 if (l2 == NULL)
14367 break;
14368 ++l2->lv_refcount;
14369
14370 li2 = listitem_alloc();
14371 if (li2 == NULL)
14372 break;
14373 list_append(l2, li2);
14374 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014375 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014376 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14377
14378 li2 = listitem_alloc();
14379 if (li2 == NULL)
14380 break;
14381 list_append(l2, li2);
14382 copy_tv(&di->di_tv, &li2->li_tv);
14383 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014384 }
14385 }
14386}
14387
14388/*
14389 * "items(dict)" function
14390 */
14391 static void
14392f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014395{
14396 dict_list(argvars, rettv, 2);
14397}
14398
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014400 * "join()" function
14401 */
14402 static void
14403f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014404 typval_T *argvars;
14405 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014406{
14407 garray_T ga;
14408 char_u *sep;
14409
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014410 if (argvars[0].v_type != VAR_LIST)
14411 {
14412 EMSG(_(e_listreq));
14413 return;
14414 }
14415 if (argvars[0].vval.v_list == NULL)
14416 return;
14417 if (argvars[1].v_type == VAR_UNKNOWN)
14418 sep = (char_u *)" ";
14419 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014421
14422 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014423
14424 if (sep != NULL)
14425 {
14426 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014427 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014428 ga_append(&ga, NUL);
14429 rettv->vval.v_string = (char_u *)ga.ga_data;
14430 }
14431 else
14432 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014433}
14434
14435/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014436 * "jsondecode()" function
14437 */
14438 static void
14439f_jsondecode(argvars, rettv)
14440 typval_T *argvars;
14441 typval_T *rettv;
14442{
14443 js_read_T reader;
14444
14445 reader.js_buf = get_tv_string(&argvars[0]);
14446 reader.js_eof = TRUE;
14447 reader.js_used = 0;
14448 json_decode(&reader, rettv);
14449}
14450
14451/*
14452 * "jsonencode()" function
14453 */
14454 static void
14455f_jsonencode(argvars, rettv)
14456 typval_T *argvars;
14457 typval_T *rettv;
14458{
14459 rettv->v_type = VAR_STRING;
14460 rettv->vval.v_string = json_encode(&argvars[0]);
14461}
14462
14463/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014464 * "keys()" function
14465 */
14466 static void
14467f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014468 typval_T *argvars;
14469 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014470{
14471 dict_list(argvars, rettv, 0);
14472}
14473
14474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 * "last_buffer_nr()" function.
14476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014479 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481{
14482 int n = 0;
14483 buf_T *buf;
14484
14485 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14486 if (n < buf->b_fnum)
14487 n = buf->b_fnum;
14488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014490}
14491
14492/*
14493 * "len()" function
14494 */
14495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014497 typval_T *argvars;
14498 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014499{
14500 switch (argvars[0].v_type)
14501 {
14502 case VAR_STRING:
14503 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 rettv->vval.v_number = (varnumber_T)STRLEN(
14505 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014506 break;
14507 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014509 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014510 case VAR_DICT:
14511 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14512 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014513 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014514 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014515 break;
14516 }
14517}
14518
Bram Moolenaar33570922005-01-25 22:26:29 +000014519static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014520
14521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 typval_T *argvars;
14524 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014525 int type;
14526{
14527#ifdef FEAT_LIBCALL
14528 char_u *string_in;
14529 char_u **string_result;
14530 int nr_result;
14531#endif
14532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014534 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014536
14537 if (check_restricted() || check_secure())
14538 return;
14539
14540#ifdef FEAT_LIBCALL
14541 /* The first two args must be strings, otherwise its meaningless */
14542 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14543 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014544 string_in = NULL;
14545 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014546 string_in = argvars[2].vval.v_string;
14547 if (type == VAR_NUMBER)
14548 string_result = NULL;
14549 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014551 if (mch_libcall(argvars[0].vval.v_string,
14552 argvars[1].vval.v_string,
14553 string_in,
14554 argvars[2].vval.v_number,
14555 string_result,
14556 &nr_result) == OK
14557 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014559 }
14560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561}
14562
14563/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564 * "libcall()" function
14565 */
14566 static void
14567f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014568 typval_T *argvars;
14569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570{
14571 libcall_common(argvars, rettv, VAR_STRING);
14572}
14573
14574/*
14575 * "libcallnr()" function
14576 */
14577 static void
14578f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 typval_T *argvars;
14580 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581{
14582 libcall_common(argvars, rettv, VAR_NUMBER);
14583}
14584
14585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 * "line(string)" function
14587 */
14588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014589f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014590 typval_T *argvars;
14591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592{
14593 linenr_T lnum = 0;
14594 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014595 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014597 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 if (fp != NULL)
14599 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014600 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601}
14602
14603/*
14604 * "line2byte(lnum)" function
14605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014608 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610{
14611#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613#else
14614 linenr_T lnum;
14615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14621 if (rettv->vval.v_number >= 0)
14622 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623#endif
14624}
14625
14626/*
14627 * "lispindent(lnum)" function
14628 */
14629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633{
14634#ifdef FEAT_LISP
14635 pos_T pos;
14636 linenr_T lnum;
14637
14638 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14641 {
14642 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 curwin->w_cursor = pos;
14645 }
14646 else
14647#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014648 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649}
14650
14651/*
14652 * "localtime()" function
14653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014656 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014659 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660}
14661
Bram Moolenaar33570922005-01-25 22:26:29 +000014662static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663
14664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 typval_T *argvars;
14667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 int exact;
14669{
14670 char_u *keys;
14671 char_u *which;
14672 char_u buf[NUMBUFLEN];
14673 char_u *keys_buf = NULL;
14674 char_u *rhs;
14675 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014676 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014677 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014678 mapblock_T *mp;
14679 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680
14681 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->v_type = VAR_STRING;
14683 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 if (*keys == NUL)
14687 return;
14688
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014689 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014691 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014692 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014693 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014694 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014695 if (argvars[3].v_type != VAR_UNKNOWN)
14696 get_dict = get_tv_number(&argvars[3]);
14697 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 else
14700 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014701 if (which == NULL)
14702 return;
14703
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 mode = get_map_mode(&which, 0);
14705
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014706 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014707 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014709
14710 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014712 /* Return a string. */
14713 if (rhs != NULL)
14714 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715
Bram Moolenaarbd743252010-10-20 21:23:33 +020014716 }
14717 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14718 {
14719 /* Return a dictionary. */
14720 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14721 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14722 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723
Bram Moolenaarbd743252010-10-20 21:23:33 +020014724 dict_add_nr_str(dict, "lhs", 0L, lhs);
14725 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14726 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14727 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14728 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14729 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14730 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014731 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014732 dict_add_nr_str(dict, "mode", 0L, mapmode);
14733
14734 vim_free(lhs);
14735 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 }
14737}
14738
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014739#ifdef FEAT_FLOAT
14740/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014741 * "log()" function
14742 */
14743 static void
14744f_log(argvars, rettv)
14745 typval_T *argvars;
14746 typval_T *rettv;
14747{
14748 float_T f;
14749
14750 rettv->v_type = VAR_FLOAT;
14751 if (get_float_arg(argvars, &f) == OK)
14752 rettv->vval.v_float = log(f);
14753 else
14754 rettv->vval.v_float = 0.0;
14755}
14756
14757/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014758 * "log10()" function
14759 */
14760 static void
14761f_log10(argvars, rettv)
14762 typval_T *argvars;
14763 typval_T *rettv;
14764{
14765 float_T f;
14766
14767 rettv->v_type = VAR_FLOAT;
14768 if (get_float_arg(argvars, &f) == OK)
14769 rettv->vval.v_float = log10(f);
14770 else
14771 rettv->vval.v_float = 0.0;
14772}
14773#endif
14774
Bram Moolenaar1dced572012-04-05 16:54:08 +020014775#ifdef FEAT_LUA
14776/*
14777 * "luaeval()" function
14778 */
14779 static void
14780f_luaeval(argvars, rettv)
14781 typval_T *argvars;
14782 typval_T *rettv;
14783{
14784 char_u *str;
14785 char_u buf[NUMBUFLEN];
14786
14787 str = get_tv_string_buf(&argvars[0], buf);
14788 do_luaeval(str, argvars + 1, rettv);
14789}
14790#endif
14791
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014793 * "map()" function
14794 */
14795 static void
14796f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014797 typval_T *argvars;
14798 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014799{
14800 filter_map(argvars, rettv, TRUE);
14801}
14802
14803/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014804 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 */
14806 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014807f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014808 typval_T *argvars;
14809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014811 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812}
14813
14814/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014815 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 */
14817 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014818f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014819 typval_T *argvars;
14820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014822 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823}
14824
Bram Moolenaar33570922005-01-25 22:26:29 +000014825static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826
14827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014828find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014829 typval_T *argvars;
14830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 int type;
14832{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014833 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014834 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014835 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 char_u *pat;
14837 regmatch_T regmatch;
14838 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014839 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 char_u *save_cpo;
14841 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014842 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014843 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014844 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014845 list_T *l = NULL;
14846 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014847 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014848 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849
14850 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14851 save_cpo = p_cpo;
14852 p_cpo = (char_u *)"";
14853
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014854 rettv->vval.v_number = -1;
14855 if (type == 3)
14856 {
14857 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014858 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014859 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014860 }
14861 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 rettv->v_type = VAR_STRING;
14864 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014867 if (argvars[0].v_type == VAR_LIST)
14868 {
14869 if ((l = argvars[0].vval.v_list) == NULL)
14870 goto theend;
14871 li = l->lv_first;
14872 }
14873 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014874 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014875 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014876 len = (long)STRLEN(str);
14877 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014879 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14880 if (pat == NULL)
14881 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014882
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014883 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 int error = FALSE;
14886
14887 start = get_tv_number_chk(&argvars[2], &error);
14888 if (error)
14889 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014890 if (l != NULL)
14891 {
14892 li = list_find(l, start);
14893 if (li == NULL)
14894 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014895 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014896 }
14897 else
14898 {
14899 if (start < 0)
14900 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014901 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014902 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014903 /* When "count" argument is there ignore matches before "start",
14904 * otherwise skip part of the string. Differs when pattern is "^"
14905 * or "\<". */
14906 if (argvars[3].v_type != VAR_UNKNOWN)
14907 startcol = start;
14908 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014909 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014910 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014911 len -= start;
14912 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014913 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014914
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014915 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014916 nth = get_tv_number_chk(&argvars[3], &error);
14917 if (error)
14918 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 }
14920
14921 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14922 if (regmatch.regprog != NULL)
14923 {
14924 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014925
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014926 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014927 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014928 if (l != NULL)
14929 {
14930 if (li == NULL)
14931 {
14932 match = FALSE;
14933 break;
14934 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014935 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014936 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014937 if (str == NULL)
14938 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014939 }
14940
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014941 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014942
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014943 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014944 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014945 if (l == NULL && !match)
14946 break;
14947
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014948 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014949 if (l != NULL)
14950 {
14951 li = li->li_next;
14952 ++idx;
14953 }
14954 else
14955 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014956#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014957 startcol = (colnr_T)(regmatch.startp[0]
14958 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014959#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014960 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014961#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014962 if (startcol > (colnr_T)len
14963 || str + startcol <= regmatch.startp[0])
14964 {
14965 match = FALSE;
14966 break;
14967 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014968 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014969 }
14970
14971 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014973 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975 int i;
14976
14977 /* return list with matched string and submatches */
14978 for (i = 0; i < NSUBEXP; ++i)
14979 {
14980 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014981 {
14982 if (list_append_string(rettv->vval.v_list,
14983 (char_u *)"", 0) == FAIL)
14984 break;
14985 }
14986 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014987 regmatch.startp[i],
14988 (int)(regmatch.endp[i] - regmatch.startp[i]))
14989 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014990 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014991 }
14992 }
14993 else if (type == 2)
14994 {
14995 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014996 if (l != NULL)
14997 copy_tv(&li->li_tv, rettv);
14998 else
14999 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015001 }
15002 else if (l != NULL)
15003 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 else
15005 {
15006 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015007 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 (varnumber_T)(regmatch.startp[0] - str);
15009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015012 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 }
15014 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015015 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 }
15017
15018theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015019 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 p_cpo = save_cpo;
15021}
15022
15023/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 * "match()" function
15025 */
15026 static void
15027f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015028 typval_T *argvars;
15029 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015030{
15031 find_some_match(argvars, rettv, 1);
15032}
15033
15034/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015035 * "matchadd()" function
15036 */
15037 static void
15038f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015039 typval_T *argvars UNUSED;
15040 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015041{
15042#ifdef FEAT_SEARCH_EXTRA
15043 char_u buf[NUMBUFLEN];
15044 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15045 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15046 int prio = 10; /* default priority */
15047 int id = -1;
15048 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015049 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015050
15051 rettv->vval.v_number = -1;
15052
15053 if (grp == NULL || pat == NULL)
15054 return;
15055 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015056 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015057 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015058 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015059 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015060 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015061 if (argvars[4].v_type != VAR_UNKNOWN)
15062 {
15063 if (argvars[4].v_type != VAR_DICT)
15064 {
15065 EMSG(_(e_dictreq));
15066 return;
15067 }
15068 if (dict_find(argvars[4].vval.v_dict,
15069 (char_u *)"conceal", -1) != NULL)
15070 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15071 (char_u *)"conceal", FALSE);
15072 }
15073 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015074 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015075 if (error == TRUE)
15076 return;
15077 if (id >= 1 && id <= 3)
15078 {
15079 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15080 return;
15081 }
15082
Bram Moolenaar6561d522015-07-21 15:48:27 +020015083 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15084 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015085#endif
15086}
15087
15088/*
15089 * "matchaddpos()" function
15090 */
15091 static void
15092f_matchaddpos(argvars, rettv)
15093 typval_T *argvars UNUSED;
15094 typval_T *rettv UNUSED;
15095{
15096#ifdef FEAT_SEARCH_EXTRA
15097 char_u buf[NUMBUFLEN];
15098 char_u *group;
15099 int prio = 10;
15100 int id = -1;
15101 int error = FALSE;
15102 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015103 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015104
15105 rettv->vval.v_number = -1;
15106
15107 group = get_tv_string_buf_chk(&argvars[0], buf);
15108 if (group == NULL)
15109 return;
15110
15111 if (argvars[1].v_type != VAR_LIST)
15112 {
15113 EMSG2(_(e_listarg), "matchaddpos()");
15114 return;
15115 }
15116 l = argvars[1].vval.v_list;
15117 if (l == NULL)
15118 return;
15119
15120 if (argvars[2].v_type != VAR_UNKNOWN)
15121 {
15122 prio = get_tv_number_chk(&argvars[2], &error);
15123 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015124 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015125 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015126 if (argvars[4].v_type != VAR_UNKNOWN)
15127 {
15128 if (argvars[4].v_type != VAR_DICT)
15129 {
15130 EMSG(_(e_dictreq));
15131 return;
15132 }
15133 if (dict_find(argvars[4].vval.v_dict,
15134 (char_u *)"conceal", -1) != NULL)
15135 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15136 (char_u *)"conceal", FALSE);
15137 }
15138 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015139 }
15140 if (error == TRUE)
15141 return;
15142
15143 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15144 if (id == 1 || id == 2)
15145 {
15146 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15147 return;
15148 }
15149
Bram Moolenaar6561d522015-07-21 15:48:27 +020015150 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15151 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015152#endif
15153}
15154
15155/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015156 * "matcharg()" function
15157 */
15158 static void
15159f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015160 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015161 typval_T *rettv;
15162{
15163 if (rettv_list_alloc(rettv) == OK)
15164 {
15165#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015166 int id = get_tv_number(&argvars[0]);
15167 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015168
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015169 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015170 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015171 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15172 {
15173 list_append_string(rettv->vval.v_list,
15174 syn_id2name(m->hlg_id), -1);
15175 list_append_string(rettv->vval.v_list, m->pattern, -1);
15176 }
15177 else
15178 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015179 list_append_string(rettv->vval.v_list, NULL, -1);
15180 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015181 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015182 }
15183#endif
15184 }
15185}
15186
15187/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015188 * "matchdelete()" function
15189 */
15190 static void
15191f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015192 typval_T *argvars UNUSED;
15193 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015194{
15195#ifdef FEAT_SEARCH_EXTRA
15196 rettv->vval.v_number = match_delete(curwin,
15197 (int)get_tv_number(&argvars[0]), TRUE);
15198#endif
15199}
15200
15201/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202 * "matchend()" function
15203 */
15204 static void
15205f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015206 typval_T *argvars;
15207 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208{
15209 find_some_match(argvars, rettv, 0);
15210}
15211
15212/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015213 * "matchlist()" function
15214 */
15215 static void
15216f_matchlist(argvars, rettv)
15217 typval_T *argvars;
15218 typval_T *rettv;
15219{
15220 find_some_match(argvars, rettv, 3);
15221}
15222
15223/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224 * "matchstr()" function
15225 */
15226 static void
15227f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015228 typval_T *argvars;
15229 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230{
15231 find_some_match(argvars, rettv, 2);
15232}
15233
Bram Moolenaar33570922005-01-25 22:26:29 +000015234static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015235
15236 static void
15237max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015238 typval_T *argvars;
15239 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015240 int domax;
15241{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015242 long n = 0;
15243 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015245
15246 if (argvars[0].v_type == VAR_LIST)
15247 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 list_T *l;
15249 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015250
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015251 l = argvars[0].vval.v_list;
15252 if (l != NULL)
15253 {
15254 li = l->lv_first;
15255 if (li != NULL)
15256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015257 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015258 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015259 {
15260 li = li->li_next;
15261 if (li == NULL)
15262 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015263 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015264 if (domax ? i > n : i < n)
15265 n = i;
15266 }
15267 }
15268 }
15269 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015270 else if (argvars[0].v_type == VAR_DICT)
15271 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015272 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015273 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015275 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015276
15277 d = argvars[0].vval.v_dict;
15278 if (d != NULL)
15279 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015280 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015281 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015283 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015284 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015285 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015287 if (first)
15288 {
15289 n = i;
15290 first = FALSE;
15291 }
15292 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015293 n = i;
15294 }
15295 }
15296 }
15297 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015298 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015299 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015300 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015301}
15302
15303/*
15304 * "max()" function
15305 */
15306 static void
15307f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015308 typval_T *argvars;
15309 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015310{
15311 max_min(argvars, rettv, TRUE);
15312}
15313
15314/*
15315 * "min()" function
15316 */
15317 static void
15318f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015319 typval_T *argvars;
15320 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015321{
15322 max_min(argvars, rettv, FALSE);
15323}
15324
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015325static int mkdir_recurse __ARGS((char_u *dir, int prot));
15326
15327/*
15328 * Create the directory in which "dir" is located, and higher levels when
15329 * needed.
15330 */
15331 static int
15332mkdir_recurse(dir, prot)
15333 char_u *dir;
15334 int prot;
15335{
15336 char_u *p;
15337 char_u *updir;
15338 int r = FAIL;
15339
15340 /* Get end of directory name in "dir".
15341 * We're done when it's "/" or "c:/". */
15342 p = gettail_sep(dir);
15343 if (p <= get_past_head(dir))
15344 return OK;
15345
15346 /* If the directory exists we're done. Otherwise: create it.*/
15347 updir = vim_strnsave(dir, (int)(p - dir));
15348 if (updir == NULL)
15349 return FAIL;
15350 if (mch_isdir(updir))
15351 r = OK;
15352 else if (mkdir_recurse(updir, prot) == OK)
15353 r = vim_mkdir_emsg(updir, prot);
15354 vim_free(updir);
15355 return r;
15356}
15357
15358#ifdef vim_mkdir
15359/*
15360 * "mkdir()" function
15361 */
15362 static void
15363f_mkdir(argvars, rettv)
15364 typval_T *argvars;
15365 typval_T *rettv;
15366{
15367 char_u *dir;
15368 char_u buf[NUMBUFLEN];
15369 int prot = 0755;
15370
15371 rettv->vval.v_number = FAIL;
15372 if (check_restricted() || check_secure())
15373 return;
15374
15375 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015376 if (*dir == NUL)
15377 rettv->vval.v_number = FAIL;
15378 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015379 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015380 if (*gettail(dir) == NUL)
15381 /* remove trailing slashes */
15382 *gettail_sep(dir) = NUL;
15383
15384 if (argvars[1].v_type != VAR_UNKNOWN)
15385 {
15386 if (argvars[2].v_type != VAR_UNKNOWN)
15387 prot = get_tv_number_chk(&argvars[2], NULL);
15388 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15389 mkdir_recurse(dir, prot);
15390 }
15391 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015392 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015393}
15394#endif
15395
Bram Moolenaar0d660222005-01-07 21:51:51 +000015396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 * "mode()" function
15398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015401 typval_T *argvars;
15402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015404 char_u buf[3];
15405
15406 buf[1] = NUL;
15407 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 if (VIsual_active)
15410 {
15411 if (VIsual_select)
15412 buf[0] = VIsual_mode + 's' - 'v';
15413 else
15414 buf[0] = VIsual_mode;
15415 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015416 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015417 || State == CONFIRM)
15418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015420 if (State == ASKMORE)
15421 buf[1] = 'm';
15422 else if (State == CONFIRM)
15423 buf[1] = '?';
15424 }
15425 else if (State == EXTERNCMD)
15426 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 else if (State & INSERT)
15428 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015429#ifdef FEAT_VREPLACE
15430 if (State & VREPLACE_FLAG)
15431 {
15432 buf[0] = 'R';
15433 buf[1] = 'v';
15434 }
15435 else
15436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 if (State & REPLACE_FLAG)
15438 buf[0] = 'R';
15439 else
15440 buf[0] = 'i';
15441 }
15442 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015445 if (exmode_active)
15446 buf[1] = 'v';
15447 }
15448 else if (exmode_active)
15449 {
15450 buf[0] = 'c';
15451 buf[1] = 'e';
15452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015456 if (finish_op)
15457 buf[1] = 'o';
15458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015460 /* Clear out the minor mode when the argument is not a non-zero number or
15461 * non-empty string. */
15462 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015463 buf[1] = NUL;
15464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 rettv->vval.v_string = vim_strsave(buf);
15466 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467}
15468
Bram Moolenaar429fa852013-04-15 12:27:36 +020015469#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015470/*
15471 * "mzeval()" function
15472 */
15473 static void
15474f_mzeval(argvars, rettv)
15475 typval_T *argvars;
15476 typval_T *rettv;
15477{
15478 char_u *str;
15479 char_u buf[NUMBUFLEN];
15480
15481 str = get_tv_string_buf(&argvars[0], buf);
15482 do_mzeval(str, rettv);
15483}
Bram Moolenaar75676462013-01-30 14:55:42 +010015484
15485 void
15486mzscheme_call_vim(name, args, rettv)
15487 char_u *name;
15488 typval_T *args;
15489 typval_T *rettv;
15490{
15491 typval_T argvars[3];
15492
15493 argvars[0].v_type = VAR_STRING;
15494 argvars[0].vval.v_string = name;
15495 copy_tv(args, &argvars[1]);
15496 argvars[2].v_type = VAR_UNKNOWN;
15497 f_call(argvars, rettv);
15498 clear_tv(&argvars[1]);
15499}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015500#endif
15501
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015503 * "nextnonblank()" function
15504 */
15505 static void
15506f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015507 typval_T *argvars;
15508 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015509{
15510 linenr_T lnum;
15511
15512 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015514 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515 {
15516 lnum = 0;
15517 break;
15518 }
15519 if (*skipwhite(ml_get(lnum)) != NUL)
15520 break;
15521 }
15522 rettv->vval.v_number = lnum;
15523}
15524
15525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 * "nr2char()" function
15527 */
15528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 typval_T *argvars;
15531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532{
15533 char_u buf[NUMBUFLEN];
15534
15535#ifdef FEAT_MBYTE
15536 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015537 {
15538 int utf8 = 0;
15539
15540 if (argvars[1].v_type != VAR_UNKNOWN)
15541 utf8 = get_tv_number_chk(&argvars[1], NULL);
15542 if (utf8)
15543 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15544 else
15545 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 else
15548#endif
15549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015550 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 buf[1] = NUL;
15552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->v_type = VAR_STRING;
15554 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015555}
15556
15557/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015558 * "or(expr, expr)" function
15559 */
15560 static void
15561f_or(argvars, rettv)
15562 typval_T *argvars;
15563 typval_T *rettv;
15564{
15565 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15566 | get_tv_number_chk(&argvars[1], NULL);
15567}
15568
15569/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015570 * "pathshorten()" function
15571 */
15572 static void
15573f_pathshorten(argvars, rettv)
15574 typval_T *argvars;
15575 typval_T *rettv;
15576{
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
15597f_perleval(argvars, rettv)
15598 typval_T *argvars;
15599 typval_T *rettv;
15600{
15601 char_u *str;
15602 char_u buf[NUMBUFLEN];
15603
15604 str = get_tv_string_buf(&argvars[0], buf);
15605 do_perleval(str, rettv);
15606}
15607#endif
15608
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015609#ifdef FEAT_FLOAT
15610/*
15611 * "pow()" function
15612 */
15613 static void
15614f_pow(argvars, rettv)
15615 typval_T *argvars;
15616 typval_T *rettv;
15617{
15618 float_T fx, fy;
15619
15620 rettv->v_type = VAR_FLOAT;
15621 if (get_float_arg(argvars, &fx) == OK
15622 && get_float_arg(&argvars[1], &fy) == OK)
15623 rettv->vval.v_float = pow(fx, fy);
15624 else
15625 rettv->vval.v_float = 0.0;
15626}
15627#endif
15628
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015629/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015630 * "prevnonblank()" function
15631 */
15632 static void
15633f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015634 typval_T *argvars;
15635 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636{
15637 linenr_T lnum;
15638
15639 lnum = get_tv_lnum(argvars);
15640 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15641 lnum = 0;
15642 else
15643 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15644 --lnum;
15645 rettv->vval.v_number = lnum;
15646}
15647
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015648#ifdef HAVE_STDARG_H
15649/* This dummy va_list is here because:
15650 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15651 * - locally in the function results in a "used before set" warning
15652 * - using va_start() to initialize it gives "function with fixed args" error */
15653static va_list ap;
15654#endif
15655
Bram Moolenaar8c711452005-01-14 21:53:12 +000015656/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015657 * "printf()" function
15658 */
15659 static void
15660f_printf(argvars, rettv)
15661 typval_T *argvars;
15662 typval_T *rettv;
15663{
15664 rettv->v_type = VAR_STRING;
15665 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015666#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015667 {
15668 char_u buf[NUMBUFLEN];
15669 int len;
15670 char_u *s;
15671 int saved_did_emsg = did_emsg;
15672 char *fmt;
15673
15674 /* Get the required length, allocate the buffer and do it for real. */
15675 did_emsg = FALSE;
15676 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015677 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015678 if (!did_emsg)
15679 {
15680 s = alloc(len + 1);
15681 if (s != NULL)
15682 {
15683 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015684 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015685 }
15686 }
15687 did_emsg |= saved_did_emsg;
15688 }
15689#endif
15690}
15691
15692/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015693 * "pumvisible()" function
15694 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015695 static void
15696f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015697 typval_T *argvars UNUSED;
15698 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015699{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015700#ifdef FEAT_INS_EXPAND
15701 if (pum_visible())
15702 rettv->vval.v_number = 1;
15703#endif
15704}
15705
Bram Moolenaardb913952012-06-29 12:54:53 +020015706#ifdef FEAT_PYTHON3
15707/*
15708 * "py3eval()" function
15709 */
15710 static void
15711f_py3eval(argvars, rettv)
15712 typval_T *argvars;
15713 typval_T *rettv;
15714{
15715 char_u *str;
15716 char_u buf[NUMBUFLEN];
15717
15718 str = get_tv_string_buf(&argvars[0], buf);
15719 do_py3eval(str, rettv);
15720}
15721#endif
15722
15723#ifdef FEAT_PYTHON
15724/*
15725 * "pyeval()" function
15726 */
15727 static void
15728f_pyeval(argvars, rettv)
15729 typval_T *argvars;
15730 typval_T *rettv;
15731{
15732 char_u *str;
15733 char_u buf[NUMBUFLEN];
15734
15735 str = get_tv_string_buf(&argvars[0], buf);
15736 do_pyeval(str, rettv);
15737}
15738#endif
15739
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015740/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015741 * "range()" function
15742 */
15743 static void
15744f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015745 typval_T *argvars;
15746 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015747{
15748 long start;
15749 long end;
15750 long stride = 1;
15751 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015752 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015754 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015755 if (argvars[1].v_type == VAR_UNKNOWN)
15756 {
15757 end = start - 1;
15758 start = 0;
15759 }
15760 else
15761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015762 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015763 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015764 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015765 }
15766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015767 if (error)
15768 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015769 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015770 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015771 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015772 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015773 else
15774 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015775 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015776 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015777 if (list_append_number(rettv->vval.v_list,
15778 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015779 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015780 }
15781}
15782
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015783/*
15784 * "readfile()" function
15785 */
15786 static void
15787f_readfile(argvars, rettv)
15788 typval_T *argvars;
15789 typval_T *rettv;
15790{
15791 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015792 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015793 char_u *fname;
15794 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015795 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15796 int io_size = sizeof(buf);
15797 int readlen; /* size of last fread() */
15798 char_u *prev = NULL; /* previously read bytes, if any */
15799 long prevlen = 0; /* length of data in prev */
15800 long prevsize = 0; /* size of prev buffer */
15801 long maxline = MAXLNUM;
15802 long cnt = 0;
15803 char_u *p; /* position in buf */
15804 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015805
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015806 if (argvars[1].v_type != VAR_UNKNOWN)
15807 {
15808 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15809 binary = TRUE;
15810 if (argvars[2].v_type != VAR_UNKNOWN)
15811 maxline = get_tv_number(&argvars[2]);
15812 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015813
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015814 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015815 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816
15817 /* Always open the file in binary mode, library functions have a mind of
15818 * their own about CR-LF conversion. */
15819 fname = get_tv_string(&argvars[0]);
15820 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15821 {
15822 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15823 return;
15824 }
15825
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015826 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015827 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015828 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015829
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015830 /* This for loop processes what was read, but is also entered at end
15831 * of file so that either:
15832 * - an incomplete line gets written
15833 * - a "binary" file gets an empty line at the end if it ends in a
15834 * newline. */
15835 for (p = buf, start = buf;
15836 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15837 ++p)
15838 {
15839 if (*p == '\n' || readlen <= 0)
15840 {
15841 listitem_T *li;
15842 char_u *s = NULL;
15843 long_u len = p - start;
15844
15845 /* Finished a line. Remove CRs before NL. */
15846 if (readlen > 0 && !binary)
15847 {
15848 while (len > 0 && start[len - 1] == '\r')
15849 --len;
15850 /* removal may cross back to the "prev" string */
15851 if (len == 0)
15852 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15853 --prevlen;
15854 }
15855 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015856 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015857 else
15858 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015859 /* Change "prev" buffer to be the right size. This way
15860 * the bytes are only copied once, and very long lines are
15861 * allocated only once. */
15862 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015863 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015864 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015865 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015866 prev = NULL; /* the list will own the string */
15867 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015868 }
15869 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015870 if (s == NULL)
15871 {
15872 do_outofmem_msg((long_u) prevlen + len + 1);
15873 failed = TRUE;
15874 break;
15875 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015876
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015877 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015878 {
15879 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015880 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015881 break;
15882 }
15883 li->li_tv.v_type = VAR_STRING;
15884 li->li_tv.v_lock = 0;
15885 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015886 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015888 start = p + 1; /* step over newline */
15889 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015890 break;
15891 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015892 else if (*p == NUL)
15893 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015894#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015895 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15896 * when finding the BF and check the previous two bytes. */
15897 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015898 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015899 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15900 * + 1, these may be in the "prev" string. */
15901 char_u back1 = p >= buf + 1 ? p[-1]
15902 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15903 char_u back2 = p >= buf + 2 ? p[-2]
15904 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15905 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015906
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015907 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015908 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015909 char_u *dest = p - 2;
15910
15911 /* Usually a BOM is at the beginning of a file, and so at
15912 * the beginning of a line; then we can just step over it.
15913 */
15914 if (start == dest)
15915 start = p + 1;
15916 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015917 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015918 /* have to shuffle buf to close gap */
15919 int adjust_prevlen = 0;
15920
15921 if (dest < buf)
15922 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015923 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015924 dest = buf;
15925 }
15926 if (readlen > p - buf + 1)
15927 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15928 readlen -= 3 - adjust_prevlen;
15929 prevlen -= adjust_prevlen;
15930 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015931 }
15932 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015933 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015934#endif
15935 } /* for */
15936
15937 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15938 break;
15939 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015940 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015941 /* There's part of a line in buf, store it in "prev". */
15942 if (p - start + prevlen >= prevsize)
15943 {
15944 /* need bigger "prev" buffer */
15945 char_u *newprev;
15946
15947 /* A common use case is ordinary text files and "prev" gets a
15948 * fragment of a line, so the first allocation is made
15949 * small, to avoid repeatedly 'allocing' large and
15950 * 'reallocing' small. */
15951 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015952 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015953 else
15954 {
15955 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015956 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015957 prevsize = grow50pc > growmin ? grow50pc : growmin;
15958 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015959 newprev = prev == NULL ? alloc(prevsize)
15960 : vim_realloc(prev, prevsize);
15961 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015962 {
15963 do_outofmem_msg((long_u)prevsize);
15964 failed = TRUE;
15965 break;
15966 }
15967 prev = newprev;
15968 }
15969 /* Add the line part to end of "prev". */
15970 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015971 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015972 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015973 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015974
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015975 /*
15976 * For a negative line count use only the lines at the end of the file,
15977 * free the rest.
15978 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015979 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015980 while (cnt > -maxline)
15981 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015982 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015983 --cnt;
15984 }
15985
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015986 if (failed)
15987 {
15988 list_free(rettv->vval.v_list, TRUE);
15989 /* readfile doc says an empty list is returned on error */
15990 rettv->vval.v_list = list_alloc();
15991 }
15992
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015993 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015994 fclose(fd);
15995}
15996
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015997#if defined(FEAT_RELTIME)
15998static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15999
16000/*
16001 * Convert a List to proftime_T.
16002 * Return FAIL when there is something wrong.
16003 */
16004 static int
16005list2proftime(arg, tm)
16006 typval_T *arg;
16007 proftime_T *tm;
16008{
16009 long n1, n2;
16010 int error = FALSE;
16011
16012 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16013 || arg->vval.v_list->lv_len != 2)
16014 return FAIL;
16015 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16016 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16017# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016018 tm->HighPart = n1;
16019 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016020# else
16021 tm->tv_sec = n1;
16022 tm->tv_usec = n2;
16023# endif
16024 return error ? FAIL : OK;
16025}
16026#endif /* FEAT_RELTIME */
16027
16028/*
16029 * "reltime()" function
16030 */
16031 static void
16032f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016033 typval_T *argvars UNUSED;
16034 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016035{
16036#ifdef FEAT_RELTIME
16037 proftime_T res;
16038 proftime_T start;
16039
16040 if (argvars[0].v_type == VAR_UNKNOWN)
16041 {
16042 /* No arguments: get current time. */
16043 profile_start(&res);
16044 }
16045 else if (argvars[1].v_type == VAR_UNKNOWN)
16046 {
16047 if (list2proftime(&argvars[0], &res) == FAIL)
16048 return;
16049 profile_end(&res);
16050 }
16051 else
16052 {
16053 /* Two arguments: compute the difference. */
16054 if (list2proftime(&argvars[0], &start) == FAIL
16055 || list2proftime(&argvars[1], &res) == FAIL)
16056 return;
16057 profile_sub(&res, &start);
16058 }
16059
16060 if (rettv_list_alloc(rettv) == OK)
16061 {
16062 long n1, n2;
16063
16064# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016065 n1 = res.HighPart;
16066 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016067# else
16068 n1 = res.tv_sec;
16069 n2 = res.tv_usec;
16070# endif
16071 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16072 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16073 }
16074#endif
16075}
16076
16077/*
16078 * "reltimestr()" function
16079 */
16080 static void
16081f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016082 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016083 typval_T *rettv;
16084{
16085#ifdef FEAT_RELTIME
16086 proftime_T tm;
16087#endif
16088
16089 rettv->v_type = VAR_STRING;
16090 rettv->vval.v_string = NULL;
16091#ifdef FEAT_RELTIME
16092 if (list2proftime(&argvars[0], &tm) == OK)
16093 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16094#endif
16095}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016096
Bram Moolenaar0d660222005-01-07 21:51:51 +000016097#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
16098static void make_connection __ARGS((void));
16099static int check_connection __ARGS((void));
16100
16101 static void
16102make_connection()
16103{
16104 if (X_DISPLAY == NULL
16105# ifdef FEAT_GUI
16106 && !gui.in_use
16107# endif
16108 )
16109 {
16110 x_force_connect = TRUE;
16111 setup_term_clip();
16112 x_force_connect = FALSE;
16113 }
16114}
16115
16116 static int
16117check_connection()
16118{
16119 make_connection();
16120 if (X_DISPLAY == NULL)
16121 {
16122 EMSG(_("E240: No connection to Vim server"));
16123 return FAIL;
16124 }
16125 return OK;
16126}
16127#endif
16128
16129#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016130static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131
16132 static void
16133remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016134 typval_T *argvars;
16135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136 int expr;
16137{
16138 char_u *server_name;
16139 char_u *keys;
16140 char_u *r = NULL;
16141 char_u buf[NUMBUFLEN];
16142# ifdef WIN32
16143 HWND w;
16144# else
16145 Window w;
16146# endif
16147
16148 if (check_restricted() || check_secure())
16149 return;
16150
16151# ifdef FEAT_X11
16152 if (check_connection() == FAIL)
16153 return;
16154# endif
16155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016156 server_name = get_tv_string_chk(&argvars[0]);
16157 if (server_name == NULL)
16158 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159 keys = get_tv_string_buf(&argvars[1], buf);
16160# ifdef WIN32
16161 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16162# else
16163 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16164 < 0)
16165# endif
16166 {
16167 if (r != NULL)
16168 EMSG(r); /* sending worked but evaluation failed */
16169 else
16170 EMSG2(_("E241: Unable to send to %s"), server_name);
16171 return;
16172 }
16173
16174 rettv->vval.v_string = r;
16175
16176 if (argvars[2].v_type != VAR_UNKNOWN)
16177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016179 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016181
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016182 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 v.di_tv.v_type = VAR_STRING;
16184 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016185 idvar = get_tv_string_chk(&argvars[2]);
16186 if (idvar != NULL)
16187 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016188 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 }
16190}
16191#endif
16192
16193/*
16194 * "remote_expr()" function
16195 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196 static void
16197f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200{
16201 rettv->v_type = VAR_STRING;
16202 rettv->vval.v_string = NULL;
16203#ifdef FEAT_CLIENTSERVER
16204 remote_common(argvars, rettv, TRUE);
16205#endif
16206}
16207
16208/*
16209 * "remote_foreground()" function
16210 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211 static void
16212f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016213 typval_T *argvars UNUSED;
16214 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016216#ifdef FEAT_CLIENTSERVER
16217# ifdef WIN32
16218 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016219 {
16220 char_u *server_name = get_tv_string_chk(&argvars[0]);
16221
16222 if (server_name != NULL)
16223 serverForeground(server_name);
16224 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225# else
16226 /* Send a foreground() expression to the server. */
16227 argvars[1].v_type = VAR_STRING;
16228 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16229 argvars[2].v_type = VAR_UNKNOWN;
16230 remote_common(argvars, rettv, TRUE);
16231 vim_free(argvars[1].vval.v_string);
16232# endif
16233#endif
16234}
16235
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236 static void
16237f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016238 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240{
16241#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016243 char_u *s = NULL;
16244# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016245 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016247 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248
16249 if (check_restricted() || check_secure())
16250 {
16251 rettv->vval.v_number = -1;
16252 return;
16253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016254 serverid = get_tv_string_chk(&argvars[0]);
16255 if (serverid == NULL)
16256 {
16257 rettv->vval.v_number = -1;
16258 return; /* type error; errmsg already given */
16259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016261 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262 if (n == 0)
16263 rettv->vval.v_number = -1;
16264 else
16265 {
16266 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16267 rettv->vval.v_number = (s != NULL);
16268 }
16269# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016270 if (check_connection() == FAIL)
16271 return;
16272
16273 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016274 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275# endif
16276
16277 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016279 char_u *retvar;
16280
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 v.di_tv.v_type = VAR_STRING;
16282 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016283 retvar = get_tv_string_chk(&argvars[1]);
16284 if (retvar != NULL)
16285 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287 }
16288#else
16289 rettv->vval.v_number = -1;
16290#endif
16291}
16292
Bram Moolenaar0d660222005-01-07 21:51:51 +000016293 static void
16294f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016296 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297{
16298 char_u *r = NULL;
16299
16300#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016301 char_u *serverid = get_tv_string_chk(&argvars[0]);
16302
16303 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016304 {
16305# ifdef WIN32
16306 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016307 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016309 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310 if (n != 0)
16311 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16312 if (r == NULL)
16313# else
16314 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016315 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316# endif
16317 EMSG(_("E277: Unable to read a server reply"));
16318 }
16319#endif
16320 rettv->v_type = VAR_STRING;
16321 rettv->vval.v_string = r;
16322}
16323
16324/*
16325 * "remote_send()" function
16326 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016327 static void
16328f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331{
16332 rettv->v_type = VAR_STRING;
16333 rettv->vval.v_string = NULL;
16334#ifdef FEAT_CLIENTSERVER
16335 remote_common(argvars, rettv, FALSE);
16336#endif
16337}
16338
16339/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016340 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016341 */
16342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016344 typval_T *argvars;
16345 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016346{
Bram Moolenaar33570922005-01-25 22:26:29 +000016347 list_T *l;
16348 listitem_T *item, *item2;
16349 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016350 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016351 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016352 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016353 dict_T *d;
16354 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016355 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016356
Bram Moolenaar8c711452005-01-14 21:53:12 +000016357 if (argvars[0].v_type == VAR_DICT)
16358 {
16359 if (argvars[2].v_type != VAR_UNKNOWN)
16360 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016361 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016362 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016364 key = get_tv_string_chk(&argvars[1]);
16365 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016367 di = dict_find(d, key, -1);
16368 if (di == NULL)
16369 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016370 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16371 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016372 {
16373 *rettv = di->di_tv;
16374 init_tv(&di->di_tv);
16375 dictitem_remove(d, di);
16376 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016377 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016378 }
16379 }
16380 else if (argvars[0].v_type != VAR_LIST)
16381 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016382 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016383 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016385 int error = FALSE;
16386
16387 idx = get_tv_number_chk(&argvars[1], &error);
16388 if (error)
16389 ; /* type error: do nothing, errmsg already given */
16390 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016391 EMSGN(_(e_listidx), idx);
16392 else
16393 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016394 if (argvars[2].v_type == VAR_UNKNOWN)
16395 {
16396 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016397 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016398 *rettv = item->li_tv;
16399 vim_free(item);
16400 }
16401 else
16402 {
16403 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 end = get_tv_number_chk(&argvars[2], &error);
16405 if (error)
16406 ; /* type error: do nothing */
16407 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016408 EMSGN(_(e_listidx), end);
16409 else
16410 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016411 int cnt = 0;
16412
16413 for (li = item; li != NULL; li = li->li_next)
16414 {
16415 ++cnt;
16416 if (li == item2)
16417 break;
16418 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016419 if (li == NULL) /* didn't find "item2" after "item" */
16420 EMSG(_(e_invrange));
16421 else
16422 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016423 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016424 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016425 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016426 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016427 l->lv_first = item;
16428 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016429 item->li_prev = NULL;
16430 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016431 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016432 }
16433 }
16434 }
16435 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016436 }
16437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438}
16439
16440/*
16441 * "rename({from}, {to})" function
16442 */
16443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016444f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016445 typval_T *argvars;
16446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447{
16448 char_u buf[NUMBUFLEN];
16449
16450 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016453 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16454 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455}
16456
16457/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016458 * "repeat()" function
16459 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016460 static void
16461f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 typval_T *argvars;
16463 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016464{
16465 char_u *p;
16466 int n;
16467 int slen;
16468 int len;
16469 char_u *r;
16470 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016471
16472 n = get_tv_number(&argvars[1]);
16473 if (argvars[0].v_type == VAR_LIST)
16474 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016475 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016476 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016477 if (list_extend(rettv->vval.v_list,
16478 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016479 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016480 }
16481 else
16482 {
16483 p = get_tv_string(&argvars[0]);
16484 rettv->v_type = VAR_STRING;
16485 rettv->vval.v_string = NULL;
16486
16487 slen = (int)STRLEN(p);
16488 len = slen * n;
16489 if (len <= 0)
16490 return;
16491
16492 r = alloc(len + 1);
16493 if (r != NULL)
16494 {
16495 for (i = 0; i < n; i++)
16496 mch_memmove(r + i * slen, p, (size_t)slen);
16497 r[len] = NUL;
16498 }
16499
16500 rettv->vval.v_string = r;
16501 }
16502}
16503
16504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 * "resolve()" function
16506 */
16507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016508f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016509 typval_T *argvars;
16510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511{
16512 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016513#ifdef HAVE_READLINK
16514 char_u *buf = NULL;
16515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016517 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518#ifdef FEAT_SHORTCUT
16519 {
16520 char_u *v = NULL;
16521
16522 v = mch_resolve_shortcut(p);
16523 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016524 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016526 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527 }
16528#else
16529# ifdef HAVE_READLINK
16530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 char_u *cpy;
16532 int len;
16533 char_u *remain = NULL;
16534 char_u *q;
16535 int is_relative_to_current = FALSE;
16536 int has_trailing_pathsep = FALSE;
16537 int limit = 100;
16538
16539 p = vim_strsave(p);
16540
16541 if (p[0] == '.' && (vim_ispathsep(p[1])
16542 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16543 is_relative_to_current = TRUE;
16544
16545 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016546 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016549 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551
16552 q = getnextcomp(p);
16553 if (*q != NUL)
16554 {
16555 /* Separate the first path component in "p", and keep the
16556 * remainder (beginning with the path separator). */
16557 remain = vim_strsave(q - 1);
16558 q[-1] = NUL;
16559 }
16560
Bram Moolenaard9462e32011-04-11 21:35:11 +020016561 buf = alloc(MAXPATHL + 1);
16562 if (buf == NULL)
16563 goto fail;
16564
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 for (;;)
16566 {
16567 for (;;)
16568 {
16569 len = readlink((char *)p, (char *)buf, MAXPATHL);
16570 if (len <= 0)
16571 break;
16572 buf[len] = NUL;
16573
16574 if (limit-- == 0)
16575 {
16576 vim_free(p);
16577 vim_free(remain);
16578 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 goto fail;
16581 }
16582
16583 /* Ensure that the result will have a trailing path separator
16584 * if the argument has one. */
16585 if (remain == NULL && has_trailing_pathsep)
16586 add_pathsep(buf);
16587
16588 /* Separate the first path component in the link value and
16589 * concatenate the remainders. */
16590 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16591 if (*q != NUL)
16592 {
16593 if (remain == NULL)
16594 remain = vim_strsave(q - 1);
16595 else
16596 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016597 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 if (cpy != NULL)
16599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 vim_free(remain);
16601 remain = cpy;
16602 }
16603 }
16604 q[-1] = NUL;
16605 }
16606
16607 q = gettail(p);
16608 if (q > p && *q == NUL)
16609 {
16610 /* Ignore trailing path separator. */
16611 q[-1] = NUL;
16612 q = gettail(p);
16613 }
16614 if (q > p && !mch_isFullName(buf))
16615 {
16616 /* symlink is relative to directory of argument */
16617 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16618 if (cpy != NULL)
16619 {
16620 STRCPY(cpy, p);
16621 STRCPY(gettail(cpy), buf);
16622 vim_free(p);
16623 p = cpy;
16624 }
16625 }
16626 else
16627 {
16628 vim_free(p);
16629 p = vim_strsave(buf);
16630 }
16631 }
16632
16633 if (remain == NULL)
16634 break;
16635
16636 /* Append the first path component of "remain" to "p". */
16637 q = getnextcomp(remain + 1);
16638 len = q - remain - (*q != NUL);
16639 cpy = vim_strnsave(p, STRLEN(p) + len);
16640 if (cpy != NULL)
16641 {
16642 STRNCAT(cpy, remain, len);
16643 vim_free(p);
16644 p = cpy;
16645 }
16646 /* Shorten "remain". */
16647 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016648 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 else
16650 {
16651 vim_free(remain);
16652 remain = NULL;
16653 }
16654 }
16655
16656 /* If the result is a relative path name, make it explicitly relative to
16657 * the current directory if and only if the argument had this form. */
16658 if (!vim_ispathsep(*p))
16659 {
16660 if (is_relative_to_current
16661 && *p != NUL
16662 && !(p[0] == '.'
16663 && (p[1] == NUL
16664 || vim_ispathsep(p[1])
16665 || (p[1] == '.'
16666 && (p[2] == NUL
16667 || vim_ispathsep(p[2]))))))
16668 {
16669 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016670 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 if (cpy != NULL)
16672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 vim_free(p);
16674 p = cpy;
16675 }
16676 }
16677 else if (!is_relative_to_current)
16678 {
16679 /* Strip leading "./". */
16680 q = p;
16681 while (q[0] == '.' && vim_ispathsep(q[1]))
16682 q += 2;
16683 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016684 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 }
16686 }
16687
16688 /* Ensure that the result will have no trailing path separator
16689 * if the argument had none. But keep "/" or "//". */
16690 if (!has_trailing_pathsep)
16691 {
16692 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016693 if (after_pathsep(p, q))
16694 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 }
16696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016697 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 }
16699# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701# endif
16702#endif
16703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016704 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705
16706#ifdef HAVE_READLINK
16707fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016708 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016710 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711}
16712
16713/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016714 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 */
16716 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016717f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016718 typval_T *argvars;
16719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720{
Bram Moolenaar33570922005-01-25 22:26:29 +000016721 list_T *l;
16722 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724 if (argvars[0].v_type != VAR_LIST)
16725 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016726 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016727 && !tv_check_lock(l->lv_lock,
16728 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 {
16730 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016731 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016732 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016733 while (li != NULL)
16734 {
16735 ni = li->li_prev;
16736 list_append(l, li);
16737 li = ni;
16738 }
16739 rettv->vval.v_list = l;
16740 rettv->v_type = VAR_LIST;
16741 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016742 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744}
16745
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016746#define SP_NOMOVE 0x01 /* don't move cursor */
16747#define SP_REPEAT 0x02 /* repeat to find outer pair */
16748#define SP_RETCOUNT 0x04 /* return matchcount */
16749#define SP_SETPCMARK 0x08 /* set previous context mark */
16750#define SP_START 0x10 /* accept match at start position */
16751#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16752#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016753#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016754
Bram Moolenaar33570922005-01-25 22:26:29 +000016755static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016756
16757/*
16758 * Get flags for a search function.
16759 * Possibly sets "p_ws".
16760 * Returns BACKWARD, FORWARD or zero (for an error).
16761 */
16762 static int
16763get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016764 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016765 int *flagsp;
16766{
16767 int dir = FORWARD;
16768 char_u *flags;
16769 char_u nbuf[NUMBUFLEN];
16770 int mask;
16771
16772 if (varp->v_type != VAR_UNKNOWN)
16773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016774 flags = get_tv_string_buf_chk(varp, nbuf);
16775 if (flags == NULL)
16776 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016777 while (*flags != NUL)
16778 {
16779 switch (*flags)
16780 {
16781 case 'b': dir = BACKWARD; break;
16782 case 'w': p_ws = TRUE; break;
16783 case 'W': p_ws = FALSE; break;
16784 default: mask = 0;
16785 if (flagsp != NULL)
16786 switch (*flags)
16787 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016788 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016789 case 'e': mask = SP_END; break;
16790 case 'm': mask = SP_RETCOUNT; break;
16791 case 'n': mask = SP_NOMOVE; break;
16792 case 'p': mask = SP_SUBPAT; break;
16793 case 'r': mask = SP_REPEAT; break;
16794 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016795 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016796 }
16797 if (mask == 0)
16798 {
16799 EMSG2(_(e_invarg2), flags);
16800 dir = 0;
16801 }
16802 else
16803 *flagsp |= mask;
16804 }
16805 if (dir == 0)
16806 break;
16807 ++flags;
16808 }
16809 }
16810 return dir;
16811}
16812
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016814 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016816 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016817search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016818 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016819 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016820 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016822 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 char_u *pat;
16824 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016825 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 int save_p_ws = p_ws;
16827 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016828 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016829 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016830 proftime_T tm;
16831#ifdef FEAT_RELTIME
16832 long time_limit = 0;
16833#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016834 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016835 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016837 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016838 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016839 if (dir == 0)
16840 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016841 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016842 if (flags & SP_START)
16843 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016844 if (flags & SP_END)
16845 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016846 if (flags & SP_COLUMN)
16847 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016848
Bram Moolenaar76929292008-01-06 19:07:36 +000016849 /* Optional arguments: line number to stop searching and timeout. */
16850 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016851 {
16852 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16853 if (lnum_stop < 0)
16854 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016855#ifdef FEAT_RELTIME
16856 if (argvars[3].v_type != VAR_UNKNOWN)
16857 {
16858 time_limit = get_tv_number_chk(&argvars[3], NULL);
16859 if (time_limit < 0)
16860 goto theend;
16861 }
16862#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016863 }
16864
Bram Moolenaar76929292008-01-06 19:07:36 +000016865#ifdef FEAT_RELTIME
16866 /* Set the time limit, if there is one. */
16867 profile_setlimit(time_limit, &tm);
16868#endif
16869
Bram Moolenaar231334e2005-07-25 20:46:57 +000016870 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016871 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016872 * Check to make sure only those flags are set.
16873 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16874 * flags cannot be set. Check for that condition also.
16875 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016876 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016877 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016879 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016880 goto theend;
16881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016883 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016884 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016885 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016886 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016888 if (flags & SP_SUBPAT)
16889 retval = subpatnum;
16890 else
16891 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016892 if (flags & SP_SETPCMARK)
16893 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016895 if (match_pos != NULL)
16896 {
16897 /* Store the match cursor position */
16898 match_pos->lnum = pos.lnum;
16899 match_pos->col = pos.col + 1;
16900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 /* "/$" will put the cursor after the end of the line, may need to
16902 * correct that here */
16903 check_cursor();
16904 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016905
16906 /* If 'n' flag is used: restore cursor position. */
16907 if (flags & SP_NOMOVE)
16908 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016909 else
16910 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016911theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016913
16914 return retval;
16915}
16916
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016917#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016918
16919/*
16920 * round() is not in C90, use ceil() or floor() instead.
16921 */
16922 float_T
16923vim_round(f)
16924 float_T f;
16925{
16926 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16927}
16928
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016929/*
16930 * "round({float})" function
16931 */
16932 static void
16933f_round(argvars, rettv)
16934 typval_T *argvars;
16935 typval_T *rettv;
16936{
16937 float_T f;
16938
16939 rettv->v_type = VAR_FLOAT;
16940 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016941 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016942 else
16943 rettv->vval.v_float = 0.0;
16944}
16945#endif
16946
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016947/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016948 * "screenattr()" function
16949 */
16950 static void
16951f_screenattr(argvars, rettv)
16952 typval_T *argvars UNUSED;
16953 typval_T *rettv;
16954{
16955 int row;
16956 int col;
16957 int c;
16958
16959 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16960 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16961 if (row < 0 || row >= screen_Rows
16962 || col < 0 || col >= screen_Columns)
16963 c = -1;
16964 else
16965 c = ScreenAttrs[LineOffset[row] + col];
16966 rettv->vval.v_number = c;
16967}
16968
16969/*
16970 * "screenchar()" function
16971 */
16972 static void
16973f_screenchar(argvars, rettv)
16974 typval_T *argvars UNUSED;
16975 typval_T *rettv;
16976{
16977 int row;
16978 int col;
16979 int off;
16980 int c;
16981
16982 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16983 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16984 if (row < 0 || row >= screen_Rows
16985 || col < 0 || col >= screen_Columns)
16986 c = -1;
16987 else
16988 {
16989 off = LineOffset[row] + col;
16990#ifdef FEAT_MBYTE
16991 if (enc_utf8 && ScreenLinesUC[off] != 0)
16992 c = ScreenLinesUC[off];
16993 else
16994#endif
16995 c = ScreenLines[off];
16996 }
16997 rettv->vval.v_number = c;
16998}
16999
17000/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017001 * "screencol()" function
17002 *
17003 * First column is 1 to be consistent with virtcol().
17004 */
17005 static void
17006f_screencol(argvars, rettv)
17007 typval_T *argvars UNUSED;
17008 typval_T *rettv;
17009{
17010 rettv->vval.v_number = screen_screencol() + 1;
17011}
17012
17013/*
17014 * "screenrow()" function
17015 */
17016 static void
17017f_screenrow(argvars, rettv)
17018 typval_T *argvars UNUSED;
17019 typval_T *rettv;
17020{
17021 rettv->vval.v_number = screen_screenrow() + 1;
17022}
17023
17024/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017025 * "search()" function
17026 */
17027 static void
17028f_search(argvars, rettv)
17029 typval_T *argvars;
17030 typval_T *rettv;
17031{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017032 int flags = 0;
17033
17034 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035}
17036
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017038 * "searchdecl()" function
17039 */
17040 static void
17041f_searchdecl(argvars, rettv)
17042 typval_T *argvars;
17043 typval_T *rettv;
17044{
17045 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017046 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017047 int error = FALSE;
17048 char_u *name;
17049
17050 rettv->vval.v_number = 1; /* default: FAIL */
17051
17052 name = get_tv_string_chk(&argvars[0]);
17053 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017054 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017055 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017056 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17057 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17058 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017059 if (!error && name != NULL)
17060 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017061 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017062}
17063
17064/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017065 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017067 static int
17068searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017070 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071{
17072 char_u *spat, *mpat, *epat;
17073 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 int dir;
17076 int flags = 0;
17077 char_u nbuf1[NUMBUFLEN];
17078 char_u nbuf2[NUMBUFLEN];
17079 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017080 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017081 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017082 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017085 spat = get_tv_string_chk(&argvars[0]);
17086 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17087 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17088 if (spat == NULL || mpat == NULL || epat == NULL)
17089 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 /* Handle the optional fourth argument: flags */
17092 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017093 if (dir == 0)
17094 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017095
17096 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017097 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17098 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017099 if ((flags & (SP_END | SP_SUBPAT)) != 0
17100 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017101 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017102 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017103 goto theend;
17104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017106 /* Using 'r' implies 'W', otherwise it doesn't work. */
17107 if (flags & SP_REPEAT)
17108 p_ws = FALSE;
17109
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017110 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017111 if (argvars[3].v_type == VAR_UNKNOWN
17112 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 skip = (char_u *)"";
17114 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017116 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017117 if (argvars[5].v_type != VAR_UNKNOWN)
17118 {
17119 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17120 if (lnum_stop < 0)
17121 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017122#ifdef FEAT_RELTIME
17123 if (argvars[6].v_type != VAR_UNKNOWN)
17124 {
17125 time_limit = get_tv_number_chk(&argvars[6], NULL);
17126 if (time_limit < 0)
17127 goto theend;
17128 }
17129#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017130 }
17131 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 if (skip == NULL)
17133 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017135 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017136 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017137
17138theend:
17139 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017140
17141 return retval;
17142}
17143
17144/*
17145 * "searchpair()" function
17146 */
17147 static void
17148f_searchpair(argvars, rettv)
17149 typval_T *argvars;
17150 typval_T *rettv;
17151{
17152 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17153}
17154
17155/*
17156 * "searchpairpos()" function
17157 */
17158 static void
17159f_searchpairpos(argvars, rettv)
17160 typval_T *argvars;
17161 typval_T *rettv;
17162{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017163 pos_T match_pos;
17164 int lnum = 0;
17165 int col = 0;
17166
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017167 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017168 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017169
17170 if (searchpair_cmn(argvars, &match_pos) > 0)
17171 {
17172 lnum = match_pos.lnum;
17173 col = match_pos.col;
17174 }
17175
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017176 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17177 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017178}
17179
17180/*
17181 * Search for a start/middle/end thing.
17182 * Used by searchpair(), see its documentation for the details.
17183 * Returns 0 or -1 for no match,
17184 */
17185 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017186do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17187 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017188 char_u *spat; /* start pattern */
17189 char_u *mpat; /* middle pattern */
17190 char_u *epat; /* end pattern */
17191 int dir; /* BACKWARD or FORWARD */
17192 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017193 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017194 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017195 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017196 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017197{
17198 char_u *save_cpo;
17199 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17200 long retval = 0;
17201 pos_T pos;
17202 pos_T firstpos;
17203 pos_T foundpos;
17204 pos_T save_cursor;
17205 pos_T save_pos;
17206 int n;
17207 int r;
17208 int nest = 1;
17209 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017210 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017211 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017212
17213 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17214 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017215 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017216
Bram Moolenaar76929292008-01-06 19:07:36 +000017217#ifdef FEAT_RELTIME
17218 /* Set the time limit, if there is one. */
17219 profile_setlimit(time_limit, &tm);
17220#endif
17221
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017222 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17223 * start/middle/end (pat3, for the top pair). */
17224 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17225 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17226 if (pat2 == NULL || pat3 == NULL)
17227 goto theend;
17228 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17229 if (*mpat == NUL)
17230 STRCPY(pat3, pat2);
17231 else
17232 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17233 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017234 if (flags & SP_START)
17235 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017236
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 save_cursor = curwin->w_cursor;
17238 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017239 clearpos(&firstpos);
17240 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 pat = pat3;
17242 for (;;)
17243 {
17244 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017245 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17247 /* didn't find it or found the first match again: FAIL */
17248 break;
17249
17250 if (firstpos.lnum == 0)
17251 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017252 if (equalpos(pos, foundpos))
17253 {
17254 /* Found the same position again. Can happen with a pattern that
17255 * has "\zs" at the end and searching backwards. Advance one
17256 * character and try again. */
17257 if (dir == BACKWARD)
17258 decl(&pos);
17259 else
17260 incl(&pos);
17261 }
17262 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017264 /* clear the start flag to avoid getting stuck here */
17265 options &= ~SEARCH_START;
17266
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 /* If the skip pattern matches, ignore this match. */
17268 if (*skip != NUL)
17269 {
17270 save_pos = curwin->w_cursor;
17271 curwin->w_cursor = pos;
17272 r = eval_to_bool(skip, &err, NULL, FALSE);
17273 curwin->w_cursor = save_pos;
17274 if (err)
17275 {
17276 /* Evaluating {skip} caused an error, break here. */
17277 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017278 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 break;
17280 }
17281 if (r)
17282 continue;
17283 }
17284
17285 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17286 {
17287 /* Found end when searching backwards or start when searching
17288 * forward: nested pair. */
17289 ++nest;
17290 pat = pat2; /* nested, don't search for middle */
17291 }
17292 else
17293 {
17294 /* Found end when searching forward or start when searching
17295 * backward: end of (nested) pair; or found middle in outer pair. */
17296 if (--nest == 1)
17297 pat = pat3; /* outer level, search for middle */
17298 }
17299
17300 if (nest == 0)
17301 {
17302 /* Found the match: return matchcount or line number. */
17303 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017304 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017306 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017307 if (flags & SP_SETPCMARK)
17308 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 curwin->w_cursor = pos;
17310 if (!(flags & SP_REPEAT))
17311 break;
17312 nest = 1; /* search for next unmatched */
17313 }
17314 }
17315
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017316 if (match_pos != NULL)
17317 {
17318 /* Store the match cursor position */
17319 match_pos->lnum = curwin->w_cursor.lnum;
17320 match_pos->col = curwin->w_cursor.col + 1;
17321 }
17322
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017324 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 curwin->w_cursor = save_cursor;
17326
17327theend:
17328 vim_free(pat2);
17329 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017330 if (p_cpo == empty_option)
17331 p_cpo = save_cpo;
17332 else
17333 /* Darn, evaluating the {skip} expression changed the value. */
17334 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017335
17336 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337}
17338
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017339/*
17340 * "searchpos()" function
17341 */
17342 static void
17343f_searchpos(argvars, rettv)
17344 typval_T *argvars;
17345 typval_T *rettv;
17346{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017347 pos_T match_pos;
17348 int lnum = 0;
17349 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017350 int n;
17351 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017352
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017353 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017354 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017355
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017356 n = search_cmn(argvars, &match_pos, &flags);
17357 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017358 {
17359 lnum = match_pos.lnum;
17360 col = match_pos.col;
17361 }
17362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017363 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17364 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017365 if (flags & SP_SUBPAT)
17366 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017367}
17368
17369
Bram Moolenaar0d660222005-01-07 21:51:51 +000017370 static void
17371f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017372 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017375#ifdef FEAT_CLIENTSERVER
17376 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 char_u *server = get_tv_string_chk(&argvars[0]);
17378 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379
Bram Moolenaar0d660222005-01-07 21:51:51 +000017380 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017381 if (server == NULL || reply == NULL)
17382 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017383 if (check_restricted() || check_secure())
17384 return;
17385# ifdef FEAT_X11
17386 if (check_connection() == FAIL)
17387 return;
17388# endif
17389
17390 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017392 EMSG(_("E258: Unable to send to client"));
17393 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017395 rettv->vval.v_number = 0;
17396#else
17397 rettv->vval.v_number = -1;
17398#endif
17399}
17400
Bram Moolenaar0d660222005-01-07 21:51:51 +000017401 static void
17402f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017403 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405{
17406 char_u *r = NULL;
17407
17408#ifdef FEAT_CLIENTSERVER
17409# ifdef WIN32
17410 r = serverGetVimNames();
17411# else
17412 make_connection();
17413 if (X_DISPLAY != NULL)
17414 r = serverGetVimNames(X_DISPLAY);
17415# endif
17416#endif
17417 rettv->v_type = VAR_STRING;
17418 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419}
17420
17421/*
17422 * "setbufvar()" function
17423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017425f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017426 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017427 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428{
17429 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 char_u nbuf[NUMBUFLEN];
17434
17435 if (check_restricted() || check_secure())
17436 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017437 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17438 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017439 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 varp = &argvars[2];
17441
17442 if (buf != NULL && varname != NULL && varp != NULL)
17443 {
17444 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446
17447 if (*varname == '&')
17448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017449 long numval;
17450 char_u *strval;
17451 int error = FALSE;
17452
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017454 numval = get_tv_number_chk(varp, &error);
17455 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017456 if (!error && strval != NULL)
17457 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 }
17459 else
17460 {
17461 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17462 if (bufvarname != NULL)
17463 {
17464 STRCPY(bufvarname, "b:");
17465 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017466 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 vim_free(bufvarname);
17468 }
17469 }
17470
17471 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474}
17475
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017476 static void
17477f_setcharsearch(argvars, rettv)
17478 typval_T *argvars;
17479 typval_T *rettv UNUSED;
17480{
17481 dict_T *d;
17482 dictitem_T *di;
17483 char_u *csearch;
17484
17485 if (argvars[0].v_type != VAR_DICT)
17486 {
17487 EMSG(_(e_dictreq));
17488 return;
17489 }
17490
17491 if ((d = argvars[0].vval.v_dict) != NULL)
17492 {
17493 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17494 if (csearch != NULL)
17495 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017496#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017497 if (enc_utf8)
17498 {
17499 int pcc[MAX_MCO];
17500 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017501
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017502 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17503 }
17504 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017505#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017506 set_last_csearch(PTR2CHAR(csearch),
17507 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017508 }
17509
17510 di = dict_find(d, (char_u *)"forward", -1);
17511 if (di != NULL)
17512 set_csearch_direction(get_tv_number(&di->di_tv)
17513 ? FORWARD : BACKWARD);
17514
17515 di = dict_find(d, (char_u *)"until", -1);
17516 if (di != NULL)
17517 set_csearch_until(!!get_tv_number(&di->di_tv));
17518 }
17519}
17520
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521/*
17522 * "setcmdpos()" function
17523 */
17524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 typval_T *argvars;
17527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017529 int pos = (int)get_tv_number(&argvars[0]) - 1;
17530
17531 if (pos >= 0)
17532 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533}
17534
17535/*
17536 * "setline()" function
17537 */
17538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017540 typval_T *argvars;
17541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542{
17543 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017544 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017545 list_T *l = NULL;
17546 listitem_T *li = NULL;
17547 long added = 0;
17548 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017550 lnum = get_tv_lnum(&argvars[0]);
17551 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017553 l = argvars[1].vval.v_list;
17554 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017556 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017557 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017558
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017559 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017560 for (;;)
17561 {
17562 if (l != NULL)
17563 {
17564 /* list argument, get next string */
17565 if (li == NULL)
17566 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017567 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017568 li = li->li_next;
17569 }
17570
17571 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017572 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017573 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017574
17575 /* When coming here from Insert mode, sync undo, so that this can be
17576 * undone separately from what was previously inserted. */
17577 if (u_sync_once == 2)
17578 {
17579 u_sync_once = 1; /* notify that u_sync() was called */
17580 u_sync(TRUE);
17581 }
17582
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017583 if (lnum <= curbuf->b_ml.ml_line_count)
17584 {
17585 /* existing line, replace it */
17586 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17587 {
17588 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017589 if (lnum == curwin->w_cursor.lnum)
17590 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017591 rettv->vval.v_number = 0; /* OK */
17592 }
17593 }
17594 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17595 {
17596 /* lnum is one past the last line, append the line */
17597 ++added;
17598 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17599 rettv->vval.v_number = 0; /* OK */
17600 }
17601
17602 if (l == NULL) /* only one string argument */
17603 break;
17604 ++lnum;
17605 }
17606
17607 if (added > 0)
17608 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609}
17610
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017611static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17612
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017614 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017615 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017616 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017617set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017618 win_T *wp UNUSED;
17619 typval_T *list_arg UNUSED;
17620 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017621 typval_T *rettv;
17622{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017623#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017624 char_u *act;
17625 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017626#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017627
Bram Moolenaar2641f772005-03-25 21:58:17 +000017628 rettv->vval.v_number = -1;
17629
17630#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017631 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017632 EMSG(_(e_listreq));
17633 else
17634 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017635 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017636
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017637 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017638 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017639 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017640 if (act == NULL)
17641 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017642 if (*act == 'a' || *act == 'r')
17643 action = *act;
17644 }
17645
Bram Moolenaar81484f42012-12-05 15:16:47 +010017646 if (l != NULL && set_errorlist(wp, l, action,
17647 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017648 rettv->vval.v_number = 0;
17649 }
17650#endif
17651}
17652
17653/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017654 * "setloclist()" function
17655 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017656 static void
17657f_setloclist(argvars, rettv)
17658 typval_T *argvars;
17659 typval_T *rettv;
17660{
17661 win_T *win;
17662
17663 rettv->vval.v_number = -1;
17664
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017665 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017666 if (win != NULL)
17667 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17668}
17669
17670/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017671 * "setmatches()" function
17672 */
17673 static void
17674f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017675 typval_T *argvars UNUSED;
17676 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017677{
17678#ifdef FEAT_SEARCH_EXTRA
17679 list_T *l;
17680 listitem_T *li;
17681 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017682 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017683
17684 rettv->vval.v_number = -1;
17685 if (argvars[0].v_type != VAR_LIST)
17686 {
17687 EMSG(_(e_listreq));
17688 return;
17689 }
17690 if ((l = argvars[0].vval.v_list) != NULL)
17691 {
17692
17693 /* To some extent make sure that we are dealing with a list from
17694 * "getmatches()". */
17695 li = l->lv_first;
17696 while (li != NULL)
17697 {
17698 if (li->li_tv.v_type != VAR_DICT
17699 || (d = li->li_tv.vval.v_dict) == NULL)
17700 {
17701 EMSG(_(e_invarg));
17702 return;
17703 }
17704 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017705 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17706 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017707 && dict_find(d, (char_u *)"priority", -1) != NULL
17708 && dict_find(d, (char_u *)"id", -1) != NULL))
17709 {
17710 EMSG(_(e_invarg));
17711 return;
17712 }
17713 li = li->li_next;
17714 }
17715
17716 clear_matches(curwin);
17717 li = l->lv_first;
17718 while (li != NULL)
17719 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017720 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017721 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017722 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017723 char_u *group;
17724 int priority;
17725 int id;
17726 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017727
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017728 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017729 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17730 {
17731 if (s == NULL)
17732 {
17733 s = list_alloc();
17734 if (s == NULL)
17735 return;
17736 }
17737
17738 /* match from matchaddpos() */
17739 for (i = 1; i < 9; i++)
17740 {
17741 sprintf((char *)buf, (char *)"pos%d", i);
17742 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17743 {
17744 if (di->di_tv.v_type != VAR_LIST)
17745 return;
17746
17747 list_append_tv(s, &di->di_tv);
17748 s->lv_refcount++;
17749 }
17750 else
17751 break;
17752 }
17753 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017754
17755 group = get_dict_string(d, (char_u *)"group", FALSE);
17756 priority = (int)get_dict_number(d, (char_u *)"priority");
17757 id = (int)get_dict_number(d, (char_u *)"id");
17758 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17759 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17760 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017761 if (i == 0)
17762 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017763 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017764 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017765 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017766 }
17767 else
17768 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017769 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017770 list_unref(s);
17771 s = NULL;
17772 }
17773
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017774 li = li->li_next;
17775 }
17776 rettv->vval.v_number = 0;
17777 }
17778#endif
17779}
17780
17781/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017782 * "setpos()" function
17783 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017784 static void
17785f_setpos(argvars, rettv)
17786 typval_T *argvars;
17787 typval_T *rettv;
17788{
17789 pos_T pos;
17790 int fnum;
17791 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017792 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017793
Bram Moolenaar08250432008-02-13 11:42:46 +000017794 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017795 name = get_tv_string_chk(argvars);
17796 if (name != NULL)
17797 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017798 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017799 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017800 if (--pos.col < 0)
17801 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017802 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017803 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017804 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017805 if (fnum == curbuf->b_fnum)
17806 {
17807 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017808 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017809 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017810 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017811 curwin->w_set_curswant = FALSE;
17812 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017813 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017814 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017815 }
17816 else
17817 EMSG(_(e_invarg));
17818 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017819 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17820 {
17821 /* set mark */
17822 if (setmark_pos(name[1], &pos, fnum) == OK)
17823 rettv->vval.v_number = 0;
17824 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017825 else
17826 EMSG(_(e_invarg));
17827 }
17828 }
17829}
17830
17831/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017832 * "setqflist()" function
17833 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017834 static void
17835f_setqflist(argvars, rettv)
17836 typval_T *argvars;
17837 typval_T *rettv;
17838{
17839 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17840}
17841
17842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 * "setreg()" function
17844 */
17845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 typval_T *argvars;
17848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849{
17850 int regname;
17851 char_u *strregname;
17852 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017853 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 int append;
17855 char_u yank_type;
17856 long block_len;
17857
17858 block_len = -1;
17859 yank_type = MAUTO;
17860 append = FALSE;
17861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017862 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017863 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017865 if (strregname == NULL)
17866 return; /* type error; errmsg already given */
17867 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 if (regname == 0 || regname == '@')
17869 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017871 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017873 stropt = get_tv_string_chk(&argvars[2]);
17874 if (stropt == NULL)
17875 return; /* type error */
17876 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 switch (*stropt)
17878 {
17879 case 'a': case 'A': /* append */
17880 append = TRUE;
17881 break;
17882 case 'v': case 'c': /* character-wise selection */
17883 yank_type = MCHAR;
17884 break;
17885 case 'V': case 'l': /* line-wise selection */
17886 yank_type = MLINE;
17887 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 case 'b': case Ctrl_V: /* block-wise selection */
17889 yank_type = MBLOCK;
17890 if (VIM_ISDIGIT(stropt[1]))
17891 {
17892 ++stropt;
17893 block_len = getdigits(&stropt) - 1;
17894 --stropt;
17895 }
17896 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 }
17898 }
17899
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017900 if (argvars[1].v_type == VAR_LIST)
17901 {
17902 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017903 char_u **allocval;
17904 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017905 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017906 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017907 int len = argvars[1].vval.v_list->lv_len;
17908 listitem_T *li;
17909
Bram Moolenaar7d647822014-04-05 21:28:56 +020017910 /* First half: use for pointers to result lines; second half: use for
17911 * pointers to allocated copies. */
17912 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017913 if (lstval == NULL)
17914 return;
17915 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017916 allocval = lstval + len + 2;
17917 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017918
17919 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17920 li = li->li_next)
17921 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017922 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017923 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017924 goto free_lstval;
17925 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017926 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017927 /* Need to make a copy, next get_tv_string_buf_chk() will
17928 * overwrite the string. */
17929 strval = vim_strsave(buf);
17930 if (strval == NULL)
17931 goto free_lstval;
17932 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017933 }
17934 *curval++ = strval;
17935 }
17936 *curval++ = NULL;
17937
17938 write_reg_contents_lst(regname, lstval, -1,
17939 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017940free_lstval:
17941 while (curallocval > allocval)
17942 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017943 vim_free(lstval);
17944 }
17945 else
17946 {
17947 strval = get_tv_string_chk(&argvars[1]);
17948 if (strval == NULL)
17949 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017950 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954}
17955
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017956/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017957 * "settabvar()" function
17958 */
17959 static void
17960f_settabvar(argvars, rettv)
17961 typval_T *argvars;
17962 typval_T *rettv;
17963{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017964#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017965 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017966 tabpage_T *tp;
17967#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017968 char_u *varname, *tabvarname;
17969 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017970
17971 rettv->vval.v_number = 0;
17972
17973 if (check_restricted() || check_secure())
17974 return;
17975
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017976#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017977 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017978#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017979 varname = get_tv_string_chk(&argvars[1]);
17980 varp = &argvars[2];
17981
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017982 if (varname != NULL && varp != NULL
17983#ifdef FEAT_WINDOWS
17984 && tp != NULL
17985#endif
17986 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017987 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017988#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017989 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017990 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017991#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017992
17993 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17994 if (tabvarname != NULL)
17995 {
17996 STRCPY(tabvarname, "t:");
17997 STRCPY(tabvarname + 2, varname);
17998 set_var(tabvarname, varp, TRUE);
17999 vim_free(tabvarname);
18000 }
18001
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018002#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018003 /* Restore current tabpage */
18004 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018005 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018006#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018007 }
18008}
18009
18010/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018011 * "settabwinvar()" function
18012 */
18013 static void
18014f_settabwinvar(argvars, rettv)
18015 typval_T *argvars;
18016 typval_T *rettv;
18017{
18018 setwinvar(argvars, rettv, 1);
18019}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020
18021/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018022 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018026 typval_T *argvars;
18027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018029 setwinvar(argvars, rettv, 0);
18030}
18031
18032/*
18033 * "setwinvar()" and "settabwinvar()" functions
18034 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018035
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018036 static void
18037setwinvar(argvars, rettv, off)
18038 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018039 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018040 int off;
18041{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 win_T *win;
18043#ifdef FEAT_WINDOWS
18044 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018045 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018046 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047#endif
18048 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018049 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018051 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052
18053 if (check_restricted() || check_secure())
18054 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018055
18056#ifdef FEAT_WINDOWS
18057 if (off == 1)
18058 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18059 else
18060 tp = curtab;
18061#endif
18062 win = find_win_by_nr(&argvars[off], tp);
18063 varname = get_tv_string_chk(&argvars[off + 1]);
18064 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065
18066 if (win != NULL && varname != NULL && varp != NULL)
18067 {
18068#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018069 need_switch_win = !(tp == curtab && win == curwin);
18070 if (!need_switch_win
18071 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018074 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018076 long numval;
18077 char_u *strval;
18078 int error = FALSE;
18079
18080 ++varname;
18081 numval = get_tv_number_chk(varp, &error);
18082 strval = get_tv_string_buf_chk(varp, nbuf);
18083 if (!error && strval != NULL)
18084 set_option_value(varname, numval, strval, OPT_LOCAL);
18085 }
18086 else
18087 {
18088 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18089 if (winvarname != NULL)
18090 {
18091 STRCPY(winvarname, "w:");
18092 STRCPY(winvarname + 2, varname);
18093 set_var(winvarname, varp, TRUE);
18094 vim_free(winvarname);
18095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 }
18097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018099 if (need_switch_win)
18100 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101#endif
18102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103}
18104
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018105#ifdef FEAT_CRYPT
18106/*
18107 * "sha256({string})" function
18108 */
18109 static void
18110f_sha256(argvars, rettv)
18111 typval_T *argvars;
18112 typval_T *rettv;
18113{
18114 char_u *p;
18115
18116 p = get_tv_string(&argvars[0]);
18117 rettv->vval.v_string = vim_strsave(
18118 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18119 rettv->v_type = VAR_STRING;
18120}
18121#endif /* FEAT_CRYPT */
18122
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018124 * "shellescape({string})" function
18125 */
18126 static void
18127f_shellescape(argvars, rettv)
18128 typval_T *argvars;
18129 typval_T *rettv;
18130{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018131 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018132 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018133 rettv->v_type = VAR_STRING;
18134}
18135
18136/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018137 * shiftwidth() function
18138 */
18139 static void
18140f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018141 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018142 typval_T *rettv;
18143{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018144 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018145}
18146
18147/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018148 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 */
18150 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018152 typval_T *argvars;
18153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156
Bram Moolenaar0d660222005-01-07 21:51:51 +000018157 p = get_tv_string(&argvars[0]);
18158 rettv->vval.v_string = vim_strsave(p);
18159 simplify_filename(rettv->vval.v_string); /* simplify in place */
18160 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161}
18162
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018163#ifdef FEAT_FLOAT
18164/*
18165 * "sin()" function
18166 */
18167 static void
18168f_sin(argvars, rettv)
18169 typval_T *argvars;
18170 typval_T *rettv;
18171{
18172 float_T f;
18173
18174 rettv->v_type = VAR_FLOAT;
18175 if (get_float_arg(argvars, &f) == OK)
18176 rettv->vval.v_float = sin(f);
18177 else
18178 rettv->vval.v_float = 0.0;
18179}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018180
18181/*
18182 * "sinh()" function
18183 */
18184 static void
18185f_sinh(argvars, rettv)
18186 typval_T *argvars;
18187 typval_T *rettv;
18188{
18189 float_T f;
18190
18191 rettv->v_type = VAR_FLOAT;
18192 if (get_float_arg(argvars, &f) == OK)
18193 rettv->vval.v_float = sinh(f);
18194 else
18195 rettv->vval.v_float = 0.0;
18196}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018197#endif
18198
Bram Moolenaar0d660222005-01-07 21:51:51 +000018199static int
18200#ifdef __BORLANDC__
18201 _RTLENTRYF
18202#endif
18203 item_compare __ARGS((const void *s1, const void *s2));
18204static int
18205#ifdef __BORLANDC__
18206 _RTLENTRYF
18207#endif
18208 item_compare2 __ARGS((const void *s1, const void *s2));
18209
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018210/* struct used in the array that's given to qsort() */
18211typedef struct
18212{
18213 listitem_T *item;
18214 int idx;
18215} sortItem_T;
18216
Bram Moolenaar0d660222005-01-07 21:51:51 +000018217static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018218static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018219static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018220#ifdef FEAT_FLOAT
18221static int item_compare_float;
18222#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018223static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018224static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018225static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018226static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018227static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018228#define ITEM_COMPARE_FAIL 999
18229
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018231 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018233 static int
18234#ifdef __BORLANDC__
18235_RTLENTRYF
18236#endif
18237item_compare(s1, s2)
18238 const void *s1;
18239 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018241 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018242 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018243 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018244 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 int res;
18246 char_u numbuf1[NUMBUFLEN];
18247 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018249 si1 = (sortItem_T *)s1;
18250 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018251 tv1 = &si1->item->li_tv;
18252 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018253
18254 if (item_compare_numbers)
18255 {
18256 long v1 = get_tv_number(tv1);
18257 long v2 = get_tv_number(tv2);
18258
18259 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18260 }
18261
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018262#ifdef FEAT_FLOAT
18263 if (item_compare_float)
18264 {
18265 float_T v1 = get_tv_float(tv1);
18266 float_T v2 = get_tv_float(tv2);
18267
18268 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18269 }
18270#endif
18271
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018272 /* tv2string() puts quotes around a string and allocates memory. Don't do
18273 * that for string variables. Use a single quote when comparing with a
18274 * non-string to do what the docs promise. */
18275 if (tv1->v_type == VAR_STRING)
18276 {
18277 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18278 p1 = (char_u *)"'";
18279 else
18280 p1 = tv1->vval.v_string;
18281 }
18282 else
18283 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18284 if (tv2->v_type == VAR_STRING)
18285 {
18286 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18287 p2 = (char_u *)"'";
18288 else
18289 p2 = tv2->vval.v_string;
18290 }
18291 else
18292 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018293 if (p1 == NULL)
18294 p1 = (char_u *)"";
18295 if (p2 == NULL)
18296 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018297 if (!item_compare_numeric)
18298 {
18299 if (item_compare_ic)
18300 res = STRICMP(p1, p2);
18301 else
18302 res = STRCMP(p1, p2);
18303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018305 {
18306 double n1, n2;
18307 n1 = strtod((char *)p1, (char **)&p1);
18308 n2 = strtod((char *)p2, (char **)&p2);
18309 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18310 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018311
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018312 /* When the result would be zero, compare the item indexes. Makes the
18313 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018314 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018315 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018316
Bram Moolenaar0d660222005-01-07 21:51:51 +000018317 vim_free(tofree1);
18318 vim_free(tofree2);
18319 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320}
18321
18322 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323#ifdef __BORLANDC__
18324_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018326item_compare2(s1, s2)
18327 const void *s1;
18328 const void *s2;
18329{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018330 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018331 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018332 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018333 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018334 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018336 /* shortcut after failure in previous call; compare all items equal */
18337 if (item_compare_func_err)
18338 return 0;
18339
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018340 si1 = (sortItem_T *)s1;
18341 si2 = (sortItem_T *)s2;
18342
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018343 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018344 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018345 copy_tv(&si1->item->li_tv, &argv[0]);
18346 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018347
18348 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018349 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018350 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18351 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018352 clear_tv(&argv[0]);
18353 clear_tv(&argv[1]);
18354
18355 if (res == FAIL)
18356 res = ITEM_COMPARE_FAIL;
18357 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018358 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18359 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018360 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018361 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018362
18363 /* When the result would be zero, compare the pointers themselves. Makes
18364 * the sort stable. */
18365 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018366 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018367
Bram Moolenaar0d660222005-01-07 21:51:51 +000018368 return res;
18369}
18370
18371/*
18372 * "sort({list})" function
18373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018375do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018376 typval_T *argvars;
18377 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018378 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379{
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 list_T *l;
18381 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018382 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018383 long len;
18384 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385
Bram Moolenaar0d660222005-01-07 21:51:51 +000018386 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018387 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 else
18389 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018390 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018391 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018392 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18393 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394 return;
18395 rettv->vval.v_list = l;
18396 rettv->v_type = VAR_LIST;
18397 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398
Bram Moolenaar0d660222005-01-07 21:51:51 +000018399 len = list_len(l);
18400 if (len <= 1)
18401 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402
Bram Moolenaar0d660222005-01-07 21:51:51 +000018403 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018404 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018405 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018406#ifdef FEAT_FLOAT
18407 item_compare_float = FALSE;
18408#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018409 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018410 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018411 if (argvars[1].v_type != VAR_UNKNOWN)
18412 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018413 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018414 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018415 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416 else
18417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018418 int error = FALSE;
18419
18420 i = get_tv_number_chk(&argvars[1], &error);
18421 if (error)
18422 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018423 if (i == 1)
18424 item_compare_ic = TRUE;
18425 else
18426 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018427 if (item_compare_func != NULL)
18428 {
18429 if (STRCMP(item_compare_func, "n") == 0)
18430 {
18431 item_compare_func = NULL;
18432 item_compare_numeric = TRUE;
18433 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018434 else if (STRCMP(item_compare_func, "N") == 0)
18435 {
18436 item_compare_func = NULL;
18437 item_compare_numbers = TRUE;
18438 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018439#ifdef FEAT_FLOAT
18440 else if (STRCMP(item_compare_func, "f") == 0)
18441 {
18442 item_compare_func = NULL;
18443 item_compare_float = TRUE;
18444 }
18445#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018446 else if (STRCMP(item_compare_func, "i") == 0)
18447 {
18448 item_compare_func = NULL;
18449 item_compare_ic = TRUE;
18450 }
18451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018452 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018453
18454 if (argvars[2].v_type != VAR_UNKNOWN)
18455 {
18456 /* optional third argument: {dict} */
18457 if (argvars[2].v_type != VAR_DICT)
18458 {
18459 EMSG(_(e_dictreq));
18460 return;
18461 }
18462 item_compare_selfdict = argvars[2].vval.v_dict;
18463 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465
Bram Moolenaar0d660222005-01-07 21:51:51 +000018466 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018467 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018468 if (ptrs == NULL)
18469 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470
Bram Moolenaar327aa022014-03-25 18:24:23 +010018471 i = 0;
18472 if (sort)
18473 {
18474 /* sort(): ptrs will be the list to sort */
18475 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018476 {
18477 ptrs[i].item = li;
18478 ptrs[i].idx = i;
18479 ++i;
18480 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018481
18482 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018483 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018484 /* test the compare function */
18485 if (item_compare_func != NULL
18486 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018487 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018488 EMSG(_("E702: Sort compare function failed"));
18489 else
18490 {
18491 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018492 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018493 item_compare_func == NULL ? item_compare : item_compare2);
18494
18495 if (!item_compare_func_err)
18496 {
18497 /* Clear the List and append the items in sorted order. */
18498 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18499 l->lv_len = 0;
18500 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018501 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018502 }
18503 }
18504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018506 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018507 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18508
18509 /* f_uniq(): ptrs will be a stack of items to remove */
18510 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018511 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018512 item_compare_func_ptr = item_compare_func
18513 ? item_compare2 : item_compare;
18514
18515 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18516 li = li->li_next)
18517 {
18518 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18519 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018520 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018521 if (item_compare_func_err)
18522 {
18523 EMSG(_("E882: Uniq compare function failed"));
18524 break;
18525 }
18526 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018528 if (!item_compare_func_err)
18529 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018530 while (--i >= 0)
18531 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018532 li = ptrs[i].item->li_next;
18533 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018534 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018535 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018536 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018537 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018538 list_fix_watch(l, li);
18539 listitem_free(li);
18540 l->lv_len--;
18541 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018542 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018543 }
18544
18545 vim_free(ptrs);
18546 }
18547}
18548
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018549/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018550 * "sort({list})" function
18551 */
18552 static void
18553f_sort(argvars, rettv)
18554 typval_T *argvars;
18555 typval_T *rettv;
18556{
18557 do_sort_uniq(argvars, rettv, TRUE);
18558}
18559
18560/*
18561 * "uniq({list})" function
18562 */
18563 static void
18564f_uniq(argvars, rettv)
18565 typval_T *argvars;
18566 typval_T *rettv;
18567{
18568 do_sort_uniq(argvars, rettv, FALSE);
18569}
18570
18571/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018572 * "soundfold({word})" function
18573 */
18574 static void
18575f_soundfold(argvars, rettv)
18576 typval_T *argvars;
18577 typval_T *rettv;
18578{
18579 char_u *s;
18580
18581 rettv->v_type = VAR_STRING;
18582 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018583#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018584 rettv->vval.v_string = eval_soundfold(s);
18585#else
18586 rettv->vval.v_string = vim_strsave(s);
18587#endif
18588}
18589
18590/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018591 * "spellbadword()" function
18592 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018593 static void
18594f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018595 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018596 typval_T *rettv;
18597{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018598 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018599 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018600 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018602 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018603 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018604
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018605#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018606 if (argvars[0].v_type == VAR_UNKNOWN)
18607 {
18608 /* Find the start and length of the badly spelled word. */
18609 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18610 if (len != 0)
18611 word = ml_get_cursor();
18612 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018613 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018614 {
18615 char_u *str = get_tv_string_chk(&argvars[0]);
18616 int capcol = -1;
18617
18618 if (str != NULL)
18619 {
18620 /* Check the argument for spelling. */
18621 while (*str != NUL)
18622 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018623 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018624 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018625 {
18626 word = str;
18627 break;
18628 }
18629 str += len;
18630 }
18631 }
18632 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018633#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018635 list_append_string(rettv->vval.v_list, word, len);
18636 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018637 attr == HLF_SPB ? "bad" :
18638 attr == HLF_SPR ? "rare" :
18639 attr == HLF_SPL ? "local" :
18640 attr == HLF_SPC ? "caps" :
18641 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018642}
18643
18644/*
18645 * "spellsuggest()" function
18646 */
18647 static void
18648f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018649 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018650 typval_T *rettv;
18651{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018652#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018653 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018654 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018655 int maxcount;
18656 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018657 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018658 listitem_T *li;
18659 int need_capital = FALSE;
18660#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018661
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018662 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018663 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018664
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018665#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018666 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018667 {
18668 str = get_tv_string(&argvars[0]);
18669 if (argvars[1].v_type != VAR_UNKNOWN)
18670 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018671 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018672 if (maxcount <= 0)
18673 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018674 if (argvars[2].v_type != VAR_UNKNOWN)
18675 {
18676 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18677 if (typeerr)
18678 return;
18679 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018680 }
18681 else
18682 maxcount = 25;
18683
Bram Moolenaar4770d092006-01-12 23:22:24 +000018684 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018685
18686 for (i = 0; i < ga.ga_len; ++i)
18687 {
18688 str = ((char_u **)ga.ga_data)[i];
18689
18690 li = listitem_alloc();
18691 if (li == NULL)
18692 vim_free(str);
18693 else
18694 {
18695 li->li_tv.v_type = VAR_STRING;
18696 li->li_tv.v_lock = 0;
18697 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018698 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018699 }
18700 }
18701 ga_clear(&ga);
18702 }
18703#endif
18704}
18705
Bram Moolenaar0d660222005-01-07 21:51:51 +000018706 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018707f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018708 typval_T *argvars;
18709 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018710{
18711 char_u *str;
18712 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018713 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714 regmatch_T regmatch;
18715 char_u patbuf[NUMBUFLEN];
18716 char_u *save_cpo;
18717 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018718 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018719 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018720 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018721
18722 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18723 save_cpo = p_cpo;
18724 p_cpo = (char_u *)"";
18725
18726 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018727 if (argvars[1].v_type != VAR_UNKNOWN)
18728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018729 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18730 if (pat == NULL)
18731 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018732 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018733 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018734 }
18735 if (pat == NULL || *pat == NUL)
18736 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018737
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018738 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018740 if (typeerr)
18741 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742
Bram Moolenaar0d660222005-01-07 21:51:51 +000018743 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18744 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018746 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018747 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018748 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018749 if (*str == NUL)
18750 match = FALSE; /* empty item at the end */
18751 else
18752 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018753 if (match)
18754 end = regmatch.startp[0];
18755 else
18756 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018757 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18758 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018759 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018760 if (list_append_string(rettv->vval.v_list, str,
18761 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018762 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018763 }
18764 if (!match)
18765 break;
18766 /* Advance to just after the match. */
18767 if (regmatch.endp[0] > str)
18768 col = 0;
18769 else
18770 {
18771 /* Don't get stuck at the same match. */
18772#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018773 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018774#else
18775 col = 1;
18776#endif
18777 }
18778 str = regmatch.endp[0];
18779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780
Bram Moolenaar473de612013-06-08 18:19:48 +020018781 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783
Bram Moolenaar0d660222005-01-07 21:51:51 +000018784 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785}
18786
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018787#ifdef FEAT_FLOAT
18788/*
18789 * "sqrt()" function
18790 */
18791 static void
18792f_sqrt(argvars, rettv)
18793 typval_T *argvars;
18794 typval_T *rettv;
18795{
18796 float_T f;
18797
18798 rettv->v_type = VAR_FLOAT;
18799 if (get_float_arg(argvars, &f) == OK)
18800 rettv->vval.v_float = sqrt(f);
18801 else
18802 rettv->vval.v_float = 0.0;
18803}
18804
18805/*
18806 * "str2float()" function
18807 */
18808 static void
18809f_str2float(argvars, rettv)
18810 typval_T *argvars;
18811 typval_T *rettv;
18812{
18813 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18814
18815 if (*p == '+')
18816 p = skipwhite(p + 1);
18817 (void)string2float(p, &rettv->vval.v_float);
18818 rettv->v_type = VAR_FLOAT;
18819}
18820#endif
18821
Bram Moolenaar2c932302006-03-18 21:42:09 +000018822/*
18823 * "str2nr()" function
18824 */
18825 static void
18826f_str2nr(argvars, rettv)
18827 typval_T *argvars;
18828 typval_T *rettv;
18829{
18830 int base = 10;
18831 char_u *p;
18832 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018833 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018834
18835 if (argvars[1].v_type != VAR_UNKNOWN)
18836 {
18837 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018838 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018839 {
18840 EMSG(_(e_invarg));
18841 return;
18842 }
18843 }
18844
18845 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018846 if (*p == '+')
18847 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018848 switch (base)
18849 {
18850 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18851 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18852 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18853 default: what = 0;
18854 }
18855 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018856 rettv->vval.v_number = n;
18857}
18858
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859#ifdef HAVE_STRFTIME
18860/*
18861 * "strftime({format}[, {time}])" function
18862 */
18863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018865 typval_T *argvars;
18866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867{
18868 char_u result_buf[256];
18869 struct tm *curtime;
18870 time_t seconds;
18871 char_u *p;
18872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018876 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 seconds = time(NULL);
18878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018879 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 curtime = localtime(&seconds);
18881 /* MSVC returns NULL for an invalid value of seconds. */
18882 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 else
18885 {
18886# ifdef FEAT_MBYTE
18887 vimconv_T conv;
18888 char_u *enc;
18889
18890 conv.vc_type = CONV_NONE;
18891 enc = enc_locale();
18892 convert_setup(&conv, p_enc, enc);
18893 if (conv.vc_type != CONV_NONE)
18894 p = string_convert(&conv, p, NULL);
18895# endif
18896 if (p != NULL)
18897 (void)strftime((char *)result_buf, sizeof(result_buf),
18898 (char *)p, curtime);
18899 else
18900 result_buf[0] = NUL;
18901
18902# ifdef FEAT_MBYTE
18903 if (conv.vc_type != CONV_NONE)
18904 vim_free(p);
18905 convert_setup(&conv, enc, p_enc);
18906 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018907 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 else
18909# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018910 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911
18912# ifdef FEAT_MBYTE
18913 /* Release conversion descriptors */
18914 convert_setup(&conv, NULL, NULL);
18915 vim_free(enc);
18916# endif
18917 }
18918}
18919#endif
18920
18921/*
18922 * "stridx()" function
18923 */
18924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018925f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 typval_T *argvars;
18927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928{
18929 char_u buf[NUMBUFLEN];
18930 char_u *needle;
18931 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018932 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018934 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018936 needle = get_tv_string_chk(&argvars[1]);
18937 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018938 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018939 if (needle == NULL || haystack == NULL)
18940 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 if (argvars[2].v_type != VAR_UNKNOWN)
18943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018944 int error = FALSE;
18945
18946 start_idx = get_tv_number_chk(&argvars[2], &error);
18947 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018949 if (start_idx >= 0)
18950 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018951 }
18952
18953 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18954 if (pos != NULL)
18955 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956}
18957
18958/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018959 * "string()" function
18960 */
18961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018962f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018963 typval_T *argvars;
18964 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018965{
18966 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018967 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018969 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018970 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018971 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018972 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018973 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974}
18975
18976/*
18977 * "strlen()" function
18978 */
18979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018981 typval_T *argvars;
18982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984 rettv->vval.v_number = (varnumber_T)(STRLEN(
18985 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986}
18987
18988/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018989 * "strchars()" function
18990 */
18991 static void
18992f_strchars(argvars, rettv)
18993 typval_T *argvars;
18994 typval_T *rettv;
18995{
18996 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018997 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018998#ifdef FEAT_MBYTE
18999 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019000 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019001#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019002
19003 if (argvars[1].v_type != VAR_UNKNOWN)
19004 skipcc = get_tv_number_chk(&argvars[1], NULL);
19005 if (skipcc < 0 || skipcc > 1)
19006 EMSG(_(e_invarg));
19007 else
19008 {
19009#ifdef FEAT_MBYTE
19010 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19011 while (*s != NUL)
19012 {
19013 func_mb_ptr2char_adv(&s);
19014 ++len;
19015 }
19016 rettv->vval.v_number = len;
19017#else
19018 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19019#endif
19020 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019021}
19022
19023/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019024 * "strdisplaywidth()" function
19025 */
19026 static void
19027f_strdisplaywidth(argvars, rettv)
19028 typval_T *argvars;
19029 typval_T *rettv;
19030{
19031 char_u *s = get_tv_string(&argvars[0]);
19032 int col = 0;
19033
19034 if (argvars[1].v_type != VAR_UNKNOWN)
19035 col = get_tv_number(&argvars[1]);
19036
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019037 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019038}
19039
19040/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019041 * "strwidth()" function
19042 */
19043 static void
19044f_strwidth(argvars, rettv)
19045 typval_T *argvars;
19046 typval_T *rettv;
19047{
19048 char_u *s = get_tv_string(&argvars[0]);
19049
19050 rettv->vval.v_number = (varnumber_T)(
19051#ifdef FEAT_MBYTE
19052 mb_string2cells(s, -1)
19053#else
19054 STRLEN(s)
19055#endif
19056 );
19057}
19058
19059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 * "strpart()" function
19061 */
19062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019063f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019064 typval_T *argvars;
19065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066{
19067 char_u *p;
19068 int n;
19069 int len;
19070 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019071 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019073 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 slen = (int)STRLEN(p);
19075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019076 n = get_tv_number_chk(&argvars[1], &error);
19077 if (error)
19078 len = 0;
19079 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019080 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 else
19082 len = slen - n; /* default len: all bytes that are available. */
19083
19084 /*
19085 * Only return the overlap between the specified part and the actual
19086 * string.
19087 */
19088 if (n < 0)
19089 {
19090 len += n;
19091 n = 0;
19092 }
19093 else if (n > slen)
19094 n = slen;
19095 if (len < 0)
19096 len = 0;
19097 else if (n + len > slen)
19098 len = slen - n;
19099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100 rettv->v_type = VAR_STRING;
19101 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102}
19103
19104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019105 * "strridx()" function
19106 */
19107 static void
19108f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019109 typval_T *argvars;
19110 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019111{
19112 char_u buf[NUMBUFLEN];
19113 char_u *needle;
19114 char_u *haystack;
19115 char_u *rest;
19116 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019117 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019119 needle = get_tv_string_chk(&argvars[1]);
19120 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019121
19122 rettv->vval.v_number = -1;
19123 if (needle == NULL || haystack == NULL)
19124 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019125
19126 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019127 if (argvars[2].v_type != VAR_UNKNOWN)
19128 {
19129 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019130 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019131 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019132 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019133 }
19134 else
19135 end_idx = haystack_len;
19136
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019138 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019139 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019140 lastmatch = haystack + end_idx;
19141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019143 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019144 for (rest = haystack; *rest != '\0'; ++rest)
19145 {
19146 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019147 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019148 break;
19149 lastmatch = rest;
19150 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019151 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152
19153 if (lastmatch == NULL)
19154 rettv->vval.v_number = -1;
19155 else
19156 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19157}
19158
19159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 * "strtrans()" function
19161 */
19162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019163f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 typval_T *argvars;
19165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167 rettv->v_type = VAR_STRING;
19168 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169}
19170
19171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019172 * "submatch()" function
19173 */
19174 static void
19175f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019176 typval_T *argvars;
19177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178{
Bram Moolenaar41571762014-04-02 19:00:58 +020019179 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019180 int no;
19181 int retList = 0;
19182
19183 no = (int)get_tv_number_chk(&argvars[0], &error);
19184 if (error)
19185 return;
19186 error = FALSE;
19187 if (argvars[1].v_type != VAR_UNKNOWN)
19188 retList = get_tv_number_chk(&argvars[1], &error);
19189 if (error)
19190 return;
19191
19192 if (retList == 0)
19193 {
19194 rettv->v_type = VAR_STRING;
19195 rettv->vval.v_string = reg_submatch(no);
19196 }
19197 else
19198 {
19199 rettv->v_type = VAR_LIST;
19200 rettv->vval.v_list = reg_submatch_list(no);
19201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019202}
19203
19204/*
19205 * "substitute()" function
19206 */
19207 static void
19208f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019209 typval_T *argvars;
19210 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019211{
19212 char_u patbuf[NUMBUFLEN];
19213 char_u subbuf[NUMBUFLEN];
19214 char_u flagsbuf[NUMBUFLEN];
19215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019216 char_u *str = get_tv_string_chk(&argvars[0]);
19217 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19218 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19219 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19220
Bram Moolenaar0d660222005-01-07 21:51:51 +000019221 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019222 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19223 rettv->vval.v_string = NULL;
19224 else
19225 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019226}
19227
19228/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019229 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019233 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235{
19236 int id = 0;
19237#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019238 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 long col;
19240 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019241 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019243 lnum = get_tv_lnum(argvars); /* -1 on type error */
19244 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19245 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019247 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019248 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019249 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250#endif
19251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253}
19254
19255/*
19256 * "synIDattr(id, what [, mode])" function
19257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019259f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019260 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262{
19263 char_u *p = NULL;
19264#ifdef FEAT_SYN_HL
19265 int id;
19266 char_u *what;
19267 char_u *mode;
19268 char_u modebuf[NUMBUFLEN];
19269 int modec;
19270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271 id = get_tv_number(&argvars[0]);
19272 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019273 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019277 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 modec = 0; /* replace invalid with current */
19279 }
19280 else
19281 {
19282#ifdef FEAT_GUI
19283 if (gui.in_use)
19284 modec = 'g';
19285 else
19286#endif
19287 if (t_colors > 1)
19288 modec = 'c';
19289 else
19290 modec = 't';
19291 }
19292
19293
19294 switch (TOLOWER_ASC(what[0]))
19295 {
19296 case 'b':
19297 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19298 p = highlight_color(id, what, modec);
19299 else /* bold */
19300 p = highlight_has_attr(id, HL_BOLD, modec);
19301 break;
19302
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019303 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 p = highlight_color(id, what, modec);
19305 break;
19306
19307 case 'i':
19308 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19309 p = highlight_has_attr(id, HL_INVERSE, modec);
19310 else /* italic */
19311 p = highlight_has_attr(id, HL_ITALIC, modec);
19312 break;
19313
19314 case 'n': /* name */
19315 p = get_highlight_name(NULL, id - 1);
19316 break;
19317
19318 case 'r': /* reverse */
19319 p = highlight_has_attr(id, HL_INVERSE, modec);
19320 break;
19321
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019322 case 's':
19323 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19324 p = highlight_color(id, what, modec);
19325 else /* standout */
19326 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 break;
19328
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019329 case 'u':
19330 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19331 /* underline */
19332 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19333 else
19334 /* undercurl */
19335 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 break;
19337 }
19338
19339 if (p != NULL)
19340 p = vim_strsave(p);
19341#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019342 rettv->v_type = VAR_STRING;
19343 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344}
19345
19346/*
19347 * "synIDtrans(id)" function
19348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353{
19354 int id;
19355
19356#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019357 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
19359 if (id > 0)
19360 id = syn_get_final_id(id);
19361 else
19362#endif
19363 id = 0;
19364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019365 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366}
19367
19368/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019369 * "synconcealed(lnum, col)" function
19370 */
19371 static void
19372f_synconcealed(argvars, rettv)
19373 typval_T *argvars UNUSED;
19374 typval_T *rettv;
19375{
19376#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19377 long lnum;
19378 long col;
19379 int syntax_flags = 0;
19380 int cchar;
19381 int matchid = 0;
19382 char_u str[NUMBUFLEN];
19383#endif
19384
19385 rettv->v_type = VAR_LIST;
19386 rettv->vval.v_list = NULL;
19387
19388#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19389 lnum = get_tv_lnum(argvars); /* -1 on type error */
19390 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19391
19392 vim_memset(str, NUL, sizeof(str));
19393
19394 if (rettv_list_alloc(rettv) != FAIL)
19395 {
19396 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19397 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19398 && curwin->w_p_cole > 0)
19399 {
19400 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19401 syntax_flags = get_syntax_info(&matchid);
19402
19403 /* get the conceal character */
19404 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19405 {
19406 cchar = syn_get_sub_char();
19407 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19408 cchar = lcs_conceal;
19409 if (cchar != NUL)
19410 {
19411# ifdef FEAT_MBYTE
19412 if (has_mbyte)
19413 (*mb_char2bytes)(cchar, str);
19414 else
19415# endif
19416 str[0] = cchar;
19417 }
19418 }
19419 }
19420
19421 list_append_number(rettv->vval.v_list,
19422 (syntax_flags & HL_CONCEAL) != 0);
19423 /* -1 to auto-determine strlen */
19424 list_append_string(rettv->vval.v_list, str, -1);
19425 list_append_number(rettv->vval.v_list, matchid);
19426 }
19427#endif
19428}
19429
19430/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019431 * "synstack(lnum, col)" function
19432 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019433 static void
19434f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019435 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019436 typval_T *rettv;
19437{
19438#ifdef FEAT_SYN_HL
19439 long lnum;
19440 long col;
19441 int i;
19442 int id;
19443#endif
19444
19445 rettv->v_type = VAR_LIST;
19446 rettv->vval.v_list = NULL;
19447
19448#ifdef FEAT_SYN_HL
19449 lnum = get_tv_lnum(argvars); /* -1 on type error */
19450 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19451
19452 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019453 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019454 && rettv_list_alloc(rettv) != FAIL)
19455 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019456 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019457 for (i = 0; ; ++i)
19458 {
19459 id = syn_get_stack_item(i);
19460 if (id < 0)
19461 break;
19462 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19463 break;
19464 }
19465 }
19466#endif
19467}
19468
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019470get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019471 typval_T *argvars;
19472 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019473 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019475 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019477 char_u *infile = NULL;
19478 char_u buf[NUMBUFLEN];
19479 int err = FALSE;
19480 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019481 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019482 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019484 rettv->v_type = VAR_STRING;
19485 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019486 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019487 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019488
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019489 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019490 {
19491 /*
19492 * Write the string to a temp file, to be used for input of the shell
19493 * command.
19494 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019495 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019496 {
19497 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019498 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019499 }
19500
19501 fd = mch_fopen((char *)infile, WRITEBIN);
19502 if (fd == NULL)
19503 {
19504 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019505 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019506 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019507 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019508 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019509 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19510 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019511 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019512 else
19513 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019514 size_t len;
19515
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019516 p = get_tv_string_buf_chk(&argvars[1], buf);
19517 if (p == NULL)
19518 {
19519 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019520 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019521 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019522 len = STRLEN(p);
19523 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019524 err = TRUE;
19525 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019526 if (fclose(fd) != 0)
19527 err = TRUE;
19528 if (err)
19529 {
19530 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019531 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019532 }
19533 }
19534
Bram Moolenaar52a72462014-08-29 15:53:52 +020019535 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19536 * echoes typeahead, that messes up the display. */
19537 if (!msg_silent)
19538 flags += SHELL_COOKED;
19539
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019540 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019542 int len;
19543 listitem_T *li;
19544 char_u *s = NULL;
19545 char_u *start;
19546 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019547 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548
Bram Moolenaar52a72462014-08-29 15:53:52 +020019549 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019550 if (res == NULL)
19551 goto errret;
19552
19553 list = list_alloc();
19554 if (list == NULL)
19555 goto errret;
19556
19557 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019559 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019560 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019561 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019562 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019563
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019564 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019565 if (s == NULL)
19566 goto errret;
19567
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019568 for (p = s; start < end; ++p, ++start)
19569 *p = *start == NUL ? NL : *start;
19570 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019571
19572 li = listitem_alloc();
19573 if (li == NULL)
19574 {
19575 vim_free(s);
19576 goto errret;
19577 }
19578 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019579 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019580 li->li_tv.vval.v_string = s;
19581 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019583
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019584 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019585 rettv->v_type = VAR_LIST;
19586 rettv->vval.v_list = list;
19587 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019589 else
19590 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019591 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019592#ifdef USE_CR
19593 /* translate <CR> into <NL> */
19594 if (res != NULL)
19595 {
19596 char_u *s;
19597
19598 for (s = res; *s; ++s)
19599 {
19600 if (*s == CAR)
19601 *s = NL;
19602 }
19603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604#else
19605# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019606 /* translate <CR><NL> into <NL> */
19607 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019609 char_u *s, *d;
19610
19611 d = res;
19612 for (s = res; *s; ++s)
19613 {
19614 if (s[0] == CAR && s[1] == NL)
19615 ++s;
19616 *d++ = *s;
19617 }
19618 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620# endif
19621#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019622 rettv->vval.v_string = res;
19623 res = NULL;
19624 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019625
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019626errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019627 if (infile != NULL)
19628 {
19629 mch_remove(infile);
19630 vim_free(infile);
19631 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019632 if (res != NULL)
19633 vim_free(res);
19634 if (list != NULL)
19635 list_free(list, TRUE);
19636}
19637
19638/*
19639 * "system()" function
19640 */
19641 static void
19642f_system(argvars, rettv)
19643 typval_T *argvars;
19644 typval_T *rettv;
19645{
19646 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19647}
19648
19649/*
19650 * "systemlist()" function
19651 */
19652 static void
19653f_systemlist(argvars, rettv)
19654 typval_T *argvars;
19655 typval_T *rettv;
19656{
19657 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658}
19659
19660/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019661 * "tabpagebuflist()" function
19662 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019663 static void
19664f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019665 typval_T *argvars UNUSED;
19666 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019667{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019668#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019669 tabpage_T *tp;
19670 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019671
19672 if (argvars[0].v_type == VAR_UNKNOWN)
19673 wp = firstwin;
19674 else
19675 {
19676 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19677 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019678 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019679 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019680 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019681 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019682 for (; wp != NULL; wp = wp->w_next)
19683 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019684 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019685 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019686 }
19687#endif
19688}
19689
19690
19691/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019692 * "tabpagenr()" function
19693 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019694 static void
19695f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019696 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019697 typval_T *rettv;
19698{
19699 int nr = 1;
19700#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019701 char_u *arg;
19702
19703 if (argvars[0].v_type != VAR_UNKNOWN)
19704 {
19705 arg = get_tv_string_chk(&argvars[0]);
19706 nr = 0;
19707 if (arg != NULL)
19708 {
19709 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019710 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019711 else
19712 EMSG2(_(e_invexpr2), arg);
19713 }
19714 }
19715 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019716 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019717#endif
19718 rettv->vval.v_number = nr;
19719}
19720
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019721
19722#ifdef FEAT_WINDOWS
19723static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19724
19725/*
19726 * Common code for tabpagewinnr() and winnr().
19727 */
19728 static int
19729get_winnr(tp, argvar)
19730 tabpage_T *tp;
19731 typval_T *argvar;
19732{
19733 win_T *twin;
19734 int nr = 1;
19735 win_T *wp;
19736 char_u *arg;
19737
19738 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19739 if (argvar->v_type != VAR_UNKNOWN)
19740 {
19741 arg = get_tv_string_chk(argvar);
19742 if (arg == NULL)
19743 nr = 0; /* type error; errmsg already given */
19744 else if (STRCMP(arg, "$") == 0)
19745 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19746 else if (STRCMP(arg, "#") == 0)
19747 {
19748 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19749 if (twin == NULL)
19750 nr = 0;
19751 }
19752 else
19753 {
19754 EMSG2(_(e_invexpr2), arg);
19755 nr = 0;
19756 }
19757 }
19758
19759 if (nr > 0)
19760 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19761 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019762 {
19763 if (wp == NULL)
19764 {
19765 /* didn't find it in this tabpage */
19766 nr = 0;
19767 break;
19768 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019769 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019770 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019771 return nr;
19772}
19773#endif
19774
19775/*
19776 * "tabpagewinnr()" function
19777 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019778 static void
19779f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019780 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019781 typval_T *rettv;
19782{
19783 int nr = 1;
19784#ifdef FEAT_WINDOWS
19785 tabpage_T *tp;
19786
19787 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19788 if (tp == NULL)
19789 nr = 0;
19790 else
19791 nr = get_winnr(tp, &argvars[1]);
19792#endif
19793 rettv->vval.v_number = nr;
19794}
19795
19796
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019797/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019798 * "tagfiles()" function
19799 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019800 static void
19801f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019802 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019803 typval_T *rettv;
19804{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019805 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019806 tagname_T tn;
19807 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019808
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019809 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019810 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019811 fname = alloc(MAXPATHL);
19812 if (fname == NULL)
19813 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019814
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019815 for (first = TRUE; ; first = FALSE)
19816 if (get_tagfname(&tn, first, fname) == FAIL
19817 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019818 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019819 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019820 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019821}
19822
19823/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019824 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019825 */
19826 static void
19827f_taglist(argvars, rettv)
19828 typval_T *argvars;
19829 typval_T *rettv;
19830{
19831 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019832
19833 tag_pattern = get_tv_string(&argvars[0]);
19834
19835 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019836 if (*tag_pattern == NUL)
19837 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019838
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019839 if (rettv_list_alloc(rettv) == OK)
19840 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019841}
19842
19843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 * "tempname()" function
19845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019847f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019848 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850{
19851 static int x = 'A';
19852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019854 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855
19856 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19857 * names. Skip 'I' and 'O', they are used for shell redirection. */
19858 do
19859 {
19860 if (x == 'Z')
19861 x = '0';
19862 else if (x == '9')
19863 x = 'A';
19864 else
19865 {
19866#ifdef EBCDIC
19867 if (x == 'I')
19868 x = 'J';
19869 else if (x == 'R')
19870 x = 'S';
19871 else
19872#endif
19873 ++x;
19874 }
19875 } while (x == 'I' || x == 'O');
19876}
19877
19878/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019879 * "test(list)" function: Just checking the walls...
19880 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019881 static void
19882f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019883 typval_T *argvars UNUSED;
19884 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019885{
19886 /* Used for unit testing. Change the code below to your liking. */
19887#if 0
19888 listitem_T *li;
19889 list_T *l;
19890 char_u *bad, *good;
19891
19892 if (argvars[0].v_type != VAR_LIST)
19893 return;
19894 l = argvars[0].vval.v_list;
19895 if (l == NULL)
19896 return;
19897 li = l->lv_first;
19898 if (li == NULL)
19899 return;
19900 bad = get_tv_string(&li->li_tv);
19901 li = li->li_next;
19902 if (li == NULL)
19903 return;
19904 good = get_tv_string(&li->li_tv);
19905 rettv->vval.v_number = test_edit_score(bad, good);
19906#endif
19907}
19908
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019909#ifdef FEAT_FLOAT
19910/*
19911 * "tan()" function
19912 */
19913 static void
19914f_tan(argvars, rettv)
19915 typval_T *argvars;
19916 typval_T *rettv;
19917{
19918 float_T f;
19919
19920 rettv->v_type = VAR_FLOAT;
19921 if (get_float_arg(argvars, &f) == OK)
19922 rettv->vval.v_float = tan(f);
19923 else
19924 rettv->vval.v_float = 0.0;
19925}
19926
19927/*
19928 * "tanh()" function
19929 */
19930 static void
19931f_tanh(argvars, rettv)
19932 typval_T *argvars;
19933 typval_T *rettv;
19934{
19935 float_T f;
19936
19937 rettv->v_type = VAR_FLOAT;
19938 if (get_float_arg(argvars, &f) == OK)
19939 rettv->vval.v_float = tanh(f);
19940 else
19941 rettv->vval.v_float = 0.0;
19942}
19943#endif
19944
Bram Moolenaard52d9742005-08-21 22:20:28 +000019945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 * "tolower(string)" function
19947 */
19948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019950 typval_T *argvars;
19951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952{
19953 char_u *p;
19954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955 p = vim_strsave(get_tv_string(&argvars[0]));
19956 rettv->v_type = VAR_STRING;
19957 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958
19959 if (p != NULL)
19960 while (*p != NUL)
19961 {
19962#ifdef FEAT_MBYTE
19963 int l;
19964
19965 if (enc_utf8)
19966 {
19967 int c, lc;
19968
19969 c = utf_ptr2char(p);
19970 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019971 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 /* TODO: reallocate string when byte count changes. */
19973 if (utf_char2len(lc) == l)
19974 utf_char2bytes(lc, p);
19975 p += l;
19976 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019977 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 p += l; /* skip multi-byte character */
19979 else
19980#endif
19981 {
19982 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19983 ++p;
19984 }
19985 }
19986}
19987
19988/*
19989 * "toupper(string)" function
19990 */
19991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019992f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019993 typval_T *argvars;
19994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019997 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998}
19999
20000/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020001 * "tr(string, fromstr, tostr)" function
20002 */
20003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020005 typval_T *argvars;
20006 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020007{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020008 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020009 char_u *fromstr;
20010 char_u *tostr;
20011 char_u *p;
20012#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020013 int inlen;
20014 int fromlen;
20015 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020016 int idx;
20017 char_u *cpstr;
20018 int cplen;
20019 int first = TRUE;
20020#endif
20021 char_u buf[NUMBUFLEN];
20022 char_u buf2[NUMBUFLEN];
20023 garray_T ga;
20024
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020025 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020026 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20027 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020028
20029 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020030 rettv->v_type = VAR_STRING;
20031 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020032 if (fromstr == NULL || tostr == NULL)
20033 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020034 ga_init2(&ga, (int)sizeof(char), 80);
20035
20036#ifdef FEAT_MBYTE
20037 if (!has_mbyte)
20038#endif
20039 /* not multi-byte: fromstr and tostr must be the same length */
20040 if (STRLEN(fromstr) != STRLEN(tostr))
20041 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020042#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020043error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020044#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020045 EMSG2(_(e_invarg2), fromstr);
20046 ga_clear(&ga);
20047 return;
20048 }
20049
20050 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020051 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020052 {
20053#ifdef FEAT_MBYTE
20054 if (has_mbyte)
20055 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020056 inlen = (*mb_ptr2len)(in_str);
20057 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020058 cplen = inlen;
20059 idx = 0;
20060 for (p = fromstr; *p != NUL; p += fromlen)
20061 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020062 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020063 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020064 {
20065 for (p = tostr; *p != NUL; p += tolen)
20066 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020067 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020068 if (idx-- == 0)
20069 {
20070 cplen = tolen;
20071 cpstr = p;
20072 break;
20073 }
20074 }
20075 if (*p == NUL) /* tostr is shorter than fromstr */
20076 goto error;
20077 break;
20078 }
20079 ++idx;
20080 }
20081
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020082 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020083 {
20084 /* Check that fromstr and tostr have the same number of
20085 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020086 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020087 first = FALSE;
20088 for (p = tostr; *p != NUL; p += tolen)
20089 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020090 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020091 --idx;
20092 }
20093 if (idx != 0)
20094 goto error;
20095 }
20096
Bram Moolenaarcde88542015-08-11 19:14:00 +020020097 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020098 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020099 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020100
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020101 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020102 }
20103 else
20104#endif
20105 {
20106 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020107 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020108 if (p != NULL)
20109 ga_append(&ga, tostr[p - fromstr]);
20110 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020111 ga_append(&ga, *in_str);
20112 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020113 }
20114 }
20115
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020116 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020117 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020118 ga_append(&ga, NUL);
20119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020120 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020121}
20122
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020123#ifdef FEAT_FLOAT
20124/*
20125 * "trunc({float})" function
20126 */
20127 static void
20128f_trunc(argvars, rettv)
20129 typval_T *argvars;
20130 typval_T *rettv;
20131{
20132 float_T f;
20133
20134 rettv->v_type = VAR_FLOAT;
20135 if (get_float_arg(argvars, &f) == OK)
20136 /* trunc() is not in C90, use floor() or ceil() instead. */
20137 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20138 else
20139 rettv->vval.v_float = 0.0;
20140}
20141#endif
20142
Bram Moolenaar8299df92004-07-10 09:47:34 +000020143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 * "type(expr)" function
20145 */
20146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020147f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 typval_T *argvars;
20149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020151 int n;
20152
20153 switch (argvars[0].v_type)
20154 {
20155 case VAR_NUMBER: n = 0; break;
20156 case VAR_STRING: n = 1; break;
20157 case VAR_FUNC: n = 2; break;
20158 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020159 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020160#ifdef FEAT_FLOAT
20161 case VAR_FLOAT: n = 5; break;
20162#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020163 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20164 }
20165 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166}
20167
20168/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020169 * "undofile(name)" function
20170 */
20171 static void
20172f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020173 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020174 typval_T *rettv;
20175{
20176 rettv->v_type = VAR_STRING;
20177#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020178 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020179 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020180
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020181 if (*fname == NUL)
20182 {
20183 /* If there is no file name there will be no undo file. */
20184 rettv->vval.v_string = NULL;
20185 }
20186 else
20187 {
20188 char_u *ffname = FullName_save(fname, FALSE);
20189
20190 if (ffname != NULL)
20191 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20192 vim_free(ffname);
20193 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020194 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020195#else
20196 rettv->vval.v_string = NULL;
20197#endif
20198}
20199
20200/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020201 * "undotree()" function
20202 */
20203 static void
20204f_undotree(argvars, rettv)
20205 typval_T *argvars UNUSED;
20206 typval_T *rettv;
20207{
20208 if (rettv_dict_alloc(rettv) == OK)
20209 {
20210 dict_T *dict = rettv->vval.v_dict;
20211 list_T *list;
20212
Bram Moolenaar730cde92010-06-27 05:18:54 +020020213 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020214 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020215 dict_add_nr_str(dict, "save_last",
20216 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020217 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20218 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020219 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020220
20221 list = list_alloc();
20222 if (list != NULL)
20223 {
20224 u_eval_tree(curbuf->b_u_oldhead, list);
20225 dict_add_list(dict, "entries", list);
20226 }
20227 }
20228}
20229
20230/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020231 * "values(dict)" function
20232 */
20233 static void
20234f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 typval_T *argvars;
20236 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020237{
20238 dict_list(argvars, rettv, 1);
20239}
20240
20241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 * "virtcol(string)" function
20243 */
20244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020245f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020246 typval_T *argvars;
20247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248{
20249 colnr_T vcol = 0;
20250 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020251 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020253 fp = var2fpos(&argvars[0], FALSE, &fnum);
20254 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20255 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 {
20257 getvvcol(curwin, fp, NULL, NULL, &vcol);
20258 ++vcol;
20259 }
20260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020261 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262}
20263
20264/*
20265 * "visualmode()" function
20266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020268f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020269 typval_T *argvars UNUSED;
20270 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 char_u str[2];
20273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020274 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 str[0] = curbuf->b_visual_mode_eval;
20276 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020277 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278
20279 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020280 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282}
20283
20284/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020285 * "wildmenumode()" function
20286 */
20287 static void
20288f_wildmenumode(argvars, rettv)
20289 typval_T *argvars UNUSED;
20290 typval_T *rettv UNUSED;
20291{
20292#ifdef FEAT_WILDMENU
20293 if (wild_menu_showing)
20294 rettv->vval.v_number = 1;
20295#endif
20296}
20297
20298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 * "winbufnr(nr)" function
20300 */
20301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020302f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 typval_T *argvars;
20304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305{
20306 win_T *wp;
20307
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020308 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020312 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313}
20314
20315/*
20316 * "wincol()" function
20317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020319f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322{
20323 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020324 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325}
20326
20327/*
20328 * "winheight(nr)" function
20329 */
20330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020331f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020332 typval_T *argvars;
20333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334{
20335 win_T *wp;
20336
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020337 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020341 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342}
20343
20344/*
20345 * "winline()" function
20346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020348f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020349 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351{
20352 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020353 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354}
20355
20356/*
20357 * "winnr()" function
20358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020360f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020361 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363{
20364 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020365
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020367 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020369 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370}
20371
20372/*
20373 * "winrestcmd()" function
20374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020376f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379{
20380#ifdef FEAT_WINDOWS
20381 win_T *wp;
20382 int winnr = 1;
20383 garray_T ga;
20384 char_u buf[50];
20385
20386 ga_init2(&ga, (int)sizeof(char), 70);
20387 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20388 {
20389 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20390 ga_concat(&ga, buf);
20391# ifdef FEAT_VERTSPLIT
20392 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20393 ga_concat(&ga, buf);
20394# endif
20395 ++winnr;
20396 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020397 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020399 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020401 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020403 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404}
20405
20406/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020407 * "winrestview()" function
20408 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020409 static void
20410f_winrestview(argvars, rettv)
20411 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020412 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020413{
20414 dict_T *dict;
20415
20416 if (argvars[0].v_type != VAR_DICT
20417 || (dict = argvars[0].vval.v_dict) == NULL)
20418 EMSG(_(e_invarg));
20419 else
20420 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020421 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20422 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20423 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20424 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020425#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020426 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20427 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020428#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020429 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20430 {
20431 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20432 curwin->w_set_curswant = FALSE;
20433 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020434
Bram Moolenaar82c25852014-05-28 16:47:16 +020020435 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20436 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020437#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020438 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20439 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020440#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020441 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20442 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20443 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20444 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020445
20446 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020447 win_new_height(curwin, curwin->w_height);
20448# ifdef FEAT_VERTSPLIT
20449 win_new_width(curwin, W_WIDTH(curwin));
20450# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020451 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020452
Bram Moolenaarb851a962014-10-31 15:45:52 +010020453 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020454 curwin->w_topline = 1;
20455 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20456 curwin->w_topline = curbuf->b_ml.ml_line_count;
20457#ifdef FEAT_DIFF
20458 check_topfill(curwin, TRUE);
20459#endif
20460 }
20461}
20462
20463/*
20464 * "winsaveview()" function
20465 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020466 static void
20467f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020468 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020469 typval_T *rettv;
20470{
20471 dict_T *dict;
20472
Bram Moolenaara800b422010-06-27 01:15:55 +020020473 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020474 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020475 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020476
20477 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20478 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20479#ifdef FEAT_VIRTUALEDIT
20480 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20481#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020482 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020483 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20484
20485 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20486#ifdef FEAT_DIFF
20487 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20488#endif
20489 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20490 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20491}
20492
20493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 * "winwidth(nr)" function
20495 */
20496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020497f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020498 typval_T *argvars;
20499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500{
20501 win_T *wp;
20502
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020503 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020505 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 else
20507#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020508 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020510 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511#endif
20512}
20513
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020515 * "wordcount()" function
20516 */
20517 static void
20518f_wordcount(argvars, rettv)
20519 typval_T *argvars UNUSED;
20520 typval_T *rettv;
20521{
20522 if (rettv_dict_alloc(rettv) == FAIL)
20523 return;
20524 cursor_pos_info(rettv->vval.v_dict);
20525}
20526
20527/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020528 * Write list of strings to file
20529 */
20530 static int
20531write_list(fd, list, binary)
20532 FILE *fd;
20533 list_T *list;
20534 int binary;
20535{
20536 listitem_T *li;
20537 int c;
20538 int ret = OK;
20539 char_u *s;
20540
20541 for (li = list->lv_first; li != NULL; li = li->li_next)
20542 {
20543 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20544 {
20545 if (*s == '\n')
20546 c = putc(NUL, fd);
20547 else
20548 c = putc(*s, fd);
20549 if (c == EOF)
20550 {
20551 ret = FAIL;
20552 break;
20553 }
20554 }
20555 if (!binary || li->li_next != NULL)
20556 if (putc('\n', fd) == EOF)
20557 {
20558 ret = FAIL;
20559 break;
20560 }
20561 if (ret == FAIL)
20562 {
20563 EMSG(_(e_write));
20564 break;
20565 }
20566 }
20567 return ret;
20568}
20569
20570/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020571 * "writefile()" function
20572 */
20573 static void
20574f_writefile(argvars, rettv)
20575 typval_T *argvars;
20576 typval_T *rettv;
20577{
20578 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020579 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020580 char_u *fname;
20581 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020582 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020583
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020584 if (check_restricted() || check_secure())
20585 return;
20586
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020587 if (argvars[0].v_type != VAR_LIST)
20588 {
20589 EMSG2(_(e_listarg), "writefile()");
20590 return;
20591 }
20592 if (argvars[0].vval.v_list == NULL)
20593 return;
20594
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020595 if (argvars[2].v_type != VAR_UNKNOWN)
20596 {
20597 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20598 binary = TRUE;
20599 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20600 append = TRUE;
20601 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020602
20603 /* Always open the file in binary mode, library functions have a mind of
20604 * their own about CR-LF conversion. */
20605 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020606 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20607 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020608 {
20609 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20610 ret = -1;
20611 }
20612 else
20613 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020614 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20615 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020616 fclose(fd);
20617 }
20618
20619 rettv->vval.v_number = ret;
20620}
20621
20622/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020623 * "xor(expr, expr)" function
20624 */
20625 static void
20626f_xor(argvars, rettv)
20627 typval_T *argvars;
20628 typval_T *rettv;
20629{
20630 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20631 ^ get_tv_number_chk(&argvars[1], NULL);
20632}
20633
20634
20635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020637 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 */
20639 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020640var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020641 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020642 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020643 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020645 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020647 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648
Bram Moolenaara5525202006-03-02 22:52:09 +000020649 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020650 if (varp->v_type == VAR_LIST)
20651 {
20652 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020653 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020654 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020655 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020656
20657 l = varp->vval.v_list;
20658 if (l == NULL)
20659 return NULL;
20660
20661 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020662 pos.lnum = list_find_nr(l, 0L, &error);
20663 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020664 return NULL; /* invalid line number */
20665
20666 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020667 pos.col = list_find_nr(l, 1L, &error);
20668 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020669 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020670 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020671
20672 /* We accept "$" for the column number: last column. */
20673 li = list_find(l, 1L);
20674 if (li != NULL && li->li_tv.v_type == VAR_STRING
20675 && li->li_tv.vval.v_string != NULL
20676 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20677 pos.col = len + 1;
20678
Bram Moolenaara5525202006-03-02 22:52:09 +000020679 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020680 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020681 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020682 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020683
Bram Moolenaara5525202006-03-02 22:52:09 +000020684#ifdef FEAT_VIRTUALEDIT
20685 /* Get the virtual offset. Defaults to zero. */
20686 pos.coladd = list_find_nr(l, 2L, &error);
20687 if (error)
20688 pos.coladd = 0;
20689#endif
20690
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020691 return &pos;
20692 }
20693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020694 name = get_tv_string_chk(varp);
20695 if (name == NULL)
20696 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020697 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020699 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20700 {
20701 if (VIsual_active)
20702 return &VIsual;
20703 return &curwin->w_cursor;
20704 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020705 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020707 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20709 return NULL;
20710 return pp;
20711 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020712
20713#ifdef FEAT_VIRTUALEDIT
20714 pos.coladd = 0;
20715#endif
20716
Bram Moolenaar477933c2007-07-17 14:32:23 +000020717 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020718 {
20719 pos.col = 0;
20720 if (name[1] == '0') /* "w0": first visible line */
20721 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020722 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020723 pos.lnum = curwin->w_topline;
20724 return &pos;
20725 }
20726 else if (name[1] == '$') /* "w$": last visible line */
20727 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020728 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020729 pos.lnum = curwin->w_botline - 1;
20730 return &pos;
20731 }
20732 }
20733 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020735 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 {
20737 pos.lnum = curbuf->b_ml.ml_line_count;
20738 pos.col = 0;
20739 }
20740 else
20741 {
20742 pos.lnum = curwin->w_cursor.lnum;
20743 pos.col = (colnr_T)STRLEN(ml_get_curline());
20744 }
20745 return &pos;
20746 }
20747 return NULL;
20748}
20749
20750/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020751 * Convert list in "arg" into a position and optional file number.
20752 * When "fnump" is NULL there is no file number, only 3 items.
20753 * Note that the column is passed on as-is, the caller may want to decrement
20754 * it to use 1 for the first column.
20755 * Return FAIL when conversion is not possible, doesn't check the position for
20756 * validity.
20757 */
20758 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020759list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020760 typval_T *arg;
20761 pos_T *posp;
20762 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020763 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020764{
20765 list_T *l = arg->vval.v_list;
20766 long i = 0;
20767 long n;
20768
Bram Moolenaar493c1782014-05-28 14:34:46 +020020769 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20770 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020771 if (arg->v_type != VAR_LIST
20772 || l == NULL
20773 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020774 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020775 return FAIL;
20776
20777 if (fnump != NULL)
20778 {
20779 n = list_find_nr(l, i++, NULL); /* fnum */
20780 if (n < 0)
20781 return FAIL;
20782 if (n == 0)
20783 n = curbuf->b_fnum; /* current buffer */
20784 *fnump = n;
20785 }
20786
20787 n = list_find_nr(l, i++, NULL); /* lnum */
20788 if (n < 0)
20789 return FAIL;
20790 posp->lnum = n;
20791
20792 n = list_find_nr(l, i++, NULL); /* col */
20793 if (n < 0)
20794 return FAIL;
20795 posp->col = n;
20796
20797#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020798 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020799 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020800 posp->coladd = 0;
20801 else
20802 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020803#endif
20804
Bram Moolenaar493c1782014-05-28 14:34:46 +020020805 if (curswantp != NULL)
20806 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20807
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020808 return OK;
20809}
20810
20811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 * Get the length of an environment variable name.
20813 * Advance "arg" to the first character after the name.
20814 * Return 0 for error.
20815 */
20816 static int
20817get_env_len(arg)
20818 char_u **arg;
20819{
20820 char_u *p;
20821 int len;
20822
20823 for (p = *arg; vim_isIDc(*p); ++p)
20824 ;
20825 if (p == *arg) /* no name found */
20826 return 0;
20827
20828 len = (int)(p - *arg);
20829 *arg = p;
20830 return len;
20831}
20832
20833/*
20834 * Get the length of the name of a function or internal variable.
20835 * "arg" is advanced to the first non-white character after the name.
20836 * Return 0 if something is wrong.
20837 */
20838 static int
20839get_id_len(arg)
20840 char_u **arg;
20841{
20842 char_u *p;
20843 int len;
20844
20845 /* Find the end of the name. */
20846 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020847 {
20848 if (*p == ':')
20849 {
20850 /* "s:" is start of "s:var", but "n:" is not and can be used in
20851 * slice "[n:]". Also "xx:" is not a namespace. */
20852 len = (int)(p - *arg);
20853 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20854 || len > 1)
20855 break;
20856 }
20857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 if (p == *arg) /* no name found */
20859 return 0;
20860
20861 len = (int)(p - *arg);
20862 *arg = skipwhite(p);
20863
20864 return len;
20865}
20866
20867/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020868 * Get the length of the name of a variable or function.
20869 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020871 * Return -1 if curly braces expansion failed.
20872 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 * If the name contains 'magic' {}'s, expand them and return the
20874 * expanded name in an allocated string via 'alias' - caller must free.
20875 */
20876 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020877get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 char_u **arg;
20879 char_u **alias;
20880 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020881 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882{
20883 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884 char_u *p;
20885 char_u *expr_start;
20886 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887
20888 *alias = NULL; /* default to no alias */
20889
20890 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20891 && (*arg)[2] == (int)KE_SNR)
20892 {
20893 /* hard coded <SNR>, already translated */
20894 *arg += 3;
20895 return get_id_len(arg) + 3;
20896 }
20897 len = eval_fname_script(*arg);
20898 if (len > 0)
20899 {
20900 /* literal "<SID>", "s:" or "<SNR>" */
20901 *arg += len;
20902 }
20903
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020905 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020907 p = find_name_end(*arg, &expr_start, &expr_end,
20908 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 if (expr_start != NULL)
20910 {
20911 char_u *temp_string;
20912
20913 if (!evaluate)
20914 {
20915 len += (int)(p - *arg);
20916 *arg = skipwhite(p);
20917 return len;
20918 }
20919
20920 /*
20921 * Include any <SID> etc in the expanded string:
20922 * Thus the -len here.
20923 */
20924 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20925 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020926 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 *alias = temp_string;
20928 *arg = skipwhite(p);
20929 return (int)STRLEN(temp_string);
20930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931
20932 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020933 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 EMSG2(_(e_invexpr2), *arg);
20935
20936 return len;
20937}
20938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020939/*
20940 * Find the end of a variable or function name, taking care of magic braces.
20941 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20942 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020943 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 * Return a pointer to just after the name. Equal to "arg" if there is no
20945 * valid name.
20946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020948find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 char_u *arg;
20950 char_u **expr_start;
20951 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020952 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020954 int mb_nest = 0;
20955 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020957 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020959 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020961 *expr_start = NULL;
20962 *expr_end = NULL;
20963 }
20964
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020965 /* Quick check for valid starting character. */
20966 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20967 return arg;
20968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020969 for (p = arg; *p != NUL
20970 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020971 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020972 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020973 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020974 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020975 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020976 if (*p == '\'')
20977 {
20978 /* skip over 'string' to avoid counting [ and ] inside it. */
20979 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20980 ;
20981 if (*p == NUL)
20982 break;
20983 }
20984 else if (*p == '"')
20985 {
20986 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20987 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20988 if (*p == '\\' && p[1] != NUL)
20989 ++p;
20990 if (*p == NUL)
20991 break;
20992 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020993 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20994 {
20995 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020996 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020997 len = (int)(p - arg);
20998 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020999 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021000 break;
21001 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021003 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021005 if (*p == '[')
21006 ++br_nest;
21007 else if (*p == ']')
21008 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021011 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021013 if (*p == '{')
21014 {
21015 mb_nest++;
21016 if (expr_start != NULL && *expr_start == NULL)
21017 *expr_start = p;
21018 }
21019 else if (*p == '}')
21020 {
21021 mb_nest--;
21022 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21023 *expr_end = p;
21024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 }
21027
21028 return p;
21029}
21030
21031/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021032 * Expands out the 'magic' {}'s in a variable/function name.
21033 * Note that this can call itself recursively, to deal with
21034 * constructs like foo{bar}{baz}{bam}
21035 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21036 * "in_start" ^
21037 * "expr_start" ^
21038 * "expr_end" ^
21039 * "in_end" ^
21040 *
21041 * Returns a new allocated string, which the caller must free.
21042 * Returns NULL for failure.
21043 */
21044 static char_u *
21045make_expanded_name(in_start, expr_start, expr_end, in_end)
21046 char_u *in_start;
21047 char_u *expr_start;
21048 char_u *expr_end;
21049 char_u *in_end;
21050{
21051 char_u c1;
21052 char_u *retval = NULL;
21053 char_u *temp_result;
21054 char_u *nextcmd = NULL;
21055
21056 if (expr_end == NULL || in_end == NULL)
21057 return NULL;
21058 *expr_start = NUL;
21059 *expr_end = NUL;
21060 c1 = *in_end;
21061 *in_end = NUL;
21062
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021063 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021064 if (temp_result != NULL && nextcmd == NULL)
21065 {
21066 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21067 + (in_end - expr_end) + 1));
21068 if (retval != NULL)
21069 {
21070 STRCPY(retval, in_start);
21071 STRCAT(retval, temp_result);
21072 STRCAT(retval, expr_end + 1);
21073 }
21074 }
21075 vim_free(temp_result);
21076
21077 *in_end = c1; /* put char back for error messages */
21078 *expr_start = '{';
21079 *expr_end = '}';
21080
21081 if (retval != NULL)
21082 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021083 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021084 if (expr_start != NULL)
21085 {
21086 /* Further expansion! */
21087 temp_result = make_expanded_name(retval, expr_start,
21088 expr_end, temp_result);
21089 vim_free(retval);
21090 retval = temp_result;
21091 }
21092 }
21093
21094 return retval;
21095}
21096
21097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021099 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 */
21101 static int
21102eval_isnamec(c)
21103 int c;
21104{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021105 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21106}
21107
21108/*
21109 * Return TRUE if character "c" can be used as the first character in a
21110 * variable or function name (excluding '{' and '}').
21111 */
21112 static int
21113eval_isnamec1(c)
21114 int c;
21115{
21116 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117}
21118
21119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 * Set number v: variable to "val".
21121 */
21122 void
21123set_vim_var_nr(idx, val)
21124 int idx;
21125 long val;
21126{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021127 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128}
21129
21130/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021131 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 */
21133 long
21134get_vim_var_nr(idx)
21135 int idx;
21136{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021137 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138}
21139
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021140/*
21141 * Get string v: variable value. Uses a static buffer, can only be used once.
21142 */
21143 char_u *
21144get_vim_var_str(idx)
21145 int idx;
21146{
21147 return get_tv_string(&vimvars[idx].vv_tv);
21148}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021149
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021151 * Get List v: variable value. Caller must take care of reference count when
21152 * needed.
21153 */
21154 list_T *
21155get_vim_var_list(idx)
21156 int idx;
21157{
21158 return vimvars[idx].vv_list;
21159}
21160
21161/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021162 * Set v:char to character "c".
21163 */
21164 void
21165set_vim_var_char(c)
21166 int c;
21167{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021168 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021169
21170#ifdef FEAT_MBYTE
21171 if (has_mbyte)
21172 buf[(*mb_char2bytes)(c, buf)] = NUL;
21173 else
21174#endif
21175 {
21176 buf[0] = c;
21177 buf[1] = NUL;
21178 }
21179 set_vim_var_string(VV_CHAR, buf, -1);
21180}
21181
21182/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021183 * Set v:count to "count" and v:count1 to "count1".
21184 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 */
21186 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021187set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 long count;
21189 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021190 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021192 if (set_prevcount)
21193 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021194 vimvars[VV_COUNT].vv_nr = count;
21195 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196}
21197
21198/*
21199 * Set string v: variable to a copy of "val".
21200 */
21201 void
21202set_vim_var_string(idx, val, len)
21203 int idx;
21204 char_u *val;
21205 int len; /* length of "val" to use or -1 (whole string) */
21206{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021207 /* Need to do this (at least) once, since we can't initialize a union.
21208 * Will always be invoked when "v:progname" is set. */
21209 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21210
Bram Moolenaare9a41262005-01-15 22:18:47 +000021211 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021213 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021215 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021217 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218}
21219
21220/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021221 * Set List v: variable to "val".
21222 */
21223 void
21224set_vim_var_list(idx, val)
21225 int idx;
21226 list_T *val;
21227{
21228 list_unref(vimvars[idx].vv_list);
21229 vimvars[idx].vv_list = val;
21230 if (val != NULL)
21231 ++val->lv_refcount;
21232}
21233
21234/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021235 * Set Dictionary v: variable to "val".
21236 */
21237 void
21238set_vim_var_dict(idx, val)
21239 int idx;
21240 dict_T *val;
21241{
21242 int todo;
21243 hashitem_T *hi;
21244
21245 dict_unref(vimvars[idx].vv_dict);
21246 vimvars[idx].vv_dict = val;
21247 if (val != NULL)
21248 {
21249 ++val->dv_refcount;
21250
21251 /* Set readonly */
21252 todo = (int)val->dv_hashtab.ht_used;
21253 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21254 {
21255 if (HASHITEM_EMPTY(hi))
21256 continue;
21257 --todo;
21258 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21259 }
21260 }
21261}
21262
21263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 * Set v:register if needed.
21265 */
21266 void
21267set_reg_var(c)
21268 int c;
21269{
21270 char_u regname;
21271
21272 if (c == 0 || c == ' ')
21273 regname = '"';
21274 else
21275 regname = c;
21276 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021277 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 set_vim_var_string(VV_REG, &regname, 1);
21279}
21280
21281/*
21282 * Get or set v:exception. If "oldval" == NULL, return the current value.
21283 * Otherwise, restore the value to "oldval" and return NULL.
21284 * Must always be called in pairs to save and restore v:exception! Does not
21285 * take care of memory allocations.
21286 */
21287 char_u *
21288v_exception(oldval)
21289 char_u *oldval;
21290{
21291 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021292 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293
Bram Moolenaare9a41262005-01-15 22:18:47 +000021294 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 return NULL;
21296}
21297
21298/*
21299 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21300 * Otherwise, restore the value to "oldval" and return NULL.
21301 * Must always be called in pairs to save and restore v:throwpoint! Does not
21302 * take care of memory allocations.
21303 */
21304 char_u *
21305v_throwpoint(oldval)
21306 char_u *oldval;
21307{
21308 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021309 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310
Bram Moolenaare9a41262005-01-15 22:18:47 +000021311 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 return NULL;
21313}
21314
21315#if defined(FEAT_AUTOCMD) || defined(PROTO)
21316/*
21317 * Set v:cmdarg.
21318 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21319 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21320 * Must always be called in pairs!
21321 */
21322 char_u *
21323set_cmdarg(eap, oldarg)
21324 exarg_T *eap;
21325 char_u *oldarg;
21326{
21327 char_u *oldval;
21328 char_u *newval;
21329 unsigned len;
21330
Bram Moolenaare9a41262005-01-15 22:18:47 +000021331 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021332 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021334 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021335 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021336 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 }
21338
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021339 if (eap->force_bin == FORCE_BIN)
21340 len = 6;
21341 else if (eap->force_bin == FORCE_NOBIN)
21342 len = 8;
21343 else
21344 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021345
21346 if (eap->read_edit)
21347 len += 7;
21348
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021349 if (eap->force_ff != 0)
21350 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21351# ifdef FEAT_MBYTE
21352 if (eap->force_enc != 0)
21353 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021354 if (eap->bad_char != 0)
21355 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021356# endif
21357
21358 newval = alloc(len + 1);
21359 if (newval == NULL)
21360 return NULL;
21361
21362 if (eap->force_bin == FORCE_BIN)
21363 sprintf((char *)newval, " ++bin");
21364 else if (eap->force_bin == FORCE_NOBIN)
21365 sprintf((char *)newval, " ++nobin");
21366 else
21367 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021368
21369 if (eap->read_edit)
21370 STRCAT(newval, " ++edit");
21371
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021372 if (eap->force_ff != 0)
21373 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21374 eap->cmd + eap->force_ff);
21375# ifdef FEAT_MBYTE
21376 if (eap->force_enc != 0)
21377 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21378 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021379 if (eap->bad_char == BAD_KEEP)
21380 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21381 else if (eap->bad_char == BAD_DROP)
21382 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21383 else if (eap->bad_char != 0)
21384 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021385# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021386 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021387 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388}
21389#endif
21390
21391/*
21392 * Get the value of internal variable "name".
21393 * Return OK or FAIL.
21394 */
21395 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021396get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 char_u *name;
21398 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021399 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021400 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021401 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021402 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403{
21404 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021405 typval_T *tv = NULL;
21406 typval_T atv;
21407 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409
21410 /* truncate the name, so that we can use strcmp() */
21411 cc = name[len];
21412 name[len] = NUL;
21413
21414 /*
21415 * Check for "b:changedtick".
21416 */
21417 if (STRCMP(name, "b:changedtick") == 0)
21418 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021419 atv.v_type = VAR_NUMBER;
21420 atv.vval.v_number = curbuf->b_changedtick;
21421 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 }
21423
21424 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 * Check for user-defined variables.
21426 */
21427 else
21428 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021429 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021431 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021432 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021433 if (dip != NULL)
21434 *dip = v;
21435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436 }
21437
Bram Moolenaare9a41262005-01-15 22:18:47 +000021438 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021440 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021441 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 ret = FAIL;
21443 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021444 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021445 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446
21447 name[len] = cc;
21448
21449 return ret;
21450}
21451
21452/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021453 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21454 * Also handle function call with Funcref variable: func(expr)
21455 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21456 */
21457 static int
21458handle_subscript(arg, rettv, evaluate, verbose)
21459 char_u **arg;
21460 typval_T *rettv;
21461 int evaluate; /* do more than finding the end */
21462 int verbose; /* give error messages */
21463{
21464 int ret = OK;
21465 dict_T *selfdict = NULL;
21466 char_u *s;
21467 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021468 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021469
21470 while (ret == OK
21471 && (**arg == '['
21472 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021473 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021474 && !vim_iswhite(*(*arg - 1)))
21475 {
21476 if (**arg == '(')
21477 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021478 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021479 if (evaluate)
21480 {
21481 functv = *rettv;
21482 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021483
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021484 /* Invoke the function. Recursive! */
21485 s = functv.vval.v_string;
21486 }
21487 else
21488 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021489 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021490 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21491 &len, evaluate, selfdict);
21492
21493 /* Clear the funcref afterwards, so that deleting it while
21494 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021495 if (evaluate)
21496 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021497
21498 /* Stop the expression evaluation when immediately aborting on
21499 * error, or when an interrupt occurred or an exception was thrown
21500 * but not caught. */
21501 if (aborting())
21502 {
21503 if (ret == OK)
21504 clear_tv(rettv);
21505 ret = FAIL;
21506 }
21507 dict_unref(selfdict);
21508 selfdict = NULL;
21509 }
21510 else /* **arg == '[' || **arg == '.' */
21511 {
21512 dict_unref(selfdict);
21513 if (rettv->v_type == VAR_DICT)
21514 {
21515 selfdict = rettv->vval.v_dict;
21516 if (selfdict != NULL)
21517 ++selfdict->dv_refcount;
21518 }
21519 else
21520 selfdict = NULL;
21521 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21522 {
21523 clear_tv(rettv);
21524 ret = FAIL;
21525 }
21526 }
21527 }
21528 dict_unref(selfdict);
21529 return ret;
21530}
21531
21532/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021533 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021534 * value).
21535 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021536 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021537alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021538{
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021540}
21541
21542/*
21543 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 * The string "s" must have been allocated, it is consumed.
21545 * Return NULL for out of memory, the variable otherwise.
21546 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021547 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021548alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 char_u *s;
21550{
Bram Moolenaar33570922005-01-25 22:26:29 +000021551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021553 rettv = alloc_tv();
21554 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021556 rettv->v_type = VAR_STRING;
21557 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 }
21559 else
21560 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021561 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562}
21563
21564/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021565 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021567 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021568free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021569 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570{
21571 if (varp != NULL)
21572 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021573 switch (varp->v_type)
21574 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021575 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021576 func_unref(varp->vval.v_string);
21577 /*FALLTHROUGH*/
21578 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021579 vim_free(varp->vval.v_string);
21580 break;
21581 case VAR_LIST:
21582 list_unref(varp->vval.v_list);
21583 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021584 case VAR_DICT:
21585 dict_unref(varp->vval.v_dict);
21586 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021587 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021588#ifdef FEAT_FLOAT
21589 case VAR_FLOAT:
21590#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021591 case VAR_UNKNOWN:
21592 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021593 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021594 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021595 break;
21596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 vim_free(varp);
21598 }
21599}
21600
21601/*
21602 * Free the memory for a variable value and set the value to NULL or 0.
21603 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021604 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021605clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607{
21608 if (varp != NULL)
21609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021610 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021612 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 func_unref(varp->vval.v_string);
21614 /*FALLTHROUGH*/
21615 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021616 vim_free(varp->vval.v_string);
21617 varp->vval.v_string = NULL;
21618 break;
21619 case VAR_LIST:
21620 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021621 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021622 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021623 case VAR_DICT:
21624 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021625 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021626 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021627 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021628 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021629 varp->vval.v_number = 0;
21630 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021631#ifdef FEAT_FLOAT
21632 case VAR_FLOAT:
21633 varp->vval.v_float = 0.0;
21634 break;
21635#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021636 case VAR_UNKNOWN:
21637 break;
21638 default:
21639 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021641 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 }
21643}
21644
21645/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021646 * Set the value of a variable to NULL without freeing items.
21647 */
21648 static void
21649init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021650 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021651{
21652 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021654}
21655
21656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 * Get the number value of a variable.
21658 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021659 * For incompatible types, return 0.
21660 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21661 * caller of incompatible types: it sets *denote to TRUE if "denote"
21662 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 */
21664 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021665get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021666 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021668 int error = FALSE;
21669
21670 return get_tv_number_chk(varp, &error); /* return 0L on error */
21671}
21672
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021673 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021674get_tv_number_chk(varp, denote)
21675 typval_T *varp;
21676 int *denote;
21677{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021678 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021682 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021683 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021684#ifdef FEAT_FLOAT
21685 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021686 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021687 break;
21688#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021689 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021690 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021691 break;
21692 case VAR_STRING:
21693 if (varp->vval.v_string != NULL)
21694 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021695 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021696 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021697 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021698 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021699 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021700 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021701 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021702 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021703 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021704 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021705 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021707 if (denote == NULL) /* useful for values that must be unsigned */
21708 n = -1;
21709 else
21710 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021711 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712}
21713
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021714#ifdef FEAT_FLOAT
21715 static float_T
21716get_tv_float(varp)
21717 typval_T *varp;
21718{
21719 switch (varp->v_type)
21720 {
21721 case VAR_NUMBER:
21722 return (float_T)(varp->vval.v_number);
21723#ifdef FEAT_FLOAT
21724 case VAR_FLOAT:
21725 return varp->vval.v_float;
21726 break;
21727#endif
21728 case VAR_FUNC:
21729 EMSG(_("E891: Using a Funcref as a Float"));
21730 break;
21731 case VAR_STRING:
21732 EMSG(_("E892: Using a String as a Float"));
21733 break;
21734 case VAR_LIST:
21735 EMSG(_("E893: Using a List as a Float"));
21736 break;
21737 case VAR_DICT:
21738 EMSG(_("E894: Using a Dictionary as a Float"));
21739 break;
21740 default:
21741 EMSG2(_(e_intern2), "get_tv_float()");
21742 break;
21743 }
21744 return 0;
21745}
21746#endif
21747
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021749 * Get the lnum from the first argument.
21750 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021751 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 */
21753 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021754get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756{
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 linenr_T lnum;
21759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021760 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 if (lnum == 0) /* no valid number, try using line() */
21762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021763 rettv.v_type = VAR_NUMBER;
21764 f_line(argvars, &rettv);
21765 lnum = rettv.vval.v_number;
21766 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 }
21768 return lnum;
21769}
21770
21771/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021772 * Get the lnum from the first argument.
21773 * Also accepts "$", then "buf" is used.
21774 * Returns 0 on error.
21775 */
21776 static linenr_T
21777get_tv_lnum_buf(argvars, buf)
21778 typval_T *argvars;
21779 buf_T *buf;
21780{
21781 if (argvars[0].v_type == VAR_STRING
21782 && argvars[0].vval.v_string != NULL
21783 && argvars[0].vval.v_string[0] == '$'
21784 && buf != NULL)
21785 return buf->b_ml.ml_line_count;
21786 return get_tv_number_chk(&argvars[0], NULL);
21787}
21788
21789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 * Get the string value of a variable.
21791 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021792 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21793 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 * If the String variable has never been set, return an empty string.
21795 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021796 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21797 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 */
21799 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021800get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802{
21803 static char_u mybuf[NUMBUFLEN];
21804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021805 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806}
21807
21808 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021809get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021811 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021813 char_u *res = get_tv_string_buf_chk(varp, buf);
21814
21815 return res != NULL ? res : (char_u *)"";
21816}
21817
Bram Moolenaar7d647822014-04-05 21:28:56 +020021818/*
21819 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21820 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021821 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021822get_tv_string_chk(varp)
21823 typval_T *varp;
21824{
21825 static char_u mybuf[NUMBUFLEN];
21826
21827 return get_tv_string_buf_chk(varp, mybuf);
21828}
21829
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021830 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021831get_tv_string_buf_chk(varp, buf)
21832 typval_T *varp;
21833 char_u *buf;
21834{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021835 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021837 case VAR_NUMBER:
21838 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21839 return buf;
21840 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021841 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021842 break;
21843 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021844 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021845 break;
21846 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021847 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021848 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021849#ifdef FEAT_FLOAT
21850 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021851 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021852 break;
21853#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021854 case VAR_STRING:
21855 if (varp->vval.v_string != NULL)
21856 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021857 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021858 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021859 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021862 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863}
21864
21865/*
21866 * Find variable "name" in the list of variables.
21867 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021868 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021869 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021870 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021872 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021873find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021876 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021879 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880
Bram Moolenaara7043832005-01-21 11:56:39 +000021881 ht = find_var_ht(name, &varname);
21882 if (htp != NULL)
21883 *htp = ht;
21884 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021886 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887}
21888
21889/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021890 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021891 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021893 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021894find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021896 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021897 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021898 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021899{
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 hashitem_T *hi;
21901
21902 if (*varname == NUL)
21903 {
21904 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021905 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021907 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021908 case 'g': return &globvars_var;
21909 case 'v': return &vimvars_var;
21910 case 'b': return &curbuf->b_bufvar;
21911 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021912#ifdef FEAT_WINDOWS
21913 case 't': return &curtab->tp_winvar;
21914#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021915 case 'l': return current_funccal == NULL
21916 ? NULL : &current_funccal->l_vars_var;
21917 case 'a': return current_funccal == NULL
21918 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021919 }
21920 return NULL;
21921 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021922
21923 hi = hash_find(ht, varname);
21924 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021925 {
21926 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021927 * worked find the variable again. Don't auto-load a script if it was
21928 * loaded already, otherwise it would be loaded every time when
21929 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021930 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021931 {
21932 /* Note: script_autoload() may make "hi" invalid. It must either
21933 * be obtained again or not used. */
21934 if (!script_autoload(varname, FALSE) || aborting())
21935 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021936 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021937 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021938 if (HASHITEM_EMPTY(hi))
21939 return NULL;
21940 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021941 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021942}
21943
21944/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021945 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021946 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021947 * Set "varname" to the start of name without ':'.
21948 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021949 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021950find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 char_u *name;
21952 char_u **varname;
21953{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021954 hashitem_T *hi;
21955
Bram Moolenaar73627d02015-08-11 15:46:09 +020021956 if (name[0] == NUL)
21957 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 if (name[1] != ':')
21959 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021960 /* The name must not start with a colon or #. */
21961 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 return NULL;
21963 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021964
21965 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021966 hi = hash_find(&compat_hashtab, name);
21967 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021968 return &compat_hashtab;
21969
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021972 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 }
21974 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021975 if (*name == 'g') /* global variable */
21976 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021977 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21978 */
21979 if (vim_strchr(name + 2, ':') != NULL
21980 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021981 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021983 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021985 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021986#ifdef FEAT_WINDOWS
21987 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021988 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021989#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 if (*name == 'v') /* v: variable */
21991 return &vimvarht;
21992 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021993 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021995 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 if (*name == 's' /* script variable */
21997 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21998 return &SCRIPT_VARS(current_SID);
21999 return NULL;
22000}
22001
22002/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022003 * Get function call environment based on bactrace debug level
22004 */
22005 static funccall_T *
22006get_funccal()
22007{
22008 int i;
22009 funccall_T *funccal;
22010 funccall_T *temp_funccal;
22011
22012 funccal = current_funccal;
22013 if (debug_backtrace_level > 0)
22014 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022015 for (i = 0; i < debug_backtrace_level; i++)
22016 {
22017 temp_funccal = funccal->caller;
22018 if (temp_funccal)
22019 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022020 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022021 /* backtrace level overflow. reset to max */
22022 debug_backtrace_level = i;
22023 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022024 }
22025 return funccal;
22026}
22027
22028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022030 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 * Returns NULL when it doesn't exist.
22032 */
22033 char_u *
22034get_var_value(name)
22035 char_u *name;
22036{
Bram Moolenaar33570922005-01-25 22:26:29 +000022037 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022039 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 if (v == NULL)
22041 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022042 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043}
22044
22045/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 * sourcing this script and when executing functions defined in the script.
22048 */
22049 void
22050new_script_vars(id)
22051 scid_T id;
22052{
Bram Moolenaara7043832005-01-21 11:56:39 +000022053 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022054 hashtab_T *ht;
22055 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022056
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022059 /* Re-allocating ga_data means that an ht_array pointing to
22060 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022061 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022062 for (i = 1; i <= ga_scripts.ga_len; ++i)
22063 {
22064 ht = &SCRIPT_VARS(i);
22065 if (ht->ht_mask == HT_INIT_SIZE - 1)
22066 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022067 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022068 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022069 }
22070
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 while (ga_scripts.ga_len < id)
22072 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022073 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022074 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022075 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 }
22078 }
22079}
22080
22081/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022082 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22083 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 */
22085 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022086init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 dict_T *dict;
22088 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022089 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090{
Bram Moolenaar33570922005-01-25 22:26:29 +000022091 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022092 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022093 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022094 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022095 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022096 dict_var->di_tv.vval.v_dict = dict;
22097 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022098 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022099 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22100 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101}
22102
22103/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022104 * Unreference a dictionary initialized by init_var_dict().
22105 */
22106 void
22107unref_var_dict(dict)
22108 dict_T *dict;
22109{
22110 /* Now the dict needs to be freed if no one else is using it, go back to
22111 * normal reference counting. */
22112 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22113 dict_unref(dict);
22114}
22115
22116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 * Frees all allocated variables and the value they contain.
22119 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 */
22121 void
Bram Moolenaara7043832005-01-21 11:56:39 +000022122vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022123 hashtab_T *ht;
22124{
22125 vars_clear_ext(ht, TRUE);
22126}
22127
22128/*
22129 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22130 */
22131 static void
22132vars_clear_ext(ht, free_val)
22133 hashtab_T *ht;
22134 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135{
Bram Moolenaara7043832005-01-21 11:56:39 +000022136 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 hashitem_T *hi;
22138 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139
Bram Moolenaar33570922005-01-25 22:26:29 +000022140 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022141 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022142 for (hi = ht->ht_array; todo > 0; ++hi)
22143 {
22144 if (!HASHITEM_EMPTY(hi))
22145 {
22146 --todo;
22147
Bram Moolenaar33570922005-01-25 22:26:29 +000022148 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022149 * ht_array might change then. hash_clear() takes care of it
22150 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022151 v = HI2DI(hi);
22152 if (free_val)
22153 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022154 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022155 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022156 }
22157 }
22158 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022159 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160}
22161
Bram Moolenaara7043832005-01-21 11:56:39 +000022162/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 * Delete a variable from hashtab "ht" at item "hi".
22164 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000022167delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000022168 hashtab_T *ht;
22169 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170{
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022172
22173 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 clear_tv(&di->di_tv);
22175 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176}
22177
22178/*
22179 * List the value of one internal variable.
22180 */
22181 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022182list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000022183 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022185 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022187 char_u *tofree;
22188 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022189 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022190
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022191 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022193 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022194 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195}
22196
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022198list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 char_u *prefix;
22200 char_u *name;
22201 int type;
22202 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022203 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204{
Bram Moolenaar31859182007-08-14 20:41:13 +000022205 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22206 msg_start();
22207 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 if (name != NULL) /* "a:" vars don't have a name stored */
22209 msg_puts(name);
22210 msg_putchar(' ');
22211 msg_advance(22);
22212 if (type == VAR_NUMBER)
22213 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022214 else if (type == VAR_FUNC)
22215 msg_putchar('*');
22216 else if (type == VAR_LIST)
22217 {
22218 msg_putchar('[');
22219 if (*string == '[')
22220 ++string;
22221 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022222 else if (type == VAR_DICT)
22223 {
22224 msg_putchar('{');
22225 if (*string == '{')
22226 ++string;
22227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 else
22229 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022230
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022232
22233 if (type == VAR_FUNC)
22234 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022235 if (*first)
22236 {
22237 msg_clr_eos();
22238 *first = FALSE;
22239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240}
22241
22242/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022243 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 * If the variable already exists, the value is updated.
22245 * Otherwise the variable is created.
22246 */
22247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022248set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022250 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022251 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252{
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022257 ht = find_var_ht(name, &varname);
22258 if (ht == NULL || *varname == NUL)
22259 {
22260 EMSG2(_(e_illvar), name);
22261 return;
22262 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022263 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022264
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022265 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22266 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022267
Bram Moolenaar33570922005-01-25 22:26:29 +000022268 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022271 if (var_check_ro(v->di_flags, name, FALSE)
22272 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 return;
22274 if (v->di_tv.v_type != tv->v_type
22275 && !((v->di_tv.v_type == VAR_STRING
22276 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022277 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022278 || tv->v_type == VAR_NUMBER))
22279#ifdef FEAT_FLOAT
22280 && !((v->di_tv.v_type == VAR_NUMBER
22281 || v->di_tv.v_type == VAR_FLOAT)
22282 && (tv->v_type == VAR_NUMBER
22283 || tv->v_type == VAR_FLOAT))
22284#endif
22285 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022286 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022287 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022288 return;
22289 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022290
22291 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022292 * Handle setting internal v: variables separately where needed to
22293 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022294 */
22295 if (ht == &vimvarht)
22296 {
22297 if (v->di_tv.v_type == VAR_STRING)
22298 {
22299 vim_free(v->di_tv.vval.v_string);
22300 if (copy || tv->v_type != VAR_STRING)
22301 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22302 else
22303 {
22304 /* Take over the string to avoid an extra alloc/free. */
22305 v->di_tv.vval.v_string = tv->vval.v_string;
22306 tv->vval.v_string = NULL;
22307 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022308 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022309 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022310 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022311 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022313 if (STRCMP(varname, "searchforward") == 0)
22314 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022315#ifdef FEAT_SEARCH_EXTRA
22316 else if (STRCMP(varname, "hlsearch") == 0)
22317 {
22318 no_hlsearch = !v->di_tv.vval.v_number;
22319 redraw_all_later(SOME_VALID);
22320 }
22321#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022322 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022323 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022324 else if (v->di_tv.v_type != tv->v_type)
22325 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022326 }
22327
22328 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 }
22330 else /* add a new variable */
22331 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022332 /* Can't add "v:" variable. */
22333 if (ht == &vimvarht)
22334 {
22335 EMSG2(_(e_illvar), name);
22336 return;
22337 }
22338
Bram Moolenaar92124a32005-06-17 22:03:40 +000022339 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022340 if (!valid_varname(varname))
22341 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022342
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022343 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22344 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022345 if (v == NULL)
22346 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022347 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022348 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022350 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 return;
22352 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022353 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022355
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022356 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022357 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022358 else
22359 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022360 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022361 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022362 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364}
22365
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022366/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022367 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022368 * Also give an error message.
22369 */
22370 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022371var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022372 int flags;
22373 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022374 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022375{
22376 if (flags & DI_FLAGS_RO)
22377 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022378 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022379 return TRUE;
22380 }
22381 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22382 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022383 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022384 return TRUE;
22385 }
22386 return FALSE;
22387}
22388
22389/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022390 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22391 * Also give an error message.
22392 */
22393 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022394var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022395 int flags;
22396 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022397 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022398{
22399 if (flags & DI_FLAGS_FIX)
22400 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022401 EMSG2(_("E795: Cannot delete variable %s"),
22402 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022403 return TRUE;
22404 }
22405 return FALSE;
22406}
22407
22408/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022409 * Check if a funcref is assigned to a valid variable name.
22410 * Return TRUE and give an error if not.
22411 */
22412 static int
22413var_check_func_name(name, new_var)
22414 char_u *name; /* points to start of variable name */
22415 int new_var; /* TRUE when creating the variable */
22416{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022417 /* Allow for w: b: s: and t:. */
22418 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022419 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22420 ? name[2] : name[0]))
22421 {
22422 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22423 name);
22424 return TRUE;
22425 }
22426 /* Don't allow hiding a function. When "v" is not NULL we might be
22427 * assigning another function to the same var, the type is checked
22428 * below. */
22429 if (new_var && function_exists(name))
22430 {
22431 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22432 name);
22433 return TRUE;
22434 }
22435 return FALSE;
22436}
22437
22438/*
22439 * Check if a variable name is valid.
22440 * Return FALSE and give an error if not.
22441 */
22442 static int
22443valid_varname(varname)
22444 char_u *varname;
22445{
22446 char_u *p;
22447
22448 for (p = varname; *p != NUL; ++p)
22449 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22450 && *p != AUTOLOAD_CHAR)
22451 {
22452 EMSG2(_(e_illvar), varname);
22453 return FALSE;
22454 }
22455 return TRUE;
22456}
22457
22458/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022459 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022460 * Also give an error message, using "name" or _("name") when use_gettext is
22461 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022462 */
22463 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022464tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022465 int lock;
22466 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022467 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022468{
22469 if (lock & VAR_LOCKED)
22470 {
22471 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022472 name == NULL ? (char_u *)_("Unknown")
22473 : use_gettext ? (char_u *)_(name)
22474 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022475 return TRUE;
22476 }
22477 if (lock & VAR_FIXED)
22478 {
22479 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022480 name == NULL ? (char_u *)_("Unknown")
22481 : use_gettext ? (char_u *)_(name)
22482 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022483 return TRUE;
22484 }
22485 return FALSE;
22486}
22487
22488/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022489 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022490 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022491 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022492 * It is OK for "from" and "to" to point to the same item. This is used to
22493 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022494 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022495 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022496copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 typval_T *from;
22498 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022500 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022501 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022502 switch (from->v_type)
22503 {
22504 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022505 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022506 to->vval.v_number = from->vval.v_number;
22507 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022508#ifdef FEAT_FLOAT
22509 case VAR_FLOAT:
22510 to->vval.v_float = from->vval.v_float;
22511 break;
22512#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022513 case VAR_STRING:
22514 case VAR_FUNC:
22515 if (from->vval.v_string == NULL)
22516 to->vval.v_string = NULL;
22517 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022518 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022519 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022520 if (from->v_type == VAR_FUNC)
22521 func_ref(to->vval.v_string);
22522 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022523 break;
22524 case VAR_LIST:
22525 if (from->vval.v_list == NULL)
22526 to->vval.v_list = NULL;
22527 else
22528 {
22529 to->vval.v_list = from->vval.v_list;
22530 ++to->vval.v_list->lv_refcount;
22531 }
22532 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022533 case VAR_DICT:
22534 if (from->vval.v_dict == NULL)
22535 to->vval.v_dict = NULL;
22536 else
22537 {
22538 to->vval.v_dict = from->vval.v_dict;
22539 ++to->vval.v_dict->dv_refcount;
22540 }
22541 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022542 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022543 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022544 break;
22545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546}
22547
22548/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022549 * Make a copy of an item.
22550 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022551 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22552 * reference to an already copied list/dict can be used.
22553 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022554 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022555 static int
22556item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 typval_T *from;
22558 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022559 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022560 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022561{
22562 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022563 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022564
Bram Moolenaar33570922005-01-25 22:26:29 +000022565 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022566 {
22567 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022568 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022569 }
22570 ++recurse;
22571
22572 switch (from->v_type)
22573 {
22574 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022575#ifdef FEAT_FLOAT
22576 case VAR_FLOAT:
22577#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022578 case VAR_STRING:
22579 case VAR_FUNC:
22580 copy_tv(from, to);
22581 break;
22582 case VAR_LIST:
22583 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022584 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022585 if (from->vval.v_list == NULL)
22586 to->vval.v_list = NULL;
22587 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22588 {
22589 /* use the copy made earlier */
22590 to->vval.v_list = from->vval.v_list->lv_copylist;
22591 ++to->vval.v_list->lv_refcount;
22592 }
22593 else
22594 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22595 if (to->vval.v_list == NULL)
22596 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022597 break;
22598 case VAR_DICT:
22599 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022600 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022601 if (from->vval.v_dict == NULL)
22602 to->vval.v_dict = NULL;
22603 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22604 {
22605 /* use the copy made earlier */
22606 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22607 ++to->vval.v_dict->dv_refcount;
22608 }
22609 else
22610 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22611 if (to->vval.v_dict == NULL)
22612 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022613 break;
22614 default:
22615 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022616 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022617 }
22618 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022619 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022620}
22621
22622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 * ":echo expr1 ..." print each argument separated with a space, add a
22624 * newline at the end.
22625 * ":echon expr1 ..." print each argument plain.
22626 */
22627 void
22628ex_echo(eap)
22629 exarg_T *eap;
22630{
22631 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022632 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022633 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 char_u *p;
22635 int needclr = TRUE;
22636 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022637 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638
22639 if (eap->skip)
22640 ++emsg_skip;
22641 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22642 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022643 /* If eval1() causes an error message the text from the command may
22644 * still need to be cleared. E.g., "echo 22,44". */
22645 need_clr_eos = needclr;
22646
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022648 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 {
22650 /*
22651 * Report the invalid expression unless the expression evaluation
22652 * has been cancelled due to an aborting error, an interrupt, or an
22653 * exception.
22654 */
22655 if (!aborting())
22656 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022657 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 break;
22659 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022660 need_clr_eos = FALSE;
22661
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 if (!eap->skip)
22663 {
22664 if (atstart)
22665 {
22666 atstart = FALSE;
22667 /* Call msg_start() after eval1(), evaluating the expression
22668 * may cause a message to appear. */
22669 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022670 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022671 /* Mark the saved text as finishing the line, so that what
22672 * follows is displayed on a new line when scrolling back
22673 * at the more prompt. */
22674 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 }
22678 else if (eap->cmdidx == CMD_echo)
22679 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022680 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022681 if (p != NULL)
22682 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022684 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022686 if (*p != TAB && needclr)
22687 {
22688 /* remove any text still there from the command */
22689 msg_clr_eos();
22690 needclr = FALSE;
22691 }
22692 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 }
22694 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022695 {
22696#ifdef FEAT_MBYTE
22697 if (has_mbyte)
22698 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022699 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022700
22701 (void)msg_outtrans_len_attr(p, i, echo_attr);
22702 p += i - 1;
22703 }
22704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022706 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022709 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 arg = skipwhite(arg);
22713 }
22714 eap->nextcmd = check_nextcmd(arg);
22715
22716 if (eap->skip)
22717 --emsg_skip;
22718 else
22719 {
22720 /* remove text that may still be there from the command */
22721 if (needclr)
22722 msg_clr_eos();
22723 if (eap->cmdidx == CMD_echo)
22724 msg_end();
22725 }
22726}
22727
22728/*
22729 * ":echohl {name}".
22730 */
22731 void
22732ex_echohl(eap)
22733 exarg_T *eap;
22734{
22735 int id;
22736
22737 id = syn_name2id(eap->arg);
22738 if (id == 0)
22739 echo_attr = 0;
22740 else
22741 echo_attr = syn_id2attr(id);
22742}
22743
22744/*
22745 * ":execute expr1 ..." execute the result of an expression.
22746 * ":echomsg expr1 ..." Print a message
22747 * ":echoerr expr1 ..." Print an error
22748 * Each gets spaces around each argument and a newline at the end for
22749 * echo commands
22750 */
22751 void
22752ex_execute(eap)
22753 exarg_T *eap;
22754{
22755 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022756 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 int ret = OK;
22758 char_u *p;
22759 garray_T ga;
22760 int len;
22761 int save_did_emsg;
22762
22763 ga_init2(&ga, 1, 80);
22764
22765 if (eap->skip)
22766 ++emsg_skip;
22767 while (*arg != NUL && *arg != '|' && *arg != '\n')
22768 {
22769 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022770 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 {
22772 /*
22773 * Report the invalid expression unless the expression evaluation
22774 * has been cancelled due to an aborting error, an interrupt, or an
22775 * exception.
22776 */
22777 if (!aborting())
22778 EMSG2(_(e_invexpr2), p);
22779 ret = FAIL;
22780 break;
22781 }
22782
22783 if (!eap->skip)
22784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022785 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 len = (int)STRLEN(p);
22787 if (ga_grow(&ga, len + 2) == FAIL)
22788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 ret = FAIL;
22791 break;
22792 }
22793 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 ga.ga_len += len;
22797 }
22798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 arg = skipwhite(arg);
22801 }
22802
22803 if (ret != FAIL && ga.ga_data != NULL)
22804 {
22805 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022808 out_flush();
22809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 else if (eap->cmdidx == CMD_echoerr)
22811 {
22812 /* We don't want to abort following commands, restore did_emsg. */
22813 save_did_emsg = did_emsg;
22814 EMSG((char_u *)ga.ga_data);
22815 if (!force_abort)
22816 did_emsg = save_did_emsg;
22817 }
22818 else if (eap->cmdidx == CMD_execute)
22819 do_cmdline((char_u *)ga.ga_data,
22820 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22821 }
22822
22823 ga_clear(&ga);
22824
22825 if (eap->skip)
22826 --emsg_skip;
22827
22828 eap->nextcmd = check_nextcmd(arg);
22829}
22830
22831/*
22832 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22833 * "arg" points to the "&" or '+' when called, to "option" when returning.
22834 * Returns NULL when no option name found. Otherwise pointer to the char
22835 * after the option name.
22836 */
22837 static char_u *
22838find_option_end(arg, opt_flags)
22839 char_u **arg;
22840 int *opt_flags;
22841{
22842 char_u *p = *arg;
22843
22844 ++p;
22845 if (*p == 'g' && p[1] == ':')
22846 {
22847 *opt_flags = OPT_GLOBAL;
22848 p += 2;
22849 }
22850 else if (*p == 'l' && p[1] == ':')
22851 {
22852 *opt_flags = OPT_LOCAL;
22853 p += 2;
22854 }
22855 else
22856 *opt_flags = 0;
22857
22858 if (!ASCII_ISALPHA(*p))
22859 return NULL;
22860 *arg = p;
22861
22862 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22863 p += 4; /* termcap option */
22864 else
22865 while (ASCII_ISALPHA(*p))
22866 ++p;
22867 return p;
22868}
22869
22870/*
22871 * ":function"
22872 */
22873 void
22874ex_function(eap)
22875 exarg_T *eap;
22876{
22877 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022878 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 int j;
22880 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022882 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 char_u *name = NULL;
22884 char_u *p;
22885 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022886 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 garray_T newargs;
22888 garray_T newlines;
22889 int varargs = FALSE;
22890 int mustend = FALSE;
22891 int flags = 0;
22892 ufunc_T *fp;
22893 int indent;
22894 int nesting;
22895 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022896 dictitem_T *v;
22897 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 static int func_nr = 0; /* number for nameless function */
22899 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022900 hashtab_T *ht;
22901 int todo;
22902 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022903 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904
22905 /*
22906 * ":function" without argument: list functions.
22907 */
22908 if (ends_excmd(*eap->arg))
22909 {
22910 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022911 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022912 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022913 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022914 {
22915 if (!HASHITEM_EMPTY(hi))
22916 {
22917 --todo;
22918 fp = HI2UF(hi);
22919 if (!isdigit(*fp->uf_name))
22920 list_func_head(fp, FALSE);
22921 }
22922 }
22923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 eap->nextcmd = check_nextcmd(eap->arg);
22925 return;
22926 }
22927
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022928 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022929 * ":function /pat": list functions matching pattern.
22930 */
22931 if (*eap->arg == '/')
22932 {
22933 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22934 if (!eap->skip)
22935 {
22936 regmatch_T regmatch;
22937
22938 c = *p;
22939 *p = NUL;
22940 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22941 *p = c;
22942 if (regmatch.regprog != NULL)
22943 {
22944 regmatch.rm_ic = p_ic;
22945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022946 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022947 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22948 {
22949 if (!HASHITEM_EMPTY(hi))
22950 {
22951 --todo;
22952 fp = HI2UF(hi);
22953 if (!isdigit(*fp->uf_name)
22954 && vim_regexec(&regmatch, fp->uf_name, 0))
22955 list_func_head(fp, FALSE);
22956 }
22957 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022958 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022959 }
22960 }
22961 if (*p == '/')
22962 ++p;
22963 eap->nextcmd = check_nextcmd(p);
22964 return;
22965 }
22966
22967 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022968 * Get the function name. There are these situations:
22969 * func normal function name
22970 * "name" == func, "fudi.fd_dict" == NULL
22971 * dict.func new dictionary entry
22972 * "name" == NULL, "fudi.fd_dict" set,
22973 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22974 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022975 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022976 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22977 * dict.func existing dict entry that's not a Funcref
22978 * "name" == NULL, "fudi.fd_dict" set,
22979 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022980 * s:func script-local function name
22981 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022984 name = trans_function_name(&p, eap->skip, 0, &fudi);
22985 paren = (vim_strchr(p, '(') != NULL);
22986 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 {
22988 /*
22989 * Return on an invalid expression in braces, unless the expression
22990 * evaluation has been cancelled due to an aborting error, an
22991 * interrupt, or an exception.
22992 */
22993 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022994 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022995 if (!eap->skip && fudi.fd_newkey != NULL)
22996 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022997 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 else
23001 eap->skip = TRUE;
23002 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023003
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 /* An error in a function call during evaluation of an expression in magic
23005 * braces should not cause the function not to be defined. */
23006 saved_did_emsg = did_emsg;
23007 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008
23009 /*
23010 * ":function func" with only function name: list function.
23011 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023012 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 {
23014 if (!ends_excmd(*skipwhite(p)))
23015 {
23016 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023017 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 }
23019 eap->nextcmd = check_nextcmd(p);
23020 if (eap->nextcmd != NULL)
23021 *p = NUL;
23022 if (!eap->skip && !got_int)
23023 {
23024 fp = find_func(name);
23025 if (fp != NULL)
23026 {
23027 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023028 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023030 if (FUNCLINE(fp, j) == NULL)
23031 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 msg_putchar('\n');
23033 msg_outnum((long)(j + 1));
23034 if (j < 9)
23035 msg_putchar(' ');
23036 if (j < 99)
23037 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023038 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 out_flush(); /* show a line at a time */
23040 ui_breakcheck();
23041 }
23042 if (!got_int)
23043 {
23044 msg_putchar('\n');
23045 msg_puts((char_u *)" endfunction");
23046 }
23047 }
23048 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023049 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023051 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 }
23053
23054 /*
23055 * ":function name(arg1, arg2)" Define function.
23056 */
23057 p = skipwhite(p);
23058 if (*p != '(')
23059 {
23060 if (!eap->skip)
23061 {
23062 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023063 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 }
23065 /* attempt to continue by skipping some text */
23066 if (vim_strchr(p, '(') != NULL)
23067 p = vim_strchr(p, '(');
23068 }
23069 p = skipwhite(p + 1);
23070
23071 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23072 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23073
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023074 if (!eap->skip)
23075 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023076 /* Check the name of the function. Unless it's a dictionary function
23077 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023078 if (name != NULL)
23079 arg = name;
23080 else
23081 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023082 if (arg != NULL && (fudi.fd_di == NULL
23083 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023084 {
23085 if (*arg == K_SPECIAL)
23086 j = 3;
23087 else
23088 j = 0;
23089 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23090 : eval_isnamec(arg[j])))
23091 ++j;
23092 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023093 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023094 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023095 /* Disallow using the g: dict. */
23096 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23097 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023098 }
23099
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 /*
23101 * Isolate the arguments: "arg1, arg2, ...)"
23102 */
23103 while (*p != ')')
23104 {
23105 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23106 {
23107 varargs = TRUE;
23108 p += 3;
23109 mustend = TRUE;
23110 }
23111 else
23112 {
23113 arg = p;
23114 while (ASCII_ISALNUM(*p) || *p == '_')
23115 ++p;
23116 if (arg == p || isdigit(*arg)
23117 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23118 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23119 {
23120 if (!eap->skip)
23121 EMSG2(_("E125: Illegal argument: %s"), arg);
23122 break;
23123 }
23124 if (ga_grow(&newargs, 1) == FAIL)
23125 goto erret;
23126 c = *p;
23127 *p = NUL;
23128 arg = vim_strsave(arg);
23129 if (arg == NULL)
23130 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023131
23132 /* Check for duplicate argument name. */
23133 for (i = 0; i < newargs.ga_len; ++i)
23134 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23135 {
23136 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023137 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023138 goto erret;
23139 }
23140
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23142 *p = c;
23143 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 if (*p == ',')
23145 ++p;
23146 else
23147 mustend = TRUE;
23148 }
23149 p = skipwhite(p);
23150 if (mustend && *p != ')')
23151 {
23152 if (!eap->skip)
23153 EMSG2(_(e_invarg2), eap->arg);
23154 break;
23155 }
23156 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023157 if (*p != ')')
23158 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 ++p; /* skip the ')' */
23160
Bram Moolenaare9a41262005-01-15 22:18:47 +000023161 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 for (;;)
23163 {
23164 p = skipwhite(p);
23165 if (STRNCMP(p, "range", 5) == 0)
23166 {
23167 flags |= FC_RANGE;
23168 p += 5;
23169 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023170 else if (STRNCMP(p, "dict", 4) == 0)
23171 {
23172 flags |= FC_DICT;
23173 p += 4;
23174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 else if (STRNCMP(p, "abort", 5) == 0)
23176 {
23177 flags |= FC_ABORT;
23178 p += 5;
23179 }
23180 else
23181 break;
23182 }
23183
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023184 /* When there is a line break use what follows for the function body.
23185 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23186 if (*p == '\n')
23187 line_arg = p + 1;
23188 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 EMSG(_(e_trailing));
23190
23191 /*
23192 * Read the body of the function, until ":endfunction" is found.
23193 */
23194 if (KeyTyped)
23195 {
23196 /* Check if the function already exists, don't let the user type the
23197 * whole function before telling him it doesn't work! For a script we
23198 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023199 if (!eap->skip && !eap->forceit)
23200 {
23201 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23202 EMSG(_(e_funcdict));
23203 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023204 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023207 if (!eap->skip && did_emsg)
23208 goto erret;
23209
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 msg_putchar('\n'); /* don't overwrite the function name */
23211 cmdline_row = msg_row;
23212 }
23213
23214 indent = 2;
23215 nesting = 0;
23216 for (;;)
23217 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023218 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023219 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023220 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023221 saved_wait_return = FALSE;
23222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023224 sourcing_lnum_off = sourcing_lnum;
23225
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023226 if (line_arg != NULL)
23227 {
23228 /* Use eap->arg, split up in parts by line breaks. */
23229 theline = line_arg;
23230 p = vim_strchr(theline, '\n');
23231 if (p == NULL)
23232 line_arg += STRLEN(line_arg);
23233 else
23234 {
23235 *p = NUL;
23236 line_arg = p + 1;
23237 }
23238 }
23239 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 theline = getcmdline(':', 0L, indent);
23241 else
23242 theline = eap->getline(':', eap->cookie, indent);
23243 if (KeyTyped)
23244 lines_left = Rows - 1;
23245 if (theline == NULL)
23246 {
23247 EMSG(_("E126: Missing :endfunction"));
23248 goto erret;
23249 }
23250
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023251 /* Detect line continuation: sourcing_lnum increased more than one. */
23252 if (sourcing_lnum > sourcing_lnum_off + 1)
23253 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23254 else
23255 sourcing_lnum_off = 0;
23256
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 if (skip_until != NULL)
23258 {
23259 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23260 * don't check for ":endfunc". */
23261 if (STRCMP(theline, skip_until) == 0)
23262 {
23263 vim_free(skip_until);
23264 skip_until = NULL;
23265 }
23266 }
23267 else
23268 {
23269 /* skip ':' and blanks*/
23270 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23271 ;
23272
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023273 /* Check for "endfunction". */
23274 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023276 if (line_arg == NULL)
23277 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278 break;
23279 }
23280
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023281 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 * at "end". */
23283 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23284 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023285 else if (STRNCMP(p, "if", 2) == 0
23286 || STRNCMP(p, "wh", 2) == 0
23287 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 || STRNCMP(p, "try", 3) == 0)
23289 indent += 2;
23290
23291 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023292 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023294 if (*p == '!')
23295 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023297 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23298 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023300 ++nesting;
23301 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 }
23303 }
23304
23305 /* Check for ":append" or ":insert". */
23306 p = skip_range(p, NULL);
23307 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23308 || (p[0] == 'i'
23309 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23310 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23311 skip_until = vim_strsave((char_u *)".");
23312
23313 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23314 arg = skipwhite(skiptowhite(p));
23315 if (arg[0] == '<' && arg[1] =='<'
23316 && ((p[0] == 'p' && p[1] == 'y'
23317 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23318 || (p[0] == 'p' && p[1] == 'e'
23319 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23320 || (p[0] == 't' && p[1] == 'c'
23321 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023322 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23323 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23325 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023326 || (p[0] == 'm' && p[1] == 'z'
23327 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 ))
23329 {
23330 /* ":python <<" continues until a dot, like ":append" */
23331 p = skipwhite(arg + 2);
23332 if (*p == NUL)
23333 skip_until = vim_strsave((char_u *)".");
23334 else
23335 skip_until = vim_strsave(p);
23336 }
23337 }
23338
23339 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023340 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023341 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023342 if (line_arg == NULL)
23343 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023345 }
23346
23347 /* Copy the line to newly allocated memory. get_one_sourceline()
23348 * allocates 250 bytes per line, this saves 80% on average. The cost
23349 * is an extra alloc/free. */
23350 p = vim_strsave(theline);
23351 if (p != NULL)
23352 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023353 if (line_arg == NULL)
23354 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023355 theline = p;
23356 }
23357
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023358 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23359
23360 /* Add NULL lines for continuation lines, so that the line count is
23361 * equal to the index in the growarray. */
23362 while (sourcing_lnum_off-- > 0)
23363 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023364
23365 /* Check for end of eap->arg. */
23366 if (line_arg != NULL && *line_arg == NUL)
23367 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 }
23369
23370 /* Don't define the function when skipping commands or when an error was
23371 * detected. */
23372 if (eap->skip || did_emsg)
23373 goto erret;
23374
23375 /*
23376 * If there are no errors, add the function
23377 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023378 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023379 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023380 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023382 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023383 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023384 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023385 goto erret;
23386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023387
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023388 fp = find_func(name);
23389 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023391 if (!eap->forceit)
23392 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023393 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023394 goto erret;
23395 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023396 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023397 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023398 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023399 name);
23400 goto erret;
23401 }
23402 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023403 ga_clear_strings(&(fp->uf_args));
23404 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023405 vim_free(name);
23406 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 }
23409 else
23410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023411 char numbuf[20];
23412
23413 fp = NULL;
23414 if (fudi.fd_newkey == NULL && !eap->forceit)
23415 {
23416 EMSG(_(e_funcdict));
23417 goto erret;
23418 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023419 if (fudi.fd_di == NULL)
23420 {
23421 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023422 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023423 goto erret;
23424 }
23425 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023426 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023427 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023428
23429 /* Give the function a sequential number. Can only be used with a
23430 * Funcref! */
23431 vim_free(name);
23432 sprintf(numbuf, "%d", ++func_nr);
23433 name = vim_strsave((char_u *)numbuf);
23434 if (name == NULL)
23435 goto erret;
23436 }
23437
23438 if (fp == NULL)
23439 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023440 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023441 {
23442 int slen, plen;
23443 char_u *scriptname;
23444
23445 /* Check that the autoload name matches the script name. */
23446 j = FAIL;
23447 if (sourcing_name != NULL)
23448 {
23449 scriptname = autoload_name(name);
23450 if (scriptname != NULL)
23451 {
23452 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023453 plen = (int)STRLEN(p);
23454 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023455 if (slen > plen && fnamecmp(p,
23456 sourcing_name + slen - plen) == 0)
23457 j = OK;
23458 vim_free(scriptname);
23459 }
23460 }
23461 if (j == FAIL)
23462 {
23463 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23464 goto erret;
23465 }
23466 }
23467
23468 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 if (fp == NULL)
23470 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471
23472 if (fudi.fd_dict != NULL)
23473 {
23474 if (fudi.fd_di == NULL)
23475 {
23476 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023477 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023478 if (fudi.fd_di == NULL)
23479 {
23480 vim_free(fp);
23481 goto erret;
23482 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023483 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23484 {
23485 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023486 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023487 goto erret;
23488 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 }
23490 else
23491 /* overwrite existing dict entry */
23492 clear_tv(&fudi.fd_di->di_tv);
23493 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023494 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023495 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023496 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023497
23498 /* behave like "dict" was used */
23499 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023500 }
23501
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023503 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023504 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23505 {
23506 vim_free(fp);
23507 goto erret;
23508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023510 fp->uf_args = newargs;
23511 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023512#ifdef FEAT_PROFILE
23513 fp->uf_tml_count = NULL;
23514 fp->uf_tml_total = NULL;
23515 fp->uf_tml_self = NULL;
23516 fp->uf_profiling = FALSE;
23517 if (prof_def_func())
23518 func_do_profile(fp);
23519#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023520 fp->uf_varargs = varargs;
23521 fp->uf_flags = flags;
23522 fp->uf_calls = 0;
23523 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023524 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525
23526erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 ga_clear_strings(&newargs);
23528 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023529ret_free:
23530 vim_free(skip_until);
23531 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023534 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535}
23536
23537/*
23538 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023539 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023541 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023542 * TFN_INT: internal function name OK
23543 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023544 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 * Advances "pp" to just after the function name (if no error).
23546 */
23547 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023548trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 char_u **pp;
23550 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023551 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023552 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023554 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 char_u *start;
23556 char_u *end;
23557 int lead;
23558 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023561
23562 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023563 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023565
23566 /* Check for hard coded <SNR>: already translated function ID (from a user
23567 * command). */
23568 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23569 && (*pp)[2] == (int)KE_SNR)
23570 {
23571 *pp += 3;
23572 len = get_id_len(pp) + 3;
23573 return vim_strnsave(start, len);
23574 }
23575
23576 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23577 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023579 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023581
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023582 /* Note that TFN_ flags use the same values as GLV_ flags. */
23583 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023584 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023585 if (end == start)
23586 {
23587 if (!skip)
23588 EMSG(_("E129: Function name required"));
23589 goto theend;
23590 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023591 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023592 {
23593 /*
23594 * Report an invalid expression in braces, unless the expression
23595 * evaluation has been cancelled due to an aborting error, an
23596 * interrupt, or an exception.
23597 */
23598 if (!aborting())
23599 {
23600 if (end != NULL)
23601 EMSG2(_(e_invarg2), start);
23602 }
23603 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023604 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023605 goto theend;
23606 }
23607
23608 if (lv.ll_tv != NULL)
23609 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023610 if (fdp != NULL)
23611 {
23612 fdp->fd_dict = lv.ll_dict;
23613 fdp->fd_newkey = lv.ll_newkey;
23614 lv.ll_newkey = NULL;
23615 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023616 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023617 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23618 {
23619 name = vim_strsave(lv.ll_tv->vval.v_string);
23620 *pp = end;
23621 }
23622 else
23623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023624 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23625 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023626 EMSG(_(e_funcref));
23627 else
23628 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023629 name = NULL;
23630 }
23631 goto theend;
23632 }
23633
23634 if (lv.ll_name == NULL)
23635 {
23636 /* Error found, but continue after the function name. */
23637 *pp = end;
23638 goto theend;
23639 }
23640
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023641 /* Check if the name is a Funcref. If so, use the value. */
23642 if (lv.ll_exp_name != NULL)
23643 {
23644 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023645 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023646 if (name == lv.ll_exp_name)
23647 name = NULL;
23648 }
23649 else
23650 {
23651 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023652 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023653 if (name == *pp)
23654 name = NULL;
23655 }
23656 if (name != NULL)
23657 {
23658 name = vim_strsave(name);
23659 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023660 if (STRNCMP(name, "<SNR>", 5) == 0)
23661 {
23662 /* Change "<SNR>" to the byte sequence. */
23663 name[0] = K_SPECIAL;
23664 name[1] = KS_EXTRA;
23665 name[2] = (int)KE_SNR;
23666 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23667 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023668 goto theend;
23669 }
23670
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023671 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023672 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023673 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023674 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23675 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23676 {
23677 /* When there was "s:" already or the name expanded to get a
23678 * leading "s:" then remove it. */
23679 lv.ll_name += 2;
23680 len -= 2;
23681 lead = 2;
23682 }
23683 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023684 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023685 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023686 /* skip over "s:" and "g:" */
23687 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023688 lv.ll_name += 2;
23689 len = (int)(end - lv.ll_name);
23690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023691
23692 /*
23693 * Copy the function name to allocated memory.
23694 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23695 * Accept <SNR>123_name() outside a script.
23696 */
23697 if (skip)
23698 lead = 0; /* do nothing */
23699 else if (lead > 0)
23700 {
23701 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023702 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23703 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023704 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023705 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023706 if (current_SID <= 0)
23707 {
23708 EMSG(_(e_usingsid));
23709 goto theend;
23710 }
23711 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23712 lead += (int)STRLEN(sid_buf);
23713 }
23714 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023715 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023716 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023717 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023718 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023719 goto theend;
23720 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023721 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023722 {
23723 char_u *cp = vim_strchr(lv.ll_name, ':');
23724
23725 if (cp != NULL && cp < end)
23726 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023727 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023728 goto theend;
23729 }
23730 }
23731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023732 name = alloc((unsigned)(len + lead + 1));
23733 if (name != NULL)
23734 {
23735 if (lead > 0)
23736 {
23737 name[0] = K_SPECIAL;
23738 name[1] = KS_EXTRA;
23739 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023740 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023741 STRCPY(name + 3, sid_buf);
23742 }
23743 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023744 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023745 }
23746 *pp = end;
23747
23748theend:
23749 clear_lval(&lv);
23750 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751}
23752
23753/*
23754 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23755 * Return 2 if "p" starts with "s:".
23756 * Return 0 otherwise.
23757 */
23758 static int
23759eval_fname_script(p)
23760 char_u *p;
23761{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023762 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23763 * the standard library function. */
23764 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23765 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 return 5;
23767 if (p[0] == 's' && p[1] == ':')
23768 return 2;
23769 return 0;
23770}
23771
23772/*
23773 * Return TRUE if "p" starts with "<SID>" or "s:".
23774 * Only works if eval_fname_script() returned non-zero for "p"!
23775 */
23776 static int
23777eval_fname_sid(p)
23778 char_u *p;
23779{
23780 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23781}
23782
23783/*
23784 * List the head of the function: "name(arg1, arg2)".
23785 */
23786 static void
23787list_func_head(fp, indent)
23788 ufunc_T *fp;
23789 int indent;
23790{
23791 int j;
23792
23793 msg_start();
23794 if (indent)
23795 MSG_PUTS(" ");
23796 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023797 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 {
23799 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023800 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801 }
23802 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023803 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023805 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806 {
23807 if (j)
23808 MSG_PUTS(", ");
23809 msg_puts(FUNCARG(fp, j));
23810 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023811 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 {
23813 if (j)
23814 MSG_PUTS(", ");
23815 MSG_PUTS("...");
23816 }
23817 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023818 if (fp->uf_flags & FC_ABORT)
23819 MSG_PUTS(" abort");
23820 if (fp->uf_flags & FC_RANGE)
23821 MSG_PUTS(" range");
23822 if (fp->uf_flags & FC_DICT)
23823 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023824 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023825 if (p_verbose > 0)
23826 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827}
23828
23829/*
23830 * Find a function by name, return pointer to it in ufuncs.
23831 * Return NULL for unknown function.
23832 */
23833 static ufunc_T *
23834find_func(name)
23835 char_u *name;
23836{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023837 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023839 hi = hash_find(&func_hashtab, name);
23840 if (!HASHITEM_EMPTY(hi))
23841 return HI2UF(hi);
23842 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843}
23844
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023845#if defined(EXITFREE) || defined(PROTO)
23846 void
23847free_all_functions()
23848{
23849 hashitem_T *hi;
23850
23851 /* Need to start all over every time, because func_free() may change the
23852 * hash table. */
23853 while (func_hashtab.ht_used > 0)
23854 for (hi = func_hashtab.ht_array; ; ++hi)
23855 if (!HASHITEM_EMPTY(hi))
23856 {
23857 func_free(HI2UF(hi));
23858 break;
23859 }
23860}
23861#endif
23862
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023863 int
23864translated_function_exists(name)
23865 char_u *name;
23866{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023867 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023868 return find_internal_func(name) >= 0;
23869 return find_func(name) != NULL;
23870}
23871
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023872/*
23873 * Return TRUE if a function "name" exists.
23874 */
23875 static int
23876function_exists(name)
23877 char_u *name;
23878{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023879 char_u *nm = name;
23880 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023881 int n = FALSE;
23882
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023883 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23884 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023885 nm = skipwhite(nm);
23886
23887 /* Only accept "funcname", "funcname ", "funcname (..." and
23888 * "funcname(...", not "funcname!...". */
23889 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023890 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023891 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023892 return n;
23893}
23894
Bram Moolenaara1544c02013-05-30 12:35:52 +020023895 char_u *
23896get_expanded_name(name, check)
23897 char_u *name;
23898 int check;
23899{
23900 char_u *nm = name;
23901 char_u *p;
23902
23903 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23904
23905 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023906 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023907 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023908
Bram Moolenaara1544c02013-05-30 12:35:52 +020023909 vim_free(p);
23910 return NULL;
23911}
23912
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023913/*
23914 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023915 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23916 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023917 */
23918 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023919builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023920 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023921 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023922{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023923 char_u *p;
23924
23925 if (!ASCII_ISLOWER(name[0]))
23926 return FALSE;
23927 p = vim_strchr(name, AUTOLOAD_CHAR);
23928 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023929}
23930
Bram Moolenaar05159a02005-02-26 23:04:13 +000023931#if defined(FEAT_PROFILE) || defined(PROTO)
23932/*
23933 * Start profiling function "fp".
23934 */
23935 static void
23936func_do_profile(fp)
23937 ufunc_T *fp;
23938{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023939 int len = fp->uf_lines.ga_len;
23940
23941 if (len == 0)
23942 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023943 fp->uf_tm_count = 0;
23944 profile_zero(&fp->uf_tm_self);
23945 profile_zero(&fp->uf_tm_total);
23946 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023947 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023948 if (fp->uf_tml_total == NULL)
23949 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023950 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023951 if (fp->uf_tml_self == NULL)
23952 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023953 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023954 fp->uf_tml_idx = -1;
23955 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23956 || fp->uf_tml_self == NULL)
23957 return; /* out of memory */
23958
23959 fp->uf_profiling = TRUE;
23960}
23961
23962/*
23963 * Dump the profiling results for all functions in file "fd".
23964 */
23965 void
23966func_dump_profile(fd)
23967 FILE *fd;
23968{
23969 hashitem_T *hi;
23970 int todo;
23971 ufunc_T *fp;
23972 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023973 ufunc_T **sorttab;
23974 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023975
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023976 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023977 if (todo == 0)
23978 return; /* nothing to dump */
23979
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023980 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023981
Bram Moolenaar05159a02005-02-26 23:04:13 +000023982 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23983 {
23984 if (!HASHITEM_EMPTY(hi))
23985 {
23986 --todo;
23987 fp = HI2UF(hi);
23988 if (fp->uf_profiling)
23989 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023990 if (sorttab != NULL)
23991 sorttab[st_len++] = fp;
23992
Bram Moolenaar05159a02005-02-26 23:04:13 +000023993 if (fp->uf_name[0] == K_SPECIAL)
23994 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23995 else
23996 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23997 if (fp->uf_tm_count == 1)
23998 fprintf(fd, "Called 1 time\n");
23999 else
24000 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24001 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24002 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24003 fprintf(fd, "\n");
24004 fprintf(fd, "count total (s) self (s)\n");
24005
24006 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24007 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024008 if (FUNCLINE(fp, i) == NULL)
24009 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024010 prof_func_line(fd, fp->uf_tml_count[i],
24011 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024012 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24013 }
24014 fprintf(fd, "\n");
24015 }
24016 }
24017 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024018
24019 if (sorttab != NULL && st_len > 0)
24020 {
24021 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24022 prof_total_cmp);
24023 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24024 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24025 prof_self_cmp);
24026 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24027 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024028
24029 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024030}
Bram Moolenaar73830342005-02-28 22:48:19 +000024031
24032 static void
24033prof_sort_list(fd, sorttab, st_len, title, prefer_self)
24034 FILE *fd;
24035 ufunc_T **sorttab;
24036 int st_len;
24037 char *title;
24038 int prefer_self; /* when equal print only self time */
24039{
24040 int i;
24041 ufunc_T *fp;
24042
24043 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24044 fprintf(fd, "count total (s) self (s) function\n");
24045 for (i = 0; i < 20 && i < st_len; ++i)
24046 {
24047 fp = sorttab[i];
24048 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24049 prefer_self);
24050 if (fp->uf_name[0] == K_SPECIAL)
24051 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24052 else
24053 fprintf(fd, " %s()\n", fp->uf_name);
24054 }
24055 fprintf(fd, "\n");
24056}
24057
24058/*
24059 * Print the count and times for one function or function line.
24060 */
24061 static void
24062prof_func_line(fd, count, total, self, prefer_self)
24063 FILE *fd;
24064 int count;
24065 proftime_T *total;
24066 proftime_T *self;
24067 int prefer_self; /* when equal print only self time */
24068{
24069 if (count > 0)
24070 {
24071 fprintf(fd, "%5d ", count);
24072 if (prefer_self && profile_equal(total, self))
24073 fprintf(fd, " ");
24074 else
24075 fprintf(fd, "%s ", profile_msg(total));
24076 if (!prefer_self && profile_equal(total, self))
24077 fprintf(fd, " ");
24078 else
24079 fprintf(fd, "%s ", profile_msg(self));
24080 }
24081 else
24082 fprintf(fd, " ");
24083}
24084
24085/*
24086 * Compare function for total time sorting.
24087 */
24088 static int
24089#ifdef __BORLANDC__
24090_RTLENTRYF
24091#endif
24092prof_total_cmp(s1, s2)
24093 const void *s1;
24094 const void *s2;
24095{
24096 ufunc_T *p1, *p2;
24097
24098 p1 = *(ufunc_T **)s1;
24099 p2 = *(ufunc_T **)s2;
24100 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24101}
24102
24103/*
24104 * Compare function for self time sorting.
24105 */
24106 static int
24107#ifdef __BORLANDC__
24108_RTLENTRYF
24109#endif
24110prof_self_cmp(s1, s2)
24111 const void *s1;
24112 const void *s2;
24113{
24114 ufunc_T *p1, *p2;
24115
24116 p1 = *(ufunc_T **)s1;
24117 p2 = *(ufunc_T **)s2;
24118 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24119}
24120
Bram Moolenaar05159a02005-02-26 23:04:13 +000024121#endif
24122
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024123/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024124 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024125 * Return TRUE if a package was loaded.
24126 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024127 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024128script_autoload(name, reload)
24129 char_u *name;
24130 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024131{
24132 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024133 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024134 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024135 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024136
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024137 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024138 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024139 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024140 return FALSE;
24141
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024142 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024143
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024144 /* Find the name in the list of previously loaded package names. Skip
24145 * "autoload/", it's always the same. */
24146 for (i = 0; i < ga_loaded.ga_len; ++i)
24147 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24148 break;
24149 if (!reload && i < ga_loaded.ga_len)
24150 ret = FALSE; /* was loaded already */
24151 else
24152 {
24153 /* Remember the name if it wasn't loaded already. */
24154 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24155 {
24156 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24157 tofree = NULL;
24158 }
24159
24160 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024161 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024162 ret = TRUE;
24163 }
24164
24165 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024166 return ret;
24167}
24168
24169/*
24170 * Return the autoload script name for a function or variable name.
24171 * Returns NULL when out of memory.
24172 */
24173 static char_u *
24174autoload_name(name)
24175 char_u *name;
24176{
24177 char_u *p;
24178 char_u *scriptname;
24179
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024180 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024181 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24182 if (scriptname == NULL)
24183 return FALSE;
24184 STRCPY(scriptname, "autoload/");
24185 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024186 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024187 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024188 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024189 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024190 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024191}
24192
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24194
24195/*
24196 * Function given to ExpandGeneric() to obtain the list of user defined
24197 * function names.
24198 */
24199 char_u *
24200get_user_func_name(xp, idx)
24201 expand_T *xp;
24202 int idx;
24203{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024204 static long_u done;
24205 static hashitem_T *hi;
24206 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207
24208 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024210 done = 0;
24211 hi = func_hashtab.ht_array;
24212 }
24213 if (done < func_hashtab.ht_used)
24214 {
24215 if (done++ > 0)
24216 ++hi;
24217 while (HASHITEM_EMPTY(hi))
24218 ++hi;
24219 fp = HI2UF(hi);
24220
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024221 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024222 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024223
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024224 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24225 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226
24227 cat_func_name(IObuff, fp);
24228 if (xp->xp_context != EXPAND_USER_FUNC)
24229 {
24230 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024231 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 STRCAT(IObuff, ")");
24233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 return IObuff;
24235 }
24236 return NULL;
24237}
24238
24239#endif /* FEAT_CMDL_COMPL */
24240
24241/*
24242 * Copy the function name of "fp" to buffer "buf".
24243 * "buf" must be able to hold the function name plus three bytes.
24244 * Takes care of script-local function names.
24245 */
24246 static void
24247cat_func_name(buf, fp)
24248 char_u *buf;
24249 ufunc_T *fp;
24250{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024251 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 {
24253 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024254 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 }
24256 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024257 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258}
24259
24260/*
24261 * ":delfunction {name}"
24262 */
24263 void
24264ex_delfunction(eap)
24265 exarg_T *eap;
24266{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024267 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 char_u *p;
24269 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024270 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271
24272 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024273 name = trans_function_name(&p, eap->skip, 0, &fudi);
24274 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024276 {
24277 if (fudi.fd_dict != NULL && !eap->skip)
24278 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 if (!ends_excmd(*skipwhite(p)))
24282 {
24283 vim_free(name);
24284 EMSG(_(e_trailing));
24285 return;
24286 }
24287 eap->nextcmd = check_nextcmd(p);
24288 if (eap->nextcmd != NULL)
24289 *p = NUL;
24290
24291 if (!eap->skip)
24292 fp = find_func(name);
24293 vim_free(name);
24294
24295 if (!eap->skip)
24296 {
24297 if (fp == NULL)
24298 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024299 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 return;
24301 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024302 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 {
24304 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24305 return;
24306 }
24307
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024308 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024310 /* Delete the dict item that refers to the function, it will
24311 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024312 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024314 else
24315 func_free(fp);
24316 }
24317}
24318
24319/*
24320 * Free a function and remove it from the list of functions.
24321 */
24322 static void
24323func_free(fp)
24324 ufunc_T *fp;
24325{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024326 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024327
24328 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024329 ga_clear_strings(&(fp->uf_args));
24330 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024331#ifdef FEAT_PROFILE
24332 vim_free(fp->uf_tml_count);
24333 vim_free(fp->uf_tml_total);
24334 vim_free(fp->uf_tml_self);
24335#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024336
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024337 /* remove the function from the function hashtable */
24338 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24339 if (HASHITEM_EMPTY(hi))
24340 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024341 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024342 hash_remove(&func_hashtab, hi);
24343
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024344 vim_free(fp);
24345}
24346
24347/*
24348 * Unreference a Function: decrement the reference count and free it when it
24349 * becomes zero. Only for numbered functions.
24350 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024351 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024352func_unref(name)
24353 char_u *name;
24354{
24355 ufunc_T *fp;
24356
24357 if (name != NULL && isdigit(*name))
24358 {
24359 fp = find_func(name);
24360 if (fp == NULL)
24361 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024362 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024363 {
24364 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024365 * when "uf_calls" becomes zero. */
24366 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024367 func_free(fp);
24368 }
24369 }
24370}
24371
24372/*
24373 * Count a reference to a Function.
24374 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024375 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024376func_ref(name)
24377 char_u *name;
24378{
24379 ufunc_T *fp;
24380
24381 if (name != NULL && isdigit(*name))
24382 {
24383 fp = find_func(name);
24384 if (fp == NULL)
24385 EMSG2(_(e_intern2), "func_ref()");
24386 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024387 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 }
24389}
24390
24391/*
24392 * Call a user function.
24393 */
24394 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024395call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396 ufunc_T *fp; /* pointer to function */
24397 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024398 typval_T *argvars; /* arguments */
24399 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 linenr_T firstline; /* first line of range */
24401 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024402 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403{
Bram Moolenaar33570922005-01-25 22:26:29 +000024404 char_u *save_sourcing_name;
24405 linenr_T save_sourcing_lnum;
24406 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024407 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024408 int save_did_emsg;
24409 static int depth = 0;
24410 dictitem_T *v;
24411 int fixvar_idx = 0; /* index in fixvar[] */
24412 int i;
24413 int ai;
24414 char_u numbuf[NUMBUFLEN];
24415 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024416 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024417#ifdef FEAT_PROFILE
24418 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024419 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421
24422 /* If depth of calling is getting too high, don't execute the function */
24423 if (depth >= p_mfd)
24424 {
24425 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024426 rettv->v_type = VAR_NUMBER;
24427 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428 return;
24429 }
24430 ++depth;
24431
24432 line_breakcheck(); /* check for CTRL-C hit */
24433
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024434 fc = (funccall_T *)alloc(sizeof(funccall_T));
24435 fc->caller = current_funccal;
24436 current_funccal = fc;
24437 fc->func = fp;
24438 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024439 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024440 fc->linenr = 0;
24441 fc->returned = FALSE;
24442 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024444 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24445 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446
Bram Moolenaar33570922005-01-25 22:26:29 +000024447 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024448 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024449 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24450 * each argument variable and saves a lot of time.
24451 */
24452 /*
24453 * Init l: variables.
24454 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024455 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024456 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024457 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024458 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24459 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024460 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024461 name = v->di_key;
24462 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024463 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024464 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024465 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024466 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024467 v->di_tv.vval.v_dict = selfdict;
24468 ++selfdict->dv_refcount;
24469 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024470
Bram Moolenaar33570922005-01-25 22:26:29 +000024471 /*
24472 * Init a: variables.
24473 * Set a:0 to "argcount".
24474 * Set a:000 to a list with room for the "..." arguments.
24475 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024476 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024477 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024478 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024479 /* Use "name" to avoid a warning from some compiler that checks the
24480 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024481 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024482 name = v->di_key;
24483 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024484 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024485 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024486 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024487 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024488 v->di_tv.vval.v_list = &fc->l_varlist;
24489 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24490 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24491 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024492
24493 /*
24494 * Set a:firstline to "firstline" and a:lastline to "lastline".
24495 * Set a:name to named arguments.
24496 * Set a:N to the "..." arguments.
24497 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024498 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024499 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024500 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024501 (varnumber_T)lastline);
24502 for (i = 0; i < argcount; ++i)
24503 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024504 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024505 if (ai < 0)
24506 /* named argument a:name */
24507 name = FUNCARG(fp, i);
24508 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024509 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024510 /* "..." argument a:1, a:2, etc. */
24511 sprintf((char *)numbuf, "%d", ai + 1);
24512 name = numbuf;
24513 }
24514 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24515 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024516 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024517 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24518 }
24519 else
24520 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024521 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24522 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024523 if (v == NULL)
24524 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024525 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024526 }
24527 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024528 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024529
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024530 /* Note: the values are copied directly to avoid alloc/free.
24531 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024532 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024533 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024534
24535 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24536 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024537 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24538 fc->l_listitems[ai].li_tv = argvars[i];
24539 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024540 }
24541 }
24542
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543 /* Don't redraw while executing the function. */
24544 ++RedrawingDisabled;
24545 save_sourcing_name = sourcing_name;
24546 save_sourcing_lnum = sourcing_lnum;
24547 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024548 /* need space for function name + ("function " + 3) or "[number]" */
24549 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24550 + STRLEN(fp->uf_name) + 20;
24551 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024552 if (sourcing_name != NULL)
24553 {
24554 if (save_sourcing_name != NULL
24555 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024556 sprintf((char *)sourcing_name, "%s[%d]..",
24557 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 else
24559 STRCPY(sourcing_name, "function ");
24560 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24561
24562 if (p_verbose >= 12)
24563 {
24564 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024565 verbose_enter_scroll();
24566
Bram Moolenaar555b2802005-05-19 21:08:39 +000024567 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568 if (p_verbose >= 14)
24569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024571 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024572 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024573 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574
24575 msg_puts((char_u *)"(");
24576 for (i = 0; i < argcount; ++i)
24577 {
24578 if (i > 0)
24579 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024580 if (argvars[i].v_type == VAR_NUMBER)
24581 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 else
24583 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024584 /* Do not want errors such as E724 here. */
24585 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024586 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024587 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024588 if (s != NULL)
24589 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024590 if (vim_strsize(s) > MSG_BUF_CLEN)
24591 {
24592 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24593 s = buf;
24594 }
24595 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024596 vim_free(tofree);
24597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 }
24599 }
24600 msg_puts((char_u *)")");
24601 }
24602 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024603
24604 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 --no_wait_return;
24606 }
24607 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024608#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024609 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024610 {
24611 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24612 func_do_profile(fp);
24613 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024614 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024615 {
24616 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024617 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024618 profile_zero(&fp->uf_tm_children);
24619 }
24620 script_prof_save(&wait_start);
24621 }
24622#endif
24623
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024625 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 save_did_emsg = did_emsg;
24627 did_emsg = FALSE;
24628
24629 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024630 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24632
24633 --RedrawingDisabled;
24634
24635 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024636 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024638 clear_tv(rettv);
24639 rettv->v_type = VAR_NUMBER;
24640 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 }
24642
Bram Moolenaar05159a02005-02-26 23:04:13 +000024643#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024644 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024645 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024646 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024647 profile_end(&call_start);
24648 profile_sub_wait(&wait_start, &call_start);
24649 profile_add(&fp->uf_tm_total, &call_start);
24650 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024651 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024652 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024653 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24654 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024655 }
24656 }
24657#endif
24658
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 /* when being verbose, mention the return value */
24660 if (p_verbose >= 12)
24661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024663 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024666 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024667 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024668 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024669 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024672 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024673 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024674 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024675 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024676
Bram Moolenaar555b2802005-05-19 21:08:39 +000024677 /* The value may be very long. Skip the middle part, so that we
24678 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024679 * truncate it at the end. Don't want errors such as E724 here. */
24680 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024681 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024682 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024683 if (s != NULL)
24684 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024685 if (vim_strsize(s) > MSG_BUF_CLEN)
24686 {
24687 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24688 s = buf;
24689 }
24690 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024691 vim_free(tofree);
24692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 }
24694 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024695
24696 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 --no_wait_return;
24698 }
24699
24700 vim_free(sourcing_name);
24701 sourcing_name = save_sourcing_name;
24702 sourcing_lnum = save_sourcing_lnum;
24703 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024704#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024705 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024706 script_prof_restore(&wait_start);
24707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708
24709 if (p_verbose >= 12 && sourcing_name != NULL)
24710 {
24711 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024712 verbose_enter_scroll();
24713
Bram Moolenaar555b2802005-05-19 21:08:39 +000024714 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024716
24717 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718 --no_wait_return;
24719 }
24720
24721 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024722 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024724
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024725 /* If the a:000 list and the l: and a: dicts are not referenced we can
24726 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024727 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24728 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24729 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24730 {
24731 free_funccal(fc, FALSE);
24732 }
24733 else
24734 {
24735 hashitem_T *hi;
24736 listitem_T *li;
24737 int todo;
24738
24739 /* "fc" is still in use. This can happen when returning "a:000" or
24740 * assigning "l:" to a global variable.
24741 * Link "fc" in the list for garbage collection later. */
24742 fc->caller = previous_funccal;
24743 previous_funccal = fc;
24744
24745 /* Make a copy of the a: variables, since we didn't do that above. */
24746 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24747 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24748 {
24749 if (!HASHITEM_EMPTY(hi))
24750 {
24751 --todo;
24752 v = HI2DI(hi);
24753 copy_tv(&v->di_tv, &v->di_tv);
24754 }
24755 }
24756
24757 /* Make a copy of the a:000 items, since we didn't do that above. */
24758 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24759 copy_tv(&li->li_tv, &li->li_tv);
24760 }
24761}
24762
24763/*
24764 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024765 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024766 */
24767 static int
24768can_free_funccal(fc, copyID)
24769 funccall_T *fc;
24770 int copyID;
24771{
24772 return (fc->l_varlist.lv_copyID != copyID
24773 && fc->l_vars.dv_copyID != copyID
24774 && fc->l_avars.dv_copyID != copyID);
24775}
24776
24777/*
24778 * Free "fc" and what it contains.
24779 */
24780 static void
24781free_funccal(fc, free_val)
24782 funccall_T *fc;
24783 int free_val; /* a: vars were allocated */
24784{
24785 listitem_T *li;
24786
24787 /* The a: variables typevals may not have been allocated, only free the
24788 * allocated variables. */
24789 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24790
24791 /* free all l: variables */
24792 vars_clear(&fc->l_vars.dv_hashtab);
24793
24794 /* Free the a:000 variables if they were allocated. */
24795 if (free_val)
24796 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24797 clear_tv(&li->li_tv);
24798
24799 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800}
24801
24802/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024803 * Add a number variable "name" to dict "dp" with value "nr".
24804 */
24805 static void
24806add_nr_var(dp, v, name, nr)
24807 dict_T *dp;
24808 dictitem_T *v;
24809 char *name;
24810 varnumber_T nr;
24811{
24812 STRCPY(v->di_key, name);
24813 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24814 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24815 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024816 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024817 v->di_tv.vval.v_number = nr;
24818}
24819
24820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821 * ":return [expr]"
24822 */
24823 void
24824ex_return(eap)
24825 exarg_T *eap;
24826{
24827 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024828 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024829 int returning = FALSE;
24830
24831 if (current_funccal == NULL)
24832 {
24833 EMSG(_("E133: :return not inside a function"));
24834 return;
24835 }
24836
24837 if (eap->skip)
24838 ++emsg_skip;
24839
24840 eap->nextcmd = NULL;
24841 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024842 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 {
24844 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024845 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024847 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848 }
24849 /* It's safer to return also on error. */
24850 else if (!eap->skip)
24851 {
24852 /*
24853 * Return unless the expression evaluation has been cancelled due to an
24854 * aborting error, an interrupt, or an exception.
24855 */
24856 if (!aborting())
24857 returning = do_return(eap, FALSE, TRUE, NULL);
24858 }
24859
24860 /* When skipping or the return gets pending, advance to the next command
24861 * in this line (!returning). Otherwise, ignore the rest of the line.
24862 * Following lines will be ignored by get_func_line(). */
24863 if (returning)
24864 eap->nextcmd = NULL;
24865 else if (eap->nextcmd == NULL) /* no argument */
24866 eap->nextcmd = check_nextcmd(arg);
24867
24868 if (eap->skip)
24869 --emsg_skip;
24870}
24871
24872/*
24873 * Return from a function. Possibly makes the return pending. Also called
24874 * for a pending return at the ":endtry" or after returning from an extra
24875 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024876 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024877 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 * FALSE when the return gets pending.
24879 */
24880 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024881do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882 exarg_T *eap;
24883 int reanimate;
24884 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024885 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886{
24887 int idx;
24888 struct condstack *cstack = eap->cstack;
24889
24890 if (reanimate)
24891 /* Undo the return. */
24892 current_funccal->returned = FALSE;
24893
24894 /*
24895 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24896 * not in its finally clause (which then is to be executed next) is found.
24897 * In this case, make the ":return" pending for execution at the ":endtry".
24898 * Otherwise, return normally.
24899 */
24900 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24901 if (idx >= 0)
24902 {
24903 cstack->cs_pending[idx] = CSTP_RETURN;
24904
24905 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024906 /* A pending return again gets pending. "rettv" points to an
24907 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024909 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910 else
24911 {
24912 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024913 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024915 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024917 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 {
24919 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024920 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024921 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 else
24923 EMSG(_(e_outofmem));
24924 }
24925 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024926 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927
24928 if (reanimate)
24929 {
24930 /* The pending return value could be overwritten by a ":return"
24931 * without argument in a finally clause; reset the default
24932 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024933 current_funccal->rettv->v_type = VAR_NUMBER;
24934 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 }
24936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024937 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 }
24939 else
24940 {
24941 current_funccal->returned = TRUE;
24942
24943 /* If the return is carried out now, store the return value. For
24944 * a return immediately after reanimation, the value is already
24945 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024946 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024948 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024949 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024951 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 }
24953 }
24954
24955 return idx < 0;
24956}
24957
24958/*
24959 * Free the variable with a pending return value.
24960 */
24961 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024962discard_pending_return(rettv)
24963 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964{
Bram Moolenaar33570922005-01-25 22:26:29 +000024965 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966}
24967
24968/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024969 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 * is an allocated string. Used by report_pending() for verbose messages.
24971 */
24972 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024973get_return_cmd(rettv)
24974 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024976 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024977 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024978 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024980 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024981 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024982 if (s == NULL)
24983 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024984
24985 STRCPY(IObuff, ":return ");
24986 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24987 if (STRLEN(s) + 8 >= IOSIZE)
24988 STRCPY(IObuff + IOSIZE - 4, "...");
24989 vim_free(tofree);
24990 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991}
24992
24993/*
24994 * Get next function line.
24995 * Called by do_cmdline() to get the next line.
24996 * Returns allocated string, or NULL for end of function.
24997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024998 char_u *
24999get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025000 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025002 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003{
Bram Moolenaar33570922005-01-25 22:26:29 +000025004 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025005 ufunc_T *fp = fcp->func;
25006 char_u *retval;
25007 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008
25009 /* If breakpoints have been added/deleted need to check for it. */
25010 if (fcp->dbg_tick != debug_tick)
25011 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025012 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 sourcing_lnum);
25014 fcp->dbg_tick = debug_tick;
25015 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025016#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025017 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025018 func_line_end(cookie);
25019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020
Bram Moolenaar05159a02005-02-26 23:04:13 +000025021 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025022 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25023 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 retval = NULL;
25025 else
25026 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025027 /* Skip NULL lines (continuation lines). */
25028 while (fcp->linenr < gap->ga_len
25029 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25030 ++fcp->linenr;
25031 if (fcp->linenr >= gap->ga_len)
25032 retval = NULL;
25033 else
25034 {
25035 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25036 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025037#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025038 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025039 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025040#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 }
25043
25044 /* Did we encounter a breakpoint? */
25045 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25046 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025047 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025048 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025049 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050 sourcing_lnum);
25051 fcp->dbg_tick = debug_tick;
25052 }
25053
25054 return retval;
25055}
25056
Bram Moolenaar05159a02005-02-26 23:04:13 +000025057#if defined(FEAT_PROFILE) || defined(PROTO)
25058/*
25059 * Called when starting to read a function line.
25060 * "sourcing_lnum" must be correct!
25061 * When skipping lines it may not actually be executed, but we won't find out
25062 * until later and we need to store the time now.
25063 */
25064 void
25065func_line_start(cookie)
25066 void *cookie;
25067{
25068 funccall_T *fcp = (funccall_T *)cookie;
25069 ufunc_T *fp = fcp->func;
25070
25071 if (fp->uf_profiling && sourcing_lnum >= 1
25072 && sourcing_lnum <= fp->uf_lines.ga_len)
25073 {
25074 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025075 /* Skip continuation lines. */
25076 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25077 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025078 fp->uf_tml_execed = FALSE;
25079 profile_start(&fp->uf_tml_start);
25080 profile_zero(&fp->uf_tml_children);
25081 profile_get_wait(&fp->uf_tml_wait);
25082 }
25083}
25084
25085/*
25086 * Called when actually executing a function line.
25087 */
25088 void
25089func_line_exec(cookie)
25090 void *cookie;
25091{
25092 funccall_T *fcp = (funccall_T *)cookie;
25093 ufunc_T *fp = fcp->func;
25094
25095 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25096 fp->uf_tml_execed = TRUE;
25097}
25098
25099/*
25100 * Called when done with a function line.
25101 */
25102 void
25103func_line_end(cookie)
25104 void *cookie;
25105{
25106 funccall_T *fcp = (funccall_T *)cookie;
25107 ufunc_T *fp = fcp->func;
25108
25109 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25110 {
25111 if (fp->uf_tml_execed)
25112 {
25113 ++fp->uf_tml_count[fp->uf_tml_idx];
25114 profile_end(&fp->uf_tml_start);
25115 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025116 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025117 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25118 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025119 }
25120 fp->uf_tml_idx = -1;
25121 }
25122}
25123#endif
25124
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125/*
25126 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025127 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 */
25129 int
25130func_has_ended(cookie)
25131 void *cookie;
25132{
Bram Moolenaar33570922005-01-25 22:26:29 +000025133 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134
25135 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25136 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025137 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 || fcp->returned);
25139}
25140
25141/*
25142 * return TRUE if cookie indicates a function which "abort"s on errors.
25143 */
25144 int
25145func_has_abort(cookie)
25146 void *cookie;
25147{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025148 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149}
25150
25151#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25152typedef enum
25153{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025154 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25155 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25156 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157} var_flavour_T;
25158
25159static var_flavour_T var_flavour __ARGS((char_u *varname));
25160
25161 static var_flavour_T
25162var_flavour(varname)
25163 char_u *varname;
25164{
25165 char_u *p = varname;
25166
25167 if (ASCII_ISUPPER(*p))
25168 {
25169 while (*(++p))
25170 if (ASCII_ISLOWER(*p))
25171 return VAR_FLAVOUR_SESSION;
25172 return VAR_FLAVOUR_VIMINFO;
25173 }
25174 else
25175 return VAR_FLAVOUR_DEFAULT;
25176}
25177#endif
25178
25179#if defined(FEAT_VIMINFO) || defined(PROTO)
25180/*
25181 * Restore global vars that start with a capital from the viminfo file
25182 */
25183 int
25184read_viminfo_varlist(virp, writing)
25185 vir_T *virp;
25186 int writing;
25187{
25188 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025189 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025190 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025191 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025192
25193 if (!writing && (find_viminfo_parameter('!') != NULL))
25194 {
25195 tab = vim_strchr(virp->vir_line + 1, '\t');
25196 if (tab != NULL)
25197 {
25198 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025199 switch (*tab)
25200 {
25201 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025202#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025203 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025204#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025205 case 'D': type = VAR_DICT; break;
25206 case 'L': type = VAR_LIST; break;
25207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208
25209 tab = vim_strchr(tab, '\t');
25210 if (tab != NULL)
25211 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025212 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025213 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025214 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025216#ifdef FEAT_FLOAT
25217 else if (type == VAR_FLOAT)
25218 (void)string2float(tab + 1, &tv.vval.v_float);
25219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025221 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025222 if (type == VAR_DICT || type == VAR_LIST)
25223 {
25224 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25225
25226 if (etv == NULL)
25227 /* Failed to parse back the dict or list, use it as a
25228 * string. */
25229 tv.v_type = VAR_STRING;
25230 else
25231 {
25232 vim_free(tv.vval.v_string);
25233 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025234 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025235 }
25236 }
25237
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025238 /* when in a function use global variables */
25239 save_funccal = current_funccal;
25240 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025241 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025242 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025243
25244 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025245 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025246 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25247 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248 }
25249 }
25250 }
25251
25252 return viminfo_readline(virp);
25253}
25254
25255/*
25256 * Write global vars that start with a capital to the viminfo file
25257 */
25258 void
25259write_viminfo_varlist(fp)
25260 FILE *fp;
25261{
Bram Moolenaar33570922005-01-25 22:26:29 +000025262 hashitem_T *hi;
25263 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025264 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025265 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025266 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025267 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025268 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269
25270 if (find_viminfo_parameter('!') == NULL)
25271 return;
25272
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025273 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025274
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025275 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025276 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025278 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025280 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025281 this_var = HI2DI(hi);
25282 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025283 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025284 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025285 {
25286 case VAR_STRING: s = "STR"; break;
25287 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025288#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025289 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025290#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025291 case VAR_DICT: s = "DIC"; break;
25292 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025293 default: continue;
25294 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025295 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025296 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025297 if (p != NULL)
25298 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025299 vim_free(tofree);
25300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025301 }
25302 }
25303}
25304#endif
25305
25306#if defined(FEAT_SESSION) || defined(PROTO)
25307 int
25308store_session_globals(fd)
25309 FILE *fd;
25310{
Bram Moolenaar33570922005-01-25 22:26:29 +000025311 hashitem_T *hi;
25312 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025313 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314 char_u *p, *t;
25315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025316 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025317 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025319 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025321 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025322 this_var = HI2DI(hi);
25323 if ((this_var->di_tv.v_type == VAR_NUMBER
25324 || this_var->di_tv.v_type == VAR_STRING)
25325 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025326 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025327 /* Escape special characters with a backslash. Turn a LF and
25328 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025329 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025330 (char_u *)"\\\"\n\r");
25331 if (p == NULL) /* out of memory */
25332 break;
25333 for (t = p; *t != NUL; ++t)
25334 if (*t == '\n')
25335 *t = 'n';
25336 else if (*t == '\r')
25337 *t = 'r';
25338 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025339 this_var->di_key,
25340 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25341 : ' ',
25342 p,
25343 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25344 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025345 || put_eol(fd) == FAIL)
25346 {
25347 vim_free(p);
25348 return FAIL;
25349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 vim_free(p);
25351 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025352#ifdef FEAT_FLOAT
25353 else if (this_var->di_tv.v_type == VAR_FLOAT
25354 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25355 {
25356 float_T f = this_var->di_tv.vval.v_float;
25357 int sign = ' ';
25358
25359 if (f < 0)
25360 {
25361 f = -f;
25362 sign = '-';
25363 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025364 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025365 this_var->di_key, sign, f) < 0)
25366 || put_eol(fd) == FAIL)
25367 return FAIL;
25368 }
25369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 }
25371 }
25372 return OK;
25373}
25374#endif
25375
Bram Moolenaar661b1822005-07-28 22:36:45 +000025376/*
25377 * Display script name where an item was last set.
25378 * Should only be invoked when 'verbose' is non-zero.
25379 */
25380 void
25381last_set_msg(scriptID)
25382 scid_T scriptID;
25383{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025384 char_u *p;
25385
Bram Moolenaar661b1822005-07-28 22:36:45 +000025386 if (scriptID != 0)
25387 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025388 p = home_replace_save(NULL, get_scriptname(scriptID));
25389 if (p != NULL)
25390 {
25391 verbose_enter();
25392 MSG_PUTS(_("\n\tLast set from "));
25393 MSG_PUTS(p);
25394 vim_free(p);
25395 verbose_leave();
25396 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025397 }
25398}
25399
Bram Moolenaard812df62008-11-09 12:46:09 +000025400/*
25401 * List v:oldfiles in a nice way.
25402 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025403 void
25404ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025405 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025406{
25407 list_T *l = vimvars[VV_OLDFILES].vv_list;
25408 listitem_T *li;
25409 int nr = 0;
25410
25411 if (l == NULL)
25412 msg((char_u *)_("No old files"));
25413 else
25414 {
25415 msg_start();
25416 msg_scroll = TRUE;
25417 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25418 {
25419 msg_outnum((long)++nr);
25420 MSG_PUTS(": ");
25421 msg_outtrans(get_tv_string(&li->li_tv));
25422 msg_putchar('\n');
25423 out_flush(); /* output one line at a time */
25424 ui_breakcheck();
25425 }
25426 /* Assume "got_int" was set to truncate the listing. */
25427 got_int = FALSE;
25428
25429#ifdef FEAT_BROWSE_CMD
25430 if (cmdmod.browse)
25431 {
25432 quit_more = FALSE;
25433 nr = prompt_for_number(FALSE);
25434 msg_starthere();
25435 if (nr > 0)
25436 {
25437 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25438 (long)nr);
25439
25440 if (p != NULL)
25441 {
25442 p = expand_env_save(p);
25443 eap->arg = p;
25444 eap->cmdidx = CMD_edit;
25445 cmdmod.browse = FALSE;
25446 do_exedit(eap, NULL);
25447 vim_free(p);
25448 }
25449 }
25450 }
25451#endif
25452 }
25453}
25454
Bram Moolenaar53744302015-07-17 17:38:22 +020025455/* reset v:option_new, v:option_old and v:option_type */
25456 void
25457reset_v_option_vars()
25458{
25459 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25460 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25461 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25462}
25463
25464
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465#endif /* FEAT_EVAL */
25466
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025468#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469
25470#ifdef WIN3264
25471/*
25472 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25473 */
25474static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25475static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25476static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25477
25478/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025479 * Get the short path (8.3) for the filename in "fnamep".
25480 * Only works for a valid file name.
25481 * When the path gets longer "fnamep" is changed and the allocated buffer
25482 * is put in "bufp".
25483 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25484 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 */
25486 static int
25487get_short_pathname(fnamep, bufp, fnamelen)
25488 char_u **fnamep;
25489 char_u **bufp;
25490 int *fnamelen;
25491{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025492 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 char_u *newbuf;
25494
25495 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496 l = GetShortPathName(*fnamep, *fnamep, len);
25497 if (l > len - 1)
25498 {
25499 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025500 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 newbuf = vim_strnsave(*fnamep, l);
25502 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504
25505 vim_free(*bufp);
25506 *fnamep = *bufp = newbuf;
25507
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025508 /* Really should always succeed, as the buffer is big enough. */
25509 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510 }
25511
25512 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025513 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514}
25515
25516/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025517 * Get the short path (8.3) for the filename in "fname". The converted
25518 * path is returned in "bufp".
25519 *
25520 * Some of the directories specified in "fname" may not exist. This function
25521 * will shorten the existing directories at the beginning of the path and then
25522 * append the remaining non-existing path.
25523 *
25524 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025525 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025526 * bufp - Pointer to an allocated buffer for the filename.
25527 * fnamelen - Length of the filename pointed to by fname
25528 *
25529 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 */
25531 static int
25532shortpath_for_invalid_fname(fname, bufp, fnamelen)
25533 char_u **fname;
25534 char_u **bufp;
25535 int *fnamelen;
25536{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025537 char_u *short_fname, *save_fname, *pbuf_unused;
25538 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025540 int old_len, len;
25541 int new_len, sfx_len;
25542 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543
25544 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025545 old_len = *fnamelen;
25546 save_fname = vim_strnsave(*fname, old_len);
25547 pbuf_unused = NULL;
25548 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025550 endp = save_fname + old_len - 1; /* Find the end of the copy */
25551 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025553 /*
25554 * Try shortening the supplied path till it succeeds by removing one
25555 * directory at a time from the tail of the path.
25556 */
25557 len = 0;
25558 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025560 /* go back one path-separator */
25561 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25562 --endp;
25563 if (endp <= save_fname)
25564 break; /* processed the complete path */
25565
25566 /*
25567 * Replace the path separator with a NUL and try to shorten the
25568 * resulting path.
25569 */
25570 ch = *endp;
25571 *endp = 0;
25572 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025573 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025574 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25575 {
25576 retval = FAIL;
25577 goto theend;
25578 }
25579 *endp = ch; /* preserve the string */
25580
25581 if (len > 0)
25582 break; /* successfully shortened the path */
25583
25584 /* failed to shorten the path. Skip the path separator */
25585 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586 }
25587
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025588 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025590 /*
25591 * Succeeded in shortening the path. Now concatenate the shortened
25592 * path with the remaining path at the tail.
25593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025595 /* Compute the length of the new path. */
25596 sfx_len = (int)(save_endp - endp) + 1;
25597 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025599 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025601 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025603 /* There is not enough space in the currently allocated string,
25604 * copy it to a buffer big enough. */
25605 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025607 {
25608 retval = FAIL;
25609 goto theend;
25610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 }
25612 else
25613 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025614 /* Transfer short_fname to the main buffer (it's big enough),
25615 * unless get_short_pathname() did its work in-place. */
25616 *fname = *bufp = save_fname;
25617 if (short_fname != save_fname)
25618 vim_strncpy(save_fname, short_fname, len);
25619 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025621
25622 /* concat the not-shortened part of the path */
25623 vim_strncpy(*fname + len, endp, sfx_len);
25624 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025626
25627theend:
25628 vim_free(pbuf_unused);
25629 vim_free(save_fname);
25630
25631 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632}
25633
25634/*
25635 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025636 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637 */
25638 static int
25639shortpath_for_partial(fnamep, bufp, fnamelen)
25640 char_u **fnamep;
25641 char_u **bufp;
25642 int *fnamelen;
25643{
25644 int sepcount, len, tflen;
25645 char_u *p;
25646 char_u *pbuf, *tfname;
25647 int hasTilde;
25648
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025649 /* Count up the path separators from the RHS.. so we know which part
25650 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025652 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 if (vim_ispathsep(*p))
25654 ++sepcount;
25655
25656 /* Need full path first (use expand_env() to remove a "~/") */
25657 hasTilde = (**fnamep == '~');
25658 if (hasTilde)
25659 pbuf = tfname = expand_env_save(*fnamep);
25660 else
25661 pbuf = tfname = FullName_save(*fnamep, FALSE);
25662
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025663 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025665 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25666 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667
25668 if (len == 0)
25669 {
25670 /* Don't have a valid filename, so shorten the rest of the
25671 * path if we can. This CAN give us invalid 8.3 filenames, but
25672 * there's not a lot of point in guessing what it might be.
25673 */
25674 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025675 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25676 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 }
25678
25679 /* Count the paths backward to find the beginning of the desired string. */
25680 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025681 {
25682#ifdef FEAT_MBYTE
25683 if (has_mbyte)
25684 p -= mb_head_off(tfname, p);
25685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 if (vim_ispathsep(*p))
25687 {
25688 if (sepcount == 0 || (hasTilde && sepcount == 1))
25689 break;
25690 else
25691 sepcount --;
25692 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694 if (hasTilde)
25695 {
25696 --p;
25697 if (p >= tfname)
25698 *p = '~';
25699 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025700 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 }
25702 else
25703 ++p;
25704
25705 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25706 vim_free(*bufp);
25707 *fnamelen = (int)STRLEN(p);
25708 *bufp = pbuf;
25709 *fnamep = p;
25710
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025711 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712}
25713#endif /* WIN3264 */
25714
25715/*
25716 * Adjust a filename, according to a string of modifiers.
25717 * *fnamep must be NUL terminated when called. When returning, the length is
25718 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025719 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 * When there is an error, *fnamep is set to NULL.
25721 */
25722 int
25723modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25724 char_u *src; /* string with modifiers */
25725 int *usedlen; /* characters after src that are used */
25726 char_u **fnamep; /* file name so far */
25727 char_u **bufp; /* buffer for allocated file name or NULL */
25728 int *fnamelen; /* length of fnamep */
25729{
25730 int valid = 0;
25731 char_u *tail;
25732 char_u *s, *p, *pbuf;
25733 char_u dirname[MAXPATHL];
25734 int c;
25735 int has_fullname = 0;
25736#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025737 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 int has_shortname = 0;
25739#endif
25740
25741repeat:
25742 /* ":p" - full path/file_name */
25743 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25744 {
25745 has_fullname = 1;
25746
25747 valid |= VALID_PATH;
25748 *usedlen += 2;
25749
25750 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25751 if ((*fnamep)[0] == '~'
25752#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25753 && ((*fnamep)[1] == '/'
25754# ifdef BACKSLASH_IN_FILENAME
25755 || (*fnamep)[1] == '\\'
25756# endif
25757 || (*fnamep)[1] == NUL)
25758
25759#endif
25760 )
25761 {
25762 *fnamep = expand_env_save(*fnamep);
25763 vim_free(*bufp); /* free any allocated file name */
25764 *bufp = *fnamep;
25765 if (*fnamep == NULL)
25766 return -1;
25767 }
25768
25769 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025770 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771 {
25772 if (vim_ispathsep(*p)
25773 && p[1] == '.'
25774 && (p[2] == NUL
25775 || vim_ispathsep(p[2])
25776 || (p[2] == '.'
25777 && (p[3] == NUL || vim_ispathsep(p[3])))))
25778 break;
25779 }
25780
25781 /* FullName_save() is slow, don't use it when not needed. */
25782 if (*p != NUL || !vim_isAbsName(*fnamep))
25783 {
25784 *fnamep = FullName_save(*fnamep, *p != NUL);
25785 vim_free(*bufp); /* free any allocated file name */
25786 *bufp = *fnamep;
25787 if (*fnamep == NULL)
25788 return -1;
25789 }
25790
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025791#ifdef WIN3264
25792# if _WIN32_WINNT >= 0x0500
25793 if (vim_strchr(*fnamep, '~') != NULL)
25794 {
25795 /* Expand 8.3 filename to full path. Needed to make sure the same
25796 * file does not have two different names.
25797 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25798 p = alloc(_MAX_PATH + 1);
25799 if (p != NULL)
25800 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025801 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025802 {
25803 vim_free(*bufp);
25804 *bufp = *fnamep = p;
25805 }
25806 else
25807 vim_free(p);
25808 }
25809 }
25810# endif
25811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 /* Append a path separator to a directory. */
25813 if (mch_isdir(*fnamep))
25814 {
25815 /* Make room for one or two extra characters. */
25816 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25817 vim_free(*bufp); /* free any allocated file name */
25818 *bufp = *fnamep;
25819 if (*fnamep == NULL)
25820 return -1;
25821 add_pathsep(*fnamep);
25822 }
25823 }
25824
25825 /* ":." - path relative to the current directory */
25826 /* ":~" - path relative to the home directory */
25827 /* ":8" - shortname path - postponed till after */
25828 while (src[*usedlen] == ':'
25829 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25830 {
25831 *usedlen += 2;
25832 if (c == '8')
25833 {
25834#ifdef WIN3264
25835 has_shortname = 1; /* Postpone this. */
25836#endif
25837 continue;
25838 }
25839 pbuf = NULL;
25840 /* Need full path first (use expand_env() to remove a "~/") */
25841 if (!has_fullname)
25842 {
25843 if (c == '.' && **fnamep == '~')
25844 p = pbuf = expand_env_save(*fnamep);
25845 else
25846 p = pbuf = FullName_save(*fnamep, FALSE);
25847 }
25848 else
25849 p = *fnamep;
25850
25851 has_fullname = 0;
25852
25853 if (p != NULL)
25854 {
25855 if (c == '.')
25856 {
25857 mch_dirname(dirname, MAXPATHL);
25858 s = shorten_fname(p, dirname);
25859 if (s != NULL)
25860 {
25861 *fnamep = s;
25862 if (pbuf != NULL)
25863 {
25864 vim_free(*bufp); /* free any allocated file name */
25865 *bufp = pbuf;
25866 pbuf = NULL;
25867 }
25868 }
25869 }
25870 else
25871 {
25872 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25873 /* Only replace it when it starts with '~' */
25874 if (*dirname == '~')
25875 {
25876 s = vim_strsave(dirname);
25877 if (s != NULL)
25878 {
25879 *fnamep = s;
25880 vim_free(*bufp);
25881 *bufp = s;
25882 }
25883 }
25884 }
25885 vim_free(pbuf);
25886 }
25887 }
25888
25889 tail = gettail(*fnamep);
25890 *fnamelen = (int)STRLEN(*fnamep);
25891
25892 /* ":h" - head, remove "/file_name", can be repeated */
25893 /* Don't remove the first "/" or "c:\" */
25894 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25895 {
25896 valid |= VALID_HEAD;
25897 *usedlen += 2;
25898 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025899 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025900 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025901 *fnamelen = (int)(tail - *fnamep);
25902#ifdef VMS
25903 if (*fnamelen > 0)
25904 *fnamelen += 1; /* the path separator is part of the path */
25905#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025906 if (*fnamelen == 0)
25907 {
25908 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25909 p = vim_strsave((char_u *)".");
25910 if (p == NULL)
25911 return -1;
25912 vim_free(*bufp);
25913 *bufp = *fnamep = tail = p;
25914 *fnamelen = 1;
25915 }
25916 else
25917 {
25918 while (tail > s && !after_pathsep(s, tail))
25919 mb_ptr_back(*fnamep, tail);
25920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 }
25922
25923 /* ":8" - shortname */
25924 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25925 {
25926 *usedlen += 2;
25927#ifdef WIN3264
25928 has_shortname = 1;
25929#endif
25930 }
25931
25932#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025933 /*
25934 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935 */
25936 if (has_shortname)
25937 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025938 /* Copy the string if it is shortened by :h and when it wasn't copied
25939 * yet, because we are going to change it in place. Avoids changing
25940 * the buffer name for "%:8". */
25941 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 {
25943 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025944 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945 return -1;
25946 vim_free(*bufp);
25947 *bufp = *fnamep = p;
25948 }
25949
25950 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025951 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952 if (!has_fullname && !vim_isAbsName(*fnamep))
25953 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025954 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955 return -1;
25956 }
25957 else
25958 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025959 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960
Bram Moolenaardc935552011-08-17 15:23:23 +020025961 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025963 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964 return -1;
25965
25966 if (l == 0)
25967 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025968 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025970 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025971 return -1;
25972 }
25973 *fnamelen = l;
25974 }
25975 }
25976#endif /* WIN3264 */
25977
25978 /* ":t" - tail, just the basename */
25979 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25980 {
25981 *usedlen += 2;
25982 *fnamelen -= (int)(tail - *fnamep);
25983 *fnamep = tail;
25984 }
25985
25986 /* ":e" - extension, can be repeated */
25987 /* ":r" - root, without extension, can be repeated */
25988 while (src[*usedlen] == ':'
25989 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25990 {
25991 /* find a '.' in the tail:
25992 * - for second :e: before the current fname
25993 * - otherwise: The last '.'
25994 */
25995 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25996 s = *fnamep - 2;
25997 else
25998 s = *fnamep + *fnamelen - 1;
25999 for ( ; s > tail; --s)
26000 if (s[0] == '.')
26001 break;
26002 if (src[*usedlen + 1] == 'e') /* :e */
26003 {
26004 if (s > tail)
26005 {
26006 *fnamelen += (int)(*fnamep - (s + 1));
26007 *fnamep = s + 1;
26008#ifdef VMS
26009 /* cut version from the extension */
26010 s = *fnamep + *fnamelen - 1;
26011 for ( ; s > *fnamep; --s)
26012 if (s[0] == ';')
26013 break;
26014 if (s > *fnamep)
26015 *fnamelen = s - *fnamep;
26016#endif
26017 }
26018 else if (*fnamep <= tail)
26019 *fnamelen = 0;
26020 }
26021 else /* :r */
26022 {
26023 if (s > tail) /* remove one extension */
26024 *fnamelen = (int)(s - *fnamep);
26025 }
26026 *usedlen += 2;
26027 }
26028
26029 /* ":s?pat?foo?" - substitute */
26030 /* ":gs?pat?foo?" - global substitute */
26031 if (src[*usedlen] == ':'
26032 && (src[*usedlen + 1] == 's'
26033 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26034 {
26035 char_u *str;
26036 char_u *pat;
26037 char_u *sub;
26038 int sep;
26039 char_u *flags;
26040 int didit = FALSE;
26041
26042 flags = (char_u *)"";
26043 s = src + *usedlen + 2;
26044 if (src[*usedlen + 1] == 'g')
26045 {
26046 flags = (char_u *)"g";
26047 ++s;
26048 }
26049
26050 sep = *s++;
26051 if (sep)
26052 {
26053 /* find end of pattern */
26054 p = vim_strchr(s, sep);
26055 if (p != NULL)
26056 {
26057 pat = vim_strnsave(s, (int)(p - s));
26058 if (pat != NULL)
26059 {
26060 s = p + 1;
26061 /* find end of substitution */
26062 p = vim_strchr(s, sep);
26063 if (p != NULL)
26064 {
26065 sub = vim_strnsave(s, (int)(p - s));
26066 str = vim_strnsave(*fnamep, *fnamelen);
26067 if (sub != NULL && str != NULL)
26068 {
26069 *usedlen = (int)(p + 1 - src);
26070 s = do_string_sub(str, pat, sub, flags);
26071 if (s != NULL)
26072 {
26073 *fnamep = s;
26074 *fnamelen = (int)STRLEN(s);
26075 vim_free(*bufp);
26076 *bufp = s;
26077 didit = TRUE;
26078 }
26079 }
26080 vim_free(sub);
26081 vim_free(str);
26082 }
26083 vim_free(pat);
26084 }
26085 }
26086 /* after using ":s", repeat all the modifiers */
26087 if (didit)
26088 goto repeat;
26089 }
26090 }
26091
Bram Moolenaar26df0922014-02-23 23:39:13 +010026092 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26093 {
26094 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26095 if (p == NULL)
26096 return -1;
26097 vim_free(*bufp);
26098 *bufp = *fnamep = p;
26099 *fnamelen = (int)STRLEN(p);
26100 *usedlen += 2;
26101 }
26102
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 return valid;
26104}
26105
26106/*
26107 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26108 * "flags" can be "g" to do a global substitute.
26109 * Returns an allocated string, NULL for error.
26110 */
26111 char_u *
26112do_string_sub(str, pat, sub, flags)
26113 char_u *str;
26114 char_u *pat;
26115 char_u *sub;
26116 char_u *flags;
26117{
26118 int sublen;
26119 regmatch_T regmatch;
26120 int i;
26121 int do_all;
26122 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026123 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124 garray_T ga;
26125 char_u *ret;
26126 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026127 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026128
26129 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26130 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026131 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132
26133 ga_init2(&ga, 1, 200);
26134
26135 do_all = (flags[0] == 'g');
26136
26137 regmatch.rm_ic = p_ic;
26138 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26139 if (regmatch.regprog != NULL)
26140 {
26141 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026142 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026143 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26144 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026145 /* Skip empty match except for first match. */
26146 if (regmatch.startp[0] == regmatch.endp[0])
26147 {
26148 if (zero_width == regmatch.startp[0])
26149 {
26150 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026151 i = MB_PTR2LEN(tail);
26152 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26153 (size_t)i);
26154 ga.ga_len += i;
26155 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026156 continue;
26157 }
26158 zero_width = regmatch.startp[0];
26159 }
26160
Bram Moolenaar071d4272004-06-13 20:20:40 +000026161 /*
26162 * Get some space for a temporary buffer to do the substitution
26163 * into. It will contain:
26164 * - The text up to where the match is.
26165 * - The substituted text.
26166 * - The text after the match.
26167 */
26168 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026169 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026170 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26171 {
26172 ga_clear(&ga);
26173 break;
26174 }
26175
26176 /* copy the text up to where the match is */
26177 i = (int)(regmatch.startp[0] - tail);
26178 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26179 /* add the substituted text */
26180 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26181 + ga.ga_len + i, TRUE, TRUE, FALSE);
26182 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026183 tail = regmatch.endp[0];
26184 if (*tail == NUL)
26185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026186 if (!do_all)
26187 break;
26188 }
26189
26190 if (ga.ga_data != NULL)
26191 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26192
Bram Moolenaar473de612013-06-08 18:19:48 +020026193 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026194 }
26195
26196 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26197 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026198 if (p_cpo == empty_option)
26199 p_cpo = save_cpo;
26200 else
26201 /* Darn, evaluating {sub} expression changed the value. */
26202 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203
26204 return ret;
26205}
26206
26207#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */