blob: 01a4512e2e3e43c332f7d2e78a5709ba4e06071b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_abs(typval_T *argvars, typval_T *rettv);
466static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_add(typval_T *argvars, typval_T *rettv);
469static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
470static void f_and(typval_T *argvars, typval_T *rettv);
471static void f_append(typval_T *argvars, typval_T *rettv);
472static void f_argc(typval_T *argvars, typval_T *rettv);
473static void f_argidx(typval_T *argvars, typval_T *rettv);
474static void f_arglistid(typval_T *argvars, typval_T *rettv);
475static void f_argv(typval_T *argvars, typval_T *rettv);
476static void f_assert_equal(typval_T *argvars, typval_T *rettv);
477static void f_assert_exception(typval_T *argvars, typval_T *rettv);
478static void f_assert_fails(typval_T *argvars, typval_T *rettv);
479static void f_assert_false(typval_T *argvars, typval_T *rettv);
480static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_asin(typval_T *argvars, typval_T *rettv);
483static void f_atan(typval_T *argvars, typval_T *rettv);
484static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_browse(typval_T *argvars, typval_T *rettv);
487static void f_browsedir(typval_T *argvars, typval_T *rettv);
488static void f_bufexists(typval_T *argvars, typval_T *rettv);
489static void f_buflisted(typval_T *argvars, typval_T *rettv);
490static void f_bufloaded(typval_T *argvars, typval_T *rettv);
491static void f_bufname(typval_T *argvars, typval_T *rettv);
492static void f_bufnr(typval_T *argvars, typval_T *rettv);
493static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
494static void f_byte2line(typval_T *argvars, typval_T *rettv);
495static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
496static void f_byteidx(typval_T *argvars, typval_T *rettv);
497static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
498static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100502static void f_changenr(typval_T *argvars, typval_T *rettv);
503static void f_char2nr(typval_T *argvars, typval_T *rettv);
504static void f_cindent(typval_T *argvars, typval_T *rettv);
505static void f_clearmatches(typval_T *argvars, typval_T *rettv);
506static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_complete(typval_T *argvars, typval_T *rettv);
509static void f_complete_add(typval_T *argvars, typval_T *rettv);
510static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_confirm(typval_T *argvars, typval_T *rettv);
513static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_cos(typval_T *argvars, typval_T *rettv);
516static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100518#ifdef FEAT_CHANNEL
519static void f_connect(typval_T *argvars, typval_T *rettv);
520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_count(typval_T *argvars, typval_T *rettv);
522static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
523static void f_cursor(typval_T *argsvars, typval_T *rettv);
524static void f_deepcopy(typval_T *argvars, typval_T *rettv);
525static void f_delete(typval_T *argvars, typval_T *rettv);
526static void f_did_filetype(typval_T *argvars, typval_T *rettv);
527static void f_diff_filler(typval_T *argvars, typval_T *rettv);
528static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100529#ifdef FEAT_CHANNEL
530static void f_disconnect(typval_T *argvars, typval_T *rettv);
531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_empty(typval_T *argvars, typval_T *rettv);
533static void f_escape(typval_T *argvars, typval_T *rettv);
534static void f_eval(typval_T *argvars, typval_T *rettv);
535static void f_eventhandler(typval_T *argvars, typval_T *rettv);
536static void f_executable(typval_T *argvars, typval_T *rettv);
537static void f_exepath(typval_T *argvars, typval_T *rettv);
538static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_expand(typval_T *argvars, typval_T *rettv);
543static void f_extend(typval_T *argvars, typval_T *rettv);
544static void f_feedkeys(typval_T *argvars, typval_T *rettv);
545static void f_filereadable(typval_T *argvars, typval_T *rettv);
546static void f_filewritable(typval_T *argvars, typval_T *rettv);
547static void f_filter(typval_T *argvars, typval_T *rettv);
548static void f_finddir(typval_T *argvars, typval_T *rettv);
549static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_float2nr(typval_T *argvars, typval_T *rettv);
552static void f_floor(typval_T *argvars, typval_T *rettv);
553static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_fnameescape(typval_T *argvars, typval_T *rettv);
556static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
557static void f_foldclosed(typval_T *argvars, typval_T *rettv);
558static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
559static void f_foldlevel(typval_T *argvars, typval_T *rettv);
560static void f_foldtext(typval_T *argvars, typval_T *rettv);
561static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
562static void f_foreground(typval_T *argvars, typval_T *rettv);
563static void f_function(typval_T *argvars, typval_T *rettv);
564static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
565static void f_get(typval_T *argvars, typval_T *rettv);
566static void f_getbufline(typval_T *argvars, typval_T *rettv);
567static void f_getbufvar(typval_T *argvars, typval_T *rettv);
568static void f_getchar(typval_T *argvars, typval_T *rettv);
569static void f_getcharmod(typval_T *argvars, typval_T *rettv);
570static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
571static void f_getcmdline(typval_T *argvars, typval_T *rettv);
572static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
573static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
574static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
575static void f_getcwd(typval_T *argvars, typval_T *rettv);
576static void f_getfontname(typval_T *argvars, typval_T *rettv);
577static void f_getfperm(typval_T *argvars, typval_T *rettv);
578static void f_getfsize(typval_T *argvars, typval_T *rettv);
579static void f_getftime(typval_T *argvars, typval_T *rettv);
580static void f_getftype(typval_T *argvars, typval_T *rettv);
581static void f_getline(typval_T *argvars, typval_T *rettv);
582static void f_getmatches(typval_T *argvars, typval_T *rettv);
583static void f_getpid(typval_T *argvars, typval_T *rettv);
584static void f_getcurpos(typval_T *argvars, typval_T *rettv);
585static void f_getpos(typval_T *argvars, typval_T *rettv);
586static void f_getqflist(typval_T *argvars, typval_T *rettv);
587static void f_getreg(typval_T *argvars, typval_T *rettv);
588static void f_getregtype(typval_T *argvars, typval_T *rettv);
589static void f_gettabvar(typval_T *argvars, typval_T *rettv);
590static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
591static void f_getwinposx(typval_T *argvars, typval_T *rettv);
592static void f_getwinposy(typval_T *argvars, typval_T *rettv);
593static void f_getwinvar(typval_T *argvars, typval_T *rettv);
594static void f_glob(typval_T *argvars, typval_T *rettv);
595static void f_globpath(typval_T *argvars, typval_T *rettv);
596static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
597static void f_has(typval_T *argvars, typval_T *rettv);
598static void f_has_key(typval_T *argvars, typval_T *rettv);
599static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
600static void f_hasmapto(typval_T *argvars, typval_T *rettv);
601static void f_histadd(typval_T *argvars, typval_T *rettv);
602static void f_histdel(typval_T *argvars, typval_T *rettv);
603static void f_histget(typval_T *argvars, typval_T *rettv);
604static void f_histnr(typval_T *argvars, typval_T *rettv);
605static void f_hlID(typval_T *argvars, typval_T *rettv);
606static void f_hlexists(typval_T *argvars, typval_T *rettv);
607static void f_hostname(typval_T *argvars, typval_T *rettv);
608static void f_iconv(typval_T *argvars, typval_T *rettv);
609static void f_indent(typval_T *argvars, typval_T *rettv);
610static void f_index(typval_T *argvars, typval_T *rettv);
611static void f_input(typval_T *argvars, typval_T *rettv);
612static void f_inputdialog(typval_T *argvars, typval_T *rettv);
613static void f_inputlist(typval_T *argvars, typval_T *rettv);
614static void f_inputrestore(typval_T *argvars, typval_T *rettv);
615static void f_inputsave(typval_T *argvars, typval_T *rettv);
616static void f_inputsecret(typval_T *argvars, typval_T *rettv);
617static void f_insert(typval_T *argvars, typval_T *rettv);
618static void f_invert(typval_T *argvars, typval_T *rettv);
619static void f_isdirectory(typval_T *argvars, typval_T *rettv);
620static void f_islocked(typval_T *argvars, typval_T *rettv);
621static void f_items(typval_T *argvars, typval_T *rettv);
622static void f_join(typval_T *argvars, typval_T *rettv);
623static void f_jsondecode(typval_T *argvars, typval_T *rettv);
624static void f_jsonencode(typval_T *argvars, typval_T *rettv);
625static void f_keys(typval_T *argvars, typval_T *rettv);
626static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
627static void f_len(typval_T *argvars, typval_T *rettv);
628static void f_libcall(typval_T *argvars, typval_T *rettv);
629static void f_libcallnr(typval_T *argvars, typval_T *rettv);
630static void f_line(typval_T *argvars, typval_T *rettv);
631static void f_line2byte(typval_T *argvars, typval_T *rettv);
632static void f_lispindent(typval_T *argvars, typval_T *rettv);
633static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_log(typval_T *argvars, typval_T *rettv);
636static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_map(typval_T *argvars, typval_T *rettv);
642static void f_maparg(typval_T *argvars, typval_T *rettv);
643static void f_mapcheck(typval_T *argvars, typval_T *rettv);
644static void f_match(typval_T *argvars, typval_T *rettv);
645static void f_matchadd(typval_T *argvars, typval_T *rettv);
646static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
647static void f_matcharg(typval_T *argvars, typval_T *rettv);
648static void f_matchdelete(typval_T *argvars, typval_T *rettv);
649static void f_matchend(typval_T *argvars, typval_T *rettv);
650static void f_matchlist(typval_T *argvars, typval_T *rettv);
651static void f_matchstr(typval_T *argvars, typval_T *rettv);
652static void f_max(typval_T *argvars, typval_T *rettv);
653static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000654#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100658#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
662static void f_nr2char(typval_T *argvars, typval_T *rettv);
663static void f_or(typval_T *argvars, typval_T *rettv);
664static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100665#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100667#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
672static void f_printf(typval_T *argvars, typval_T *rettv);
673static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200676#endif
677#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_range(typval_T *argvars, typval_T *rettv);
681static void f_readfile(typval_T *argvars, typval_T *rettv);
682static void f_reltime(typval_T *argvars, typval_T *rettv);
683static void f_reltimestr(typval_T *argvars, typval_T *rettv);
684static void f_remote_expr(typval_T *argvars, typval_T *rettv);
685static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
686static void f_remote_peek(typval_T *argvars, typval_T *rettv);
687static void f_remote_read(typval_T *argvars, typval_T *rettv);
688static void f_remote_send(typval_T *argvars, typval_T *rettv);
689static void f_remove(typval_T *argvars, typval_T *rettv);
690static void f_rename(typval_T *argvars, typval_T *rettv);
691static void f_repeat(typval_T *argvars, typval_T *rettv);
692static void f_resolve(typval_T *argvars, typval_T *rettv);
693static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_screenattr(typval_T *argvars, typval_T *rettv);
698static void f_screenchar(typval_T *argvars, typval_T *rettv);
699static void f_screencol(typval_T *argvars, typval_T *rettv);
700static void f_screenrow(typval_T *argvars, typval_T *rettv);
701static void f_search(typval_T *argvars, typval_T *rettv);
702static void f_searchdecl(typval_T *argvars, typval_T *rettv);
703static void f_searchpair(typval_T *argvars, typval_T *rettv);
704static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
705static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100706#ifdef FEAT_CHANNEL
707static void f_sendexpr(typval_T *argvars, typval_T *rettv);
708static void f_sendraw(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_server2client(typval_T *argvars, typval_T *rettv);
711static void f_serverlist(typval_T *argvars, typval_T *rettv);
712static void f_setbufvar(typval_T *argvars, typval_T *rettv);
713static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
714static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
715static void f_setline(typval_T *argvars, typval_T *rettv);
716static void f_setloclist(typval_T *argvars, typval_T *rettv);
717static void f_setmatches(typval_T *argvars, typval_T *rettv);
718static void f_setpos(typval_T *argvars, typval_T *rettv);
719static void f_setqflist(typval_T *argvars, typval_T *rettv);
720static void f_setreg(typval_T *argvars, typval_T *rettv);
721static void f_settabvar(typval_T *argvars, typval_T *rettv);
722static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
723static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100724#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100726#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_shellescape(typval_T *argvars, typval_T *rettv);
728static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
729static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_sin(typval_T *argvars, typval_T *rettv);
732static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_sort(typval_T *argvars, typval_T *rettv);
735static void f_soundfold(typval_T *argvars, typval_T *rettv);
736static void f_spellbadword(typval_T *argvars, typval_T *rettv);
737static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
738static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_sqrt(typval_T *argvars, typval_T *rettv);
741static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_str2nr(typval_T *argvars, typval_T *rettv);
744static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000745#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000747#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_stridx(typval_T *argvars, typval_T *rettv);
749static void f_string(typval_T *argvars, typval_T *rettv);
750static void f_strlen(typval_T *argvars, typval_T *rettv);
751static void f_strpart(typval_T *argvars, typval_T *rettv);
752static void f_strridx(typval_T *argvars, typval_T *rettv);
753static void f_strtrans(typval_T *argvars, typval_T *rettv);
754static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
755static void f_strwidth(typval_T *argvars, typval_T *rettv);
756static void f_submatch(typval_T *argvars, typval_T *rettv);
757static void f_substitute(typval_T *argvars, typval_T *rettv);
758static void f_synID(typval_T *argvars, typval_T *rettv);
759static void f_synIDattr(typval_T *argvars, typval_T *rettv);
760static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
761static void f_synstack(typval_T *argvars, typval_T *rettv);
762static void f_synconcealed(typval_T *argvars, typval_T *rettv);
763static void f_system(typval_T *argvars, typval_T *rettv);
764static void f_systemlist(typval_T *argvars, typval_T *rettv);
765static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
766static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
767static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
768static void f_taglist(typval_T *argvars, typval_T *rettv);
769static void f_tagfiles(typval_T *argvars, typval_T *rettv);
770static void f_tempname(typval_T *argvars, typval_T *rettv);
771static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200772#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_tan(typval_T *argvars, typval_T *rettv);
774static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_tolower(typval_T *argvars, typval_T *rettv);
777static void f_toupper(typval_T *argvars, typval_T *rettv);
778static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000779#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000781#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_type(typval_T *argvars, typval_T *rettv);
783static void f_undofile(typval_T *argvars, typval_T *rettv);
784static void f_undotree(typval_T *argvars, typval_T *rettv);
785static void f_uniq(typval_T *argvars, typval_T *rettv);
786static void f_values(typval_T *argvars, typval_T *rettv);
787static void f_virtcol(typval_T *argvars, typval_T *rettv);
788static void f_visualmode(typval_T *argvars, typval_T *rettv);
789static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
790static void f_winbufnr(typval_T *argvars, typval_T *rettv);
791static void f_wincol(typval_T *argvars, typval_T *rettv);
792static void f_winheight(typval_T *argvars, typval_T *rettv);
793static void f_winline(typval_T *argvars, typval_T *rettv);
794static void f_winnr(typval_T *argvars, typval_T *rettv);
795static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
796static void f_winrestview(typval_T *argvars, typval_T *rettv);
797static void f_winsaveview(typval_T *argvars, typval_T *rettv);
798static void f_winwidth(typval_T *argvars, typval_T *rettv);
799static void f_writefile(typval_T *argvars, typval_T *rettv);
800static void f_wordcount(typval_T *argvars, typval_T *rettv);
801static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000802
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
804static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
805static int get_env_len(char_u **arg);
806static int get_id_len(char_u **arg);
807static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
808static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000809#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
810#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
811 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
813static int eval_isnamec(int c);
814static int eval_isnamec1(int c);
815static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
816static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100817static typval_T *alloc_string_tv(char_u *string);
818static void init_tv(typval_T *varp);
819static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100820#ifdef FEAT_FLOAT
821static float_T get_tv_float(typval_T *varp);
822#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100823static linenr_T get_tv_lnum(typval_T *argvars);
824static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
825static char_u *get_tv_string(typval_T *varp);
826static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
827static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
828static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
829static hashtab_T *find_var_ht(char_u *name, char_u **varname);
830static funccall_T *get_funccal(void);
831static void vars_clear_ext(hashtab_T *ht, int free_val);
832static void delete_var(hashtab_T *ht, hashitem_T *hi);
833static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
834static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
835static void set_var(char_u *name, typval_T *varp, int copy);
836static int var_check_ro(int flags, char_u *name, int use_gettext);
837static int var_check_fixed(int flags, char_u *name, int use_gettext);
838static int var_check_func_name(char_u *name, int new_var);
839static int valid_varname(char_u *varname);
840static int tv_check_lock(int lock, char_u *name, int use_gettext);
841static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
842static char_u *find_option_end(char_u **arg, int *opt_flags);
843static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
844static int eval_fname_script(char_u *p);
845static int eval_fname_sid(char_u *p);
846static void list_func_head(ufunc_T *fp, int indent);
847static ufunc_T *find_func(char_u *name);
848static int function_exists(char_u *name);
849static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static void func_do_profile(ufunc_T *fp);
852static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
853static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000854static int
855# ifdef __BORLANDC__
856 _RTLENTRYF
857# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100858 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000859static int
860# ifdef __BORLANDC__
861 _RTLENTRYF
862# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100863 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865static int script_autoload(char_u *name, int reload);
866static char_u *autoload_name(char_u *name);
867static void cat_func_name(char_u *buf, ufunc_T *fp);
868static void func_free(ufunc_T *fp);
869static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
870static int can_free_funccal(funccall_T *fc, int copyID) ;
871static void free_funccal(funccall_T *fc, int free_val);
872static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
873static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
874static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
875static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
876static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
877static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
878static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
879static int write_list(FILE *fd, list_T *list, int binary);
880static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882
883#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884static int compare_func_name(const void *s1, const void *s2);
885static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886#endif
887
Bram Moolenaar33570922005-01-25 22:26:29 +0000888/*
889 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000890 */
891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100892eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000893{
Bram Moolenaar33570922005-01-25 22:26:29 +0000894 int i;
895 struct vimvar *p;
896
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200897 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
898 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200899 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000900 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000901 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000902
903 for (i = 0; i < VV_LEN; ++i)
904 {
905 p = &vimvars[i];
906 STRCPY(p->vv_di.di_key, p->vv_name);
907 if (p->vv_flags & VV_RO)
908 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
909 else if (p->vv_flags & VV_RO_SBX)
910 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
911 else
912 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000913
914 /* add to v: scope dict, unless the value is not always available */
915 if (p->vv_type != VAR_UNKNOWN)
916 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000917 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000918 /* add to compat scope dict */
919 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000920 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100921 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
922
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000923 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100924 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200925 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100926 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100927
928 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
929 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
930 set_vim_var_nr(VV_NONE, VVAL_NONE);
931 set_vim_var_nr(VV_NULL, VVAL_NULL);
932
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200933 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200934
935#ifdef EBCDIC
936 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100937 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200938 */
939 sortFunctions();
940#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000941}
942
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000943#if defined(EXITFREE) || defined(PROTO)
944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000946{
947 int i;
948 struct vimvar *p;
949
950 for (i = 0; i < VV_LEN; ++i)
951 {
952 p = &vimvars[i];
953 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000954 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000955 vim_free(p->vv_str);
956 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000957 }
958 else if (p->vv_di.di_tv.v_type == VAR_LIST)
959 {
960 list_unref(p->vv_list);
961 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000962 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000963 }
964 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000965 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966 hash_clear(&compat_hashtab);
967
Bram Moolenaard9fba312005-06-26 22:34:35 +0000968 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100969# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200970 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100971# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972
973 /* global variables */
974 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000975
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000976 /* autoloaded script names */
977 ga_clear_strings(&ga_loaded);
978
Bram Moolenaarcca74132013-09-25 21:00:28 +0200979 /* Script-local variables. First clear all the variables and in a second
980 * loop free the scriptvar_T, because a variable in one script might hold
981 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200983 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200984 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200985 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200986 ga_clear(&ga_scripts);
987
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000988 /* unreferenced lists and dicts */
989 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000990
991 /* functions */
992 free_all_functions();
993 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000994}
995#endif
996
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * Return the name of the executed function.
999 */
1000 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001001func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001003 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004}
1005
1006/*
1007 * Return the address holding the next breakpoint line for a funccall cookie.
1008 */
1009 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001010func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015/*
1016 * Return the address holding the debug tick for a funccall cookie.
1017 */
1018 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001019func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * Return the nesting level for a funccall cookie.
1026 */
1027 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001028func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001034funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001036/* pointer to list of previously used funccal, still around because some
1037 * item in it is still being used. */
1038funccall_T *previous_funccal = NULL;
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040/*
1041 * Return TRUE when a function was ended by a ":return" command.
1042 */
1043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001044current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 return current_funccal->returned;
1047}
1048
1049
1050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 * Set an internal variable to a string value. Creates the variable if it does
1052 * not already exist.
1053 */
1054 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001055set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001057 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001058 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 val = vim_strsave(value);
1061 if (val != NULL)
1062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001064 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001066 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001067 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 }
1070}
1071
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074static char_u *redir_endp = NULL;
1075static char_u *redir_varname = NULL;
1076
1077/*
1078 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 * Returns OK if successfully completed the setup. FAIL otherwise.
1081 */
1082 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001083var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084{
1085 int save_emsg;
1086 int err;
1087 typval_T tv;
1088
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001089 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001090 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 {
1092 EMSG(_(e_invarg));
1093 return FAIL;
1094 }
1095
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001096 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 redir_varname = vim_strsave(name);
1098 if (redir_varname == NULL)
1099 return FAIL;
1100
1101 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1102 if (redir_lval == NULL)
1103 {
1104 var_redir_stop();
1105 return FAIL;
1106 }
1107
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 /* The output is stored in growarray "redir_ga" until redirection ends. */
1109 ga_init2(&redir_ga, (int)sizeof(char), 500);
1110
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001112 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1115 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001116 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 if (redir_endp != NULL && *redir_endp != NUL)
1118 /* Trailing characters are present after the variable name */
1119 EMSG(_(e_trailing));
1120 else
1121 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001122 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
1124 return FAIL;
1125 }
1126
1127 /* check if we can write to the variable: set it to or append an empty
1128 * string */
1129 save_emsg = did_emsg;
1130 did_emsg = FALSE;
1131 tv.v_type = VAR_STRING;
1132 tv.vval.v_string = (char_u *)"";
1133 if (append)
1134 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1135 else
1136 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001137 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001139 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (err)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 var_redir_stop();
1144 return FAIL;
1145 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
1147 return OK;
1148}
1149
1150/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 * Append "value[value_len]" to the variable set by var_redir_start().
1152 * The actual appending is postponed until redirection ends, because the value
1153 * appended may in fact be the string we write to, changing it may cause freed
1154 * memory to be used:
1155 * :redir => foo
1156 * :let foo
1157 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 */
1159 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001160var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001162 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163
1164 if (redir_lval == NULL)
1165 return;
1166
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001168 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001170 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001172 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 {
1174 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001175 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176 }
1177 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179}
1180
1181/*
1182 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001183 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 */
1185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001186var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001188 typval_T tv;
1189
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 if (redir_lval != NULL)
1191 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001192 /* If there was no error: assign the text to the variable. */
1193 if (redir_endp != NULL)
1194 {
1195 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1196 tv.v_type = VAR_STRING;
1197 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001198 /* Call get_lval() again, if it's inside a Dict or List it may
1199 * have changed. */
1200 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001201 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001202 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1203 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1204 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001205 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 /* free the collected output */
1208 vim_free(redir_ga.ga_data);
1209 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 vim_free(redir_lval);
1212 redir_lval = NULL;
1213 }
1214 vim_free(redir_varname);
1215 redir_varname = NULL;
1216}
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218# if defined(FEAT_MBYTE) || defined(PROTO)
1219 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001220eval_charconvert(
1221 char_u *enc_from,
1222 char_u *enc_to,
1223 char_u *fname_from,
1224 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1229 set_vim_var_string(VV_CC_TO, enc_to, -1);
1230 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1231 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1232 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1233 err = TRUE;
1234 set_vim_var_string(VV_CC_FROM, NULL, -1);
1235 set_vim_var_string(VV_CC_TO, NULL, -1);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1238
1239 if (err)
1240 return FAIL;
1241 return OK;
1242}
1243# endif
1244
1245# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001247eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_FNAME_IN, fname, -1);
1252 set_vim_var_string(VV_CMDARG, args, -1);
1253 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1254 err = TRUE;
1255 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1256 set_vim_var_string(VV_CMDARG, NULL, -1);
1257
1258 if (err)
1259 {
1260 mch_remove(fname);
1261 return FAIL;
1262 }
1263 return OK;
1264}
1265# endif
1266
1267# if defined(FEAT_DIFF) || defined(PROTO)
1268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001269eval_diff(
1270 char_u *origfile,
1271 char_u *newfile,
1272 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273{
1274 int err = FALSE;
1275
1276 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1277 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1278 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1279 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1280 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1281 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1282 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1283}
1284
1285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001286eval_patch(
1287 char_u *origfile,
1288 char_u *difffile,
1289 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
1291 int err;
1292
1293 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1294 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1295 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1296 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1297 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1298 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1299 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1300}
1301# endif
1302
1303/*
1304 * Top level evaluation function, returning a boolean.
1305 * Sets "error" to TRUE if there was an error.
1306 * Return TRUE or FALSE.
1307 */
1308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309eval_to_bool(
1310 char_u *arg,
1311 int *error,
1312 char_u **nextcmd,
1313 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
Bram Moolenaar33570922005-01-25 22:26:29 +00001315 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 int retval = FALSE;
1317
1318 if (skip)
1319 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001320 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 else
1323 {
1324 *error = FALSE;
1325 if (!skip)
1326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001327 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001328 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330 }
1331 if (skip)
1332 --emsg_skip;
1333
1334 return retval;
1335}
1336
1337/*
1338 * Top level evaluation function, returning a string. If "skip" is TRUE,
1339 * only parsing to "nextcmd" is done, without reporting errors. Return
1340 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1341 */
1342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001343eval_to_string_skip(
1344 char_u *arg,
1345 char_u **nextcmd,
1346 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
1350
1351 if (skip)
1352 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 retval = NULL;
1355 else
1356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 retval = vim_strsave(get_tv_string(&tv));
1358 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360 if (skip)
1361 --emsg_skip;
1362
1363 return retval;
1364}
1365
1366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001367 * Skip over an expression at "*pp".
1368 * Return FAIL for an error, OK otherwise.
1369 */
1370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001371skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001374
1375 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001377}
1378
1379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001381 * When "convert" is TRUE convert a List into a sequence of lines and convert
1382 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 * Return pointer to allocated memory, or NULL for failure.
1384 */
1385 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001386eval_to_string(
1387 char_u *arg,
1388 char_u **nextcmd,
1389 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001393 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001394#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001395 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001398 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 retval = NULL;
1400 else
1401 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001402 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 {
1404 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001405 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001406 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001407 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001408 if (tv.vval.v_list->lv_len > 0)
1409 ga_append(&ga, NL);
1410 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001411 ga_append(&ga, NUL);
1412 retval = (char_u *)ga.ga_data;
1413 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001414#ifdef FEAT_FLOAT
1415 else if (convert && tv.v_type == VAR_FLOAT)
1416 {
1417 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1418 retval = vim_strsave(numbuf);
1419 }
1420#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001421 else
1422 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425
1426 return retval;
1427}
1428
1429/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001430 * Call eval_to_string() without using current local variables and using
1431 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 */
1433 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001434eval_to_string_safe(
1435 char_u *arg,
1436 char_u **nextcmd,
1437 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438{
1439 char_u *retval;
1440 void *save_funccalp;
1441
1442 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001443 if (use_sandbox)
1444 ++sandbox;
1445 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001446 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001447 if (use_sandbox)
1448 --sandbox;
1449 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 restore_funccal(save_funccalp);
1451 return retval;
1452}
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454/*
1455 * Top level evaluation function, returning a number.
1456 * Evaluates "expr" silently.
1457 * Returns -1 for an error.
1458 */
1459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
Bram Moolenaar33570922005-01-25 22:26:29 +00001462 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001464 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466 ++emsg_off;
1467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001468 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 retval = -1;
1470 else
1471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001472 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001473 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 }
1475 --emsg_off;
1476
1477 return retval;
1478}
1479
Bram Moolenaara40058a2005-07-11 22:42:07 +00001480/*
1481 * Prepare v: variable "idx" to be used.
1482 * Save the current typeval in "save_tv".
1483 * When not used yet add the variable to the v: hashtable.
1484 */
1485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001486prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001487{
1488 *save_tv = vimvars[idx].vv_tv;
1489 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1490 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1491}
1492
1493/*
1494 * Restore v: variable "idx" to typeval "save_tv".
1495 * When no longer defined, remove the variable from the v: hashtable.
1496 */
1497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001498restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001499{
1500 hashitem_T *hi;
1501
Bram Moolenaara40058a2005-07-11 22:42:07 +00001502 vimvars[idx].vv_tv = *save_tv;
1503 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1504 {
1505 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1506 if (HASHITEM_EMPTY(hi))
1507 EMSG2(_(e_intern2), "restore_vimvar()");
1508 else
1509 hash_remove(&vimvarht, hi);
1510 }
1511}
1512
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001513#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514/*
1515 * Evaluate an expression to a list with suggestions.
1516 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001517 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001518 */
1519 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521{
1522 typval_T save_val;
1523 typval_T rettv;
1524 list_T *list = NULL;
1525 char_u *p = skipwhite(expr);
1526
1527 /* Set "v:val" to the bad word. */
1528 prepare_vimvar(VV_VAL, &save_val);
1529 vimvars[VV_VAL].vv_type = VAR_STRING;
1530 vimvars[VV_VAL].vv_str = badword;
1531 if (p_verbose == 0)
1532 ++emsg_off;
1533
1534 if (eval1(&p, &rettv, TRUE) == OK)
1535 {
1536 if (rettv.v_type != VAR_LIST)
1537 clear_tv(&rettv);
1538 else
1539 list = rettv.vval.v_list;
1540 }
1541
1542 if (p_verbose == 0)
1543 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544 restore_vimvar(VV_VAL, &save_val);
1545
1546 return list;
1547}
1548
1549/*
1550 * "list" is supposed to contain two items: a word and a number. Return the
1551 * word in "pp" and the number as the return value.
1552 * Return -1 if anything isn't right.
1553 * Used to get the good word and score from the eval_spell_expr() result.
1554 */
1555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001557{
1558 listitem_T *li;
1559
1560 li = list->lv_first;
1561 if (li == NULL)
1562 return -1;
1563 *pp = get_tv_string(&li->li_tv);
1564
1565 li = li->li_next;
1566 if (li == NULL)
1567 return -1;
1568 return get_tv_number(&li->li_tv);
1569}
1570#endif
1571
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001572/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001573 * Top level evaluation function.
1574 * Returns an allocated typval_T with the result.
1575 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001576 */
1577 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001579{
1580 typval_T *tv;
1581
1582 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001583 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584 {
1585 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001586 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001587 }
1588
1589 return tv;
1590}
1591
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 * Uses argv[argc] for the function arguments. Only Number and String
1596 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001597 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001600call_vim_function(
1601 char_u *func,
1602 int argc,
1603 char_u **argv,
1604 int safe, /* use the sandbox */
1605 int str_arg_only, /* all arguments are strings */
1606 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar33570922005-01-25 22:26:29 +00001608 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 long n;
1610 int len;
1611 int i;
1612 int doesrange;
1613 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001616 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
1620 for (i = 0; i < argc; i++)
1621 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001622 /* Pass a NULL or empty argument as an empty string */
1623 if (argv[i] == NULL || *argv[i] == NUL)
1624 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001625 argvars[i].v_type = VAR_STRING;
1626 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001627 continue;
1628 }
1629
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001630 if (str_arg_only)
1631 len = 0;
1632 else
1633 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001634 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (len != 0 && len == (int)STRLEN(argv[i]))
1636 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001637 argvars[i].v_type = VAR_NUMBER;
1638 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 else
1641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001642 argvars[i].v_type = VAR_STRING;
1643 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 }
1646
1647 if (safe)
1648 {
1649 save_funccalp = save_funccal();
1650 ++sandbox;
1651 }
1652
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1654 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (safe)
1658 {
1659 --sandbox;
1660 restore_funccal(save_funccalp);
1661 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 vim_free(argvars);
1663
1664 if (ret == FAIL)
1665 clear_tv(rettv);
1666
1667 return ret;
1668}
1669
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001670/*
1671 * Call vimL function "func" and return the result as a number.
1672 * Returns -1 when calling the function fails.
1673 * Uses argv[argc] for the function arguments.
1674 */
1675 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001676call_func_retnr(
1677 char_u *func,
1678 int argc,
1679 char_u **argv,
1680 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001681{
1682 typval_T rettv;
1683 long retval;
1684
1685 /* All arguments are passed as strings, no conversion to number. */
1686 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1687 return -1;
1688
1689 retval = get_tv_number_chk(&rettv, NULL);
1690 clear_tv(&rettv);
1691 return retval;
1692}
1693
1694#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1695 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1696
Bram Moolenaar4f688582007-07-24 12:34:30 +00001697# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001699 * Call vimL function "func" and return the result as a string.
1700 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 * Uses argv[argc] for the function arguments.
1702 */
1703 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704call_func_retstr(
1705 char_u *func,
1706 int argc,
1707 char_u **argv,
1708 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709{
1710 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001711 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001713 /* All arguments are passed as strings, no conversion to number. */
1714 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 return NULL;
1716
1717 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return retval;
1720}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001721# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001723/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001724 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001726 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 */
1728 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001729call_func_retlist(
1730 char_u *func,
1731 int argc,
1732 char_u **argv,
1733 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734{
1735 typval_T rettv;
1736
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001737 /* All arguments are passed as strings, no conversion to number. */
1738 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 return NULL;
1740
1741 if (rettv.v_type != VAR_LIST)
1742 {
1743 clear_tv(&rettv);
1744 return NULL;
1745 }
1746
1747 return rettv.vval.v_list;
1748}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749#endif
1750
1751/*
1752 * Save the current function call pointer, and set it to NULL.
1753 * Used when executing autocommands and for ":source".
1754 */
1755 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001756save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001758 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 current_funccal = NULL;
1761 return (void *)fc;
1762}
1763
1764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001767 funccall_T *fc = (funccall_T *)vfc;
1768
1769 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}
1771
Bram Moolenaar05159a02005-02-26 23:04:13 +00001772#if defined(FEAT_PROFILE) || defined(PROTO)
1773/*
1774 * Prepare profiling for entering a child or something else that is not
1775 * counted for the script/function itself.
1776 * Should always be called in pair with prof_child_exit().
1777 */
1778 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779prof_child_enter(
1780 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781{
1782 funccall_T *fc = current_funccal;
1783
1784 if (fc != NULL && fc->func->uf_profiling)
1785 profile_start(&fc->prof_child);
1786 script_prof_save(tm);
1787}
1788
1789/*
1790 * Take care of time spent in a child.
1791 * Should always be called after prof_child_enter().
1792 */
1793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794prof_child_exit(
1795 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796{
1797 funccall_T *fc = current_funccal;
1798
1799 if (fc != NULL && fc->func->uf_profiling)
1800 {
1801 profile_end(&fc->prof_child);
1802 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1803 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1804 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1805 }
1806 script_prof_restore(tm);
1807}
1808#endif
1809
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#ifdef FEAT_FOLDING
1812/*
1813 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1814 * it in "*cp". Doesn't give error messages.
1815 */
1816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 int retval;
1821 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001822 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1823 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 if (use_sandbox)
1827 ++sandbox;
1828 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 retval = 0;
1832 else
1833 {
1834 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 if (tv.v_type == VAR_NUMBER)
1836 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001837 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 retval = 0;
1839 else
1840 {
1841 /* If the result is a string, check if there is a non-digit before
1842 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (!VIM_ISDIGIT(*s) && *s != '-')
1845 *cp = *s++;
1846 retval = atol((char *)s);
1847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001851 if (use_sandbox)
1852 --sandbox;
1853 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 return retval;
1856}
1857#endif
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 * ":let" list all variable values
1861 * ":let var1 var2" list variable values
1862 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 * ":let var += expr" assignment command.
1864 * ":let var -= expr" assignment command.
1865 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
1871 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 int var_count = 0;
1876 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001878 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 argend = skip_var_list(arg, &var_count, &semicolon);
1882 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001884 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1885 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001886 expr = skipwhite(argend);
1887 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1888 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001890 /*
1891 * ":let" without "=": list variables
1892 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 if (*arg == '[')
1894 EMSG(_(e_invarg));
1895 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 list_glob_vars(&first);
1902 list_buf_vars(&first);
1903 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001904#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001906#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001907 list_script_vars(&first);
1908 list_func_vars(&first);
1909 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 eap->nextcmd = check_nextcmd(arg);
1912 }
1913 else
1914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 op[0] = '=';
1916 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001917 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001919 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1920 op[0] = *expr; /* +=, -= or .= */
1921 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001923 else
1924 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (eap->skip)
1927 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (eap->skip)
1930 {
1931 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 --emsg_skip;
1934 }
1935 else if (i != FAIL)
1936 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941 }
1942}
1943
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944/*
1945 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1946 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 * When "nextchars" is not NULL it points to a string with characters that
1948 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1949 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 * Returns OK or FAIL;
1951 */
1952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001953ex_let_vars(
1954 char_u *arg_start,
1955 typval_T *tv,
1956 int copy, /* copy values from "tv", don't move */
1957 int semicolon, /* from skip_var_list() */
1958 int var_count, /* from skip_var_list() */
1959 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960{
1961 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001962 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 listitem_T *item;
1965 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966
1967 if (*arg != '[')
1968 {
1969 /*
1970 * ":let var = expr" or ":for var in list"
1971 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001972 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 return OK;
1975 }
1976
1977 /*
1978 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1979 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001980 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 {
1982 EMSG(_(e_listreq));
1983 return FAIL;
1984 }
1985
1986 i = list_len(l);
1987 if (semicolon == 0 && var_count < i)
1988 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001989 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 return FAIL;
1991 }
1992 if (var_count - semicolon > i)
1993 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001994 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 return FAIL;
1996 }
1997
1998 item = l->lv_first;
1999 while (*arg != ']')
2000 {
2001 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 item = item->li_next;
2004 if (arg == NULL)
2005 return FAIL;
2006
2007 arg = skipwhite(arg);
2008 if (*arg == ';')
2009 {
2010 /* Put the rest of the list (may be empty) in the var after ';'.
2011 * Create a new list for this. */
2012 l = list_alloc();
2013 if (l == NULL)
2014 return FAIL;
2015 while (item != NULL)
2016 {
2017 list_append_tv(l, &item->li_tv);
2018 item = item->li_next;
2019 }
2020
2021 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002022 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 ltv.vval.v_list = l;
2024 l->lv_refcount = 1;
2025
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002026 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2027 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 clear_tv(&ltv);
2029 if (arg == NULL)
2030 return FAIL;
2031 break;
2032 }
2033 else if (*arg != ',' && *arg != ']')
2034 {
2035 EMSG2(_(e_intern2), "ex_let_vars()");
2036 return FAIL;
2037 }
2038 }
2039
2040 return OK;
2041}
2042
2043/*
2044 * Skip over assignable variable "var" or list of variables "[var, var]".
2045 * Used for ":let varvar = expr" and ":for varvar in expr".
2046 * For "[var, var]" increment "*var_count" for each variable.
2047 * for "[var, var; var]" set "semicolon".
2048 * Return NULL for an error.
2049 */
2050 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002051skip_var_list(
2052 char_u *arg,
2053 int *var_count,
2054 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055{
2056 char_u *p, *s;
2057
2058 if (*arg == '[')
2059 {
2060 /* "[var, var]": find the matching ']'. */
2061 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002062 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 {
2064 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2065 s = skip_var_one(p);
2066 if (s == p)
2067 {
2068 EMSG2(_(e_invarg2), p);
2069 return NULL;
2070 }
2071 ++*var_count;
2072
2073 p = skipwhite(s);
2074 if (*p == ']')
2075 break;
2076 else if (*p == ';')
2077 {
2078 if (*semicolon == 1)
2079 {
2080 EMSG(_("Double ; in list of variables"));
2081 return NULL;
2082 }
2083 *semicolon = 1;
2084 }
2085 else if (*p != ',')
2086 {
2087 EMSG2(_(e_invarg2), p);
2088 return NULL;
2089 }
2090 }
2091 return p + 1;
2092 }
2093 else
2094 return skip_var_one(arg);
2095}
2096
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002098 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002099 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002102skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002103{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002104 if (*arg == '@' && arg[1] != NUL)
2105 return arg + 2;
2106 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2107 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002108}
2109
Bram Moolenaara7043832005-01-21 11:56:39 +00002110/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 * List variables for hashtab "ht" with prefix "prefix".
2112 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002115list_hashtable_vars(
2116 hashtab_T *ht,
2117 char_u *prefix,
2118 int empty,
2119 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002120{
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 hashitem_T *hi;
2122 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002123 int todo;
2124
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002125 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002126 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2127 {
2128 if (!HASHITEM_EMPTY(hi))
2129 {
2130 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 di = HI2DI(hi);
2132 if (empty || di->di_tv.v_type != VAR_STRING
2133 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 }
2136 }
2137}
2138
2139/*
2140 * List global variables.
2141 */
2142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002143list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002146}
2147
2148/*
2149 * List buffer variables.
2150 */
2151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002152list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002153{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154 char_u numbuf[NUMBUFLEN];
2155
Bram Moolenaar429fa852013-04-15 12:27:36 +02002156 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158
2159 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2161 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162}
2163
2164/*
2165 * List window variables.
2166 */
2167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002168list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002169{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002170 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002172}
2173
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002174#ifdef FEAT_WINDOWS
2175/*
2176 * List tab page variables.
2177 */
2178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002179list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002180{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002181 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183}
2184#endif
2185
Bram Moolenaara7043832005-01-21 11:56:39 +00002186/*
2187 * List Vim variables.
2188 */
2189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193}
2194
2195/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196 * List script-local variables, if there is a script.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200{
2201 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2203 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204}
2205
2206/*
2207 * List function variables, if there is a function.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211{
2212 if (current_funccal != NULL)
2213 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002215}
2216
2217/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 * List variables in "arg".
2219 */
2220 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002221list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222{
2223 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 char_u *name_start;
2227 char_u *arg_subsc;
2228 char_u *tofree;
2229 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230
2231 while (!ends_excmd(*arg) && !got_int)
2232 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002235 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2237 {
2238 emsg_severe = TRUE;
2239 EMSG(_(e_trailing));
2240 break;
2241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 /* get_name_len() takes care of expanding curly braces */
2246 name_start = name = arg;
2247 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2248 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 /* This is mainly to keep test 49 working: when expanding
2251 * curly braces fails overrule the exception error message. */
2252 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 emsg_severe = TRUE;
2255 EMSG2(_(e_invarg2), arg);
2256 break;
2257 }
2258 error = TRUE;
2259 }
2260 else
2261 {
2262 if (tofree != NULL)
2263 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002264 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 else
2267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* handle d.key, l[idx], f(expr) */
2269 arg_subsc = arg;
2270 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 'g': list_glob_vars(first); break;
2279 case 'b': list_buf_vars(first); break;
2280 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002281#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002282 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002283#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002284 case 'v': list_vim_vars(first); break;
2285 case 's': list_script_vars(first); break;
2286 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 default:
2288 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
2291 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 {
2293 char_u numbuf[NUMBUFLEN];
2294 char_u *tf;
2295 int c;
2296 char_u *s;
2297
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002298 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 c = *arg;
2300 *arg = NUL;
2301 list_one_var_a((char_u *)"",
2302 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002303 tv.v_type,
2304 s == NULL ? (char_u *)"" : s,
2305 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 *arg = c;
2307 vim_free(tf);
2308 }
2309 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
2312 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313
2314 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316
2317 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 }
2319
2320 return arg;
2321}
2322
2323/*
2324 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2325 * Returns a pointer to the char just after the var name.
2326 * Returns NULL if there is an error.
2327 */
2328 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002329ex_let_one(
2330 char_u *arg, /* points to variable name */
2331 typval_T *tv, /* value to assign to variable */
2332 int copy, /* copy value from "tv" */
2333 char_u *endchars, /* valid chars after variable name or NULL */
2334 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335{
2336 int c1;
2337 char_u *name;
2338 char_u *p;
2339 char_u *arg_end = NULL;
2340 int len;
2341 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343
2344 /*
2345 * ":let $VAR = expr": Set environment variable.
2346 */
2347 if (*arg == '$')
2348 {
2349 /* Find the end of the name. */
2350 ++arg;
2351 name = arg;
2352 len = get_env_len(&arg);
2353 if (len == 0)
2354 EMSG2(_(e_invarg2), name - 1);
2355 else
2356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 if (op != NULL && (*op == '+' || *op == '-'))
2358 EMSG2(_(e_letwrong), op);
2359 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002360 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002362 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 {
2364 c1 = name[len];
2365 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 p = get_tv_string_chk(tv);
2367 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 {
2369 int mustfree = FALSE;
2370 char_u *s = vim_getenv(name, &mustfree);
2371
2372 if (s != NULL)
2373 {
2374 p = tofree = concat_str(s, p);
2375 if (mustfree)
2376 vim_free(s);
2377 }
2378 }
2379 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002380 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 if (STRICMP(name, "HOME") == 0)
2383 init_homedir();
2384 else if (didset_vim && STRICMP(name, "VIM") == 0)
2385 didset_vim = FALSE;
2386 else if (didset_vimruntime
2387 && STRICMP(name, "VIMRUNTIME") == 0)
2388 didset_vimruntime = FALSE;
2389 arg_end = arg;
2390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 }
2394 }
2395 }
2396
2397 /*
2398 * ":let &option = expr": Set option value.
2399 * ":let &l:option = expr": Set local option value.
2400 * ":let &g:option = expr": Set global option value.
2401 */
2402 else if (*arg == '&')
2403 {
2404 /* Find the end of the name. */
2405 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002406 if (p == NULL || (endchars != NULL
2407 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 EMSG(_(e_letunexp));
2409 else
2410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002411 long n;
2412 int opt_type;
2413 long numval;
2414 char_u *stringval = NULL;
2415 char_u *s;
2416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 c1 = *p;
2418 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419
2420 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002421 s = get_tv_string_chk(tv); /* != NULL if number or string */
2422 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 {
2424 opt_type = get_option_value(arg, &numval,
2425 &stringval, opt_flags);
2426 if ((opt_type == 1 && *op == '.')
2427 || (opt_type == 0 && *op != '.'))
2428 EMSG2(_(e_letwrong), op);
2429 else
2430 {
2431 if (opt_type == 1) /* number */
2432 {
2433 if (*op == '+')
2434 n = numval + n;
2435 else
2436 n = numval - n;
2437 }
2438 else if (opt_type == 0 && stringval != NULL) /* string */
2439 {
2440 s = concat_str(stringval, s);
2441 vim_free(stringval);
2442 stringval = s;
2443 }
2444 }
2445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 if (s != NULL)
2447 {
2448 set_option_value(arg, n, s, opt_flags);
2449 arg_end = p;
2450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 }
2454 }
2455
2456 /*
2457 * ":let @r = expr": Set register contents.
2458 */
2459 else if (*arg == '@')
2460 {
2461 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (op != NULL && (*op == '+' || *op == '-'))
2463 EMSG2(_(e_letwrong), op);
2464 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002465 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 EMSG(_(e_letunexp));
2467 else
2468 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002469 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 char_u *s;
2471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 p = get_tv_string_chk(tv);
2473 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002475 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 if (s != NULL)
2477 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002478 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 vim_free(s);
2480 }
2481 }
2482 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002483 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002485 arg_end = arg + 1;
2486 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 }
2489 }
2490
2491 /*
2492 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002495 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002497 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002499 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2503 EMSG(_(e_letunexp));
2504 else
2505 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 arg_end = p;
2508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512
2513 else
2514 EMSG2(_(e_invarg2), arg);
2515
2516 return arg_end;
2517}
2518
2519/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2521 */
2522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002523check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524{
2525 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2526 {
2527 EMSG2(_(e_readonlyvar), arg);
2528 return TRUE;
2529 }
2530 return FALSE;
2531}
2532
2533/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 * Get an lval: variable, Dict item or List item that can be assigned a value
2535 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2536 * "name.key", "name.key[expr]" etc.
2537 * Indexing only works if "name" is an existing List or Dictionary.
2538 * "name" points to the start of the name.
2539 * If "rettv" is not NULL it points to the value to be assigned.
2540 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2541 * wrong; must end in space or cmd separator.
2542 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002543 * flags:
2544 * GLV_QUIET: do not give error messages
2545 * GLV_NO_AUTOLOAD: do not use script autoloading
2546 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002548 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 */
2551 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002552get_lval(
2553 char_u *name,
2554 typval_T *rettv,
2555 lval_T *lp,
2556 int unlet,
2557 int skip,
2558 int flags, /* GLV_ values */
2559 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 char_u *expr_start, *expr_end;
2563 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 dictitem_T *v;
2565 typval_T var1;
2566 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002572 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576
2577 if (skip)
2578 {
2579 /* When skipping just find the end of the name. */
2580 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 }
2583
2584 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002585 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (expr_start != NULL)
2587 {
2588 /* Don't expand the name when we already know there is an error. */
2589 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2590 && *p != '[' && *p != '.')
2591 {
2592 EMSG(_(e_trailing));
2593 return NULL;
2594 }
2595
2596 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2597 if (lp->ll_exp_name == NULL)
2598 {
2599 /* Report an invalid expression in braces, unless the
2600 * expression evaluation has been cancelled due to an
2601 * aborting error, an interrupt, or an exception. */
2602 if (!aborting() && !quiet)
2603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002604 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 EMSG2(_(e_invarg2), name);
2606 return NULL;
2607 }
2608 }
2609 lp->ll_name = lp->ll_exp_name;
2610 }
2611 else
2612 lp->ll_name = name;
2613
2614 /* Without [idx] or .key we are done. */
2615 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2616 return p;
2617
2618 cc = *p;
2619 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002620 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (v == NULL && !quiet)
2622 EMSG2(_(e_undefvar), lp->ll_name);
2623 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 if (v == NULL)
2625 return NULL;
2626
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 /*
2628 * Loop until no more [idx] or .key is following.
2629 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002630 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2634 && !(lp->ll_tv->v_type == VAR_DICT
2635 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_("E689: Can only index a List or Dictionary"));
2639 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_("E708: [:] must come last"));
2645 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 len = -1;
2649 if (*p == '.')
2650 {
2651 key = p + 1;
2652 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2653 ;
2654 if (len == 0)
2655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_(e_emptykey));
2658 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 }
2660 p = key + len;
2661 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 else
2663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (*p == ':')
2667 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 else
2669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 empty1 = FALSE;
2671 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002673 if (get_tv_string_chk(&var1) == NULL)
2674 {
2675 /* not a number or string */
2676 clear_tv(&var1);
2677 return NULL;
2678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
2680
2681 /* Optionally get the second index [ :expr]. */
2682 if (*p == ':')
2683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002687 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 if (!empty1)
2689 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (rettv != NULL && (rettv->v_type != VAR_LIST
2693 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701 p = skipwhite(p + 1);
2702 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 else
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002713 if (get_tv_string_chk(&var2) == NULL)
2714 {
2715 /* not a number or string */
2716 if (!empty1)
2717 clear_tv(&var1);
2718 clear_tv(&var2);
2719 return NULL;
2720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (!quiet)
2730 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (!empty1)
2732 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737
2738 /* Skip to past ']'. */
2739 ++p;
2740 }
2741
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
2744 if (len == -1)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (*key == NUL)
2749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (!quiet)
2751 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 lp->ll_list = NULL;
2757 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002758 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002759
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002760 /* When assigning to a scope dictionary check that a function and
2761 * variable name is valid (only variable name unless it is l: or
2762 * g: dictionary). Disallow overwriting a builtin function. */
2763 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 int prevval;
2766 int wrong;
2767
2768 if (len != -1)
2769 {
2770 prevval = key[len];
2771 key[len] = NUL;
2772 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002773 else
2774 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2776 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 || !valid_varname(key);
2779 if (len != -1)
2780 key[len] = prevval;
2781 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 return NULL;
2783 }
2784
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 /* Can't add "v:" variable. */
2788 if (lp->ll_dict == &vimvardict)
2789 {
2790 EMSG2(_(e_illvar), name);
2791 return NULL;
2792 }
2793
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002794 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002798 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 if (len == -1)
2800 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 }
2803 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 if (len == -1)
2808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 p = NULL;
2811 break;
2812 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002814 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815 return NULL;
2816
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821 else
2822 {
2823 /*
2824 * Get the number and item for the only or first index of the List.
2825 */
2826 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 else
2829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002830 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 clear_tv(&var1);
2832 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002833 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 lp->ll_list = lp->ll_tv->vval.v_list;
2835 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2836 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002838 if (lp->ll_n1 < 0)
2839 {
2840 lp->ll_n1 = 0;
2841 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2842 }
2843 }
2844 if (lp->ll_li == NULL)
2845 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 if (!quiet)
2849 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 }
2852
2853 /*
2854 * May need to find the item or absolute index for the second
2855 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 * When no index given: "lp->ll_empty2" is TRUE.
2857 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002861 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 {
2868 if (!quiet)
2869 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 }
2874
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2876 if (lp->ll_n1 < 0)
2877 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2878 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 {
2880 if (!quiet)
2881 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 }
2889
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 return p;
2891}
2892
2893/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 */
2896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002897clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898{
2899 vim_free(lp->ll_exp_name);
2900 vim_free(lp->ll_newkey);
2901}
2902
2903/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002904 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 */
2908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002909set_var_lval(
2910 lval_T *lp,
2911 char_u *endp,
2912 typval_T *rettv,
2913 int copy,
2914 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915{
2916 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 listitem_T *ri;
2918 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919
2920 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 cc = *endp;
2925 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 if (op != NULL && *op != '=')
2927 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002928 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002931 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002932 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002933 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002935 if ((di == NULL
2936 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2937 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2938 FALSE)))
2939 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 set_var(lp->ll_name, &tv, FALSE);
2941 clear_tv(&tv);
2942 }
2943 }
2944 else
2945 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002949 else if (tv_check_lock(lp->ll_newkey == NULL
2950 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002951 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 else if (lp->ll_range)
2954 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002955 listitem_T *ll_li = lp->ll_li;
2956 int ll_n1 = lp->ll_n1;
2957
2958 /*
2959 * Check whether any of the list items is locked
2960 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002961 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002962 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002963 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002964 return;
2965 ri = ri->li_next;
2966 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2967 break;
2968 ll_li = ll_li->li_next;
2969 ++ll_n1;
2970 }
2971
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 /*
2973 * Assign the List values to the list items.
2974 */
2975 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002976 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 if (op != NULL && *op != '=')
2978 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2979 else
2980 {
2981 clear_tv(&lp->ll_li->li_tv);
2982 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2983 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 ri = ri->li_next;
2985 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2986 break;
2987 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002990 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002991 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 ri = NULL;
2993 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 lp->ll_li = lp->ll_li->li_next;
2997 ++lp->ll_n1;
2998 }
2999 if (ri != NULL)
3000 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 else if (lp->ll_empty2
3002 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 : lp->ll_n1 != lp->ll_n2)
3004 EMSG(_("E711: List value has not enough items"));
3005 }
3006 else
3007 {
3008 /*
3009 * Assign to a List or Dictionary item.
3010 */
3011 if (lp->ll_newkey != NULL)
3012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 if (op != NULL && *op != '=')
3014 {
3015 EMSG2(_(e_letwrong), op);
3016 return;
3017 }
3018
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003020 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 if (di == NULL)
3022 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003023 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3024 {
3025 vim_free(di);
3026 return;
3027 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003029 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 else if (op != NULL && *op != '=')
3031 {
3032 tv_op(lp->ll_tv, rettv, op);
3033 return;
3034 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003035 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003037
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003038 /*
3039 * Assign the value to the variable or list item.
3040 */
3041 if (copy)
3042 copy_tv(rettv, lp->ll_tv);
3043 else
3044 {
3045 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003046 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048 }
3049 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003050}
3051
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3054 * Returns OK or FAIL.
3055 */
3056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058{
3059 long n;
3060 char_u numbuf[NUMBUFLEN];
3061 char_u *s;
3062
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003063 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3064 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3065 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 {
3067 switch (tv1->v_type)
3068 {
3069 case VAR_DICT:
3070 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003071 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 break;
3073
3074 case VAR_LIST:
3075 if (*op != '+' || tv2->v_type != VAR_LIST)
3076 break;
3077 /* List += List */
3078 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3079 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3080 return OK;
3081
3082 case VAR_NUMBER:
3083 case VAR_STRING:
3084 if (tv2->v_type == VAR_LIST)
3085 break;
3086 if (*op == '+' || *op == '-')
3087 {
3088 /* nr += nr or nr -= nr*/
3089 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003090#ifdef FEAT_FLOAT
3091 if (tv2->v_type == VAR_FLOAT)
3092 {
3093 float_T f = n;
3094
3095 if (*op == '+')
3096 f += tv2->vval.v_float;
3097 else
3098 f -= tv2->vval.v_float;
3099 clear_tv(tv1);
3100 tv1->v_type = VAR_FLOAT;
3101 tv1->vval.v_float = f;
3102 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003104#endif
3105 {
3106 if (*op == '+')
3107 n += get_tv_number(tv2);
3108 else
3109 n -= get_tv_number(tv2);
3110 clear_tv(tv1);
3111 tv1->v_type = VAR_NUMBER;
3112 tv1->vval.v_number = n;
3113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 }
3115 else
3116 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117 if (tv2->v_type == VAR_FLOAT)
3118 break;
3119
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 /* str .= str */
3121 s = get_tv_string(tv1);
3122 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3123 clear_tv(tv1);
3124 tv1->v_type = VAR_STRING;
3125 tv1->vval.v_string = s;
3126 }
3127 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128
3129#ifdef FEAT_FLOAT
3130 case VAR_FLOAT:
3131 {
3132 float_T f;
3133
3134 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3135 && tv2->v_type != VAR_NUMBER
3136 && tv2->v_type != VAR_STRING))
3137 break;
3138 if (tv2->v_type == VAR_FLOAT)
3139 f = tv2->vval.v_float;
3140 else
3141 f = get_tv_number(tv2);
3142 if (*op == '+')
3143 tv1->vval.v_float += f;
3144 else
3145 tv1->vval.v_float -= f;
3146 }
3147 return OK;
3148#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003149 }
3150 }
3151
3152 EMSG2(_(e_letwrong), op);
3153 return FAIL;
3154}
3155
3156/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157 * Add a watcher to a list.
3158 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003160list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003161{
3162 lw->lw_next = l->lv_watch;
3163 l->lv_watch = lw;
3164}
3165
3166/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003167 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 * No warning when it isn't found...
3169 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003171list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172{
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174
3175 lwp = &l->lv_watch;
3176 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3177 {
3178 if (lw == lwrem)
3179 {
3180 *lwp = lw->lw_next;
3181 break;
3182 }
3183 lwp = &lw->lw_next;
3184 }
3185}
3186
3187/*
3188 * Just before removing an item from a list: advance watchers to the next
3189 * item.
3190 */
3191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3197 if (lw->lw_item == item)
3198 lw->lw_item = item->li_next;
3199}
3200
3201/*
3202 * Evaluate the expression used in a ":for var in expr" command.
3203 * "arg" points to "var".
3204 * Set "*errp" to TRUE for an error, FALSE otherwise;
3205 * Return a pointer that holds the info. Null when there is an error.
3206 */
3207 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003208eval_for_line(
3209 char_u *arg,
3210 int *errp,
3211 char_u **nextcmdp,
3212 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213{
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 typval_T tv;
3217 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 *errp = TRUE; /* default: there is an error */
3220
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 if (fi == NULL)
3223 return NULL;
3224
3225 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3226 if (expr == NULL)
3227 return fi;
3228
3229 expr = skipwhite(expr);
3230 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3231 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003232 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 return fi;
3234 }
3235
3236 if (skip)
3237 ++emsg_skip;
3238 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3239 {
3240 *errp = FALSE;
3241 if (!skip)
3242 {
3243 l = tv.vval.v_list;
3244 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 clear_tv(&tv);
3248 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 else
3250 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003251 /* No need to increment the refcount, it's already set for the
3252 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 fi->fi_list = l;
3254 list_add_watch(l, &fi->fi_lw);
3255 fi->fi_lw.lw_item = l->lv_first;
3256 }
3257 }
3258 }
3259 if (skip)
3260 --emsg_skip;
3261
3262 return fi;
3263}
3264
3265/*
3266 * Use the first item in a ":for" list. Advance to the next.
3267 * Assign the values to the variable (list). "arg" points to the first one.
3268 * Return TRUE when a valid item was found, FALSE when at end of list or
3269 * something wrong.
3270 */
3271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003274 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277
3278 item = fi->fi_lw.lw_item;
3279 if (item == NULL)
3280 result = FALSE;
3281 else
3282 {
3283 fi->fi_lw.lw_item = item->li_next;
3284 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3285 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3286 }
3287 return result;
3288}
3289
3290/*
3291 * Free the structure used to store info used by ":for".
3292 */
3293 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003294free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295{
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003298 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003299 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003301 list_unref(fi->fi_list);
3302 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 vim_free(fi);
3304}
3305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3307
3308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003309set_context_for_expression(
3310 expand_T *xp,
3311 char_u *arg,
3312 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 int got_eq = FALSE;
3315 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 if (cmdidx == CMD_let)
3319 {
3320 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003321 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 {
3323 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003324 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 {
3326 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 if (vim_iswhite(*p))
3329 break;
3330 }
3331 return;
3332 }
3333 }
3334 else
3335 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3336 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 while ((xp->xp_pattern = vim_strpbrk(arg,
3338 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3339 {
3340 c = *xp->xp_pattern;
3341 if (c == '&')
3342 {
3343 c = xp->xp_pattern[1];
3344 if (c == '&')
3345 {
3346 ++xp->xp_pattern;
3347 xp->xp_context = cmdidx != CMD_let || got_eq
3348 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3349 }
3350 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003353 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3354 xp->xp_pattern += 2;
3355
3356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358 else if (c == '$')
3359 {
3360 /* environment variable */
3361 xp->xp_context = EXPAND_ENV_VARS;
3362 }
3363 else if (c == '=')
3364 {
3365 got_eq = TRUE;
3366 xp->xp_context = EXPAND_EXPRESSION;
3367 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003368 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 && xp->xp_context == EXPAND_FUNCTIONS
3370 && vim_strchr(xp->xp_pattern, '(') == NULL)
3371 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003372 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 break;
3374 }
3375 else if (cmdidx != CMD_let || got_eq)
3376 {
3377 if (c == '"') /* string */
3378 {
3379 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3380 if (c == '\\' && xp->xp_pattern[1] != NUL)
3381 ++xp->xp_pattern;
3382 xp->xp_context = EXPAND_NOTHING;
3383 }
3384 else if (c == '\'') /* literal string */
3385 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003386 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3388 /* skip */ ;
3389 xp->xp_context = EXPAND_NOTHING;
3390 }
3391 else if (c == '|')
3392 {
3393 if (xp->xp_pattern[1] == '|')
3394 {
3395 ++xp->xp_pattern;
3396 xp->xp_context = EXPAND_EXPRESSION;
3397 }
3398 else
3399 xp->xp_context = EXPAND_COMMANDS;
3400 }
3401 else
3402 xp->xp_context = EXPAND_EXPRESSION;
3403 }
3404 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003405 /* Doesn't look like something valid, expand as an expression
3406 * anyway. */
3407 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 arg = xp->xp_pattern;
3409 if (*arg != NUL)
3410 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3411 /* skip */ ;
3412 }
3413 xp->xp_pattern = arg;
3414}
3415
3416#endif /* FEAT_CMDL_COMPL */
3417
3418/*
3419 * ":1,25call func(arg1, arg2)" function call.
3420 */
3421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003422ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 char_u *arg = eap->arg;
3425 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003427 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003429 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 linenr_T lnum;
3431 int doesrange;
3432 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003435 if (eap->skip)
3436 {
3437 /* trans_function_name() doesn't work well when skipping, use eval0()
3438 * instead to skip to any following command, e.g. for:
3439 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003440 ++emsg_skip;
3441 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3442 clear_tv(&rettv);
3443 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003444 return;
3445 }
3446
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003447 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003448 if (fudi.fd_newkey != NULL)
3449 {
3450 /* Still need to give an error message for missing key. */
3451 EMSG2(_(e_dictkey), fudi.fd_newkey);
3452 vim_free(fudi.fd_newkey);
3453 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003454 if (tofree == NULL)
3455 return;
3456
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003457 /* Increase refcount on dictionary, it could get deleted when evaluating
3458 * the arguments. */
3459 if (fudi.fd_dict != NULL)
3460 ++fudi.fd_dict->dv_refcount;
3461
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003462 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003463 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003464 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar532c7802005-01-27 14:44:31 +00003466 /* Skip white space to allow ":call func ()". Not good, but required for
3467 * backward compatibility. */
3468 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003469 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471 if (*startarg != '(')
3472 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003473 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 goto end;
3475 }
3476
3477 /*
3478 * When skipping, evaluate the function once, to find the end of the
3479 * arguments.
3480 * When the function takes a range, this is discovered after the first
3481 * call, and the loop is broken.
3482 */
3483 if (eap->skip)
3484 {
3485 ++emsg_skip;
3486 lnum = eap->line2; /* do it once, also with an invalid range */
3487 }
3488 else
3489 lnum = eap->line1;
3490 for ( ; lnum <= eap->line2; ++lnum)
3491 {
3492 if (!eap->skip && eap->addr_count > 0)
3493 {
3494 curwin->w_cursor.lnum = lnum;
3495 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003496#ifdef FEAT_VIRTUALEDIT
3497 curwin->w_cursor.coladd = 0;
3498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
3500 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003501 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003502 eap->line1, eap->line2, &doesrange,
3503 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 failed = TRUE;
3506 break;
3507 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003508
3509 /* Handle a function returning a Funcref, Dictionary or List. */
3510 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3511 {
3512 failed = TRUE;
3513 break;
3514 }
3515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003516 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (doesrange || eap->skip)
3518 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003521 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003523 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (aborting())
3525 break;
3526 }
3527 if (eap->skip)
3528 --emsg_skip;
3529
3530 if (!failed)
3531 {
3532 /* Check for trailing illegal characters and a following command. */
3533 if (!ends_excmd(*arg))
3534 {
3535 emsg_severe = TRUE;
3536 EMSG(_(e_trailing));
3537 }
3538 else
3539 eap->nextcmd = check_nextcmd(arg);
3540 }
3541
3542end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003543 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003544 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545}
3546
3547/*
3548 * ":unlet[!] var1 ... " command.
3549 */
3550 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003551ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003553 ex_unletlock(eap, eap->arg, 0);
3554}
3555
3556/*
3557 * ":lockvar" and ":unlockvar" commands
3558 */
3559 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003560ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003563 int deep = 2;
3564
3565 if (eap->forceit)
3566 deep = -1;
3567 else if (vim_isdigit(*arg))
3568 {
3569 deep = getdigits(&arg);
3570 arg = skipwhite(arg);
3571 }
3572
3573 ex_unletlock(eap, arg, deep);
3574}
3575
3576/*
3577 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3578 */
3579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003580ex_unletlock(
3581 exarg_T *eap,
3582 char_u *argstart,
3583 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584{
3585 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 do
3591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003593 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003594 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595 if (lv.ll_name == NULL)
3596 error = TRUE; /* error but continue parsing */
3597 if (name_end == NULL || (!vim_iswhite(*name_end)
3598 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 if (name_end != NULL)
3601 {
3602 emsg_severe = TRUE;
3603 EMSG(_(e_trailing));
3604 }
3605 if (!(eap->skip || error))
3606 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 break;
3608 }
3609
3610 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 {
3612 if (eap->cmdidx == CMD_unlet)
3613 {
3614 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3615 error = TRUE;
3616 }
3617 else
3618 {
3619 if (do_lock_var(&lv, name_end, deep,
3620 eap->cmdidx == CMD_lockvar) == FAIL)
3621 error = TRUE;
3622 }
3623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 if (!eap->skip)
3626 clear_lval(&lv);
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 arg = skipwhite(name_end);
3629 } while (!ends_excmd(*arg));
3630
3631 eap->nextcmd = check_nextcmd(arg);
3632}
3633
Bram Moolenaar8c711452005-01-14 21:53:12 +00003634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635do_unlet_var(
3636 lval_T *lp,
3637 char_u *name_end,
3638 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 int ret = OK;
3641 int cc;
3642
3643 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 cc = *name_end;
3646 *name_end = NUL;
3647
3648 /* Normal name or expanded name. */
3649 if (check_changedtick(lp->ll_name))
3650 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003655 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003656 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003657 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003658 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 else if (lp->ll_range)
3661 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003663 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003664 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003665
3666 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3667 {
3668 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003669 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003670 return FAIL;
3671 ll_li = li;
3672 ++ll_n1;
3673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674
3675 /* Delete a range of List items. */
3676 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3677 {
3678 li = lp->ll_li->li_next;
3679 listitem_remove(lp->ll_list, lp->ll_li);
3680 lp->ll_li = li;
3681 ++lp->ll_n1;
3682 }
3683 }
3684 else
3685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 /* unlet a List item. */
3688 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003691 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 }
3693
3694 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695}
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697/*
3698 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 */
3701 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003702do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
Bram Moolenaar33570922005-01-25 22:26:29 +00003704 hashtab_T *ht;
3705 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003706 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003707 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003708 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
Bram Moolenaar33570922005-01-25 22:26:29 +00003710 ht = find_var_ht(name, &varname);
3711 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003713 if (ht == &globvarht)
3714 d = &globvardict;
3715 else if (current_funccal != NULL
3716 && ht == &current_funccal->l_vars.dv_hashtab)
3717 d = &current_funccal->l_vars;
3718 else if (ht == &compat_hashtab)
3719 d = &vimvardict;
3720 else
3721 {
3722 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3723 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3724 }
3725 if (d == NULL)
3726 {
3727 EMSG2(_(e_intern2), "do_unlet()");
3728 return FAIL;
3729 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 hi = hash_find(ht, varname);
3731 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003732 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003733 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003734 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003735 || var_check_ro(di->di_flags, name, FALSE)
3736 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003737 return FAIL;
3738
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739 delete_var(ht, hi);
3740 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003743 if (forceit)
3744 return OK;
3745 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 return FAIL;
3747}
3748
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749/*
3750 * Lock or unlock variable indicated by "lp".
3751 * "deep" is the levels to go (-1 for unlimited);
3752 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3753 */
3754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003755do_lock_var(
3756 lval_T *lp,
3757 char_u *name_end,
3758 int deep,
3759 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760{
3761 int ret = OK;
3762 int cc;
3763 dictitem_T *di;
3764
3765 if (deep == 0) /* nothing to do */
3766 return OK;
3767
3768 if (lp->ll_tv == NULL)
3769 {
3770 cc = *name_end;
3771 *name_end = NUL;
3772
3773 /* Normal name or expanded name. */
3774 if (check_changedtick(lp->ll_name))
3775 ret = FAIL;
3776 else
3777 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003778 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779 if (di == NULL)
3780 ret = FAIL;
3781 else
3782 {
3783 if (lock)
3784 di->di_flags |= DI_FLAGS_LOCK;
3785 else
3786 di->di_flags &= ~DI_FLAGS_LOCK;
3787 item_lock(&di->di_tv, deep, lock);
3788 }
3789 }
3790 *name_end = cc;
3791 }
3792 else if (lp->ll_range)
3793 {
3794 listitem_T *li = lp->ll_li;
3795
3796 /* (un)lock a range of List items. */
3797 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3798 {
3799 item_lock(&li->li_tv, deep, lock);
3800 li = li->li_next;
3801 ++lp->ll_n1;
3802 }
3803 }
3804 else if (lp->ll_list != NULL)
3805 /* (un)lock a List item. */
3806 item_lock(&lp->ll_li->li_tv, deep, lock);
3807 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003808 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003809 item_lock(&lp->ll_di->di_tv, deep, lock);
3810
3811 return ret;
3812}
3813
3814/*
3815 * Lock or unlock an item. "deep" is nr of levels to go.
3816 */
3817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003818item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819{
3820 static int recurse = 0;
3821 list_T *l;
3822 listitem_T *li;
3823 dict_T *d;
3824 hashitem_T *hi;
3825 int todo;
3826
3827 if (recurse >= DICT_MAXNEST)
3828 {
3829 EMSG(_("E743: variable nested too deep for (un)lock"));
3830 return;
3831 }
3832 if (deep == 0)
3833 return;
3834 ++recurse;
3835
3836 /* lock/unlock the item itself */
3837 if (lock)
3838 tv->v_lock |= VAR_LOCKED;
3839 else
3840 tv->v_lock &= ~VAR_LOCKED;
3841
3842 switch (tv->v_type)
3843 {
3844 case VAR_LIST:
3845 if ((l = tv->vval.v_list) != NULL)
3846 {
3847 if (lock)
3848 l->lv_lock |= VAR_LOCKED;
3849 else
3850 l->lv_lock &= ~VAR_LOCKED;
3851 if (deep < 0 || deep > 1)
3852 /* recursive: lock/unlock the items the List contains */
3853 for (li = l->lv_first; li != NULL; li = li->li_next)
3854 item_lock(&li->li_tv, deep - 1, lock);
3855 }
3856 break;
3857 case VAR_DICT:
3858 if ((d = tv->vval.v_dict) != NULL)
3859 {
3860 if (lock)
3861 d->dv_lock |= VAR_LOCKED;
3862 else
3863 d->dv_lock &= ~VAR_LOCKED;
3864 if (deep < 0 || deep > 1)
3865 {
3866 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003867 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003868 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3869 {
3870 if (!HASHITEM_EMPTY(hi))
3871 {
3872 --todo;
3873 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3874 }
3875 }
3876 }
3877 }
3878 }
3879 --recurse;
3880}
3881
Bram Moolenaara40058a2005-07-11 22:42:07 +00003882/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003883 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3884 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003885 */
3886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003887tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003888{
3889 return (tv->v_lock & VAR_LOCKED)
3890 || (tv->v_type == VAR_LIST
3891 && tv->vval.v_list != NULL
3892 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3893 || (tv->v_type == VAR_DICT
3894 && tv->vval.v_dict != NULL
3895 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3896}
3897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3899/*
3900 * Delete all "menutrans_" variables.
3901 */
3902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003903del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar33570922005-01-25 22:26:29 +00003905 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003906 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Bram Moolenaar33570922005-01-25 22:26:29 +00003908 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003909 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003910 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003911 {
3912 if (!HASHITEM_EMPTY(hi))
3913 {
3914 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3916 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 }
3918 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920}
3921#endif
3922
3923#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3924
3925/*
3926 * Local string buffer for the next two functions to store a variable name
3927 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3928 * get_user_var_name().
3929 */
3930
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003931static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
3933static char_u *varnamebuf = NULL;
3934static int varnamebuflen = 0;
3935
3936/*
3937 * Function to concatenate a prefix and a variable name.
3938 */
3939 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003940cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 int len;
3943
3944 len = (int)STRLEN(name) + 3;
3945 if (len > varnamebuflen)
3946 {
3947 vim_free(varnamebuf);
3948 len += 10; /* some additional space */
3949 varnamebuf = alloc(len);
3950 if (varnamebuf == NULL)
3951 {
3952 varnamebuflen = 0;
3953 return NULL;
3954 }
3955 varnamebuflen = len;
3956 }
3957 *varnamebuf = prefix;
3958 varnamebuf[1] = ':';
3959 STRCPY(varnamebuf + 2, name);
3960 return varnamebuf;
3961}
3962
3963/*
3964 * Function given to ExpandGeneric() to obtain the list of user defined
3965 * (global/buffer/window/built-in) variable names.
3966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003968get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003970 static long_u gdone;
3971 static long_u bdone;
3972 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973#ifdef FEAT_WINDOWS
3974 static long_u tdone;
3975#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003976 static int vidx;
3977 static hashitem_T *hi;
3978 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003982 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003983#ifdef FEAT_WINDOWS
3984 tdone = 0;
3985#endif
3986 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003987
3988 /* Global variables */
3989 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003991 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003993 else
3994 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003995 while (HASHITEM_EMPTY(hi))
3996 ++hi;
3997 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3998 return cat_prefix_varname('g', hi->hi_key);
3999 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004001
4002 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004003 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004004 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004006 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004008 else
4009 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004010 while (HASHITEM_EMPTY(hi))
4011 ++hi;
4012 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004014 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return (char_u *)"b:changedtick";
4018 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004019
4020 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004021 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004024 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004026 else
4027 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 while (HASHITEM_EMPTY(hi))
4029 ++hi;
4030 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004033#ifdef FEAT_WINDOWS
4034 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004035 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 if (tdone < ht->ht_used)
4037 {
4038 if (tdone++ == 0)
4039 hi = ht->ht_array;
4040 else
4041 ++hi;
4042 while (HASHITEM_EMPTY(hi))
4043 ++hi;
4044 return cat_prefix_varname('t', hi->hi_key);
4045 }
4046#endif
4047
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 /* v: variables */
4049 if (vidx < VV_LEN)
4050 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 vim_free(varnamebuf);
4053 varnamebuf = NULL;
4054 varnamebuflen = 0;
4055 return NULL;
4056}
4057
4058#endif /* FEAT_CMDL_COMPL */
4059
4060/*
4061 * types for expressions.
4062 */
4063typedef enum
4064{
4065 TYPE_UNKNOWN = 0
4066 , TYPE_EQUAL /* == */
4067 , TYPE_NEQUAL /* != */
4068 , TYPE_GREATER /* > */
4069 , TYPE_GEQUAL /* >= */
4070 , TYPE_SMALLER /* < */
4071 , TYPE_SEQUAL /* <= */
4072 , TYPE_MATCH /* =~ */
4073 , TYPE_NOMATCH /* !~ */
4074} exptype_T;
4075
4076/*
4077 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4080 */
4081
4082/*
4083 * Handle zero level expression.
4084 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004086 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Return OK or FAIL.
4088 */
4089 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004090eval0(
4091 char_u *arg,
4092 typval_T *rettv,
4093 char_u **nextcmd,
4094 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
4096 int ret;
4097 char_u *p;
4098
4099 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (ret == FAIL || !ends_excmd(*p))
4102 {
4103 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /*
4106 * Report the invalid expression unless the expression evaluation has
4107 * been cancelled due to an aborting error, an interrupt, or an
4108 * exception.
4109 */
4110 if (!aborting())
4111 EMSG2(_(e_invexpr2), arg);
4112 ret = FAIL;
4113 }
4114 if (nextcmd != NULL)
4115 *nextcmd = check_nextcmd(p);
4116
4117 return ret;
4118}
4119
4120/*
4121 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004122 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 *
4124 * "arg" must point to the first non-white of the expression.
4125 * "arg" is advanced to the next non-white after the recognized expression.
4126 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004127 * Note: "rettv.v_lock" is not set.
4128 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * Return OK or FAIL.
4130 */
4131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004132eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004135 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
4137 /*
4138 * Get the first variable.
4139 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
4142
4143 if ((*arg)[0] == '?')
4144 {
4145 result = FALSE;
4146 if (evaluate)
4147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004148 int error = FALSE;
4149
4150 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 if (error)
4154 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156
4157 /*
4158 * Get the second variable.
4159 */
4160 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 return FAIL;
4163
4164 /*
4165 * Check for the ":".
4166 */
4167 if ((*arg)[0] != ':')
4168 {
4169 EMSG(_("E109: Missing ':' after '?'"));
4170 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return FAIL;
4173 }
4174
4175 /*
4176 * Get the third variable.
4177 */
4178 *arg = skipwhite(*arg + 1);
4179 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4180 {
4181 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184 }
4185 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188
4189 return OK;
4190}
4191
4192/*
4193 * Handle first level expression:
4194 * expr2 || expr2 || expr2 logical OR
4195 *
4196 * "arg" must point to the first non-white of the expression.
4197 * "arg" is advanced to the next non-white after the recognized expression.
4198 *
4199 * Return OK or FAIL.
4200 */
4201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004202eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
Bram Moolenaar33570922005-01-25 22:26:29 +00004204 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 long result;
4206 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209 /*
4210 * Get the first variable.
4211 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214
4215 /*
4216 * Repeat until there is no following "||".
4217 */
4218 first = TRUE;
4219 result = FALSE;
4220 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4221 {
4222 if (evaluate && first)
4223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 if (error)
4228 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 first = FALSE;
4230 }
4231
4232 /*
4233 * Get the second variable.
4234 */
4235 *arg = skipwhite(*arg + 2);
4236 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4237 return FAIL;
4238
4239 /*
4240 * Compute the result.
4241 */
4242 if (evaluate && !result)
4243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (error)
4248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 if (evaluate)
4251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 rettv->v_type = VAR_NUMBER;
4253 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 }
4256
4257 return OK;
4258}
4259
4260/*
4261 * Handle second level expression:
4262 * expr3 && expr3 && expr3 logical AND
4263 *
4264 * "arg" must point to the first non-white of the expression.
4265 * "arg" is advanced to the next non-white after the recognized expression.
4266 *
4267 * Return OK or FAIL.
4268 */
4269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004270eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
Bram Moolenaar33570922005-01-25 22:26:29 +00004272 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 long result;
4274 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /*
4278 * Get the first variable.
4279 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 return FAIL;
4282
4283 /*
4284 * Repeat until there is no following "&&".
4285 */
4286 first = TRUE;
4287 result = TRUE;
4288 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4289 {
4290 if (evaluate && first)
4291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 if (error)
4296 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 first = FALSE;
4298 }
4299
4300 /*
4301 * Get the second variable.
4302 */
4303 *arg = skipwhite(*arg + 2);
4304 if (eval4(arg, &var2, evaluate && result) == FAIL)
4305 return FAIL;
4306
4307 /*
4308 * Compute the result.
4309 */
4310 if (evaluate && result)
4311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 if (error)
4316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318 if (evaluate)
4319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 rettv->v_type = VAR_NUMBER;
4321 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 }
4324
4325 return OK;
4326}
4327
4328/*
4329 * Handle third level expression:
4330 * var1 == var2
4331 * var1 =~ var2
4332 * var1 != var2
4333 * var1 !~ var2
4334 * var1 > var2
4335 * var1 >= var2
4336 * var1 < var2
4337 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004338 * var1 is var2
4339 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 *
4341 * "arg" must point to the first non-white of the expression.
4342 * "arg" is advanced to the next non-white after the recognized expression.
4343 *
4344 * Return OK or FAIL.
4345 */
4346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004347eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
Bram Moolenaar33570922005-01-25 22:26:29 +00004349 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 char_u *p;
4351 int i;
4352 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004353 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 int len = 2;
4355 long n1, n2;
4356 char_u *s1, *s2;
4357 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4358 regmatch_T regmatch;
4359 int ic;
4360 char_u *save_cpo;
4361
4362 /*
4363 * Get the first variable.
4364 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 return FAIL;
4367
4368 p = *arg;
4369 switch (p[0])
4370 {
4371 case '=': if (p[1] == '=')
4372 type = TYPE_EQUAL;
4373 else if (p[1] == '~')
4374 type = TYPE_MATCH;
4375 break;
4376 case '!': if (p[1] == '=')
4377 type = TYPE_NEQUAL;
4378 else if (p[1] == '~')
4379 type = TYPE_NOMATCH;
4380 break;
4381 case '>': if (p[1] != '=')
4382 {
4383 type = TYPE_GREATER;
4384 len = 1;
4385 }
4386 else
4387 type = TYPE_GEQUAL;
4388 break;
4389 case '<': if (p[1] != '=')
4390 {
4391 type = TYPE_SMALLER;
4392 len = 1;
4393 }
4394 else
4395 type = TYPE_SEQUAL;
4396 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 case 'i': if (p[1] == 's')
4398 {
4399 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4400 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004401 i = p[len];
4402 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 {
4404 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4405 type_is = TRUE;
4406 }
4407 }
4408 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410
4411 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004412 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 */
4414 if (type != TYPE_UNKNOWN)
4415 {
4416 /* extra question mark appended: ignore case */
4417 if (p[len] == '?')
4418 {
4419 ic = TRUE;
4420 ++len;
4421 }
4422 /* extra '#' appended: match case */
4423 else if (p[len] == '#')
4424 {
4425 ic = FALSE;
4426 ++len;
4427 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004428 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 else
4430 ic = p_ic;
4431
4432 /*
4433 * Get the second variable.
4434 */
4435 *arg = skipwhite(p + len);
4436 if (eval5(arg, &var2, evaluate) == FAIL)
4437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 return FAIL;
4440 }
4441
4442 if (evaluate)
4443 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 if (type_is && rettv->v_type != var2.v_type)
4445 {
4446 /* For "is" a different type always means FALSE, for "notis"
4447 * it means TRUE. */
4448 n1 = (type == TYPE_NEQUAL);
4449 }
4450 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4451 {
4452 if (type_is)
4453 {
4454 n1 = (rettv->v_type == var2.v_type
4455 && rettv->vval.v_list == var2.vval.v_list);
4456 if (type == TYPE_NEQUAL)
4457 n1 = !n1;
4458 }
4459 else if (rettv->v_type != var2.v_type
4460 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4461 {
4462 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004463 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004465 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004466 clear_tv(rettv);
4467 clear_tv(&var2);
4468 return FAIL;
4469 }
4470 else
4471 {
4472 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004473 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4474 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004475 if (type == TYPE_NEQUAL)
4476 n1 = !n1;
4477 }
4478 }
4479
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004480 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4481 {
4482 if (type_is)
4483 {
4484 n1 = (rettv->v_type == var2.v_type
4485 && rettv->vval.v_dict == var2.vval.v_dict);
4486 if (type == TYPE_NEQUAL)
4487 n1 = !n1;
4488 }
4489 else if (rettv->v_type != var2.v_type
4490 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4491 {
4492 if (rettv->v_type != var2.v_type)
4493 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4494 else
4495 EMSG(_("E736: Invalid operation for Dictionary"));
4496 clear_tv(rettv);
4497 clear_tv(&var2);
4498 return FAIL;
4499 }
4500 else
4501 {
4502 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004503 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4504 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 }
4509
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004510 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4511 {
4512 if (rettv->v_type != var2.v_type
4513 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4514 {
4515 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004516 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004518 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004519 clear_tv(rettv);
4520 clear_tv(&var2);
4521 return FAIL;
4522 }
4523 else
4524 {
4525 /* Compare two Funcrefs for being equal or unequal. */
4526 if (rettv->vval.v_string == NULL
4527 || var2.vval.v_string == NULL)
4528 n1 = FALSE;
4529 else
4530 n1 = STRCMP(rettv->vval.v_string,
4531 var2.vval.v_string) == 0;
4532 if (type == TYPE_NEQUAL)
4533 n1 = !n1;
4534 }
4535 }
4536
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537#ifdef FEAT_FLOAT
4538 /*
4539 * If one of the two variables is a float, compare as a float.
4540 * When using "=~" or "!~", always compare as string.
4541 */
4542 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4543 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4544 {
4545 float_T f1, f2;
4546
4547 if (rettv->v_type == VAR_FLOAT)
4548 f1 = rettv->vval.v_float;
4549 else
4550 f1 = get_tv_number(rettv);
4551 if (var2.v_type == VAR_FLOAT)
4552 f2 = var2.vval.v_float;
4553 else
4554 f2 = get_tv_number(&var2);
4555 n1 = FALSE;
4556 switch (type)
4557 {
4558 case TYPE_EQUAL: n1 = (f1 == f2); break;
4559 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4560 case TYPE_GREATER: n1 = (f1 > f2); break;
4561 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4562 case TYPE_SMALLER: n1 = (f1 < f2); break;
4563 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4564 case TYPE_UNKNOWN:
4565 case TYPE_MATCH:
4566 case TYPE_NOMATCH: break; /* avoid gcc warning */
4567 }
4568 }
4569#endif
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 /*
4572 * If one of the two variables is a number, compare as a number.
4573 * When using "=~" or "!~", always compare as string.
4574 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 n1 = get_tv_number(rettv);
4579 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 switch (type)
4581 {
4582 case TYPE_EQUAL: n1 = (n1 == n2); break;
4583 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4584 case TYPE_GREATER: n1 = (n1 > n2); break;
4585 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4586 case TYPE_SMALLER: n1 = (n1 < n2); break;
4587 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4588 case TYPE_UNKNOWN:
4589 case TYPE_MATCH:
4590 case TYPE_NOMATCH: break; /* avoid gcc warning */
4591 }
4592 }
4593 else
4594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 s1 = get_tv_string_buf(rettv, buf1);
4596 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4598 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4599 else
4600 i = 0;
4601 n1 = FALSE;
4602 switch (type)
4603 {
4604 case TYPE_EQUAL: n1 = (i == 0); break;
4605 case TYPE_NEQUAL: n1 = (i != 0); break;
4606 case TYPE_GREATER: n1 = (i > 0); break;
4607 case TYPE_GEQUAL: n1 = (i >= 0); break;
4608 case TYPE_SMALLER: n1 = (i < 0); break;
4609 case TYPE_SEQUAL: n1 = (i <= 0); break;
4610
4611 case TYPE_MATCH:
4612 case TYPE_NOMATCH:
4613 /* avoid 'l' flag in 'cpoptions' */
4614 save_cpo = p_cpo;
4615 p_cpo = (char_u *)"";
4616 regmatch.regprog = vim_regcomp(s2,
4617 RE_MAGIC + RE_STRING);
4618 regmatch.rm_ic = ic;
4619 if (regmatch.regprog != NULL)
4620 {
4621 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004622 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (type == TYPE_NOMATCH)
4624 n1 = !n1;
4625 }
4626 p_cpo = save_cpo;
4627 break;
4628
4629 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4630 }
4631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 clear_tv(rettv);
4633 clear_tv(&var2);
4634 rettv->v_type = VAR_NUMBER;
4635 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637 }
4638
4639 return OK;
4640}
4641
4642/*
4643 * Handle fourth level expression:
4644 * + number addition
4645 * - number subtraction
4646 * . string concatenation
4647 *
4648 * "arg" must point to the first non-white of the expression.
4649 * "arg" is advanced to the next non-white after the recognized expression.
4650 *
4651 * Return OK or FAIL.
4652 */
4653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004654eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
Bram Moolenaar33570922005-01-25 22:26:29 +00004656 typval_T var2;
4657 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 int op;
4659 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004660#ifdef FEAT_FLOAT
4661 float_T f1 = 0, f2 = 0;
4662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 char_u *s1, *s2;
4664 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4665 char_u *p;
4666
4667 /*
4668 * Get the first variable.
4669 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004670 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return FAIL;
4672
4673 /*
4674 * Repeat computing, until no '+', '-' or '.' is following.
4675 */
4676 for (;;)
4677 {
4678 op = **arg;
4679 if (op != '+' && op != '-' && op != '.')
4680 break;
4681
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004682 if ((op != '+' || rettv->v_type != VAR_LIST)
4683#ifdef FEAT_FLOAT
4684 && (op == '.' || rettv->v_type != VAR_FLOAT)
4685#endif
4686 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687 {
4688 /* For "list + ...", an illegal use of the first operand as
4689 * a number cannot be determined before evaluating the 2nd
4690 * operand: if this is also a list, all is ok.
4691 * For "something . ...", "something - ..." or "non-list + ...",
4692 * we know that the first operand needs to be a string or number
4693 * without evaluating the 2nd operand. So check before to avoid
4694 * side effects after an error. */
4695 if (evaluate && get_tv_string_chk(rettv) == NULL)
4696 {
4697 clear_tv(rettv);
4698 return FAIL;
4699 }
4700 }
4701
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /*
4703 * Get the second variable.
4704 */
4705 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004708 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return FAIL;
4710 }
4711
4712 if (evaluate)
4713 {
4714 /*
4715 * Compute the result.
4716 */
4717 if (op == '.')
4718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4720 s2 = get_tv_string_buf_chk(&var2, buf2);
4721 if (s2 == NULL) /* type error ? */
4722 {
4723 clear_tv(rettv);
4724 clear_tv(&var2);
4725 return FAIL;
4726 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004727 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 clear_tv(rettv);
4729 rettv->v_type = VAR_STRING;
4730 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004732 else if (op == '+' && rettv->v_type == VAR_LIST
4733 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004734 {
4735 /* concatenate Lists */
4736 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4737 &var3) == FAIL)
4738 {
4739 clear_tv(rettv);
4740 clear_tv(&var2);
4741 return FAIL;
4742 }
4743 clear_tv(rettv);
4744 *rettv = var3;
4745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 else
4747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 int error = FALSE;
4749
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004750#ifdef FEAT_FLOAT
4751 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753 f1 = rettv->vval.v_float;
4754 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756 else
4757#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004758 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759 n1 = get_tv_number_chk(rettv, &error);
4760 if (error)
4761 {
4762 /* This can only happen for "list + non-list". For
4763 * "non-list + ..." or "something - ...", we returned
4764 * before evaluating the 2nd operand. */
4765 clear_tv(rettv);
4766 return FAIL;
4767 }
4768#ifdef FEAT_FLOAT
4769 if (var2.v_type == VAR_FLOAT)
4770 f1 = n1;
4771#endif
4772 }
4773#ifdef FEAT_FLOAT
4774 if (var2.v_type == VAR_FLOAT)
4775 {
4776 f2 = var2.vval.v_float;
4777 n2 = 0;
4778 }
4779 else
4780#endif
4781 {
4782 n2 = get_tv_number_chk(&var2, &error);
4783 if (error)
4784 {
4785 clear_tv(rettv);
4786 clear_tv(&var2);
4787 return FAIL;
4788 }
4789#ifdef FEAT_FLOAT
4790 if (rettv->v_type == VAR_FLOAT)
4791 f2 = n2;
4792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795
4796#ifdef FEAT_FLOAT
4797 /* If there is a float on either side the result is a float. */
4798 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4799 {
4800 if (op == '+')
4801 f1 = f1 + f2;
4802 else
4803 f1 = f1 - f2;
4804 rettv->v_type = VAR_FLOAT;
4805 rettv->vval.v_float = f1;
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#endif
4809 {
4810 if (op == '+')
4811 n1 = n1 + n2;
4812 else
4813 n1 = n1 - n2;
4814 rettv->v_type = VAR_NUMBER;
4815 rettv->vval.v_number = n1;
4816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820 }
4821 return OK;
4822}
4823
4824/*
4825 * Handle fifth level expression:
4826 * * number multiplication
4827 * / number division
4828 * % number modulo
4829 *
4830 * "arg" must point to the first non-white of the expression.
4831 * "arg" is advanced to the next non-white after the recognized expression.
4832 *
4833 * Return OK or FAIL.
4834 */
4835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004836eval6(
4837 char_u **arg,
4838 typval_T *rettv,
4839 int evaluate,
4840 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaar33570922005-01-25 22:26:29 +00004842 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 int op;
4844 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#ifdef FEAT_FLOAT
4846 int use_float = FALSE;
4847 float_T f1 = 0, f2;
4848#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004849 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 /*
4852 * Get the first variable.
4853 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004854 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return FAIL;
4856
4857 /*
4858 * Repeat computing, until no '*', '/' or '%' is following.
4859 */
4860 for (;;)
4861 {
4862 op = **arg;
4863 if (op != '*' && op != '/' && op != '%')
4864 break;
4865
4866 if (evaluate)
4867 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868#ifdef FEAT_FLOAT
4869 if (rettv->v_type == VAR_FLOAT)
4870 {
4871 f1 = rettv->vval.v_float;
4872 use_float = TRUE;
4873 n1 = 0;
4874 }
4875 else
4876#endif
4877 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 if (error)
4880 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 else
4883 n1 = 0;
4884
4885 /*
4886 * Get the second variable.
4887 */
4888 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004889 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return FAIL;
4891
4892 if (evaluate)
4893 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#ifdef FEAT_FLOAT
4895 if (var2.v_type == VAR_FLOAT)
4896 {
4897 if (!use_float)
4898 {
4899 f1 = n1;
4900 use_float = TRUE;
4901 }
4902 f2 = var2.vval.v_float;
4903 n2 = 0;
4904 }
4905 else
4906#endif
4907 {
4908 n2 = get_tv_number_chk(&var2, &error);
4909 clear_tv(&var2);
4910 if (error)
4911 return FAIL;
4912#ifdef FEAT_FLOAT
4913 if (use_float)
4914 f2 = n2;
4915#endif
4916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 /*
4919 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922#ifdef FEAT_FLOAT
4923 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925 if (op == '*')
4926 f1 = f1 * f2;
4927 else if (op == '/')
4928 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004929# ifdef VMS
4930 /* VMS crashes on divide by zero, work around it */
4931 if (f2 == 0.0)
4932 {
4933 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004934 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004935 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004936 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004937 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004938 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004939 }
4940 else
4941 f1 = f1 / f2;
4942# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943 /* We rely on the floating point library to handle divide
4944 * by zero to result in "inf" and not a crash. */
4945 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004946# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004950 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951 return FAIL;
4952 }
4953 rettv->v_type = VAR_FLOAT;
4954 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004959 if (op == '*')
4960 n1 = n1 * n2;
4961 else if (op == '/')
4962 {
4963 if (n2 == 0) /* give an error message? */
4964 {
4965 if (n1 == 0)
4966 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4967 else if (n1 < 0)
4968 n1 = -0x7fffffffL;
4969 else
4970 n1 = 0x7fffffffL;
4971 }
4972 else
4973 n1 = n1 / n2;
4974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 {
4977 if (n2 == 0) /* give an error message? */
4978 n1 = 0;
4979 else
4980 n1 = n1 % n2;
4981 }
4982 rettv->v_type = VAR_NUMBER;
4983 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 }
4987
4988 return OK;
4989}
4990
4991/*
4992 * Handle sixth level expression:
4993 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004994 * "string" string constant
4995 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 * &option-name option value
4997 * @r register contents
4998 * identifier variable value
4999 * function() function call
5000 * $VAR environment variable
5001 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005002 * [expr, expr] List
5003 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 *
5005 * Also handle:
5006 * ! in front logical NOT
5007 * - in front unary minus
5008 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 * trailing [] subscript in String or List
5010 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 *
5012 * "arg" must point to the first non-white of the expression.
5013 * "arg" is advanced to the next non-white after the recognized expression.
5014 *
5015 * Return OK or FAIL.
5016 */
5017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005018eval7(
5019 char_u **arg,
5020 typval_T *rettv,
5021 int evaluate,
5022 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 long n;
5025 int len;
5026 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 char_u *start_leader, *end_leader;
5028 int ret = OK;
5029 char_u *alias;
5030
5031 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005032 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005033 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005035 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 /*
5038 * Skip '!' and '-' characters. They are handled later.
5039 */
5040 start_leader = *arg;
5041 while (**arg == '!' || **arg == '-' || **arg == '+')
5042 *arg = skipwhite(*arg + 1);
5043 end_leader = *arg;
5044
5045 switch (**arg)
5046 {
5047 /*
5048 * Number constant.
5049 */
5050 case '0':
5051 case '1':
5052 case '2':
5053 case '3':
5054 case '4':
5055 case '5':
5056 case '6':
5057 case '7':
5058 case '8':
5059 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005060 {
5061#ifdef FEAT_FLOAT
5062 char_u *p = skipdigits(*arg + 1);
5063 int get_float = FALSE;
5064
5065 /* We accept a float when the format matches
5066 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005067 * strict to avoid backwards compatibility problems.
5068 * Don't look for a float after the "." operator, so that
5069 * ":let vers = 1.2.3" doesn't fail. */
5070 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072 get_float = TRUE;
5073 p = skipdigits(p + 2);
5074 if (*p == 'e' || *p == 'E')
5075 {
5076 ++p;
5077 if (*p == '-' || *p == '+')
5078 ++p;
5079 if (!vim_isdigit(*p))
5080 get_float = FALSE;
5081 else
5082 p = skipdigits(p + 1);
5083 }
5084 if (ASCII_ISALPHA(*p) || *p == '.')
5085 get_float = FALSE;
5086 }
5087 if (get_float)
5088 {
5089 float_T f;
5090
5091 *arg += string2float(*arg, &f);
5092 if (evaluate)
5093 {
5094 rettv->v_type = VAR_FLOAT;
5095 rettv->vval.v_float = f;
5096 }
5097 }
5098 else
5099#endif
5100 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005101 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 *arg += len;
5103 if (evaluate)
5104 {
5105 rettv->v_type = VAR_NUMBER;
5106 rettv->vval.v_number = n;
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 /*
5113 * String constant: "string".
5114 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 break;
5117
5118 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 break;
5123
5124 /*
5125 * List: [expr, expr]
5126 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129
5130 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 * Dictionary: {key: val, key: val}
5132 */
5133 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5134 break;
5135
5136 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005137 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005139 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 break;
5141
5142 /*
5143 * Environment variable: $VAR.
5144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147
5148 /*
5149 * Register contents: @r.
5150 */
5151 case '@': ++*arg;
5152 if (evaluate)
5153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005155 rettv->vval.v_string = get_reg_contents(**arg,
5156 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 if (**arg != NUL)
5159 ++*arg;
5160 break;
5161
5162 /*
5163 * nested expression: (expression).
5164 */
5165 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (**arg == ')')
5168 ++*arg;
5169 else if (ret == OK)
5170 {
5171 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 ret = FAIL;
5174 }
5175 break;
5176
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 break;
5179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180
5181 if (ret == NOTDONE)
5182 {
5183 /*
5184 * Must be a variable or function name.
5185 * Can also be a curly-braces kind of name: {expr}.
5186 */
5187 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 if (alias != NULL)
5190 s = alias;
5191
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005192 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 ret = FAIL;
5194 else
5195 {
5196 if (**arg == '(') /* recursive! */
5197 {
5198 /* If "s" is the name of a variable of type VAR_FUNC
5199 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005200 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201
5202 /* Invoke the function. */
5203 ret = get_func_tv(s, len, rettv, arg,
5204 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005205 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005206
5207 /* If evaluate is FALSE rettv->v_type was not set in
5208 * get_func_tv, but it's needed in handle_subscript() to parse
5209 * what follows. So set it here. */
5210 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5211 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005212 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005213 rettv->v_type = VAR_FUNC;
5214 }
5215
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 /* Stop the expression evaluation when immediately
5217 * aborting on error, or when an interrupt occurred or
5218 * an exception was thrown but not caught. */
5219 if (aborting())
5220 {
5221 if (ret == OK)
5222 clear_tv(rettv);
5223 ret = FAIL;
5224 }
5225 }
5226 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005227 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005228 else
5229 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005231 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 }
5233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 *arg = skipwhite(*arg);
5235
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5237 * expr(expr). */
5238 if (ret == OK)
5239 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 /*
5242 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5243 */
5244 if (ret == OK && evaluate && end_leader > start_leader)
5245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247 int val = 0;
5248#ifdef FEAT_FLOAT
5249 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005250
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005251 if (rettv->v_type == VAR_FLOAT)
5252 f = rettv->vval.v_float;
5253 else
5254#endif
5255 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005258 clear_tv(rettv);
5259 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005261 else
5262 {
5263 while (end_leader > start_leader)
5264 {
5265 --end_leader;
5266 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005267 {
5268#ifdef FEAT_FLOAT
5269 if (rettv->v_type == VAR_FLOAT)
5270 f = !f;
5271 else
5272#endif
5273 val = !val;
5274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005276 {
5277#ifdef FEAT_FLOAT
5278 if (rettv->v_type == VAR_FLOAT)
5279 f = -f;
5280 else
5281#endif
5282 val = -val;
5283 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285#ifdef FEAT_FLOAT
5286 if (rettv->v_type == VAR_FLOAT)
5287 {
5288 clear_tv(rettv);
5289 rettv->vval.v_float = f;
5290 }
5291 else
5292#endif
5293 {
5294 clear_tv(rettv);
5295 rettv->v_type = VAR_NUMBER;
5296 rettv->vval.v_number = val;
5297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300
5301 return ret;
5302}
5303
5304/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005305 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5306 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5308 */
5309 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005310eval_index(
5311 char_u **arg,
5312 typval_T *rettv,
5313 int evaluate,
5314 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315{
5316 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 long len = -1;
5320 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005324 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005326 if (verbose)
5327 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 return FAIL;
5329 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005330#ifdef FEAT_FLOAT
5331 else if (rettv->v_type == VAR_FLOAT)
5332 {
5333 if (verbose)
5334 EMSG(_(e_float_as_string));
5335 return FAIL;
5336 }
5337#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005338 else if (rettv->v_type == VAR_SPECIAL)
5339 {
5340 return FAIL;
5341 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005343 init_tv(&var1);
5344 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005345 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 /*
5348 * dict.name
5349 */
5350 key = *arg + 1;
5351 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5352 ;
5353 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 /*
5360 * something[idx]
5361 *
5362 * Get the (first) variable from inside the [].
5363 */
5364 *arg = skipwhite(*arg + 1);
5365 if (**arg == ':')
5366 empty1 = TRUE;
5367 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5368 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5370 {
5371 /* not a number or string */
5372 clear_tv(&var1);
5373 return FAIL;
5374 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005375
5376 /*
5377 * Get the second variable from inside the [:].
5378 */
5379 if (**arg == ':')
5380 {
5381 range = TRUE;
5382 *arg = skipwhite(*arg + 1);
5383 if (**arg == ']')
5384 empty2 = TRUE;
5385 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 if (!empty1)
5388 clear_tv(&var1);
5389 return FAIL;
5390 }
5391 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5392 {
5393 /* not a number or string */
5394 if (!empty1)
5395 clear_tv(&var1);
5396 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 return FAIL;
5398 }
5399 }
5400
5401 /* Check for the ']'. */
5402 if (**arg != ']')
5403 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005404 if (verbose)
5405 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 clear_tv(&var1);
5407 if (range)
5408 clear_tv(&var2);
5409 return FAIL;
5410 }
5411 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 }
5413
5414 if (evaluate)
5415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416 n1 = 0;
5417 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 n1 = get_tv_number(&var1);
5420 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 if (range)
5423 {
5424 if (empty2)
5425 n2 = -1;
5426 else
5427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005428 n2 = get_tv_number(&var2);
5429 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 }
5431 }
5432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 {
5435 case VAR_NUMBER:
5436 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 len = (long)STRLEN(s);
5439 if (range)
5440 {
5441 /* The resulting variable is a substring. If the indexes
5442 * are out of range the result is empty. */
5443 if (n1 < 0)
5444 {
5445 n1 = len + n1;
5446 if (n1 < 0)
5447 n1 = 0;
5448 }
5449 if (n2 < 0)
5450 n2 = len + n2;
5451 else if (n2 >= len)
5452 n2 = len;
5453 if (n1 >= len || n2 < 0 || n1 > n2)
5454 s = NULL;
5455 else
5456 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5457 }
5458 else
5459 {
5460 /* The resulting variable is a string of a single
5461 * character. If the index is too big or negative the
5462 * result is empty. */
5463 if (n1 >= len || n1 < 0)
5464 s = NULL;
5465 else
5466 s = vim_strnsave(s + n1, 1);
5467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 clear_tv(rettv);
5469 rettv->v_type = VAR_STRING;
5470 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 break;
5472
5473 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 if (n1 < 0)
5476 n1 = len + n1;
5477 if (!empty1 && (n1 < 0 || n1 >= len))
5478 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005479 /* For a range we allow invalid values and return an empty
5480 * list. A list index out of range is an error. */
5481 if (!range)
5482 {
5483 if (verbose)
5484 EMSGN(_(e_listidx), n1);
5485 return FAIL;
5486 }
5487 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 }
5489 if (range)
5490 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 list_T *l;
5492 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493
5494 if (n2 < 0)
5495 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005496 else if (n2 >= len)
5497 n2 = len - 1;
5498 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005499 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 l = list_alloc();
5501 if (l == NULL)
5502 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 n1 <= n2; ++n1)
5505 {
5506 if (list_append_tv(l, &item->li_tv) == FAIL)
5507 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005508 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 return FAIL;
5510 }
5511 item = item->li_next;
5512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 clear_tv(rettv);
5514 rettv->v_type = VAR_LIST;
5515 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005516 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 }
5518 else
5519 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005520 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 clear_tv(rettv);
5522 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 }
5524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005525
5526 case VAR_DICT:
5527 if (range)
5528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005529 if (verbose)
5530 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005531 if (len == -1)
5532 clear_tv(&var1);
5533 return FAIL;
5534 }
5535 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005536 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537
5538 if (len == -1)
5539 {
5540 key = get_tv_string(&var1);
5541 if (*key == NUL)
5542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005543 if (verbose)
5544 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005545 clear_tv(&var1);
5546 return FAIL;
5547 }
5548 }
5549
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005550 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005552 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005553 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554 if (len == -1)
5555 clear_tv(&var1);
5556 if (item == NULL)
5557 return FAIL;
5558
5559 copy_tv(&item->di_tv, &var1);
5560 clear_tv(rettv);
5561 *rettv = var1;
5562 }
5563 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 }
5566
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 return OK;
5568}
5569
5570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 * Get an option value.
5572 * "arg" points to the '&' or '+' before the option name.
5573 * "arg" is advanced to character after the option name.
5574 * Return OK or FAIL.
5575 */
5576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005577get_option_tv(
5578 char_u **arg,
5579 typval_T *rettv, /* when NULL, only check if option exists */
5580 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 char_u *option_end;
5583 long numval;
5584 char_u *stringval;
5585 int opt_type;
5586 int c;
5587 int working = (**arg == '+'); /* has("+option") */
5588 int ret = OK;
5589 int opt_flags;
5590
5591 /*
5592 * Isolate the option name and find its value.
5593 */
5594 option_end = find_option_end(arg, &opt_flags);
5595 if (option_end == NULL)
5596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 EMSG2(_("E112: Option name missing: %s"), *arg);
5599 return FAIL;
5600 }
5601
5602 if (!evaluate)
5603 {
5604 *arg = option_end;
5605 return OK;
5606 }
5607
5608 c = *option_end;
5609 *option_end = NUL;
5610 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
5613 if (opt_type == -3) /* invalid name */
5614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 EMSG2(_("E113: Unknown option: %s"), *arg);
5617 ret = FAIL;
5618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (opt_type == -2) /* hidden string option */
5622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 rettv->v_type = VAR_STRING;
5624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626 else if (opt_type == -1) /* hidden number option */
5627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628 rettv->v_type = VAR_NUMBER;
5629 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631 else if (opt_type == 1) /* number option */
5632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633 rettv->v_type = VAR_NUMBER;
5634 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 }
5636 else /* string option */
5637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 rettv->v_type = VAR_STRING;
5639 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641 }
5642 else if (working && (opt_type == -2 || opt_type == -1))
5643 ret = FAIL;
5644
5645 *option_end = c; /* put back for error messages */
5646 *arg = option_end;
5647
5648 return ret;
5649}
5650
5651/*
5652 * Allocate a variable for a string constant.
5653 * Return OK or FAIL.
5654 */
5655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005656get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 char_u *p;
5659 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 int extra = 0;
5661
5662 /*
5663 * Find the end of the string, skipping backslashed characters.
5664 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 if (*p == '\\' && p[1] != NUL)
5668 {
5669 ++p;
5670 /* A "\<x>" form occupies at least 4 characters, and produces up
5671 * to 6 characters: reserve space for 2 extra */
5672 if (*p == '<')
5673 extra += 2;
5674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676
5677 if (*p != '"')
5678 {
5679 EMSG2(_("E114: Missing quote: %s"), *arg);
5680 return FAIL;
5681 }
5682
5683 /* If only parsing, set *arg and return here */
5684 if (!evaluate)
5685 {
5686 *arg = p + 1;
5687 return OK;
5688 }
5689
5690 /*
5691 * Copy the string into allocated memory, handling backslashed
5692 * characters.
5693 */
5694 name = alloc((unsigned)(p - *arg + extra));
5695 if (name == NULL)
5696 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 rettv->v_type = VAR_STRING;
5698 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 if (*p == '\\')
5703 {
5704 switch (*++p)
5705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 case 'b': *name++ = BS; ++p; break;
5707 case 'e': *name++ = ESC; ++p; break;
5708 case 'f': *name++ = FF; ++p; break;
5709 case 'n': *name++ = NL; ++p; break;
5710 case 'r': *name++ = CAR; ++p; break;
5711 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713 case 'X': /* hex: "\x1", "\x12" */
5714 case 'x':
5715 case 'u': /* Unicode: "\u0023" */
5716 case 'U':
5717 if (vim_isxdigit(p[1]))
5718 {
5719 int n, nr;
5720 int c = toupper(*p);
5721
5722 if (c == 'X')
5723 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005724 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005726 else
5727 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 nr = 0;
5729 while (--n >= 0 && vim_isxdigit(p[1]))
5730 {
5731 ++p;
5732 nr = (nr << 4) + hex2nr(*p);
5733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735#ifdef FEAT_MBYTE
5736 /* For "\u" store the number according to
5737 * 'encoding'. */
5738 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 else
5741#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 break;
5745
5746 /* octal: "\1", "\12", "\123" */
5747 case '0':
5748 case '1':
5749 case '2':
5750 case '3':
5751 case '4':
5752 case '5':
5753 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 case '7': *name = *p++ - '0';
5755 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 *name = (*name << 3) + *p++ - '0';
5758 if (*p >= '0' && *p <= '7')
5759 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 break;
5763
5764 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (extra != 0)
5767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 break;
5770 }
5771 /* FALLTHROUGH */
5772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 break;
5775 }
5776 }
5777 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 *arg = p + 1;
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 return OK;
5785}
5786
5787/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 * Return OK or FAIL.
5790 */
5791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005792get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 int reduce = 0;
5797
5798 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 */
5801 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005806 break;
5807 ++reduce;
5808 ++p;
5809 }
5810 }
5811
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 return FAIL;
5816 }
5817
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005819 if (!evaluate)
5820 {
5821 *arg = p + 1;
5822 return OK;
5823 }
5824
5825 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005827 */
5828 str = alloc((unsigned)((p - *arg) - reduce));
5829 if (str == NULL)
5830 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 rettv->v_type = VAR_STRING;
5832 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 break;
5840 ++p;
5841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 *arg = p + 1;
5846
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 return OK;
5848}
5849
5850/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 * Allocate a variable for a List and fill it from "*arg".
5852 * Return OK or FAIL.
5853 */
5854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005855get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856{
Bram Moolenaar33570922005-01-25 22:26:29 +00005857 list_T *l = NULL;
5858 typval_T tv;
5859 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860
5861 if (evaluate)
5862 {
5863 l = list_alloc();
5864 if (l == NULL)
5865 return FAIL;
5866 }
5867
5868 *arg = skipwhite(*arg + 1);
5869 while (**arg != ']' && **arg != NUL)
5870 {
5871 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5872 goto failret;
5873 if (evaluate)
5874 {
5875 item = listitem_alloc();
5876 if (item != NULL)
5877 {
5878 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005879 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 list_append(l, item);
5881 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005882 else
5883 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 }
5885
5886 if (**arg == ']')
5887 break;
5888 if (**arg != ',')
5889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 goto failret;
5892 }
5893 *arg = skipwhite(*arg + 1);
5894 }
5895
5896 if (**arg != ']')
5897 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899failret:
5900 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005901 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 return FAIL;
5903 }
5904
5905 *arg = skipwhite(*arg + 1);
5906 if (evaluate)
5907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908 rettv->v_type = VAR_LIST;
5909 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 ++l->lv_refcount;
5911 }
5912
5913 return OK;
5914}
5915
5916/*
5917 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005918 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005920 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005921list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005923 list_T *l;
5924
5925 l = (list_T *)alloc_clear(sizeof(list_T));
5926 if (l != NULL)
5927 {
5928 /* Prepend the list to the list of lists for garbage collection. */
5929 if (first_list != NULL)
5930 first_list->lv_used_prev = l;
5931 l->lv_used_prev = NULL;
5932 l->lv_used_next = first_list;
5933 first_list = l;
5934 }
5935 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936}
5937
5938/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005939 * Allocate an empty list for a return value.
5940 * Returns OK or FAIL.
5941 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005942 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005943rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005944{
5945 list_T *l = list_alloc();
5946
5947 if (l == NULL)
5948 return FAIL;
5949
5950 rettv->vval.v_list = l;
5951 rettv->v_type = VAR_LIST;
5952 ++l->lv_refcount;
5953 return OK;
5954}
5955
5956/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 * Unreference a list: decrement the reference count and free it when it
5958 * becomes zero.
5959 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005961list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005963 if (l != NULL && --l->lv_refcount <= 0)
5964 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965}
5966
5967/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005968 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 * Ignores the reference count.
5970 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005972list_free(
5973 list_T *l,
5974 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975{
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005978 /* Remove the list from the list of lists for garbage collection. */
5979 if (l->lv_used_prev == NULL)
5980 first_list = l->lv_used_next;
5981 else
5982 l->lv_used_prev->lv_used_next = l->lv_used_next;
5983 if (l->lv_used_next != NULL)
5984 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5985
Bram Moolenaard9fba312005-06-26 22:34:35 +00005986 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005988 /* Remove the item before deleting it. */
5989 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005990 if (recurse || (item->li_tv.v_type != VAR_LIST
5991 && item->li_tv.v_type != VAR_DICT))
5992 clear_tv(&item->li_tv);
5993 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 }
5995 vim_free(l);
5996}
5997
5998/*
5999 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006000 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006002 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006003listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006}
6007
6008/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006009 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006014 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 vim_free(item);
6016}
6017
6018/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006019 * Remove a list item from a List and free it. Also clears the value.
6020 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006023{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006024 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006025 listitem_free(item);
6026}
6027
6028/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 * Get the number of items in a list.
6030 */
6031 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006032list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 if (l == NULL)
6035 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006036 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037}
6038
6039/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040 * Return TRUE when two lists have exactly the same values.
6041 */
6042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043list_equal(
6044 list_T *l1,
6045 list_T *l2,
6046 int ic, /* ignore case for strings */
6047 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006048{
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006050
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006051 if (l1 == NULL || l2 == NULL)
6052 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006053 if (l1 == l2)
6054 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006055 if (list_len(l1) != list_len(l2))
6056 return FALSE;
6057
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058 for (item1 = l1->lv_first, item2 = l2->lv_first;
6059 item1 != NULL && item2 != NULL;
6060 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062 return FALSE;
6063 return item1 == NULL && item2 == NULL;
6064}
6065
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006066/*
6067 * Return the dictitem that an entry in a hashtable points to.
6068 */
6069 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006071{
6072 return HI2DI(hi);
6073}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006074
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006076 * Return TRUE when two dictionaries have exactly the same key/values.
6077 */
6078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006079dict_equal(
6080 dict_T *d1,
6081 dict_T *d2,
6082 int ic, /* ignore case for strings */
6083 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006084{
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 hashitem_T *hi;
6086 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006088
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006089 if (d1 == NULL || d2 == NULL)
6090 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 if (d1 == d2)
6092 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006093 if (dict_len(d1) != dict_len(d2))
6094 return FALSE;
6095
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006096 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006099 if (!HASHITEM_EMPTY(hi))
6100 {
6101 item2 = dict_find(d2, hi->hi_key, -1);
6102 if (item2 == NULL)
6103 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006104 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006105 return FALSE;
6106 --todo;
6107 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006108 }
6109 return TRUE;
6110}
6111
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112static int tv_equal_recurse_limit;
6113
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006114/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006116 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006117 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 */
6119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120tv_equal(
6121 typval_T *tv1,
6122 typval_T *tv2,
6123 int ic, /* ignore case */
6124 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125{
6126 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006127 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006128 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006129 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006131 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006134 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006135 * recursiveness to a limit. We guess they are equal then.
6136 * A fixed limit has the problem of still taking an awful long time.
6137 * Reduce the limit every time running into it. That should work fine for
6138 * deeply linked structures that are not recursively linked and catch
6139 * recursiveness quickly. */
6140 if (!recursive)
6141 tv_equal_recurse_limit = 1000;
6142 if (recursive_cnt >= tv_equal_recurse_limit)
6143 {
6144 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006145 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147
6148 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006149 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006150 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006151 ++recursive_cnt;
6152 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6153 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006154 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006155
6156 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006157 ++recursive_cnt;
6158 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6159 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006160 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006161
6162 case VAR_FUNC:
6163 return (tv1->vval.v_string != NULL
6164 && tv2->vval.v_string != NULL
6165 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6166
6167 case VAR_NUMBER:
6168 return tv1->vval.v_number == tv2->vval.v_number;
6169
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006170#ifdef FEAT_FLOAT
6171 case VAR_FLOAT:
6172 return tv1->vval.v_float == tv2->vval.v_float;
6173#endif
6174
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006175 case VAR_STRING:
6176 s1 = get_tv_string_buf(tv1, buf1);
6177 s2 = get_tv_string_buf(tv2, buf2);
6178 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006179
6180 case VAR_SPECIAL:
6181 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183
6184 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 return TRUE;
6186}
6187
6188/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 * Locate item with index "n" in list "l" and return it.
6190 * A negative index is counted from the end; -1 is the last item.
6191 * Returns NULL when "n" is out of range.
6192 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006193 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006194list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195{
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 long idx;
6198
6199 if (l == NULL)
6200 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006201
6202 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006203 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006204 n = l->lv_len + n;
6205
6206 /* Check for index out of range. */
6207 if (n < 0 || n >= l->lv_len)
6208 return NULL;
6209
6210 /* When there is a cached index may start search from there. */
6211 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006213 if (n < l->lv_idx / 2)
6214 {
6215 /* closest to the start of the list */
6216 item = l->lv_first;
6217 idx = 0;
6218 }
6219 else if (n > (l->lv_idx + l->lv_len) / 2)
6220 {
6221 /* closest to the end of the list */
6222 item = l->lv_last;
6223 idx = l->lv_len - 1;
6224 }
6225 else
6226 {
6227 /* closest to the cached index */
6228 item = l->lv_idx_item;
6229 idx = l->lv_idx;
6230 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 }
6232 else
6233 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006234 if (n < l->lv_len / 2)
6235 {
6236 /* closest to the start of the list */
6237 item = l->lv_first;
6238 idx = 0;
6239 }
6240 else
6241 {
6242 /* closest to the end of the list */
6243 item = l->lv_last;
6244 idx = l->lv_len - 1;
6245 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006247
6248 while (n > idx)
6249 {
6250 /* search forward */
6251 item = item->li_next;
6252 ++idx;
6253 }
6254 while (n < idx)
6255 {
6256 /* search backward */
6257 item = item->li_prev;
6258 --idx;
6259 }
6260
6261 /* cache the used index */
6262 l->lv_idx = idx;
6263 l->lv_idx_item = item;
6264
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006265 return item;
6266}
6267
6268/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006269 * Get list item "l[idx]" as a number.
6270 */
6271 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006272list_find_nr(
6273 list_T *l,
6274 long idx,
6275 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006276{
6277 listitem_T *li;
6278
6279 li = list_find(l, idx);
6280 if (li == NULL)
6281 {
6282 if (errorp != NULL)
6283 *errorp = TRUE;
6284 return -1L;
6285 }
6286 return get_tv_number_chk(&li->li_tv, errorp);
6287}
6288
6289/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006290 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6291 */
6292 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006293list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006294{
6295 listitem_T *li;
6296
6297 li = list_find(l, idx - 1);
6298 if (li == NULL)
6299 {
6300 EMSGN(_(e_listidx), idx);
6301 return NULL;
6302 }
6303 return get_tv_string(&li->li_tv);
6304}
6305
6306/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006307 * Locate "item" list "l" and return its index.
6308 * Returns -1 when "item" is not in the list.
6309 */
6310 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006311list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006312{
6313 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006315
6316 if (l == NULL)
6317 return -1;
6318 idx = 0;
6319 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6320 ++idx;
6321 if (li == NULL)
6322 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006323 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006324}
6325
6326/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 * Append item "item" to the end of list "l".
6328 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006330list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331{
6332 if (l->lv_last == NULL)
6333 {
6334 /* empty list */
6335 l->lv_first = item;
6336 l->lv_last = item;
6337 item->li_prev = NULL;
6338 }
6339 else
6340 {
6341 l->lv_last->li_next = item;
6342 item->li_prev = l->lv_last;
6343 l->lv_last = item;
6344 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006345 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 item->li_next = NULL;
6347}
6348
6349/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006354list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357
Bram Moolenaar05159a02005-02-26 23:04:13 +00006358 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006360 copy_tv(tv, &li->li_tv);
6361 list_append(l, li);
6362 return OK;
6363}
6364
6365/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006366 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006367 * Return FAIL when out of memory.
6368 */
6369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006371{
6372 listitem_T *li = listitem_alloc();
6373
6374 if (li == NULL)
6375 return FAIL;
6376 li->li_tv.v_type = VAR_DICT;
6377 li->li_tv.v_lock = 0;
6378 li->li_tv.vval.v_dict = dict;
6379 list_append(list, li);
6380 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381 return OK;
6382}
6383
6384/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006386 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387 * Returns FAIL when out of memory.
6388 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006391{
6392 listitem_T *li = listitem_alloc();
6393
6394 if (li == NULL)
6395 return FAIL;
6396 list_append(l, li);
6397 li->li_tv.v_type = VAR_STRING;
6398 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006399 if (str == NULL)
6400 li->li_tv.vval.v_string = NULL;
6401 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006402 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006403 return FAIL;
6404 return OK;
6405}
6406
6407/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006408 * Append "n" to list "l".
6409 * Returns FAIL when out of memory.
6410 */
6411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006412list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006413{
6414 listitem_T *li;
6415
6416 li = listitem_alloc();
6417 if (li == NULL)
6418 return FAIL;
6419 li->li_tv.v_type = VAR_NUMBER;
6420 li->li_tv.v_lock = 0;
6421 li->li_tv.vval.v_number = n;
6422 list_append(l, li);
6423 return OK;
6424}
6425
6426/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 * If "item" is NULL append at the end.
6429 * Return FAIL when out of memory.
6430 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006431 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006432list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433{
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435
6436 if (ni == NULL)
6437 return FAIL;
6438 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006439 list_insert(l, ni, item);
6440 return OK;
6441}
6442
6443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006445{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446 if (item == NULL)
6447 /* Append new item at end of list. */
6448 list_append(l, ni);
6449 else
6450 {
6451 /* Insert new item before existing item. */
6452 ni->li_prev = item->li_prev;
6453 ni->li_next = item;
6454 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006455 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 ++l->lv_idx;
6458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006460 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 l->lv_idx_item = NULL;
6463 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006465 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467}
6468
6469/*
6470 * Extend "l1" with "l2".
6471 * If "bef" is NULL append at the end, otherwise insert before this item.
6472 * Returns FAIL when out of memory.
6473 */
6474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006475list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476{
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006478 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006480 /* We also quit the loop when we have inserted the original item count of
6481 * the list, avoid a hang when we extend a list with itself. */
6482 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6484 return FAIL;
6485 return OK;
6486}
6487
6488/*
6489 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6490 * Return FAIL when out of memory.
6491 */
6492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006493list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494{
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006497 if (l1 == NULL || l2 == NULL)
6498 return FAIL;
6499
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 if (l == NULL)
6503 return FAIL;
6504 tv->v_type = VAR_LIST;
6505 tv->vval.v_list = l;
6506
6507 /* append all items from the second list */
6508 return list_extend(l, l2, NULL);
6509}
6510
6511/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006512 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 * Returns NULL when out of memory.
6516 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519{
Bram Moolenaar33570922005-01-25 22:26:29 +00006520 list_T *copy;
6521 listitem_T *item;
6522 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523
6524 if (orig == NULL)
6525 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526
6527 copy = list_alloc();
6528 if (copy != NULL)
6529 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006530 if (copyID != 0)
6531 {
6532 /* Do this before adding the items, because one of the items may
6533 * refer back to this list. */
6534 orig->lv_copyID = copyID;
6535 orig->lv_copylist = copy;
6536 }
6537 for (item = orig->lv_first; item != NULL && !got_int;
6538 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539 {
6540 ni = listitem_alloc();
6541 if (ni == NULL)
6542 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006543 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006544 {
6545 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6546 {
6547 vim_free(ni);
6548 break;
6549 }
6550 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006552 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553 list_append(copy, ni);
6554 }
6555 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006556 if (item != NULL)
6557 {
6558 list_unref(copy);
6559 copy = NULL;
6560 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 }
6562
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 return copy;
6564}
6565
6566/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006568 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006569 * This used to be called list_remove, but that conflicts with a Sun header
6570 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574{
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 /* notify watchers */
6578 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006580 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581 list_fix_watch(l, ip);
6582 if (ip == item2)
6583 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585
6586 if (item2->li_next == NULL)
6587 l->lv_last = item->li_prev;
6588 else
6589 item2->li_next->li_prev = item->li_prev;
6590 if (item->li_prev == NULL)
6591 l->lv_first = item2->li_next;
6592 else
6593 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006594 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595}
6596
6597/*
6598 * Return an allocated string with the string representation of a list.
6599 * May return NULL.
6600 */
6601 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006602list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603{
6604 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605
6606 if (tv->vval.v_list == NULL)
6607 return NULL;
6608 ga_init2(&ga, (int)sizeof(char), 80);
6609 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006610 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 {
6612 vim_free(ga.ga_data);
6613 return NULL;
6614 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 ga_append(&ga, ']');
6616 ga_append(&ga, NUL);
6617 return (char_u *)ga.ga_data;
6618}
6619
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006620typedef struct join_S {
6621 char_u *s;
6622 char_u *tofree;
6623} join_T;
6624
6625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006626list_join_inner(
6627 garray_T *gap, /* to store the result in */
6628 list_T *l,
6629 char_u *sep,
6630 int echo_style,
6631 int copyID,
6632 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006633{
6634 int i;
6635 join_T *p;
6636 int len;
6637 int sumlen = 0;
6638 int first = TRUE;
6639 char_u *tofree;
6640 char_u numbuf[NUMBUFLEN];
6641 listitem_T *item;
6642 char_u *s;
6643
6644 /* Stringify each item in the list. */
6645 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6646 {
6647 if (echo_style)
6648 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6649 else
6650 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6651 if (s == NULL)
6652 return FAIL;
6653
6654 len = (int)STRLEN(s);
6655 sumlen += len;
6656
Bram Moolenaarcde88542015-08-11 19:14:00 +02006657 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006658 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6659 if (tofree != NULL || s != numbuf)
6660 {
6661 p->s = s;
6662 p->tofree = tofree;
6663 }
6664 else
6665 {
6666 p->s = vim_strnsave(s, len);
6667 p->tofree = p->s;
6668 }
6669
6670 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006671 if (did_echo_string_emsg) /* recursion error, bail out */
6672 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006673 }
6674
6675 /* Allocate result buffer with its total size, avoid re-allocation and
6676 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6677 if (join_gap->ga_len >= 2)
6678 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6679 if (ga_grow(gap, sumlen + 2) == FAIL)
6680 return FAIL;
6681
6682 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6683 {
6684 if (first)
6685 first = FALSE;
6686 else
6687 ga_concat(gap, sep);
6688 p = ((join_T *)join_gap->ga_data) + i;
6689
6690 if (p->s != NULL)
6691 ga_concat(gap, p->s);
6692 line_breakcheck();
6693 }
6694
6695 return OK;
6696}
6697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006698/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006700 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006701 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006704list_join(
6705 garray_T *gap,
6706 list_T *l,
6707 char_u *sep,
6708 int echo_style,
6709 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006711 garray_T join_ga;
6712 int retval;
6713 join_T *p;
6714 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715
Bram Moolenaard39a7512015-04-16 22:51:22 +02006716 if (l->lv_len < 1)
6717 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6719 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6720
6721 /* Dispose each item in join_ga. */
6722 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = (join_T *)join_ga.ga_data;
6725 for (i = 0; i < join_ga.ga_len; ++i)
6726 {
6727 vim_free(p->tofree);
6728 ++p;
6729 }
6730 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732
6733 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734}
6735
6736/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006737 * Return the next (unique) copy ID.
6738 * Used for serializing nested structures.
6739 */
6740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006742{
6743 current_copyID += COPYID_INC;
6744 return current_copyID;
6745}
6746
6747/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748 * Garbage collection for lists and dictionaries.
6749 *
6750 * We use reference counts to be able to free most items right away when they
6751 * are no longer used. But for composite items it's possible that it becomes
6752 * unused while the reference count is > 0: When there is a recursive
6753 * reference. Example:
6754 * :let l = [1, 2, 3]
6755 * :let d = {9: l}
6756 * :let l[1] = d
6757 *
6758 * Since this is quite unusual we handle this with garbage collection: every
6759 * once in a while find out which lists and dicts are not referenced from any
6760 * variable.
6761 *
6762 * Here is a good reference text about garbage collection (refers to Python
6763 * but it applies to all reference-counting mechanisms):
6764 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766
6767/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 * Do garbage collection for lists and dicts.
6769 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006770 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006772garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006773{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006774 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006775 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006776 buf_T *buf;
6777 win_T *wp;
6778 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006779 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006780 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006781 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782#ifdef FEAT_WINDOWS
6783 tabpage_T *tp;
6784#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786 /* Only do this once. */
6787 want_garbage_collect = FALSE;
6788 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006789 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006790
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006791 /* We advance by two because we add one for items referenced through
6792 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006793 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006794
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006795 /*
6796 * 1. Go through all accessible variables and mark all lists and dicts
6797 * with copyID.
6798 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006799
6800 /* Don't free variables in the previous_funccal list unless they are only
6801 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006802 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006803 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6804 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006805 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6806 NULL);
6807 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6808 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006809 }
6810
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 /* script-local variables */
6812 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006813 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814
6815 /* buffer-local variables */
6816 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006817 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6818 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819
6820 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006821 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006822 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6823 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006824#ifdef FEAT_AUTOCMD
6825 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006826 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6827 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006828#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006830#ifdef FEAT_WINDOWS
6831 /* tabpage-local variables */
6832 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006833 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6834 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006835#endif
6836
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006838 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839
6840 /* function-local variables */
6841 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6842 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006843 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6844 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 }
6846
Bram Moolenaard812df62008-11-09 12:46:09 +00006847 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006848 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006849
Bram Moolenaar1dced572012-04-05 16:54:08 +02006850#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006851 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006852#endif
6853
Bram Moolenaardb913952012-06-29 12:54:53 +02006854#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006855 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006856#endif
6857
6858#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006859 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006860#endif
6861
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006864 /*
6865 * 2. Free lists and dictionaries that are not referenced.
6866 */
6867 did_free = free_unref_items(copyID);
6868
6869 /*
6870 * 3. Check if any funccal can be freed now.
6871 */
6872 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 if (can_free_funccal(*pfc, copyID))
6875 {
6876 fc = *pfc;
6877 *pfc = fc->caller;
6878 free_funccal(fc, TRUE);
6879 did_free = TRUE;
6880 did_free_funccal = TRUE;
6881 }
6882 else
6883 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 if (did_free_funccal)
6886 /* When a funccal was freed some more items might be garbage
6887 * collected, so run again. */
6888 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 else if (p_verbose > 0)
6891 {
6892 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6893 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894
6895 return did_free;
6896}
6897
6898/*
6899 * Free lists and dictionaries that are no longer referenced.
6900 */
6901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006904 dict_T *dd, *dd_next;
6905 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006906 int did_free = FALSE;
6907
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 */
6911 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006912 {
6913 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006916 /* Free the Dictionary and ordinary items it contains, but don't
6917 * recurse into Lists and Dictionaries, they will be in the list
6918 * of dicts or list of lists. */
6919 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006922 dd = dd_next;
6923 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924
6925 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 * Go through the list of lists and free items without the copyID.
6927 * But don't free a list that has a watcher (used in a for loop), these
6928 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 */
6930 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006931 {
6932 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6934 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006936 /* Free the List and ordinary items it contains, but don't recurse
6937 * into Lists and Dictionaries, they will be in the list of dicts
6938 * or list of lists. */
6939 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006942 ll = ll_next;
6943 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 return did_free;
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 * "list_stack" is used to add lists to be marked. Can be NULL.
6950 *
6951 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006954set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955{
6956 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 hashtab_T *cur_ht;
6960 ht_stack_T *ht_stack = NULL;
6961 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 cur_ht = ht;
6964 for (;;)
6965 {
6966 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 /* Mark each item in the hashtab. If the item contains a hashtab
6969 * it is added to ht_stack, if it contains a list it is added to
6970 * list_stack. */
6971 todo = (int)cur_ht->ht_used;
6972 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6973 if (!HASHITEM_EMPTY(hi))
6974 {
6975 --todo;
6976 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6977 &ht_stack, list_stack);
6978 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980
6981 if (ht_stack == NULL)
6982 break;
6983
6984 /* take an item from the stack */
6985 cur_ht = ht_stack->ht;
6986 tempitem = ht_stack;
6987 ht_stack = ht_stack->prev;
6988 free(tempitem);
6989 }
6990
6991 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992}
6993
6994/*
6995 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6997 *
6998 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007001set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 listitem_T *li;
7004 int abort = FALSE;
7005 list_T *cur_l;
7006 list_stack_T *list_stack = NULL;
7007 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 cur_l = l;
7010 for (;;)
7011 {
7012 if (!abort)
7013 /* Mark each item in the list. If the item contains a hashtab
7014 * it is added to ht_stack, if it contains a list it is added to
7015 * list_stack. */
7016 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7017 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7018 ht_stack, &list_stack);
7019 if (list_stack == NULL)
7020 break;
7021
7022 /* take an item from the stack */
7023 cur_l = list_stack->list;
7024 tempitem = list_stack;
7025 list_stack = list_stack->prev;
7026 free(tempitem);
7027 }
7028
7029 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030}
7031
7032/*
7033 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 * "list_stack" is used to add lists to be marked. Can be NULL.
7035 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7036 *
7037 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007040set_ref_in_item(
7041 typval_T *tv,
7042 int copyID,
7043 ht_stack_T **ht_stack,
7044 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045{
7046 dict_T *dd;
7047 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049
7050 switch (tv->v_type)
7051 {
7052 case VAR_DICT:
7053 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007054 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007055 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 /* Didn't see this dict yet. */
7057 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 if (ht_stack == NULL)
7059 {
7060 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7061 }
7062 else
7063 {
7064 ht_stack_T *newitem = (ht_stack_T*)malloc(
7065 sizeof(ht_stack_T));
7066 if (newitem == NULL)
7067 abort = TRUE;
7068 else
7069 {
7070 newitem->ht = &dd->dv_hashtab;
7071 newitem->prev = *ht_stack;
7072 *ht_stack = newitem;
7073 }
7074 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007075 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077
7078 case VAR_LIST:
7079 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007080 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007081 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /* Didn't see this list yet. */
7083 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 if (list_stack == NULL)
7085 {
7086 abort = set_ref_in_list(ll, copyID, ht_stack);
7087 }
7088 else
7089 {
7090 list_stack_T *newitem = (list_stack_T*)malloc(
7091 sizeof(list_stack_T));
7092 if (newitem == NULL)
7093 abort = TRUE;
7094 else
7095 {
7096 newitem->list = ll;
7097 newitem->prev = *list_stack;
7098 *list_stack = newitem;
7099 }
7100 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007101 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007103 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007105}
7106
7107/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 * Allocate an empty header for a dictionary.
7109 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007110 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112{
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007117 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007118 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119 if (first_dict != NULL)
7120 first_dict->dv_used_prev = d;
7121 d->dv_used_next = first_dict;
7122 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007123 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007126 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007127 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007128 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007129 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007130 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132}
7133
7134/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007135 * Allocate an empty dict for a return value.
7136 * Returns OK or FAIL.
7137 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007138 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007139rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007140{
7141 dict_T *d = dict_alloc();
7142
7143 if (d == NULL)
7144 return FAIL;
7145
7146 rettv->vval.v_dict = d;
7147 rettv->v_type = VAR_DICT;
7148 ++d->dv_refcount;
7149 return OK;
7150}
7151
7152
7153/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 * Unreference a Dictionary: decrement the reference count and free it when it
7155 * becomes zero.
7156 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007160 if (d != NULL && --d->dv_refcount <= 0)
7161 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162}
7163
7164/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007165 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 * Ignores the reference count.
7167 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169dict_free(
7170 dict_T *d,
7171 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177 /* Remove the dict from the list of dicts for garbage collection. */
7178 if (d->dv_used_prev == NULL)
7179 first_dict = d->dv_used_next;
7180 else
7181 d->dv_used_prev->dv_used_next = d->dv_used_next;
7182 if (d->dv_used_next != NULL)
7183 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7184
7185 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007186 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007187 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 if (!HASHITEM_EMPTY(hi))
7191 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192 /* Remove the item before deleting it, just in case there is
7193 * something recursive causing trouble. */
7194 di = HI2DI(hi);
7195 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007196 if (recurse || (di->di_tv.v_type != VAR_LIST
7197 && di->di_tv.v_type != VAR_DICT))
7198 clear_tv(&di->di_tv);
7199 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 --todo;
7201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 vim_free(d);
7205}
7206
7207/*
7208 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209 * The "key" is copied to the new item.
7210 * Note that the value of the item "di_tv" still needs to be initialized!
7211 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215{
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007218 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007222 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007224 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225}
7226
7227/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228 * Make a copy of a Dictionary item.
7229 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007231dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232{
Bram Moolenaar33570922005-01-25 22:26:29 +00007233 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007235 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7236 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 if (di != NULL)
7238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007240 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007241 copy_tv(&org->di_tv, &di->di_tv);
7242 }
7243 return di;
7244}
7245
7246/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 * Remove item "item" from Dictionary "dict" and free it.
7248 */
7249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251{
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (HASHITEM_EMPTY(hi))
7256 EMSG2(_(e_intern2), "dictitem_remove()");
7257 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 dictitem_free(item);
7260}
7261
7262/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 * Free a dict item. Also clears the value.
7264 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007266dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007269 if (item->di_flags & DI_FLAGS_ALLOC)
7270 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271}
7272
7273/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7275 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007276 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 * Returns NULL when out of memory.
7278 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281{
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dict_T *copy;
7283 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286
7287 if (orig == NULL)
7288 return NULL;
7289
7290 copy = dict_alloc();
7291 if (copy != NULL)
7292 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007293 if (copyID != 0)
7294 {
7295 orig->dv_copyID = copyID;
7296 orig->dv_copydict = copy;
7297 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007299 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 --todo;
7304
7305 di = dictitem_alloc(hi->hi_key);
7306 if (di == NULL)
7307 break;
7308 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007309 {
7310 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7311 copyID) == FAIL)
7312 {
7313 vim_free(di);
7314 break;
7315 }
7316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 else
7318 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7319 if (dict_add(copy, di) == FAIL)
7320 {
7321 dictitem_free(di);
7322 break;
7323 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007328 if (todo > 0)
7329 {
7330 dict_unref(copy);
7331 copy = NULL;
7332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 }
7334
7335 return copy;
7336}
7337
7338/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007340 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346}
7347
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007349 * Add a number or string entry to dictionary "d".
7350 * When "str" is NULL use number "nr", otherwise use "str".
7351 * Returns FAIL when out of memory and when key already exists.
7352 */
7353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354dict_add_nr_str(
7355 dict_T *d,
7356 char *key,
7357 long nr,
7358 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007359{
7360 dictitem_T *item;
7361
7362 item = dictitem_alloc((char_u *)key);
7363 if (item == NULL)
7364 return FAIL;
7365 item->di_tv.v_lock = 0;
7366 if (str == NULL)
7367 {
7368 item->di_tv.v_type = VAR_NUMBER;
7369 item->di_tv.vval.v_number = nr;
7370 }
7371 else
7372 {
7373 item->di_tv.v_type = VAR_STRING;
7374 item->di_tv.vval.v_string = vim_strsave(str);
7375 }
7376 if (dict_add(d, item) == FAIL)
7377 {
7378 dictitem_free(item);
7379 return FAIL;
7380 }
7381 return OK;
7382}
7383
7384/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007385 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007386 * Returns FAIL when out of memory and when key already exists.
7387 */
7388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007390{
7391 dictitem_T *item;
7392
7393 item = dictitem_alloc((char_u *)key);
7394 if (item == NULL)
7395 return FAIL;
7396 item->di_tv.v_lock = 0;
7397 item->di_tv.v_type = VAR_LIST;
7398 item->di_tv.vval.v_list = list;
7399 if (dict_add(d, item) == FAIL)
7400 {
7401 dictitem_free(item);
7402 return FAIL;
7403 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007404 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007405 return OK;
7406}
7407
7408/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 * Get the number of items in a Dictionary.
7410 */
7411 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 if (d == NULL)
7415 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007416 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417}
7418
7419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 * Find item "key[len]" in Dictionary "d".
7421 * If "len" is negative use strlen(key).
7422 * Returns NULL when not found.
7423 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007424 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007425dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427#define AKEYLEN 200
7428 char_u buf[AKEYLEN];
7429 char_u *akey;
7430 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 if (len < 0)
7434 akey = key;
7435 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 tofree = akey = vim_strnsave(key, len);
7438 if (akey == NULL)
7439 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007440 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 else
7442 {
7443 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007444 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445 akey = buf;
7446 }
7447
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007449 vim_free(tofree);
7450 if (HASHITEM_EMPTY(hi))
7451 return NULL;
7452 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453}
7454
7455/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007456 * Get a string item from a dictionary.
7457 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007458 * Returns NULL if the entry doesn't exist or out of memory.
7459 */
7460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007462{
7463 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007464 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007465
7466 di = dict_find(d, key, -1);
7467 if (di == NULL)
7468 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007469 s = get_tv_string(&di->di_tv);
7470 if (save && s != NULL)
7471 s = vim_strsave(s);
7472 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007473}
7474
7475/*
7476 * Get a number item from a dictionary.
7477 * Returns 0 if the entry doesn't exist or out of memory.
7478 */
7479 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007481{
7482 dictitem_T *di;
7483
7484 di = dict_find(d, key, -1);
7485 if (di == NULL)
7486 return 0;
7487 return get_tv_number(&di->di_tv);
7488}
7489
7490/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 * Return an allocated string with the string representation of a Dictionary.
7492 * May return NULL.
7493 */
7494 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007495dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496{
7497 garray_T ga;
7498 int first = TRUE;
7499 char_u *tofree;
7500 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007503 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 return NULL;
7508 ga_init2(&ga, (int)sizeof(char), 80);
7509 ga_append(&ga, '{');
7510
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007511 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007512 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 --todo;
7517
7518 if (first)
7519 first = FALSE;
7520 else
7521 ga_concat(&ga, (char_u *)", ");
7522
7523 tofree = string_quote(hi->hi_key, FALSE);
7524 if (tofree != NULL)
7525 {
7526 ga_concat(&ga, tofree);
7527 vim_free(tofree);
7528 }
7529 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007530 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (s != NULL)
7532 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007534 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007535 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007536 line_breakcheck();
7537
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007540 if (todo > 0)
7541 {
7542 vim_free(ga.ga_data);
7543 return NULL;
7544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545
7546 ga_append(&ga, '}');
7547 ga_append(&ga, NUL);
7548 return (char_u *)ga.ga_data;
7549}
7550
7551/*
7552 * Allocate a variable for a Dictionary and fill it from "*arg".
7553 * Return OK or FAIL. Returns NOTDONE for {expr}.
7554 */
7555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 dict_T *d = NULL;
7559 typval_T tvkey;
7560 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007561 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565
7566 /*
7567 * First check if it's not a curly-braces thing: {expr}.
7568 * Must do this without evaluating, otherwise a function may be called
7569 * twice. Unfortunately this means we need to call eval1() twice for the
7570 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 if (*start != '}')
7574 {
7575 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7576 return FAIL;
7577 if (*start == '}')
7578 return NOTDONE;
7579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580
7581 if (evaluate)
7582 {
7583 d = dict_alloc();
7584 if (d == NULL)
7585 return FAIL;
7586 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007587 tvkey.v_type = VAR_UNKNOWN;
7588 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589
7590 *arg = skipwhite(*arg + 1);
7591 while (**arg != '}' && **arg != NUL)
7592 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 goto failret;
7595 if (**arg != ':')
7596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007597 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 goto failret;
7600 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007601 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007603 key = get_tv_string_buf_chk(&tvkey, buf);
7604 if (key == NULL || *key == NUL)
7605 {
7606 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7607 if (key != NULL)
7608 EMSG(_(e_emptykey));
7609 clear_tv(&tvkey);
7610 goto failret;
7611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
7614 *arg = skipwhite(*arg + 1);
7615 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7616 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007617 if (evaluate)
7618 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 goto failret;
7620 }
7621 if (evaluate)
7622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 if (item != NULL)
7625 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007626 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 clear_tv(&tv);
7629 goto failret;
7630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 item = dictitem_alloc(key);
7632 clear_tv(&tvkey);
7633 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007636 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 if (dict_add(d, item) == FAIL)
7638 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 }
7640 }
7641
7642 if (**arg == '}')
7643 break;
7644 if (**arg != ',')
7645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007646 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 goto failret;
7648 }
7649 *arg = skipwhite(*arg + 1);
7650 }
7651
7652 if (**arg != '}')
7653 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007654 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655failret:
7656 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007657 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 return FAIL;
7659 }
7660
7661 *arg = skipwhite(*arg + 1);
7662 if (evaluate)
7663 {
7664 rettv->v_type = VAR_DICT;
7665 rettv->vval.v_dict = d;
7666 ++d->dv_refcount;
7667 }
7668
7669 return OK;
7670}
7671
Bram Moolenaar17a13432016-01-24 14:22:10 +01007672 static char *
7673get_var_special_name(int nr)
7674{
7675 switch (nr)
7676 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007677 case VVAL_FALSE: return "v:false";
7678 case VVAL_TRUE: return "v:true";
7679 case VVAL_NONE: return "v:none";
7680 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007681 }
7682 EMSG2(_(e_intern2), "get_var_special_name()");
7683 return "42";
7684}
7685
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 * Return a string with the string representation of a variable.
7688 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007689 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007691 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007692 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 */
7694 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695echo_string(
7696 typval_T *tv,
7697 char_u **tofree,
7698 char_u *numbuf,
7699 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701 static int recurse = 0;
7702 char_u *r = NULL;
7703
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007705 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007706 if (!did_echo_string_emsg)
7707 {
7708 /* Only give this message once for a recursive call to avoid
7709 * flooding the user with errors. And stop iterating over lists
7710 * and dicts. */
7711 did_echo_string_emsg = TRUE;
7712 EMSG(_("E724: variable nested too deep for displaying"));
7713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007715 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 }
7717 ++recurse;
7718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719 switch (tv->v_type)
7720 {
7721 case VAR_FUNC:
7722 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 r = tv->vval.v_string;
7724 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007725
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007727 if (tv->vval.v_list == NULL)
7728 {
7729 *tofree = NULL;
7730 r = NULL;
7731 }
7732 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7733 {
7734 *tofree = NULL;
7735 r = (char_u *)"[...]";
7736 }
7737 else
7738 {
7739 tv->vval.v_list->lv_copyID = copyID;
7740 *tofree = list2string(tv, copyID);
7741 r = *tofree;
7742 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007743 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007744
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007746 if (tv->vval.v_dict == NULL)
7747 {
7748 *tofree = NULL;
7749 r = NULL;
7750 }
7751 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7752 {
7753 *tofree = NULL;
7754 r = (char_u *)"{...}";
7755 }
7756 else
7757 {
7758 tv->vval.v_dict->dv_copyID = copyID;
7759 *tofree = dict2string(tv, copyID);
7760 r = *tofree;
7761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007762 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764 case VAR_STRING:
7765 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007766 *tofree = NULL;
7767 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007770#ifdef FEAT_FLOAT
7771 case VAR_FLOAT:
7772 *tofree = NULL;
7773 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7774 r = numbuf;
7775 break;
7776#endif
7777
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007778 case VAR_SPECIAL:
7779 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007780 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007781 break;
7782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007787
Bram Moolenaar8502c702014-06-17 12:51:16 +02007788 if (--recurse == 0)
7789 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791}
7792
7793/*
7794 * Return a string with the string representation of a variable.
7795 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7796 * "numbuf" is used for a number.
7797 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007798 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007799 */
7800 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801tv2string(
7802 typval_T *tv,
7803 char_u **tofree,
7804 char_u *numbuf,
7805 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007806{
7807 switch (tv->v_type)
7808 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007809 case VAR_FUNC:
7810 *tofree = string_quote(tv->vval.v_string, TRUE);
7811 return *tofree;
7812 case VAR_STRING:
7813 *tofree = string_quote(tv->vval.v_string, FALSE);
7814 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007815#ifdef FEAT_FLOAT
7816 case VAR_FLOAT:
7817 *tofree = NULL;
7818 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7819 return numbuf;
7820#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007822 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007823 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007824 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007826 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007829 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007830}
7831
7832/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 * Return string "str" in ' quotes, doubling ' characters.
7834 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007836 */
7837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007838string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007839{
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007841 char_u *p, *r, *s;
7842
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 len = (function ? 13 : 3);
7844 if (str != NULL)
7845 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007846 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 for (p = str; *p != NUL; mb_ptr_adv(p))
7848 if (*p == '\'')
7849 ++len;
7850 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007851 s = r = alloc(len);
7852 if (r != NULL)
7853 {
7854 if (function)
7855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007857 r += 10;
7858 }
7859 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 if (str != NULL)
7862 for (p = str; *p != NUL; )
7863 {
7864 if (*p == '\'')
7865 *r++ = '\'';
7866 MB_COPY_CHAR(p, r);
7867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869 if (function)
7870 *r++ = ')';
7871 *r++ = NUL;
7872 }
7873 return s;
7874}
7875
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007876#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877/*
7878 * Convert the string "text" to a floating point number.
7879 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7880 * this always uses a decimal point.
7881 * Returns the length of the text that was consumed.
7882 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884string2float(
7885 char_u *text,
7886 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007887{
7888 char *s = (char *)text;
7889 float_T f;
7890
7891 f = strtod(s, &s);
7892 *value = f;
7893 return (int)((char_u *)s - text);
7894}
7895#endif
7896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 * Get the value of an environment variable.
7899 * "arg" is pointing to the '$'. It is advanced to after the name.
7900 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007901 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 */
7903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007904get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 char_u *string = NULL;
7907 int len;
7908 int cc;
7909 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007910 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911
7912 ++*arg;
7913 name = *arg;
7914 len = get_env_len(arg);
7915 if (evaluate)
7916 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007917 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007918 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007919
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007920 cc = name[len];
7921 name[len] = NUL;
7922 /* first try vim_getenv(), fast for normal environment vars */
7923 string = vim_getenv(name, &mustfree);
7924 if (string != NULL && *string != NUL)
7925 {
7926 if (!mustfree)
7927 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007929 else
7930 {
7931 if (mustfree)
7932 vim_free(string);
7933
7934 /* next try expanding things like $VIM and ${HOME} */
7935 string = expand_env_save(name - 1);
7936 if (string != NULL && *string == '$')
7937 {
7938 vim_free(string);
7939 string = NULL;
7940 }
7941 }
7942 name[len] = cc;
7943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944 rettv->v_type = VAR_STRING;
7945 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 }
7947
7948 return OK;
7949}
7950
7951/*
7952 * Array with names and number of arguments of all internal functions
7953 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7954 */
7955static struct fst
7956{
7957 char *f_name; /* function name */
7958 char f_min_argc; /* minimal number of arguments */
7959 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007960 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007961 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962} functions[] =
7963{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964#ifdef FEAT_FLOAT
7965 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007966 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007967#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007968 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007969 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007970 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"append", 2, 2, f_append},
7972 {"argc", 0, 0, f_argc},
7973 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007974 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007975 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007976#ifdef FEAT_FLOAT
7977 {"asin", 1, 1, f_asin}, /* WJMc */
7978#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007979 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007980 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007981 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007982 {"assert_false", 1, 2, f_assert_false},
7983 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007984#ifdef FEAT_FLOAT
7985 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007986 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007989 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"bufexists", 1, 1, f_bufexists},
7991 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7992 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7993 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7994 {"buflisted", 1, 1, f_buflisted},
7995 {"bufloaded", 1, 1, f_bufloaded},
7996 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007997 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {"bufwinnr", 1, 1, f_bufwinnr},
7999 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008000 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008001 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003#ifdef FEAT_FLOAT
8004 {"ceil", 1, 1, f_ceil},
8005#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008006 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008007 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008009 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008011#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008012 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008013 {"complete_add", 1, 1, f_complete_add},
8014 {"complete_check", 0, 0, f_complete_check},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"confirm", 1, 4, f_confirm},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008017#ifdef FEAT_CHANNEL
8018 {"connect", 2, 3, f_connect},
8019#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008020 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008021#ifdef FEAT_FLOAT
8022 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008023 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008025 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008027 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008028 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008029 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008031 {"diff_filler", 1, 1, f_diff_filler},
8032 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008033#ifdef FEAT_CHANNEL
8034 {"disconnect", 1, 1, f_disconnect},
8035#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008036 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008038 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"eventhandler", 0, 0, f_eventhandler},
8040 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008041 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008043#ifdef FEAT_FLOAT
8044 {"exp", 1, 1, f_exp},
8045#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008046 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008047 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008048 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8050 {"filereadable", 1, 1, f_filereadable},
8051 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008053 {"finddir", 1, 3, f_finddir},
8054 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#ifdef FEAT_FLOAT
8056 {"float2nr", 1, 1, f_float2nr},
8057 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008058 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008059#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008060 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"fnamemodify", 2, 2, f_fnamemodify},
8062 {"foldclosed", 1, 1, f_foldclosed},
8063 {"foldclosedend", 1, 1, f_foldclosedend},
8064 {"foldlevel", 1, 1, f_foldlevel},
8065 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008066 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008069 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008070 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008071 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008072 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"getchar", 0, 1, f_getchar},
8074 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008075 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"getcmdline", 0, 0, f_getcmdline},
8077 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008078 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008079 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008080 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008081 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008082 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008083 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {"getfsize", 1, 1, f_getfsize},
8085 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008086 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008087 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008088 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008089 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008090 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008091 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008092 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008093 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008095 {"gettabvar", 2, 3, f_gettabvar},
8096 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"getwinposx", 0, 0, f_getwinposx},
8098 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008099 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008100 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008101 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008102 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008105 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008106 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8108 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8109 {"histadd", 2, 2, f_histadd},
8110 {"histdel", 1, 2, f_histdel},
8111 {"histget", 1, 2, f_histget},
8112 {"histnr", 1, 1, f_histnr},
8113 {"hlID", 1, 1, f_hlID},
8114 {"hlexists", 1, 1, f_hlexists},
8115 {"hostname", 0, 0, f_hostname},
8116 {"iconv", 3, 3, f_iconv},
8117 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008118 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008119 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008121 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"inputrestore", 0, 0, f_inputrestore},
8123 {"inputsave", 0, 0, f_inputsave},
8124 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008126 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008128 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008129 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008131 {"jsondecode", 1, 1, f_jsondecode},
8132 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008133 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"libcall", 3, 3, f_libcall},
8137 {"libcallnr", 3, 3, f_libcallnr},
8138 {"line", 1, 1, f_line},
8139 {"line2byte", 1, 1, f_line2byte},
8140 {"lispindent", 1, 1, f_lispindent},
8141 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008142#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008143 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008144 {"log10", 1, 1, f_log10},
8145#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008146#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008147 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008148#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008150 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008151 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008152 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008153 {"matchadd", 2, 5, f_matchadd},
8154 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008155 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008156 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008157 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008158 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008159 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008160 {"max", 1, 1, f_max},
8161 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008162#ifdef vim_mkdir
8163 {"mkdir", 1, 3, f_mkdir},
8164#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008166#ifdef FEAT_MZSCHEME
8167 {"mzeval", 1, 1, f_mzeval},
8168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008170 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008172 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008173#ifdef FEAT_PERL
8174 {"perleval", 1, 1, f_perleval},
8175#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008176#ifdef FEAT_FLOAT
8177 {"pow", 2, 2, f_pow},
8178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008180 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008181 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008182#ifdef FEAT_PYTHON3
8183 {"py3eval", 1, 1, f_py3eval},
8184#endif
8185#ifdef FEAT_PYTHON
8186 {"pyeval", 1, 1, f_pyeval},
8187#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008188 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008189 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008190 {"reltime", 0, 2, f_reltime},
8191 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"remote_expr", 2, 3, f_remote_expr},
8193 {"remote_foreground", 1, 1, f_remote_foreground},
8194 {"remote_peek", 1, 2, f_remote_peek},
8195 {"remote_read", 1, 1, f_remote_read},
8196 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008197 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008199 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008202#ifdef FEAT_FLOAT
8203 {"round", 1, 1, f_round},
8204#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008205 {"screenattr", 2, 2, f_screenattr},
8206 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008207 {"screencol", 0, 0, f_screencol},
8208 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008209 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008210 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008211 {"searchpair", 3, 7, f_searchpair},
8212 {"searchpairpos", 3, 7, f_searchpairpos},
8213 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008214#ifdef FEAT_CHANNEL
8215 {"sendexpr", 2, 3, f_sendexpr},
8216 {"sendraw", 2, 3, f_sendraw},
8217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"server2client", 2, 2, f_server2client},
8219 {"serverlist", 0, 0, f_serverlist},
8220 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008221 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"setcmdpos", 1, 1, f_setcmdpos},
8223 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008224 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008225 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008226 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008227 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008229 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008230 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008232#ifdef FEAT_CRYPT
8233 {"sha256", 1, 1, f_sha256},
8234#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008235 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008236 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008238#ifdef FEAT_FLOAT
8239 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008240 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008241#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008242 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008243 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008244 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008245 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008246 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247#ifdef FEAT_FLOAT
8248 {"sqrt", 1, 1, f_sqrt},
8249 {"str2float", 1, 1, f_str2float},
8250#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008251 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008252 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008253 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254#ifdef HAVE_STRFTIME
8255 {"strftime", 1, 2, f_strftime},
8256#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008258 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"strlen", 1, 1, f_strlen},
8260 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008261 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008263 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008264 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {"substitute", 4, 4, f_substitute},
8266 {"synID", 3, 3, f_synID},
8267 {"synIDattr", 2, 3, f_synIDattr},
8268 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008269 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008270 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008271 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008272 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008273 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008274 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008275 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008276 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008277 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008278#ifdef FEAT_FLOAT
8279 {"tan", 1, 1, f_tan},
8280 {"tanh", 1, 1, f_tanh},
8281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008283 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"tolower", 1, 1, f_tolower},
8285 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008286 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008287#ifdef FEAT_FLOAT
8288 {"trunc", 1, 1, f_trunc},
8289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008291 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008292 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008293 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008294 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"virtcol", 1, 1, f_virtcol},
8296 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008297 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"winbufnr", 1, 1, f_winbufnr},
8299 {"wincol", 0, 0, f_wincol},
8300 {"winheight", 1, 1, f_winheight},
8301 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008302 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008304 {"winrestview", 1, 1, f_winrestview},
8305 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008307 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008308 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008309 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310};
8311
8312#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8313
8314/*
8315 * Function given to ExpandGeneric() to obtain the list of internal
8316 * or user defined function names.
8317 */
8318 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008319get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 static int intidx = -1;
8322 char_u *name;
8323
8324 if (idx == 0)
8325 intidx = -1;
8326 if (intidx < 0)
8327 {
8328 name = get_user_func_name(xp, idx);
8329 if (name != NULL)
8330 return name;
8331 }
8332 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8333 {
8334 STRCPY(IObuff, functions[intidx].f_name);
8335 STRCAT(IObuff, "(");
8336 if (functions[intidx].f_max_argc == 0)
8337 STRCAT(IObuff, ")");
8338 return IObuff;
8339 }
8340
8341 return NULL;
8342}
8343
8344/*
8345 * Function given to ExpandGeneric() to obtain the list of internal or
8346 * user defined variable or function names.
8347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008349get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 static int intidx = -1;
8352 char_u *name;
8353
8354 if (idx == 0)
8355 intidx = -1;
8356 if (intidx < 0)
8357 {
8358 name = get_function_name(xp, idx);
8359 if (name != NULL)
8360 return name;
8361 }
8362 return get_user_var_name(xp, ++intidx);
8363}
8364
8365#endif /* FEAT_CMDL_COMPL */
8366
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008367#if defined(EBCDIC) || defined(PROTO)
8368/*
8369 * Compare struct fst by function name.
8370 */
8371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008372compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008373{
8374 struct fst *p1 = (struct fst *)s1;
8375 struct fst *p2 = (struct fst *)s2;
8376
8377 return STRCMP(p1->f_name, p2->f_name);
8378}
8379
8380/*
8381 * Sort the function table by function name.
8382 * The sorting of the table above is ASCII dependant.
8383 * On machines using EBCDIC we have to sort it.
8384 */
8385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008386sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008387{
8388 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8389
8390 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8391}
8392#endif
8393
8394
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395/*
8396 * Find internal function in table above.
8397 * Return index, or -1 if not found
8398 */
8399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008400find_internal_func(
8401 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 int first = 0;
8404 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8405 int cmp;
8406 int x;
8407
8408 /*
8409 * Find the function name in the table. Binary search.
8410 */
8411 while (first <= last)
8412 {
8413 x = first + ((unsigned)(last - first) >> 1);
8414 cmp = STRCMP(name, functions[x].f_name);
8415 if (cmp < 0)
8416 last = x - 1;
8417 else if (cmp > 0)
8418 first = x + 1;
8419 else
8420 return x;
8421 }
8422 return -1;
8423}
8424
8425/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008426 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8427 * name it contains, otherwise return "name".
8428 */
8429 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008430deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008431{
Bram Moolenaar33570922005-01-25 22:26:29 +00008432 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008433 int cc;
8434
8435 cc = name[*lenp];
8436 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008437 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008440 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008442 {
8443 *lenp = 0;
8444 return (char_u *)""; /* just in case */
8445 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008446 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008447 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008448 }
8449
8450 return name;
8451}
8452
8453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 * Allocate a variable for the result of a function.
8455 * Return OK or FAIL.
8456 */
8457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008458get_func_tv(
8459 char_u *name, /* name of the function */
8460 int len, /* length of "name" */
8461 typval_T *rettv,
8462 char_u **arg, /* argument, pointing to the '(' */
8463 linenr_T firstline, /* first line of range */
8464 linenr_T lastline, /* last line of range */
8465 int *doesrange, /* return: function handled range */
8466 int evaluate,
8467 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469 char_u *argp;
8470 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008471 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 int argcount = 0; /* number of arguments found */
8473
8474 /*
8475 * Get the arguments.
8476 */
8477 argp = *arg;
8478 while (argcount < MAX_FUNC_ARGS)
8479 {
8480 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8481 if (*argp == ')' || *argp == ',' || *argp == NUL)
8482 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8484 {
8485 ret = FAIL;
8486 break;
8487 }
8488 ++argcount;
8489 if (*argp != ',')
8490 break;
8491 }
8492 if (*argp == ')')
8493 ++argp;
8494 else
8495 ret = FAIL;
8496
8497 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 {
8502 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008503 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008504 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008505 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507
8508 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
8511 *arg = skipwhite(argp);
8512 return ret;
8513}
8514
8515
8516/*
8517 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008518 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008519 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008522call_func(
8523 char_u *funcname, /* name of the function */
8524 int len, /* length of "name" */
8525 typval_T *rettv, /* return value goes here */
8526 int argcount, /* number of "argvars" */
8527 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008528 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008529 linenr_T firstline, /* first line of range */
8530 linenr_T lastline, /* last line of range */
8531 int *doesrange, /* return: function handled range */
8532 int evaluate,
8533 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536#define ERROR_UNKNOWN 0
8537#define ERROR_TOOMANY 1
8538#define ERROR_TOOFEW 2
8539#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540#define ERROR_DICT 4
8541#define ERROR_NONE 5
8542#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 int error = ERROR_NONE;
8544 int i;
8545 int llen;
8546 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547#define FLEN_FIXED 40
8548 char_u fname_buf[FLEN_FIXED + 1];
8549 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008550 char_u *name;
8551
8552 /* Make a copy of the name, if it comes from a funcref variable it could
8553 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008554 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008555 if (name == NULL)
8556 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558 /*
8559 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8560 * Change <SNR>123_name() to K_SNR 123_name().
8561 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 llen = eval_fname_script(name);
8564 if (llen > 0)
8565 {
8566 fname_buf[0] = K_SPECIAL;
8567 fname_buf[1] = KS_EXTRA;
8568 fname_buf[2] = (int)KE_SNR;
8569 i = 3;
8570 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8571 {
8572 if (current_SID <= 0)
8573 error = ERROR_SCRIPT;
8574 else
8575 {
8576 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8577 i = (int)STRLEN(fname_buf);
8578 }
8579 }
8580 if (i + STRLEN(name + llen) < FLEN_FIXED)
8581 {
8582 STRCPY(fname_buf + i, name + llen);
8583 fname = fname_buf;
8584 }
8585 else
8586 {
8587 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8588 if (fname == NULL)
8589 error = ERROR_OTHER;
8590 else
8591 {
8592 mch_memmove(fname, fname_buf, (size_t)i);
8593 STRCPY(fname + i, name + llen);
8594 }
8595 }
8596 }
8597 else
8598 fname = name;
8599
8600 *doesrange = FALSE;
8601
8602
8603 /* execute the function if no errors detected and executing */
8604 if (evaluate && error == ERROR_NONE)
8605 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008606 char_u *rfname = fname;
8607
8608 /* Ignore "g:" before a function name. */
8609 if (fname[0] == 'g' && fname[1] == ':')
8610 rfname = fname + 2;
8611
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008612 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8613 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 error = ERROR_UNKNOWN;
8615
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008616 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
8618 /*
8619 * User defined function.
8620 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008621 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008622
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008624 /* Trigger FuncUndefined event, may load the function. */
8625 if (fp == NULL
8626 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008627 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008628 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008630 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008631 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 }
8633#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008634 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008635 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008636 {
8637 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008638 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008639 }
8640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (fp != NULL)
8642 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008643 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008645 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008647 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008649 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008650 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 else
8652 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008653 int did_save_redo = FALSE;
8654
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 /*
8656 * Call the user function.
8657 * Save and restore search patterns, script variables and
8658 * redo buffer.
8659 */
8660 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008661#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008662 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008663#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008664 {
8665 saveRedobuff();
8666 did_save_redo = TRUE;
8667 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008668 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008671 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8672 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8673 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008674 /* Function was unreferenced while being used, free it
8675 * now. */
8676 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008677 if (did_save_redo)
8678 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 restore_search_patterns();
8680 error = ERROR_NONE;
8681 }
8682 }
8683 }
8684 else
8685 {
8686 /*
8687 * Find the function name in the table, call its implementation.
8688 */
8689 i = find_internal_func(fname);
8690 if (i >= 0)
8691 {
8692 if (argcount < functions[i].f_min_argc)
8693 error = ERROR_TOOFEW;
8694 else if (argcount > functions[i].f_max_argc)
8695 error = ERROR_TOOMANY;
8696 else
8697 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008698 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 error = ERROR_NONE;
8701 }
8702 }
8703 }
8704 /*
8705 * The function call (or "FuncUndefined" autocommand sequence) might
8706 * have been aborted by an error, an interrupt, or an explicitly thrown
8707 * exception that has not been caught so far. This situation can be
8708 * tested for by calling aborting(). For an error in an internal
8709 * function or for the "E132" error in call_user_func(), however, the
8710 * throw point at which the "force_abort" flag (temporarily reset by
8711 * emsg()) is normally updated has not been reached yet. We need to
8712 * update that flag first to make aborting() reliable.
8713 */
8714 update_force_abort();
8715 }
8716 if (error == ERROR_NONE)
8717 ret = OK;
8718
8719 /*
8720 * Report an error unless the argument evaluation or function call has been
8721 * cancelled due to an aborting error, an interrupt, or an exception.
8722 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008723 if (!aborting())
8724 {
8725 switch (error)
8726 {
8727 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008728 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008729 break;
8730 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008731 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008732 break;
8733 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008734 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008735 name);
8736 break;
8737 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008738 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008739 name);
8740 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008742 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008743 name);
8744 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008745 }
8746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (fname != name && fname != fname_buf)
8749 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008750 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
8752 return ret;
8753}
8754
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008755/*
8756 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008757 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008758 */
8759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008760emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008761{
8762 char_u *p;
8763
8764 if (*name == K_SPECIAL)
8765 p = concat_str((char_u *)"<SNR>", name + 3);
8766 else
8767 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008768 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008769 if (p != name)
8770 vim_free(p);
8771}
8772
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008773/*
8774 * Return TRUE for a non-zero Number and a non-empty String.
8775 */
8776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008777non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008778{
8779 return ((argvars[0].v_type == VAR_NUMBER
8780 && argvars[0].vval.v_number != 0)
8781 || (argvars[0].v_type == VAR_STRING
8782 && argvars[0].vval.v_string != NULL
8783 && *argvars[0].vval.v_string != NUL));
8784}
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786/*********************************************
8787 * Implementation of the built-in functions
8788 */
8789
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008790#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008791static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008792
8793/*
8794 * Get the float value of "argvars[0]" into "f".
8795 * Returns FAIL when the argument is not a Number or Float.
8796 */
8797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008798get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008799{
8800 if (argvars[0].v_type == VAR_FLOAT)
8801 {
8802 *f = argvars[0].vval.v_float;
8803 return OK;
8804 }
8805 if (argvars[0].v_type == VAR_NUMBER)
8806 {
8807 *f = (float_T)argvars[0].vval.v_number;
8808 return OK;
8809 }
8810 EMSG(_("E808: Number or Float required"));
8811 return FAIL;
8812}
8813
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008814/*
8815 * "abs(expr)" function
8816 */
8817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008818f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008819{
8820 if (argvars[0].v_type == VAR_FLOAT)
8821 {
8822 rettv->v_type = VAR_FLOAT;
8823 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8824 }
8825 else
8826 {
8827 varnumber_T n;
8828 int error = FALSE;
8829
8830 n = get_tv_number_chk(&argvars[0], &error);
8831 if (error)
8832 rettv->vval.v_number = -1;
8833 else if (n > 0)
8834 rettv->vval.v_number = n;
8835 else
8836 rettv->vval.v_number = -n;
8837 }
8838}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008839
8840/*
8841 * "acos()" function
8842 */
8843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008844f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008845{
8846 float_T f;
8847
8848 rettv->v_type = VAR_FLOAT;
8849 if (get_float_arg(argvars, &f) == OK)
8850 rettv->vval.v_float = acos(f);
8851 else
8852 rettv->vval.v_float = 0.0;
8853}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008854#endif
8855
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008857 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 */
8859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008860f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008865 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008867 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008868 && !tv_check_lock(l->lv_lock,
8869 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008870 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008872 }
8873 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008874 EMSG(_(e_listreq));
8875}
8876
8877/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008878 * "alloc_fail(id, countdown, repeat)" function
8879 */
8880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008881f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008882{
8883 if (argvars[0].v_type != VAR_NUMBER
8884 || argvars[0].vval.v_number <= 0
8885 || argvars[1].v_type != VAR_NUMBER
8886 || argvars[1].vval.v_number < 0
8887 || argvars[2].v_type != VAR_NUMBER)
8888 EMSG(_(e_invarg));
8889 else
8890 {
8891 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008892 if (alloc_fail_id >= aid_last)
8893 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008894 alloc_fail_countdown = argvars[1].vval.v_number;
8895 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008896 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008897 }
8898}
8899
8900/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008901 * "and(expr, expr)" function
8902 */
8903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008904f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008905{
8906 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8907 & get_tv_number_chk(&argvars[1], NULL);
8908}
8909
8910/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008911 * "append(lnum, string/list)" function
8912 */
8913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008914f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008915{
8916 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008918 list_T *l = NULL;
8919 listitem_T *li = NULL;
8920 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008921 long added = 0;
8922
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008923 /* When coming here from Insert mode, sync undo, so that this can be
8924 * undone separately from what was previously inserted. */
8925 if (u_sync_once == 2)
8926 {
8927 u_sync_once = 1; /* notify that u_sync() was called */
8928 u_sync(TRUE);
8929 }
8930
Bram Moolenaar0d660222005-01-07 21:51:51 +00008931 lnum = get_tv_lnum(argvars);
8932 if (lnum >= 0
8933 && lnum <= curbuf->b_ml.ml_line_count
8934 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008935 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008936 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008937 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008938 l = argvars[1].vval.v_list;
8939 if (l == NULL)
8940 return;
8941 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008942 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943 for (;;)
8944 {
8945 if (l == NULL)
8946 tv = &argvars[1]; /* append a string */
8947 else if (li == NULL)
8948 break; /* end of list */
8949 else
8950 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 line = get_tv_string_chk(tv);
8952 if (line == NULL) /* type error */
8953 {
8954 rettv->vval.v_number = 1; /* Failed */
8955 break;
8956 }
8957 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008958 ++added;
8959 if (l == NULL)
8960 break;
8961 li = li->li_next;
8962 }
8963
8964 appended_lines_mark(lnum, added);
8965 if (curwin->w_cursor.lnum > lnum)
8966 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 else
8969 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970}
8971
8972/*
8973 * "argc()" function
8974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008976f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979}
8980
8981/*
8982 * "argidx()" function
8983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008985f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988}
8989
8990/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008991 * "arglistid()" function
8992 */
8993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008994f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008995{
8996 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008997
8998 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01008999 wp = find_tabwin(&argvars[0], &argvars[1]);
9000 if (wp != NULL)
9001 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009002}
9003
9004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 * "argv(nr)" function
9006 */
9007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009008f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 int idx;
9011
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009012 if (argvars[0].v_type != VAR_UNKNOWN)
9013 {
9014 idx = get_tv_number_chk(&argvars[0], NULL);
9015 if (idx >= 0 && idx < ARGCOUNT)
9016 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9017 else
9018 rettv->vval.v_string = NULL;
9019 rettv->v_type = VAR_STRING;
9020 }
9021 else if (rettv_list_alloc(rettv) == OK)
9022 for (idx = 0; idx < ARGCOUNT; ++idx)
9023 list_append_string(rettv->vval.v_list,
9024 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025}
9026
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009027static void prepare_assert_error(garray_T*gap);
9028static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9029static void assert_error(garray_T *gap);
9030static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009031
9032/*
9033 * Prepare "gap" for an assert error and add the sourcing position.
9034 */
9035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009036prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009037{
9038 char buf[NUMBUFLEN];
9039
9040 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009041 if (sourcing_name != NULL)
9042 {
9043 ga_concat(gap, sourcing_name);
9044 if (sourcing_lnum > 0)
9045 ga_concat(gap, (char_u *)" ");
9046 }
9047 if (sourcing_lnum > 0)
9048 {
9049 sprintf(buf, "line %ld", (long)sourcing_lnum);
9050 ga_concat(gap, (char_u *)buf);
9051 }
9052 if (sourcing_name != NULL || sourcing_lnum > 0)
9053 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009054}
9055
9056/*
9057 * Fill "gap" with information about an assert error.
9058 */
9059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009060fill_assert_error(
9061 garray_T *gap,
9062 typval_T *opt_msg_tv,
9063 char_u *exp_str,
9064 typval_T *exp_tv,
9065 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009066{
9067 char_u numbuf[NUMBUFLEN];
9068 char_u *tofree;
9069
9070 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9071 {
9072 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9073 vim_free(tofree);
9074 }
9075 else
9076 {
9077 ga_concat(gap, (char_u *)"Expected ");
9078 if (exp_str == NULL)
9079 {
9080 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9081 vim_free(tofree);
9082 }
9083 else
9084 ga_concat(gap, exp_str);
9085 ga_concat(gap, (char_u *)" but got ");
9086 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9087 vim_free(tofree);
9088 }
9089}
Bram Moolenaar43345542015-11-29 17:35:35 +01009090
9091/*
9092 * Add an assert error to v:errors.
9093 */
9094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009095assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009096{
9097 struct vimvar *vp = &vimvars[VV_ERRORS];
9098
9099 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9100 /* Make sure v:errors is a list. */
9101 set_vim_var_list(VV_ERRORS, list_alloc());
9102 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9103}
9104
Bram Moolenaar43345542015-11-29 17:35:35 +01009105/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009106 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009107 */
9108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009109f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009110{
9111 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009112
9113 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9114 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009115 prepare_assert_error(&ga);
9116 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9117 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009118 ga_clear(&ga);
9119 }
9120}
9121
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009122/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009123 * "assert_exception(string[, msg])" function
9124 */
9125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009126f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009127{
9128 garray_T ga;
9129 char *error;
9130
9131 error = (char *)get_tv_string_chk(&argvars[0]);
9132 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9133 {
9134 prepare_assert_error(&ga);
9135 ga_concat(&ga, (char_u *)"v:exception is not set");
9136 assert_error(&ga);
9137 ga_clear(&ga);
9138 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009139 else if (error != NULL
9140 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009141 {
9142 prepare_assert_error(&ga);
9143 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9144 &vimvars[VV_EXCEPTION].vv_tv);
9145 assert_error(&ga);
9146 ga_clear(&ga);
9147 }
9148}
9149
9150/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009151 * "assert_fails(cmd [, error])" function
9152 */
9153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009154f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009155{
9156 char_u *cmd = get_tv_string_chk(&argvars[0]);
9157 garray_T ga;
9158
9159 called_emsg = FALSE;
9160 suppress_errthrow = TRUE;
9161 emsg_silent = TRUE;
9162 do_cmdline_cmd(cmd);
9163 if (!called_emsg)
9164 {
9165 prepare_assert_error(&ga);
9166 ga_concat(&ga, (char_u *)"command did not fail: ");
9167 ga_concat(&ga, cmd);
9168 assert_error(&ga);
9169 ga_clear(&ga);
9170 }
9171 else if (argvars[1].v_type != VAR_UNKNOWN)
9172 {
9173 char_u buf[NUMBUFLEN];
9174 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9175
9176 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9177 {
9178 prepare_assert_error(&ga);
9179 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9180 &vimvars[VV_ERRMSG].vv_tv);
9181 assert_error(&ga);
9182 ga_clear(&ga);
9183 }
9184 }
9185
9186 called_emsg = FALSE;
9187 suppress_errthrow = FALSE;
9188 emsg_silent = FALSE;
9189 emsg_on_display = FALSE;
9190 set_vim_var_string(VV_ERRMSG, NULL, 0);
9191}
9192
9193/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009194 * Common for assert_true() and assert_false().
9195 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009197assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009198{
9199 int error = FALSE;
9200 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009201
9202 if (argvars[0].v_type != VAR_NUMBER
9203 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9204 || error)
9205 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009206 prepare_assert_error(&ga);
9207 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009208 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009209 NULL, &argvars[0]);
9210 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009211 ga_clear(&ga);
9212 }
9213}
9214
9215/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009216 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009217 */
9218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009219f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009220{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009221 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009222}
9223
9224/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009225 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009226 */
9227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009228f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009229{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009230 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009231}
9232
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009233#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009234/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009235 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009236 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009238f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009239{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009240 float_T f;
9241
9242 rettv->v_type = VAR_FLOAT;
9243 if (get_float_arg(argvars, &f) == OK)
9244 rettv->vval.v_float = asin(f);
9245 else
9246 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009247}
9248
9249/*
9250 * "atan()" function
9251 */
9252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009253f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009254{
9255 float_T f;
9256
9257 rettv->v_type = VAR_FLOAT;
9258 if (get_float_arg(argvars, &f) == OK)
9259 rettv->vval.v_float = atan(f);
9260 else
9261 rettv->vval.v_float = 0.0;
9262}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009263
9264/*
9265 * "atan2()" function
9266 */
9267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009268f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009269{
9270 float_T fx, fy;
9271
9272 rettv->v_type = VAR_FLOAT;
9273 if (get_float_arg(argvars, &fx) == OK
9274 && get_float_arg(&argvars[1], &fy) == OK)
9275 rettv->vval.v_float = atan2(fx, fy);
9276 else
9277 rettv->vval.v_float = 0.0;
9278}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009279#endif
9280
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281/*
9282 * "browse(save, title, initdir, default)" function
9283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009285f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286{
9287#ifdef FEAT_BROWSE
9288 int save;
9289 char_u *title;
9290 char_u *initdir;
9291 char_u *defname;
9292 char_u buf[NUMBUFLEN];
9293 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009296 save = get_tv_number_chk(&argvars[0], &error);
9297 title = get_tv_string_chk(&argvars[1]);
9298 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9299 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 if (error || title == NULL || initdir == NULL || defname == NULL)
9302 rettv->vval.v_string = NULL;
9303 else
9304 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009305 do_browse(save ? BROWSE_SAVE : 0,
9306 title, defname, NULL, initdir, NULL, curbuf);
9307#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009311}
9312
9313/*
9314 * "browsedir(title, initdir)" function
9315 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009317f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009318{
9319#ifdef FEAT_BROWSE
9320 char_u *title;
9321 char_u *initdir;
9322 char_u buf[NUMBUFLEN];
9323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009324 title = get_tv_string_chk(&argvars[0]);
9325 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 if (title == NULL || initdir == NULL)
9328 rettv->vval.v_string = NULL;
9329 else
9330 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009331 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336}
9337
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009338static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009339
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340/*
9341 * Find a buffer by number or exact name.
9342 */
9343 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009344find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009348 if (avar->v_type == VAR_NUMBER)
9349 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009350 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009352 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009353 if (buf == NULL)
9354 {
9355 /* No full path name match, try a match with a URL or a "nofile"
9356 * buffer, these don't use the full path. */
9357 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9358 if (buf->b_fname != NULL
9359 && (path_with_url(buf->b_fname)
9360#ifdef FEAT_QUICKFIX
9361 || bt_nofile(buf)
9362#endif
9363 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009364 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009365 break;
9366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 }
9368 return buf;
9369}
9370
9371/*
9372 * "bufexists(expr)" function
9373 */
9374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009375f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378}
9379
9380/*
9381 * "buflisted(expr)" function
9382 */
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 buf_T *buf;
9387
9388 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009389 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390}
9391
9392/*
9393 * "bufloaded(expr)" function
9394 */
9395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009396f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 buf_T *buf;
9399
9400 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009404static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406/*
9407 * Get buffer by number or pattern.
9408 */
9409 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009410get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 int save_magic;
9414 char_u *save_cpo;
9415 buf_T *buf;
9416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417 if (tv->v_type == VAR_NUMBER)
9418 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009419 if (tv->v_type != VAR_STRING)
9420 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 if (name == NULL || *name == NUL)
9422 return curbuf;
9423 if (name[0] == '$' && name[1] == NUL)
9424 return lastbuf;
9425
9426 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9427 save_magic = p_magic;
9428 p_magic = TRUE;
9429 save_cpo = p_cpo;
9430 p_cpo = (char_u *)"";
9431
9432 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009433 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434
9435 p_magic = save_magic;
9436 p_cpo = save_cpo;
9437
9438 /* If not found, try expanding the name, like done for bufexists(). */
9439 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
9442 return buf;
9443}
9444
9445/*
9446 * "bufname(expr)" function
9447 */
9448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009449f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 buf_T *buf;
9452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009455 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 --emsg_off;
9462}
9463
9464/*
9465 * "bufnr(expr)" function
9466 */
9467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009468f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009471 int error = FALSE;
9472 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009476 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009477 --emsg_off;
9478
9479 /* If the buffer isn't found and the second argument is not zero create a
9480 * new buffer. */
9481 if (buf == NULL
9482 && argvars[1].v_type != VAR_UNKNOWN
9483 && get_tv_number_chk(&argvars[1], &error) != 0
9484 && !error
9485 && (name = get_tv_string_chk(&argvars[0])) != NULL
9486 && !error)
9487 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9488
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
9495/*
9496 * "bufwinnr(nr)" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501#ifdef FEAT_WINDOWS
9502 win_T *wp;
9503 int winnr = 0;
9504#endif
9505 buf_T *buf;
9506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009509 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510#ifdef FEAT_WINDOWS
9511 for (wp = firstwin; wp; wp = wp->w_next)
9512 {
9513 ++winnr;
9514 if (wp->w_buffer == buf)
9515 break;
9516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520#endif
9521 --emsg_off;
9522}
9523
9524/*
9525 * "byte2line(byte)" function
9526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532#else
9533 long boff = 0;
9534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 (linenr_T)0, &boff);
9541#endif
9542}
9543
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009545byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009546{
9547#ifdef FEAT_MBYTE
9548 char_u *t;
9549#endif
9550 char_u *str;
9551 long idx;
9552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 str = get_tv_string_chk(&argvars[0]);
9554 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009557 return;
9558
9559#ifdef FEAT_MBYTE
9560 t = str;
9561 for ( ; idx > 0; idx--)
9562 {
9563 if (*t == NUL) /* EOL reached */
9564 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009565 if (enc_utf8 && comp)
9566 t += utf_ptr2len(t);
9567 else
9568 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009569 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009570 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009571#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009572 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009574#endif
9575}
9576
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009577/*
9578 * "byteidx()" function
9579 */
9580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009581f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009582{
9583 byteidx(argvars, rettv, FALSE);
9584}
9585
9586/*
9587 * "byteidxcomp()" function
9588 */
9589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009590f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009591{
9592 byteidx(argvars, rettv, TRUE);
9593}
9594
Bram Moolenaardb913952012-06-29 12:54:53 +02009595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009596func_call(
9597 char_u *name,
9598 typval_T *args,
9599 dict_T *selfdict,
9600 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009601{
9602 listitem_T *item;
9603 typval_T argv[MAX_FUNC_ARGS + 1];
9604 int argc = 0;
9605 int dummy;
9606 int r = 0;
9607
9608 for (item = args->vval.v_list->lv_first; item != NULL;
9609 item = item->li_next)
9610 {
9611 if (argc == MAX_FUNC_ARGS)
9612 {
9613 EMSG(_("E699: Too many arguments"));
9614 break;
9615 }
9616 /* Make a copy of each argument. This is needed to be able to set
9617 * v_lock to VAR_FIXED in the copy without changing the original list.
9618 */
9619 copy_tv(&item->li_tv, &argv[argc++]);
9620 }
9621
9622 if (item == NULL)
9623 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9624 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9625 &dummy, TRUE, selfdict);
9626
9627 /* Free the arguments. */
9628 while (argc > 0)
9629 clear_tv(&argv[--argc]);
9630
9631 return r;
9632}
9633
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009634/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009635 * "call(func, arglist)" function
9636 */
9637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009638f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009639{
9640 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009641 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009643 if (argvars[1].v_type != VAR_LIST)
9644 {
9645 EMSG(_(e_listreq));
9646 return;
9647 }
9648 if (argvars[1].vval.v_list == NULL)
9649 return;
9650
9651 if (argvars[0].v_type == VAR_FUNC)
9652 func = argvars[0].vval.v_string;
9653 else
9654 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 if (*func == NUL)
9656 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009657
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 if (argvars[2].v_type != VAR_UNKNOWN)
9659 {
9660 if (argvars[2].v_type != VAR_DICT)
9661 {
9662 EMSG(_(e_dictreq));
9663 return;
9664 }
9665 selfdict = argvars[2].vval.v_dict;
9666 }
9667
Bram Moolenaardb913952012-06-29 12:54:53 +02009668 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009669}
9670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009671#ifdef FEAT_FLOAT
9672/*
9673 * "ceil({float})" function
9674 */
9675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009676f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009677{
9678 float_T f;
9679
9680 rettv->v_type = VAR_FLOAT;
9681 if (get_float_arg(argvars, &f) == OK)
9682 rettv->vval.v_float = ceil(f);
9683 else
9684 rettv->vval.v_float = 0.0;
9685}
9686#endif
9687
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009688/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009689 * "changenr()" function
9690 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009692f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009693{
9694 rettv->vval.v_number = curbuf->b_u_seq_cur;
9695}
9696
9697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 * "char2nr(string)" function
9699 */
9700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009701f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703#ifdef FEAT_MBYTE
9704 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009705 {
9706 int utf8 = 0;
9707
9708 if (argvars[1].v_type != VAR_UNKNOWN)
9709 utf8 = get_tv_number_chk(&argvars[1], NULL);
9710
9711 if (utf8)
9712 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9713 else
9714 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 else
9717#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719}
9720
9721/*
9722 * "cindent(lnum)" function
9723 */
9724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009725f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
9727#ifdef FEAT_CINDENT
9728 pos_T pos;
9729 linenr_T lnum;
9730
9731 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9734 {
9735 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 curwin->w_cursor = pos;
9738 }
9739 else
9740#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742}
9743
9744/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009745 * "clearmatches()" function
9746 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009749{
9750#ifdef FEAT_SEARCH_EXTRA
9751 clear_matches(curwin);
9752#endif
9753}
9754
9755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 * "col(string)" function
9757 */
9758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009759f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761 colnr_T col = 0;
9762 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009763 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009765 fp = var2fpos(&argvars[0], FALSE, &fnum);
9766 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 {
9768 if (fp->col == MAXCOL)
9769 {
9770 /* '> can be MAXCOL, get the length of the line then */
9771 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009772 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 else
9774 col = MAXCOL;
9775 }
9776 else
9777 {
9778 col = fp->col + 1;
9779#ifdef FEAT_VIRTUALEDIT
9780 /* col(".") when the cursor is on the NUL at the end of the line
9781 * because of "coladd" can be seen as an extra column. */
9782 if (virtual_active() && fp == &curwin->w_cursor)
9783 {
9784 char_u *p = ml_get_cursor();
9785
9786 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9787 curwin->w_virtcol - curwin->w_cursor.coladd))
9788 {
9789# ifdef FEAT_MBYTE
9790 int l;
9791
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009792 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 col += l;
9794# else
9795 if (*p != NUL && p[1] == NUL)
9796 ++col;
9797# endif
9798 }
9799 }
9800#endif
9801 }
9802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804}
9805
Bram Moolenaar572cb562005-08-05 21:35:02 +00009806#if defined(FEAT_INS_EXPAND)
9807/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009808 * "complete()" function
9809 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009811f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +00009812{
9813 int startcol;
9814
9815 if ((State & INSERT) == 0)
9816 {
9817 EMSG(_("E785: complete() can only be used in Insert mode"));
9818 return;
9819 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009820
9821 /* Check for undo allowed here, because if something was already inserted
9822 * the line was already saved for undo and this check isn't done. */
9823 if (!undo_allowed())
9824 return;
9825
Bram Moolenaarade00832006-03-10 21:46:58 +00009826 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9827 {
9828 EMSG(_(e_invarg));
9829 return;
9830 }
9831
9832 startcol = get_tv_number_chk(&argvars[0], NULL);
9833 if (startcol <= 0)
9834 return;
9835
9836 set_completion(startcol - 1, argvars[1].vval.v_list);
9837}
9838
9839/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009840 * "complete_add()" function
9841 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009843f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009844{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009845 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009846}
9847
9848/*
9849 * "complete_check()" function
9850 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009853{
9854 int saved = RedrawingDisabled;
9855
9856 RedrawingDisabled = 0;
9857 ins_compl_check_keys(0);
9858 rettv->vval.v_number = compl_interrupted;
9859 RedrawingDisabled = saved;
9860}
9861#endif
9862
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863/*
9864 * "confirm(message, buttons[, default [, type]])" function
9865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009867f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868{
9869#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9870 char_u *message;
9871 char_u *buttons = NULL;
9872 char_u buf[NUMBUFLEN];
9873 char_u buf2[NUMBUFLEN];
9874 int def = 1;
9875 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 char_u *typestr;
9877 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 message = get_tv_string_chk(&argvars[0]);
9880 if (message == NULL)
9881 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009882 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9885 if (buttons == NULL)
9886 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009890 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009892 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9893 if (typestr == NULL)
9894 error = TRUE;
9895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009897 switch (TOUPPER_ASC(*typestr))
9898 {
9899 case 'E': type = VIM_ERROR; break;
9900 case 'Q': type = VIM_QUESTION; break;
9901 case 'I': type = VIM_INFO; break;
9902 case 'W': type = VIM_WARNING; break;
9903 case 'G': type = VIM_GENERIC; break;
9904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 }
9906 }
9907 }
9908 }
9909
9910 if (buttons == NULL || *buttons == NUL)
9911 buttons = (char_u *)_("&Ok");
9912
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009913 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009915 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916#endif
9917}
9918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009919/*
9920 * "copy()" function
9921 */
9922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009923f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009924{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009925 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009928#ifdef FEAT_FLOAT
9929/*
9930 * "cos()" function
9931 */
9932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009933f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009934{
9935 float_T f;
9936
9937 rettv->v_type = VAR_FLOAT;
9938 if (get_float_arg(argvars, &f) == OK)
9939 rettv->vval.v_float = cos(f);
9940 else
9941 rettv->vval.v_float = 0.0;
9942}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009943
9944/*
9945 * "cosh()" function
9946 */
9947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009948f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009949{
9950 float_T f;
9951
9952 rettv->v_type = VAR_FLOAT;
9953 if (get_float_arg(argvars, &f) == OK)
9954 rettv->vval.v_float = cosh(f);
9955 else
9956 rettv->vval.v_float = 0.0;
9957}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009958#endif
9959
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009961 * "count()" function
9962 */
9963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009964f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009965{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009966 long n = 0;
9967 int ic = FALSE;
9968
Bram Moolenaare9a41262005-01-15 22:18:47 +00009969 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009970 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009971 listitem_T *li;
9972 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009974
Bram Moolenaare9a41262005-01-15 22:18:47 +00009975 if ((l = argvars[0].vval.v_list) != NULL)
9976 {
9977 li = l->lv_first;
9978 if (argvars[2].v_type != VAR_UNKNOWN)
9979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 int error = FALSE;
9981
9982 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 if (argvars[3].v_type != VAR_UNKNOWN)
9984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009985 idx = get_tv_number_chk(&argvars[3], &error);
9986 if (!error)
9987 {
9988 li = list_find(l, idx);
9989 if (li == NULL)
9990 EMSGN(_(e_listidx), idx);
9991 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009992 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 if (error)
9994 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009995 }
9996
9997 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009998 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 ++n;
10000 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010001 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010002 else if (argvars[0].v_type == VAR_DICT)
10003 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010004 int todo;
10005 dict_T *d;
10006 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010007
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010008 if ((d = argvars[0].vval.v_dict) != NULL)
10009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 int error = FALSE;
10011
Bram Moolenaare9a41262005-01-15 22:18:47 +000010012 if (argvars[2].v_type != VAR_UNKNOWN)
10013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010015 if (argvars[3].v_type != VAR_UNKNOWN)
10016 EMSG(_(e_invarg));
10017 }
10018
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010019 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010020 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010021 {
10022 if (!HASHITEM_EMPTY(hi))
10023 {
10024 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010025 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010026 ++n;
10027 }
10028 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010029 }
10030 }
10031 else
10032 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010033 rettv->vval.v_number = n;
10034}
10035
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010036#ifdef FEAT_CHANNEL
10037/*
10038 * Get a callback from "arg". It can be a Funcref or a function name.
10039 * When "arg" is zero return an empty string.
10040 * Return NULL for an invalid argument.
10041 */
10042 static char_u *
10043get_callback(typval_T *arg)
10044{
10045 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
10046 return arg->vval.v_string;
10047 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
10048 return (char_u *)"";
10049 EMSG(_("E999: Invalid callback argument"));
10050 return NULL;
10051}
10052
10053/*
10054 * "connect()" function
10055 */
10056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010057f_connect(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010058{
10059 char_u *address;
10060 char_u *mode;
10061 char_u *callback = NULL;
10062 char_u buf1[NUMBUFLEN];
10063 char_u *p;
10064 int port;
10065 int json_mode = FALSE;
10066
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010010067 /* default: fail */
10068 rettv->vval.v_number = -1;
10069
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010070 address = get_tv_string(&argvars[0]);
10071 mode = get_tv_string_buf(&argvars[1], buf1);
10072 if (argvars[2].v_type != VAR_UNKNOWN)
10073 {
10074 callback = get_callback(&argvars[2]);
10075 if (callback == NULL)
10076 return;
10077 }
10078
10079 /* parse address */
10080 p = vim_strchr(address, ':');
10081 if (p == NULL)
10082 {
10083 EMSG2(_(e_invarg2), address);
10084 return;
10085 }
10086 *p++ = NUL;
10087 port = atoi((char *)p);
10088 if (*address == NUL || port <= 0)
10089 {
10090 p[-1] = ':';
10091 EMSG2(_(e_invarg2), address);
10092 return;
10093 }
10094
10095 /* parse mode */
10096 if (STRCMP(mode, "json") == 0)
10097 json_mode = TRUE;
10098 else if (STRCMP(mode, "raw") != 0)
10099 {
10100 EMSG2(_(e_invarg2), mode);
10101 return;
10102 }
10103
10104 rettv->vval.v_number = channel_open((char *)address, port, NULL);
10105 if (rettv->vval.v_number >= 0)
10106 {
10107 channel_set_json_mode(rettv->vval.v_number, json_mode);
10108 if (callback != NULL && *callback != NUL)
10109 channel_set_callback(rettv->vval.v_number, callback);
10110 }
10111}
10112#endif
10113
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10116 *
10117 * Checks the existence of a cscope connection.
10118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010120f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121{
10122#ifdef FEAT_CSCOPE
10123 int num = 0;
10124 char_u *dbpath = NULL;
10125 char_u *prepend = NULL;
10126 char_u buf[NUMBUFLEN];
10127
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010128 if (argvars[0].v_type != VAR_UNKNOWN
10129 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 num = (int)get_tv_number(&argvars[0]);
10132 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 }
10136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#endif
10139}
10140
10141/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010142 * "cursor(lnum, col)" function, or
10143 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010145 * Moves the cursor to the specified line and column.
10146 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010149f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010152#ifdef FEAT_VIRTUALEDIT
10153 long coladd = 0;
10154#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010155 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010157 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010158 if (argvars[1].v_type == VAR_UNKNOWN)
10159 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010160 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010161 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010162
Bram Moolenaar493c1782014-05-28 14:34:46 +020010163 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010164 {
10165 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010166 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010167 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010168 line = pos.lnum;
10169 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010170#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010171 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010172#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010173 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010174 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010175 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010176 set_curswant = FALSE;
10177 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010178 }
10179 else
10180 {
10181 line = get_tv_lnum(argvars);
10182 col = get_tv_number_chk(&argvars[1], NULL);
10183#ifdef FEAT_VIRTUALEDIT
10184 if (argvars[2].v_type != VAR_UNKNOWN)
10185 coladd = get_tv_number_chk(&argvars[2], NULL);
10186#endif
10187 }
10188 if (line < 0 || col < 0
10189#ifdef FEAT_VIRTUALEDIT
10190 || coladd < 0
10191#endif
10192 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (line > 0)
10195 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 if (col > 0)
10197 curwin->w_cursor.col = col - 1;
10198#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010199 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200#endif
10201
10202 /* Make sure the cursor is in a valid position. */
10203 check_cursor();
10204#ifdef FEAT_MBYTE
10205 /* Correct cursor for multi-byte character. */
10206 if (has_mbyte)
10207 mb_adjust_cursor();
10208#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010209
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010210 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010211 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212}
10213
10214/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010215 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 */
10217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010218f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010220 int noref = 0;
10221
10222 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010224 if (noref < 0 || noref > 1)
10225 EMSG(_(e_invarg));
10226 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010227 {
10228 current_copyID += COPYID_INC;
10229 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231}
10232
10233/*
10234 * "delete()" function
10235 */
10236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010237f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010239 char_u nbuf[NUMBUFLEN];
10240 char_u *name;
10241 char_u *flags;
10242
10243 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010245 return;
10246
10247 name = get_tv_string(&argvars[0]);
10248 if (name == NULL || *name == NUL)
10249 {
10250 EMSG(_(e_invarg));
10251 return;
10252 }
10253
10254 if (argvars[1].v_type != VAR_UNKNOWN)
10255 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010257 flags = (char_u *)"";
10258
10259 if (*flags == NUL)
10260 /* delete a file */
10261 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10262 else if (STRCMP(flags, "d") == 0)
10263 /* delete an empty directory */
10264 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10265 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010266 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010267 rettv->vval.v_number = delete_recursive(name);
10268 else
10269 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270}
10271
10272/*
10273 * "did_filetype()" function
10274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010276f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277{
10278#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010279 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280#endif
10281}
10282
10283/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010284 * "diff_filler()" function
10285 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010287f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010288{
10289#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010291#endif
10292}
10293
10294/*
10295 * "diff_hlID()" function
10296 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010298f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010299{
10300#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010302 static linenr_T prev_lnum = 0;
10303 static int changedtick = 0;
10304 static int fnum = 0;
10305 static int change_start = 0;
10306 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010307 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010308 int filler_lines;
10309 int col;
10310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 if (lnum < 0) /* ignore type error in {lnum} arg */
10312 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010313 if (lnum != prev_lnum
10314 || changedtick != curbuf->b_changedtick
10315 || fnum != curbuf->b_fnum)
10316 {
10317 /* New line, buffer, change: need to get the values. */
10318 filler_lines = diff_check(curwin, lnum);
10319 if (filler_lines < 0)
10320 {
10321 if (filler_lines == -1)
10322 {
10323 change_start = MAXCOL;
10324 change_end = -1;
10325 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10326 hlID = HLF_ADD; /* added line */
10327 else
10328 hlID = HLF_CHD; /* changed line */
10329 }
10330 else
10331 hlID = HLF_ADD; /* added line */
10332 }
10333 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010334 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010335 prev_lnum = lnum;
10336 changedtick = curbuf->b_changedtick;
10337 fnum = curbuf->b_fnum;
10338 }
10339
10340 if (hlID == HLF_CHD || hlID == HLF_TXD)
10341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010343 if (col >= change_start && col <= change_end)
10344 hlID = HLF_TXD; /* changed text */
10345 else
10346 hlID = HLF_CHD; /* changed line */
10347 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010348 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010349#endif
10350}
10351
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010352#ifdef FEAT_CHANNEL
10353/*
10354 * Get the channel index from the handle argument.
10355 * Returns -1 if the handle is invalid or the channel is closed.
10356 */
10357 static int
10358get_channel_arg(typval_T *tv)
10359{
10360 int ch_idx;
10361
10362 if (tv->v_type != VAR_NUMBER)
10363 {
10364 EMSG2(_(e_invarg2), get_tv_string(tv));
10365 return -1;
10366 }
10367 ch_idx = tv->vval.v_number;
10368
10369 if (!channel_is_open(ch_idx))
10370 {
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010010371 EMSGN(_("E906: not an open channel"), ch_idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010372 return -1;
10373 }
10374 return ch_idx;
10375}
10376
10377/*
10378 * "disconnect()" function
10379 */
10380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010381f_disconnect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010382{
10383 int ch_idx = get_channel_arg(&argvars[0]);
10384
10385 if (ch_idx >= 0)
10386 channel_close(ch_idx);
10387}
10388#endif
10389
Bram Moolenaar47136d72004-10-12 20:02:24 +000010390/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010391 * "empty({expr})" function
10392 */
10393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010394f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010395{
10396 int n;
10397
10398 switch (argvars[0].v_type)
10399 {
10400 case VAR_STRING:
10401 case VAR_FUNC:
10402 n = argvars[0].vval.v_string == NULL
10403 || *argvars[0].vval.v_string == NUL;
10404 break;
10405 case VAR_NUMBER:
10406 n = argvars[0].vval.v_number == 0;
10407 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010408#ifdef FEAT_FLOAT
10409 case VAR_FLOAT:
10410 n = argvars[0].vval.v_float == 0.0;
10411 break;
10412#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010413 case VAR_LIST:
10414 n = argvars[0].vval.v_list == NULL
10415 || argvars[0].vval.v_list->lv_first == NULL;
10416 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 case VAR_DICT:
10418 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010420 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010421 case VAR_SPECIAL:
10422 n = argvars[0].vval.v_number != VVAL_TRUE;
10423 break;
10424
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010425 default:
10426 EMSG2(_(e_intern2), "f_empty()");
10427 n = 0;
10428 }
10429
10430 rettv->vval.v_number = n;
10431}
10432
10433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 * "escape({string}, {chars})" function
10435 */
10436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010437f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438{
10439 char_u buf[NUMBUFLEN];
10440
Bram Moolenaar758711c2005-02-02 23:11:38 +000010441 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10442 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444}
10445
10446/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010447 * "eval()" function
10448 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010450f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010451{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010452 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010454 s = get_tv_string_chk(&argvars[0]);
10455 if (s != NULL)
10456 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010457
Bram Moolenaar615b9972015-01-14 17:15:05 +010010458 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010459 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10460 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010461 if (p != NULL && !aborting())
10462 EMSG2(_(e_invexpr2), p);
10463 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010465 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010467 else if (*s != NUL)
10468 EMSG(_(e_trailing));
10469}
10470
10471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 * "eventhandler()" function
10473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010475f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478}
10479
10480/*
10481 * "executable()" function
10482 */
10483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010484f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010486 char_u *name = get_tv_string(&argvars[0]);
10487
10488 /* Check in $PATH and also check directly if there is a directory name. */
10489 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10490 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010491}
10492
10493/*
10494 * "exepath()" function
10495 */
10496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010497f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010498{
10499 char_u *p = NULL;
10500
Bram Moolenaarb5971142015-03-21 17:32:19 +010010501 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010502 rettv->v_type = VAR_STRING;
10503 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504}
10505
10506/*
10507 * "exists()" function
10508 */
10509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010510f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512 char_u *p;
10513 char_u *name;
10514 int n = FALSE;
10515 int len = 0;
10516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 if (*p == '$') /* environment variable */
10519 {
10520 /* first try "normal" environment variables (fast) */
10521 if (mch_getenv(p + 1) != NULL)
10522 n = TRUE;
10523 else
10524 {
10525 /* try expanding things like $VIM and ${HOME} */
10526 p = expand_env_save(p);
10527 if (p != NULL && *p != '$')
10528 n = TRUE;
10529 vim_free(p);
10530 }
10531 }
10532 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010535 if (*skipwhite(p) != NUL)
10536 n = FALSE; /* trailing garbage */
10537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 else if (*p == '*') /* internal or user defined function */
10539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010540 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 }
10542 else if (*p == ':')
10543 {
10544 n = cmd_exists(p + 1);
10545 }
10546 else if (*p == '#')
10547 {
10548#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010549 if (p[1] == '#')
10550 n = autocmd_supported(p + 2);
10551 else
10552 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553#endif
10554 }
10555 else /* internal variable */
10556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010557 char_u *tofree;
10558 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010560 /* get_name_len() takes care of expanding curly braces */
10561 name = p;
10562 len = get_name_len(&p, &tofree, TRUE, FALSE);
10563 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010565 if (tofree != NULL)
10566 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010567 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010568 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010570 /* handle d.key, l[idx], f(expr) */
10571 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10572 if (n)
10573 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 }
10575 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010576 if (*p != NUL)
10577 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010579 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 }
10581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583}
10584
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010585#ifdef FEAT_FLOAT
10586/*
10587 * "exp()" function
10588 */
10589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010590f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010591{
10592 float_T f;
10593
10594 rettv->v_type = VAR_FLOAT;
10595 if (get_float_arg(argvars, &f) == OK)
10596 rettv->vval.v_float = exp(f);
10597 else
10598 rettv->vval.v_float = 0.0;
10599}
10600#endif
10601
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602/*
10603 * "expand()" function
10604 */
10605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010606f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607{
10608 char_u *s;
10609 int len;
10610 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010611 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010613 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010614 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010617 if (argvars[1].v_type != VAR_UNKNOWN
10618 && argvars[2].v_type != VAR_UNKNOWN
10619 && get_tv_number_chk(&argvars[2], &error)
10620 && !error)
10621 {
10622 rettv->v_type = VAR_LIST;
10623 rettv->vval.v_list = NULL;
10624 }
10625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010626 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 if (*s == '%' || *s == '#' || *s == '<')
10628 {
10629 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010630 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010632 if (rettv->v_type == VAR_LIST)
10633 {
10634 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10635 list_append_string(rettv->vval.v_list, result, -1);
10636 else
10637 vim_free(result);
10638 }
10639 else
10640 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 }
10642 else
10643 {
10644 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010645 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 if (argvars[1].v_type != VAR_UNKNOWN
10647 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010648 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 if (!error)
10650 {
10651 ExpandInit(&xpc);
10652 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010653 if (p_wic)
10654 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010655 if (rettv->v_type == VAR_STRING)
10656 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10657 options, WILD_ALL);
10658 else if (rettv_list_alloc(rettv) != FAIL)
10659 {
10660 int i;
10661
10662 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10663 for (i = 0; i < xpc.xp_numfiles; i++)
10664 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10665 ExpandCleanup(&xpc);
10666 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667 }
10668 else
10669 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 }
10671}
10672
10673/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010674 * Go over all entries in "d2" and add them to "d1".
10675 * When "action" is "error" then a duplicate key is an error.
10676 * When "action" is "force" then a duplicate key is overwritten.
10677 * Otherwise duplicate keys are ignored ("action" is "keep").
10678 */
10679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010680dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010681{
10682 dictitem_T *di1;
10683 hashitem_T *hi2;
10684 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010685 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010686
10687 todo = (int)d2->dv_hashtab.ht_used;
10688 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10689 {
10690 if (!HASHITEM_EMPTY(hi2))
10691 {
10692 --todo;
10693 di1 = dict_find(d1, hi2->hi_key, -1);
10694 if (d1->dv_scope != 0)
10695 {
10696 /* Disallow replacing a builtin function in l: and g:.
10697 * Check the key to be valid when adding to any
10698 * scope. */
10699 if (d1->dv_scope == VAR_DEF_SCOPE
10700 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10701 && var_check_func_name(hi2->hi_key,
10702 di1 == NULL))
10703 break;
10704 if (!valid_varname(hi2->hi_key))
10705 break;
10706 }
10707 if (di1 == NULL)
10708 {
10709 di1 = dictitem_copy(HI2DI(hi2));
10710 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10711 dictitem_free(di1);
10712 }
10713 else if (*action == 'e')
10714 {
10715 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10716 break;
10717 }
10718 else if (*action == 'f' && HI2DI(hi2) != di1)
10719 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010720 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10721 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010722 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010723 clear_tv(&di1->di_tv);
10724 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10725 }
10726 }
10727 }
10728}
10729
10730/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010731 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010732 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010733 */
10734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010735f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010736{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010737 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010738
Bram Moolenaare9a41262005-01-15 22:18:47 +000010739 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 list_T *l1, *l2;
10742 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010743 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010745
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 l1 = argvars[0].vval.v_list;
10747 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010748 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010749 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010750 {
10751 if (argvars[2].v_type != VAR_UNKNOWN)
10752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 before = get_tv_number_chk(&argvars[2], &error);
10754 if (error)
10755 return; /* type error; errmsg already given */
10756
Bram Moolenaar758711c2005-02-02 23:11:38 +000010757 if (before == l1->lv_len)
10758 item = NULL;
10759 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010760 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010761 item = list_find(l1, before);
10762 if (item == NULL)
10763 {
10764 EMSGN(_(e_listidx), before);
10765 return;
10766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010767 }
10768 }
10769 else
10770 item = NULL;
10771 list_extend(l1, l2, item);
10772
Bram Moolenaare9a41262005-01-15 22:18:47 +000010773 copy_tv(&argvars[0], rettv);
10774 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010775 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010776 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10777 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010778 dict_T *d1, *d2;
10779 char_u *action;
10780 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781
10782 d1 = argvars[0].vval.v_dict;
10783 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010784 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010785 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010786 {
10787 /* Check the third argument. */
10788 if (argvars[2].v_type != VAR_UNKNOWN)
10789 {
10790 static char *(av[]) = {"keep", "force", "error"};
10791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 action = get_tv_string_chk(&argvars[2]);
10793 if (action == NULL)
10794 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010795 for (i = 0; i < 3; ++i)
10796 if (STRCMP(action, av[i]) == 0)
10797 break;
10798 if (i == 3)
10799 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010800 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010801 return;
10802 }
10803 }
10804 else
10805 action = (char_u *)"force";
10806
Bram Moolenaara9922d62013-05-30 13:01:18 +020010807 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010808
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 copy_tv(&argvars[0], rettv);
10810 }
10811 }
10812 else
10813 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010814}
10815
10816/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010817 * "feedkeys()" function
10818 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010821{
10822 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010823 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010824 char_u *keys, *flags;
10825 char_u nbuf[NUMBUFLEN];
10826 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010827 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010828 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010829
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010830 /* This is not allowed in the sandbox. If the commands would still be
10831 * executed in the sandbox it would be OK, but it probably happens later,
10832 * when "sandbox" is no longer set. */
10833 if (check_secure())
10834 return;
10835
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010836 keys = get_tv_string(&argvars[0]);
10837 if (*keys != NUL)
10838 {
10839 if (argvars[1].v_type != VAR_UNKNOWN)
10840 {
10841 flags = get_tv_string_buf(&argvars[1], nbuf);
10842 for ( ; *flags != NUL; ++flags)
10843 {
10844 switch (*flags)
10845 {
10846 case 'n': remap = FALSE; break;
10847 case 'm': remap = TRUE; break;
10848 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010849 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010850 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010851 }
10852 }
10853 }
10854
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010855 /* Need to escape K_SPECIAL and CSI before putting the string in the
10856 * typeahead buffer. */
10857 keys_esc = vim_strsave_escape_csi(keys);
10858 if (keys_esc != NULL)
10859 {
10860 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010861 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010862 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010863 if (vgetc_busy)
10864 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010865 if (execute)
10866 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010867 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010868 }
10869}
10870
10871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 * "filereadable()" function
10873 */
10874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010875f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010877 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 char_u *p;
10879 int n;
10880
Bram Moolenaarc236c162008-07-13 17:41:49 +000010881#ifndef O_NONBLOCK
10882# define O_NONBLOCK 0
10883#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010885 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10886 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 {
10888 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010889 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 }
10891 else
10892 n = FALSE;
10893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895}
10896
10897/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010898 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 * rights to write into.
10900 */
10901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010902f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010904 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905}
10906
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010907 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010010908findfilendir(
10909 typval_T *argvars UNUSED,
10910 typval_T *rettv,
10911 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010912{
10913#ifdef FEAT_SEARCHPATH
10914 char_u *fname;
10915 char_u *fresult = NULL;
10916 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10917 char_u *p;
10918 char_u pathbuf[NUMBUFLEN];
10919 int count = 1;
10920 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010921 int error = FALSE;
10922#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010923
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010924 rettv->vval.v_string = NULL;
10925 rettv->v_type = VAR_STRING;
10926
10927#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010929
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010930 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010931 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10933 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010934 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010935 else
10936 {
10937 if (*p != NUL)
10938 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010941 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010943 }
10944
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010945 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10946 error = TRUE;
10947
10948 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 do
10951 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010952 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010953 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 fresult = find_file_in_path_option(first ? fname : NULL,
10955 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010956 0, first, path,
10957 find_what,
10958 curbuf->b_ffname,
10959 find_what == FINDFILE_DIR
10960 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010962
10963 if (fresult != NULL && rettv->v_type == VAR_LIST)
10964 list_append_string(rettv->vval.v_list, fresult, -1);
10965
10966 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010968
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010969 if (rettv->v_type == VAR_STRING)
10970 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010971#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010972}
10973
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010974static void filter_map(typval_T *argvars, typval_T *rettv, int map);
10975static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010976
10977/*
10978 * Implementation of map() and filter().
10979 */
10980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010981filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010982{
10983 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 listitem_T *li, *nli;
10986 list_T *l = NULL;
10987 dictitem_T *di;
10988 hashtab_T *ht;
10989 hashitem_T *hi;
10990 dict_T *d = NULL;
10991 typval_T save_val;
10992 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010993 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010994 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010995 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010996 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010997 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010998 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010999 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011000
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011002 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011003 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011004 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011005 return;
11006 }
11007 else if (argvars[0].v_type == VAR_DICT)
11008 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011009 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011010 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011011 return;
11012 }
11013 else
11014 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011015 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 return;
11017 }
11018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011019 expr = get_tv_string_buf_chk(&argvars[1], buf);
11020 /* On type errors, the preceding call has already displayed an error
11021 * message. Avoid a misleading error message for an empty string that
11022 * was not passed as argument. */
11023 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011025 prepare_vimvar(VV_VAL, &save_val);
11026 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011027
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011028 /* We reset "did_emsg" to be able to detect whether an error
11029 * occurred during evaluation of the expression. */
11030 save_did_emsg = did_emsg;
11031 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011032
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011033 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 vimvars[VV_KEY].vv_type = VAR_STRING;
11037
11038 ht = &d->dv_hashtab;
11039 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011040 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 if (!HASHITEM_EMPTY(hi))
11044 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011045 int r;
11046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 --todo;
11048 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011049 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011050 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11051 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 break;
11053 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011054 r = filter_map_one(&di->di_tv, expr, map, &rem);
11055 clear_tv(&vimvars[VV_KEY].vv_tv);
11056 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011057 break;
11058 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011059 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011060 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11061 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011062 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 }
11066 }
11067 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068 }
11069 else
11070 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011071 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 for (li = l->lv_first; li != NULL; li = nli)
11074 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011075 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011076 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011078 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011079 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011080 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011081 break;
11082 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011084 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011085 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011086 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011087
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011088 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011090
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011091 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011092 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093
11094 copy_tv(&argvars[0], rettv);
11095}
11096
11097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011098filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099{
Bram Moolenaar33570922005-01-25 22:26:29 +000011100 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011102 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011105 s = expr;
11106 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011107 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011108 if (*s != NUL) /* check for trailing chars after expr */
11109 {
11110 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011111 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011112 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 }
11114 if (map)
11115 {
11116 /* map(): replace the list item value */
11117 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011118 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 *tv = rettv;
11120 }
11121 else
11122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 int error = FALSE;
11124
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 /* On type error, nothing has been removed; return FAIL to stop the
11129 * loop. The error message was given by get_tv_number_chk(). */
11130 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011131 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011133 retval = OK;
11134theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011136 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011137}
11138
11139/*
11140 * "filter()" function
11141 */
11142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011143f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011144{
11145 filter_map(argvars, rettv, FALSE);
11146}
11147
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149 * "finddir({fname}[, {path}[, {count}]])" function
11150 */
11151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011152f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011154 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155}
11156
11157/*
11158 * "findfile({fname}[, {path}[, {count}]])" function
11159 */
11160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011161f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011163 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164}
11165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011166#ifdef FEAT_FLOAT
11167/*
11168 * "float2nr({float})" function
11169 */
11170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011171f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011172{
11173 float_T f;
11174
11175 if (get_float_arg(argvars, &f) == OK)
11176 {
11177 if (f < -0x7fffffff)
11178 rettv->vval.v_number = -0x7fffffff;
11179 else if (f > 0x7fffffff)
11180 rettv->vval.v_number = 0x7fffffff;
11181 else
11182 rettv->vval.v_number = (varnumber_T)f;
11183 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011184}
11185
11186/*
11187 * "floor({float})" function
11188 */
11189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011190f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011191{
11192 float_T f;
11193
11194 rettv->v_type = VAR_FLOAT;
11195 if (get_float_arg(argvars, &f) == OK)
11196 rettv->vval.v_float = floor(f);
11197 else
11198 rettv->vval.v_float = 0.0;
11199}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011200
11201/*
11202 * "fmod()" function
11203 */
11204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011205f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011206{
11207 float_T fx, fy;
11208
11209 rettv->v_type = VAR_FLOAT;
11210 if (get_float_arg(argvars, &fx) == OK
11211 && get_float_arg(&argvars[1], &fy) == OK)
11212 rettv->vval.v_float = fmod(fx, fy);
11213 else
11214 rettv->vval.v_float = 0.0;
11215}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011216#endif
11217
Bram Moolenaar0d660222005-01-07 21:51:51 +000011218/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011219 * "fnameescape({string})" function
11220 */
11221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011222f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011223{
11224 rettv->vval.v_string = vim_strsave_fnameescape(
11225 get_tv_string(&argvars[0]), FALSE);
11226 rettv->v_type = VAR_STRING;
11227}
11228
11229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 * "fnamemodify({fname}, {mods})" function
11231 */
11232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011233f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234{
11235 char_u *fname;
11236 char_u *mods;
11237 int usedlen = 0;
11238 int len;
11239 char_u *fbuf = NULL;
11240 char_u buf[NUMBUFLEN];
11241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 fname = get_tv_string_chk(&argvars[0]);
11243 mods = get_tv_string_buf_chk(&argvars[1], buf);
11244 if (fname == NULL || mods == NULL)
11245 fname = NULL;
11246 else
11247 {
11248 len = (int)STRLEN(fname);
11249 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 vim_free(fbuf);
11258}
11259
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011260static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261
11262/*
11263 * "foldclosed()" function
11264 */
11265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011266foldclosed_both(
11267 typval_T *argvars UNUSED,
11268 typval_T *rettv,
11269 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270{
11271#ifdef FEAT_FOLDING
11272 linenr_T lnum;
11273 linenr_T first, last;
11274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11277 {
11278 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11279 {
11280 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 return;
11285 }
11286 }
11287#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289}
11290
11291/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011292 * "foldclosed()" function
11293 */
11294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011295f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296{
11297 foldclosed_both(argvars, rettv, FALSE);
11298}
11299
11300/*
11301 * "foldclosedend()" function
11302 */
11303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011304f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305{
11306 foldclosed_both(argvars, rettv, TRUE);
11307}
11308
11309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 * "foldlevel()" function
11311 */
11312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011313f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314{
11315#ifdef FEAT_FOLDING
11316 linenr_T lnum;
11317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322}
11323
11324/*
11325 * "foldtext()" function
11326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011328f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
11330#ifdef FEAT_FOLDING
11331 linenr_T lnum;
11332 char_u *s;
11333 char_u *r;
11334 int len;
11335 char *txt;
11336#endif
11337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->v_type = VAR_STRING;
11339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011341 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11342 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11343 <= curbuf->b_ml.ml_line_count
11344 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 {
11346 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011347 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11348 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 {
11350 if (!linewhite(lnum))
11351 break;
11352 ++lnum;
11353 }
11354
11355 /* Find interesting text in this line. */
11356 s = skipwhite(ml_get(lnum));
11357 /* skip C comment-start */
11358 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011361 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011363 {
11364 s = skipwhite(ml_get(lnum + 1));
11365 if (*s == '*')
11366 s = skipwhite(s + 1);
11367 }
11368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 txt = _("+-%s%3ld lines: ");
11370 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 + 20 /* for %3ld */
11373 + STRLEN(s))); /* concatenated */
11374 if (r != NULL)
11375 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011376 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11377 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11378 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 len = (int)STRLEN(r);
11380 STRCAT(r, s);
11381 /* remove 'foldmarker' and 'commentstring' */
11382 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 }
11385 }
11386#endif
11387}
11388
11389/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011390 * "foldtextresult(lnum)" function
11391 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011393f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011394{
11395#ifdef FEAT_FOLDING
11396 linenr_T lnum;
11397 char_u *text;
11398 char_u buf[51];
11399 foldinfo_T foldinfo;
11400 int fold_count;
11401#endif
11402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->v_type = VAR_STRING;
11404 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011405#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 /* treat illegal types and illegal string values for {lnum} the same */
11408 if (lnum < 0)
11409 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011410 fold_count = foldedCount(curwin, lnum, &foldinfo);
11411 if (fold_count > 0)
11412 {
11413 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11414 &foldinfo, buf);
11415 if (text == buf)
11416 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011418 }
11419#endif
11420}
11421
11422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 * "foreground()" function
11424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011426f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428#ifdef FEAT_GUI
11429 if (gui.in_use)
11430 gui_mch_set_foreground();
11431#else
11432# ifdef WIN32
11433 win32_set_foreground();
11434# endif
11435#endif
11436}
11437
11438/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011439 * "function()" function
11440 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011443{
11444 char_u *s;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011447 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011448 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011449 /* Don't check an autoload name for existence here. */
11450 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011451 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011452 else
11453 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011454 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011455 {
11456 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011457 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011458
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011459 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11460 * also be called from another script. Using trans_function_name()
11461 * would also work, but some plugins depend on the name being
11462 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011463 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011464 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011465 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011466 if (rettv->vval.v_string != NULL)
11467 {
11468 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011469 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011470 }
11471 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011472 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011473 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 }
11476}
11477
11478/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011479 * "garbagecollect()" function
11480 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011482f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011483{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011484 /* This is postponed until we are back at the toplevel, because we may be
11485 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11486 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011487
11488 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11489 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011490}
11491
11492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493 * "get()" function
11494 */
11495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011496f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497{
Bram Moolenaar33570922005-01-25 22:26:29 +000011498 listitem_T *li;
11499 list_T *l;
11500 dictitem_T *di;
11501 dict_T *d;
11502 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 int error = FALSE;
11509
11510 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11511 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011515 else if (argvars[0].v_type == VAR_DICT)
11516 {
11517 if ((d = argvars[0].vval.v_dict) != NULL)
11518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011519 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 if (di != NULL)
11521 tv = &di->di_tv;
11522 }
11523 }
11524 else
11525 EMSG2(_(e_listdictarg), "get()");
11526
11527 if (tv == NULL)
11528 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011530 copy_tv(&argvars[2], rettv);
11531 }
11532 else
11533 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011534}
11535
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011536static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011537
11538/*
11539 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011540 * Return a range (from start to end) of lines in rettv from the specified
11541 * buffer.
11542 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011543 */
11544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011545get_buffer_lines(
11546 buf_T *buf,
11547 linenr_T start,
11548 linenr_T end,
11549 int retlist,
11550 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011551{
11552 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011553
Bram Moolenaar959a1432013-12-14 12:17:38 +010011554 rettv->v_type = VAR_STRING;
11555 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011556 if (retlist && rettv_list_alloc(rettv) == FAIL)
11557 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011558
11559 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11560 return;
11561
11562 if (!retlist)
11563 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011564 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11565 p = ml_get_buf(buf, start, FALSE);
11566 else
11567 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011568 rettv->vval.v_string = vim_strsave(p);
11569 }
11570 else
11571 {
11572 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011573 return;
11574
11575 if (start < 1)
11576 start = 1;
11577 if (end > buf->b_ml.ml_line_count)
11578 end = buf->b_ml.ml_line_count;
11579 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011580 if (list_append_string(rettv->vval.v_list,
11581 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011582 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011583 }
11584}
11585
11586/*
11587 * "getbufline()" function
11588 */
11589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011590f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011591{
11592 linenr_T lnum;
11593 linenr_T end;
11594 buf_T *buf;
11595
11596 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11597 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011598 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011599 --emsg_off;
11600
Bram Moolenaar661b1822005-07-28 22:36:45 +000011601 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011602 if (argvars[2].v_type == VAR_UNKNOWN)
11603 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011604 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011605 end = get_tv_lnum_buf(&argvars[2], buf);
11606
Bram Moolenaar342337a2005-07-21 21:11:17 +000011607 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011608}
11609
Bram Moolenaar0d660222005-01-07 21:51:51 +000011610/*
11611 * "getbufvar()" function
11612 */
11613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011614f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011615{
11616 buf_T *buf;
11617 buf_T *save_curbuf;
11618 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011619 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011620 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11623 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011625 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011626
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011627 rettv->v_type = VAR_STRING;
11628 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629
11630 if (buf != NULL && varname != NULL)
11631 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011632 /* set curbuf to be our buf, temporarily */
11633 save_curbuf = curbuf;
11634 curbuf = buf;
11635
Bram Moolenaar0d660222005-01-07 21:51:51 +000011636 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011637 {
11638 if (get_option_tv(&varname, rettv, TRUE) == OK)
11639 done = TRUE;
11640 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011641 else if (STRCMP(varname, "changedtick") == 0)
11642 {
11643 rettv->v_type = VAR_NUMBER;
11644 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011645 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011646 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647 else
11648 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011649 /* Look up the variable. */
11650 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11651 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11652 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011653 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011654 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011656 done = TRUE;
11657 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011658 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011659
11660 /* restore previous notion of curbuf */
11661 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 }
11663
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011664 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11665 /* use the default value */
11666 copy_tv(&argvars[2], rettv);
11667
Bram Moolenaar0d660222005-01-07 21:51:51 +000011668 --emsg_off;
11669}
11670
11671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 * "getchar()" function
11673 */
11674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011675f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676{
11677 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011680 /* Position the cursor. Needed after a message that ends in a space. */
11681 windgoto(msg_row, msg_col);
11682
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 ++no_mapping;
11684 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011685 for (;;)
11686 {
11687 if (argvars[0].v_type == VAR_UNKNOWN)
11688 /* getchar(): blocking wait. */
11689 n = safe_vgetc();
11690 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11691 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011692 n = vpeekc_any();
11693 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011694 /* illegal argument or getchar(0) and no char avail: return zero */
11695 n = 0;
11696 else
11697 /* getchar(0) and char avail: return char */
11698 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011699
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011700 if (n == K_IGNORE)
11701 continue;
11702 break;
11703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 --no_mapping;
11705 --allow_keys;
11706
Bram Moolenaar219b8702006-11-01 14:32:36 +000011707 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11708 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11709 vimvars[VV_MOUSE_COL].vv_nr = 0;
11710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 if (IS_SPECIAL(n) || mod_mask != 0)
11713 {
11714 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11715 int i = 0;
11716
11717 /* Turn a special key into three bytes, plus modifier. */
11718 if (mod_mask != 0)
11719 {
11720 temp[i++] = K_SPECIAL;
11721 temp[i++] = KS_MODIFIER;
11722 temp[i++] = mod_mask;
11723 }
11724 if (IS_SPECIAL(n))
11725 {
11726 temp[i++] = K_SPECIAL;
11727 temp[i++] = K_SECOND(n);
11728 temp[i++] = K_THIRD(n);
11729 }
11730#ifdef FEAT_MBYTE
11731 else if (has_mbyte)
11732 i += (*mb_char2bytes)(n, temp + i);
11733#endif
11734 else
11735 temp[i++] = n;
11736 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->v_type = VAR_STRING;
11738 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011739
11740#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011741 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011742 {
11743 int row = mouse_row;
11744 int col = mouse_col;
11745 win_T *win;
11746 linenr_T lnum;
11747# ifdef FEAT_WINDOWS
11748 win_T *wp;
11749# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011750 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011751
11752 if (row >= 0 && col >= 0)
11753 {
11754 /* Find the window at the mouse coordinates and compute the
11755 * text position. */
11756 win = mouse_find_win(&row, &col);
11757 (void)mouse_comp_pos(win, &row, &col, &lnum);
11758# ifdef FEAT_WINDOWS
11759 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011760 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011761# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011762 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011763 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11764 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11765 }
11766 }
11767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 }
11769}
11770
11771/*
11772 * "getcharmod()" function
11773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011775f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778}
11779
11780/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011781 * "getcharsearch()" function
11782 */
11783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011784f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011785{
11786 if (rettv_dict_alloc(rettv) != FAIL)
11787 {
11788 dict_T *dict = rettv->vval.v_dict;
11789
11790 dict_add_nr_str(dict, "char", 0L, last_csearch());
11791 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11792 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11793 }
11794}
11795
11796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 * "getcmdline()" function
11798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011800f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->v_type = VAR_STRING;
11803 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
11807 * "getcmdpos()" function
11808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011810f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813}
11814
11815/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011816 * "getcmdtype()" function
11817 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011819f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011820{
11821 rettv->v_type = VAR_STRING;
11822 rettv->vval.v_string = alloc(2);
11823 if (rettv->vval.v_string != NULL)
11824 {
11825 rettv->vval.v_string[0] = get_cmdline_type();
11826 rettv->vval.v_string[1] = NUL;
11827 }
11828}
11829
11830/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011831 * "getcmdwintype()" function
11832 */
11833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011834f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011835{
11836 rettv->v_type = VAR_STRING;
11837 rettv->vval.v_string = NULL;
11838#ifdef FEAT_CMDWIN
11839 rettv->vval.v_string = alloc(2);
11840 if (rettv->vval.v_string != NULL)
11841 {
11842 rettv->vval.v_string[0] = cmdwin_type;
11843 rettv->vval.v_string[1] = NUL;
11844 }
11845#endif
11846}
11847
11848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 * "getcwd()" function
11850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011852f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011854 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011855 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011858 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011859
11860 wp = find_tabwin(&argvars[0], &argvars[1]);
11861 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011863 if (wp->w_localdir != NULL)
11864 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11865 else if(globaldir != NULL)
11866 rettv->vval.v_string = vim_strsave(globaldir);
11867 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011868 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011869 cwd = alloc(MAXPATHL);
11870 if (cwd != NULL)
11871 {
11872 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11873 rettv->vval.v_string = vim_strsave(cwd);
11874 vim_free(cwd);
11875 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011876 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011877#ifdef BACKSLASH_IN_FILENAME
11878 if (rettv->vval.v_string != NULL)
11879 slash_adjust(rettv->vval.v_string);
11880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 }
11882}
11883
11884/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011885 * "getfontname()" function
11886 */
11887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011888f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->v_type = VAR_STRING;
11891 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011892#ifdef FEAT_GUI
11893 if (gui.in_use)
11894 {
11895 GuiFont font;
11896 char_u *name = NULL;
11897
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011898 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011899 {
11900 /* Get the "Normal" font. Either the name saved by
11901 * hl_set_font_name() or from the font ID. */
11902 font = gui.norm_font;
11903 name = hl_get_font_name();
11904 }
11905 else
11906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011908 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11909 return;
11910 font = gui_mch_get_font(name, FALSE);
11911 if (font == NOFONT)
11912 return; /* Invalid font name, return empty string. */
11913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011915 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011916 gui_mch_free_font(font);
11917 }
11918#endif
11919}
11920
11921/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011922 * "getfperm({fname})" function
11923 */
11924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011925f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011926{
11927 char_u *fname;
11928 struct stat st;
11929 char_u *perm = NULL;
11930 char_u flags[] = "rwx";
11931 int i;
11932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011936 if (mch_stat((char *)fname, &st) >= 0)
11937 {
11938 perm = vim_strsave((char_u *)"---------");
11939 if (perm != NULL)
11940 {
11941 for (i = 0; i < 9; i++)
11942 {
11943 if (st.st_mode & (1 << (8 - i)))
11944 perm[i] = flags[i % 3];
11945 }
11946 }
11947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011949}
11950
11951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 * "getfsize({fname})" function
11953 */
11954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011955f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956{
11957 char_u *fname;
11958 struct stat st;
11959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011962 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
11964 if (mch_stat((char *)fname, &st) >= 0)
11965 {
11966 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011971
11972 /* non-perfect check for overflow */
11973 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11974 rettv->vval.v_number = -2;
11975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 }
11977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979}
11980
11981/*
11982 * "getftime({fname})" function
11983 */
11984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011985f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986{
11987 char_u *fname;
11988 struct stat st;
11989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991
11992 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996}
11997
11998/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011999 * "getftype({fname})" function
12000 */
12001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012002f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012003{
12004 char_u *fname;
12005 struct stat st;
12006 char_u *type = NULL;
12007 char *t;
12008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012012 if (mch_lstat((char *)fname, &st) >= 0)
12013 {
12014#ifdef S_ISREG
12015 if (S_ISREG(st.st_mode))
12016 t = "file";
12017 else if (S_ISDIR(st.st_mode))
12018 t = "dir";
12019# ifdef S_ISLNK
12020 else if (S_ISLNK(st.st_mode))
12021 t = "link";
12022# endif
12023# ifdef S_ISBLK
12024 else if (S_ISBLK(st.st_mode))
12025 t = "bdev";
12026# endif
12027# ifdef S_ISCHR
12028 else if (S_ISCHR(st.st_mode))
12029 t = "cdev";
12030# endif
12031# ifdef S_ISFIFO
12032 else if (S_ISFIFO(st.st_mode))
12033 t = "fifo";
12034# endif
12035# ifdef S_ISSOCK
12036 else if (S_ISSOCK(st.st_mode))
12037 t = "fifo";
12038# endif
12039 else
12040 t = "other";
12041#else
12042# ifdef S_IFMT
12043 switch (st.st_mode & S_IFMT)
12044 {
12045 case S_IFREG: t = "file"; break;
12046 case S_IFDIR: t = "dir"; break;
12047# ifdef S_IFLNK
12048 case S_IFLNK: t = "link"; break;
12049# endif
12050# ifdef S_IFBLK
12051 case S_IFBLK: t = "bdev"; break;
12052# endif
12053# ifdef S_IFCHR
12054 case S_IFCHR: t = "cdev"; break;
12055# endif
12056# ifdef S_IFIFO
12057 case S_IFIFO: t = "fifo"; break;
12058# endif
12059# ifdef S_IFSOCK
12060 case S_IFSOCK: t = "socket"; break;
12061# endif
12062 default: t = "other";
12063 }
12064# else
12065 if (mch_isdir(fname))
12066 t = "dir";
12067 else
12068 t = "file";
12069# endif
12070#endif
12071 type = vim_strsave((char_u *)t);
12072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012074}
12075
12076/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012077 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012078 */
12079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012080f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012081{
12082 linenr_T lnum;
12083 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012084 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085
12086 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012088 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012089 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012090 retlist = FALSE;
12091 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012093 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012094 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012095 retlist = TRUE;
12096 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012097
Bram Moolenaar342337a2005-07-21 21:11:17 +000012098 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099}
12100
12101/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012102 * "getmatches()" function
12103 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012105f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012106{
12107#ifdef FEAT_SEARCH_EXTRA
12108 dict_T *dict;
12109 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012110 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012111
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012112 if (rettv_list_alloc(rettv) == OK)
12113 {
12114 while (cur != NULL)
12115 {
12116 dict = dict_alloc();
12117 if (dict == NULL)
12118 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012119 if (cur->match.regprog == NULL)
12120 {
12121 /* match added with matchaddpos() */
12122 for (i = 0; i < MAXPOSMATCH; ++i)
12123 {
12124 llpos_T *llpos;
12125 char buf[6];
12126 list_T *l;
12127
12128 llpos = &cur->pos.pos[i];
12129 if (llpos->lnum == 0)
12130 break;
12131 l = list_alloc();
12132 if (l == NULL)
12133 break;
12134 list_append_number(l, (varnumber_T)llpos->lnum);
12135 if (llpos->col > 0)
12136 {
12137 list_append_number(l, (varnumber_T)llpos->col);
12138 list_append_number(l, (varnumber_T)llpos->len);
12139 }
12140 sprintf(buf, "pos%d", i + 1);
12141 dict_add_list(dict, buf, l);
12142 }
12143 }
12144 else
12145 {
12146 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12147 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012148 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012149 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12150 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012151# ifdef FEAT_CONCEAL
12152 if (cur->conceal_char)
12153 {
12154 char_u buf[MB_MAXBYTES + 1];
12155
12156 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12157 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12158 }
12159# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012160 list_append_dict(rettv->vval.v_list, dict);
12161 cur = cur->next;
12162 }
12163 }
12164#endif
12165}
12166
12167/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012168 * "getpid()" function
12169 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012172{
12173 rettv->vval.v_number = mch_get_pid();
12174}
12175
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012176static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012177
12178/*
12179 * "getcurpos()" function
12180 */
12181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012182f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012183{
12184 getpos_both(argvars, rettv, TRUE);
12185}
12186
Bram Moolenaar18081e32008-02-20 19:11:07 +000012187/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012188 * "getpos(string)" function
12189 */
12190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012191f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012192{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012193 getpos_both(argvars, rettv, FALSE);
12194}
12195
12196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012197getpos_both(
12198 typval_T *argvars,
12199 typval_T *rettv,
12200 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012201{
Bram Moolenaara5525202006-03-02 22:52:09 +000012202 pos_T *fp;
12203 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012204 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012205
12206 if (rettv_list_alloc(rettv) == OK)
12207 {
12208 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012209 if (getcurpos)
12210 fp = &curwin->w_cursor;
12211 else
12212 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012213 if (fnum != -1)
12214 list_append_number(l, (varnumber_T)fnum);
12215 else
12216 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012217 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12218 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012219 list_append_number(l, (fp != NULL)
12220 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012221 : (varnumber_T)0);
12222 list_append_number(l,
12223#ifdef FEAT_VIRTUALEDIT
12224 (fp != NULL) ? (varnumber_T)fp->coladd :
12225#endif
12226 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012227 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012228 list_append_number(l, curwin->w_curswant == MAXCOL ?
12229 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012230 }
12231 else
12232 rettv->vval.v_number = FALSE;
12233}
12234
12235/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012236 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012237 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012239f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012240{
12241#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012242 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012243#endif
12244
Bram Moolenaar2641f772005-03-25 21:58:17 +000012245#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012246 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012247 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012248 wp = NULL;
12249 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12250 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012251 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012252 if (wp == NULL)
12253 return;
12254 }
12255
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012256 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012257 }
12258#endif
12259}
12260
12261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 * "getreg()" function
12263 */
12264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266{
12267 char_u *strregname;
12268 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012269 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012270 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012273 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 strregname = get_tv_string_chk(&argvars[0]);
12276 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012277 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012280 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12281 return_list = get_tv_number_chk(&argvars[2], &error);
12282 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012285 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012286
12287 if (error)
12288 return;
12289
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 regname = (strregname == NULL ? '"' : *strregname);
12291 if (regname == 0)
12292 regname = '"';
12293
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012294 if (return_list)
12295 {
12296 rettv->v_type = VAR_LIST;
12297 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12298 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012299 if (rettv->vval.v_list != NULL)
12300 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012301 }
12302 else
12303 {
12304 rettv->v_type = VAR_STRING;
12305 rettv->vval.v_string = get_reg_contents(regname,
12306 arg2 ? GREG_EXPR_SRC : 0);
12307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308}
12309
12310/*
12311 * "getregtype()" function
12312 */
12313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012314f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315{
12316 char_u *strregname;
12317 int regname;
12318 char_u buf[NUMBUFLEN + 2];
12319 long reglen = 0;
12320
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012321 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012322 {
12323 strregname = get_tv_string_chk(&argvars[0]);
12324 if (strregname == NULL) /* type error; errmsg already given */
12325 {
12326 rettv->v_type = VAR_STRING;
12327 rettv->vval.v_string = NULL;
12328 return;
12329 }
12330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 else
12332 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012333 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334
12335 regname = (strregname == NULL ? '"' : *strregname);
12336 if (regname == 0)
12337 regname = '"';
12338
12339 buf[0] = NUL;
12340 buf[1] = NUL;
12341 switch (get_reg_type(regname, &reglen))
12342 {
12343 case MLINE: buf[0] = 'V'; break;
12344 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 case MBLOCK:
12346 buf[0] = Ctrl_V;
12347 sprintf((char *)buf + 1, "%ld", reglen + 1);
12348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350 rettv->v_type = VAR_STRING;
12351 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352}
12353
12354/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012355 * "gettabvar()" function
12356 */
12357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012358f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012359{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012360 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012361 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012362 dictitem_T *v;
12363 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012364 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012365
12366 rettv->v_type = VAR_STRING;
12367 rettv->vval.v_string = NULL;
12368
12369 varname = get_tv_string_chk(&argvars[1]);
12370 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12371 if (tp != NULL && varname != NULL)
12372 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012373 /* Set tp to be our tabpage, temporarily. Also set the window to the
12374 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012375 if (switch_win(&oldcurwin, &oldtabpage,
12376 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012377 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012378 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012379 /* look up the variable */
12380 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12381 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12382 if (v != NULL)
12383 {
12384 copy_tv(&v->di_tv, rettv);
12385 done = TRUE;
12386 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012387 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012388
12389 /* restore previous notion of curwin */
12390 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012391 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012392
12393 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012394 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012395}
12396
12397/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012398 * "gettabwinvar()" function
12399 */
12400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012401f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012402{
12403 getwinvar(argvars, rettv, 1);
12404}
12405
12406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 * "getwinposx()" function
12408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012410f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012412 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413#ifdef FEAT_GUI
12414 if (gui.in_use)
12415 {
12416 int x, y;
12417
12418 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 }
12421#endif
12422}
12423
12424/*
12425 * "getwinposy()" function
12426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012428f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431#ifdef FEAT_GUI
12432 if (gui.in_use)
12433 {
12434 int x, y;
12435
12436 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 }
12439#endif
12440}
12441
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012442/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012443 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012444 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012445 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012446find_win_by_nr(
12447 typval_T *vp,
12448 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012449{
12450#ifdef FEAT_WINDOWS
12451 win_T *wp;
12452#endif
12453 int nr;
12454
12455 nr = get_tv_number_chk(vp, NULL);
12456
12457#ifdef FEAT_WINDOWS
12458 if (nr < 0)
12459 return NULL;
12460 if (nr == 0)
12461 return curwin;
12462
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012463 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12464 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012465 if (--nr <= 0)
12466 break;
12467 return wp;
12468#else
12469 if (nr == 0 || nr == 1)
12470 return curwin;
12471 return NULL;
12472#endif
12473}
12474
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012476 * Find window specified by "wvp" in tabpage "tvp".
12477 */
12478 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012479find_tabwin(
12480 typval_T *wvp, /* VAR_UNKNOWN for current window */
12481 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012482{
12483 win_T *wp = NULL;
12484 tabpage_T *tp = NULL;
12485 long n;
12486
12487 if (wvp->v_type != VAR_UNKNOWN)
12488 {
12489 if (tvp->v_type != VAR_UNKNOWN)
12490 {
12491 n = get_tv_number(tvp);
12492 if (n >= 0)
12493 tp = find_tabpage(n);
12494 }
12495 else
12496 tp = curtab;
12497
12498 if (tp != NULL)
12499 wp = find_win_by_nr(wvp, tp);
12500 }
12501 else
12502 wp = curwin;
12503
12504 return wp;
12505}
12506
12507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 * "getwinvar()" function
12509 */
12510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012511f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012513 getwinvar(argvars, rettv, 0);
12514}
12515
12516/*
12517 * getwinvar() and gettabwinvar()
12518 */
12519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012520getwinvar(
12521 typval_T *argvars,
12522 typval_T *rettv,
12523 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012524{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012525 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012528 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012529 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012530#ifdef FEAT_WINDOWS
12531 win_T *oldcurwin;
12532 tabpage_T *oldtabpage;
12533 int need_switch_win;
12534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012536#ifdef FEAT_WINDOWS
12537 if (off == 1)
12538 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12539 else
12540 tp = curtab;
12541#endif
12542 win = find_win_by_nr(&argvars[off], tp);
12543 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012546 rettv->v_type = VAR_STRING;
12547 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548
12549 if (win != NULL && varname != NULL)
12550 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012551#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012552 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012553 * otherwise the window is not valid. Only do this when needed,
12554 * autocommands get blocked. */
12555 need_switch_win = !(tp == curtab && win == curwin);
12556 if (!need_switch_win
12557 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12558#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012559 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012560 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012561 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012562 if (get_option_tv(&varname, rettv, 1) == OK)
12563 done = TRUE;
12564 }
12565 else
12566 {
12567 /* Look up the variable. */
12568 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12569 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12570 varname, FALSE);
12571 if (v != NULL)
12572 {
12573 copy_tv(&v->di_tv, rettv);
12574 done = TRUE;
12575 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012578
Bram Moolenaarba117c22015-09-29 16:53:22 +020012579#ifdef FEAT_WINDOWS
12580 if (need_switch_win)
12581 /* restore previous notion of curwin */
12582 restore_win(oldcurwin, oldtabpage, TRUE);
12583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 }
12585
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012586 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12587 /* use the default return value */
12588 copy_tv(&argvars[off + 2], rettv);
12589
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 --emsg_off;
12591}
12592
12593/*
12594 * "glob()" function
12595 */
12596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012597f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012599 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012601 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012603 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012604 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012606 if (argvars[1].v_type != VAR_UNKNOWN)
12607 {
12608 if (get_tv_number_chk(&argvars[1], &error))
12609 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012611 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012612 if (get_tv_number_chk(&argvars[2], &error))
12613 {
12614 rettv->v_type = VAR_LIST;
12615 rettv->vval.v_list = NULL;
12616 }
12617 if (argvars[3].v_type != VAR_UNKNOWN
12618 && get_tv_number_chk(&argvars[3], &error))
12619 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012620 }
12621 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012622 if (!error)
12623 {
12624 ExpandInit(&xpc);
12625 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012626 if (p_wic)
12627 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012628 if (rettv->v_type == VAR_STRING)
12629 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012630 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012631 else if (rettv_list_alloc(rettv) != FAIL)
12632 {
12633 int i;
12634
12635 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12636 NULL, options, WILD_ALL_KEEP);
12637 for (i = 0; i < xpc.xp_numfiles; i++)
12638 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12639
12640 ExpandCleanup(&xpc);
12641 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012642 }
12643 else
12644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645}
12646
12647/*
12648 * "globpath()" function
12649 */
12650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012651f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012653 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012655 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012656 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012657 garray_T ga;
12658 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012660 /* When the optional second argument is non-zero, don't remove matches
12661 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012663 if (argvars[2].v_type != VAR_UNKNOWN)
12664 {
12665 if (get_tv_number_chk(&argvars[2], &error))
12666 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012667 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012668 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012669 if (get_tv_number_chk(&argvars[3], &error))
12670 {
12671 rettv->v_type = VAR_LIST;
12672 rettv->vval.v_list = NULL;
12673 }
12674 if (argvars[4].v_type != VAR_UNKNOWN
12675 && get_tv_number_chk(&argvars[4], &error))
12676 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012677 }
12678 }
12679 if (file != NULL && !error)
12680 {
12681 ga_init2(&ga, (int)sizeof(char_u *), 10);
12682 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12683 if (rettv->v_type == VAR_STRING)
12684 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12685 else if (rettv_list_alloc(rettv) != FAIL)
12686 for (i = 0; i < ga.ga_len; ++i)
12687 list_append_string(rettv->vval.v_list,
12688 ((char_u **)(ga.ga_data))[i], -1);
12689 ga_clear_strings(&ga);
12690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012691 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012692 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693}
12694
12695/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012696 * "glob2regpat()" function
12697 */
12698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012699f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012700{
12701 char_u *pat = get_tv_string_chk(&argvars[0]);
12702
12703 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012704 rettv->vval.v_string = (pat == NULL)
12705 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012706}
12707
12708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 * "has()" function
12710 */
12711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
12714 int i;
12715 char_u *name;
12716 int n = FALSE;
12717 static char *(has_list[]) =
12718 {
12719#ifdef AMIGA
12720 "amiga",
12721# ifdef FEAT_ARP
12722 "arp",
12723# endif
12724#endif
12725#ifdef __BEOS__
12726 "beos",
12727#endif
12728#ifdef MSDOS
12729# ifdef DJGPP
12730 "dos32",
12731# else
12732 "dos16",
12733# endif
12734#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012735#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 "mac",
12737#endif
12738#if defined(MACOS_X_UNIX)
12739 "macunix",
12740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741#ifdef __QNX__
12742 "qnx",
12743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744#ifdef UNIX
12745 "unix",
12746#endif
12747#ifdef VMS
12748 "vms",
12749#endif
12750#ifdef WIN16
12751 "win16",
12752#endif
12753#ifdef WIN32
12754 "win32",
12755#endif
12756#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12757 "win32unix",
12758#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012759#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 "win64",
12761#endif
12762#ifdef EBCDIC
12763 "ebcdic",
12764#endif
12765#ifndef CASE_INSENSITIVE_FILENAME
12766 "fname_case",
12767#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012768#ifdef HAVE_ACL
12769 "acl",
12770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771#ifdef FEAT_ARABIC
12772 "arabic",
12773#endif
12774#ifdef FEAT_AUTOCMD
12775 "autocmd",
12776#endif
12777#ifdef FEAT_BEVAL
12778 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012779# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12780 "balloon_multiline",
12781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782#endif
12783#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12784 "builtin_terms",
12785# ifdef ALL_BUILTIN_TCAPS
12786 "all_builtin_terms",
12787# endif
12788#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012789#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12790 || defined(FEAT_GUI_W32) \
12791 || defined(FEAT_GUI_MOTIF))
12792 "browsefilter",
12793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794#ifdef FEAT_BYTEOFF
12795 "byte_offset",
12796#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012797#ifdef FEAT_CHANNEL
12798 "channel",
12799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800#ifdef FEAT_CINDENT
12801 "cindent",
12802#endif
12803#ifdef FEAT_CLIENTSERVER
12804 "clientserver",
12805#endif
12806#ifdef FEAT_CLIPBOARD
12807 "clipboard",
12808#endif
12809#ifdef FEAT_CMDL_COMPL
12810 "cmdline_compl",
12811#endif
12812#ifdef FEAT_CMDHIST
12813 "cmdline_hist",
12814#endif
12815#ifdef FEAT_COMMENTS
12816 "comments",
12817#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012818#ifdef FEAT_CONCEAL
12819 "conceal",
12820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821#ifdef FEAT_CRYPT
12822 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012823 "crypt-blowfish",
12824 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825#endif
12826#ifdef FEAT_CSCOPE
12827 "cscope",
12828#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012829#ifdef FEAT_CURSORBIND
12830 "cursorbind",
12831#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012832#ifdef CURSOR_SHAPE
12833 "cursorshape",
12834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835#ifdef DEBUG
12836 "debug",
12837#endif
12838#ifdef FEAT_CON_DIALOG
12839 "dialog_con",
12840#endif
12841#ifdef FEAT_GUI_DIALOG
12842 "dialog_gui",
12843#endif
12844#ifdef FEAT_DIFF
12845 "diff",
12846#endif
12847#ifdef FEAT_DIGRAPHS
12848 "digraphs",
12849#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012850#ifdef FEAT_DIRECTX
12851 "directx",
12852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853#ifdef FEAT_DND
12854 "dnd",
12855#endif
12856#ifdef FEAT_EMACS_TAGS
12857 "emacs_tags",
12858#endif
12859 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012860 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861#ifdef FEAT_SEARCH_EXTRA
12862 "extra_search",
12863#endif
12864#ifdef FEAT_FKMAP
12865 "farsi",
12866#endif
12867#ifdef FEAT_SEARCHPATH
12868 "file_in_path",
12869#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012870#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012871 "filterpipe",
12872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873#ifdef FEAT_FIND_ID
12874 "find_in_path",
12875#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012876#ifdef FEAT_FLOAT
12877 "float",
12878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879#ifdef FEAT_FOLDING
12880 "folding",
12881#endif
12882#ifdef FEAT_FOOTER
12883 "footer",
12884#endif
12885#if !defined(USE_SYSTEM) && defined(UNIX)
12886 "fork",
12887#endif
12888#ifdef FEAT_GETTEXT
12889 "gettext",
12890#endif
12891#ifdef FEAT_GUI
12892 "gui",
12893#endif
12894#ifdef FEAT_GUI_ATHENA
12895# ifdef FEAT_GUI_NEXTAW
12896 "gui_neXtaw",
12897# else
12898 "gui_athena",
12899# endif
12900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901#ifdef FEAT_GUI_GTK
12902 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012905#ifdef FEAT_GUI_GNOME
12906 "gui_gnome",
12907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908#ifdef FEAT_GUI_MAC
12909 "gui_mac",
12910#endif
12911#ifdef FEAT_GUI_MOTIF
12912 "gui_motif",
12913#endif
12914#ifdef FEAT_GUI_PHOTON
12915 "gui_photon",
12916#endif
12917#ifdef FEAT_GUI_W16
12918 "gui_win16",
12919#endif
12920#ifdef FEAT_GUI_W32
12921 "gui_win32",
12922#endif
12923#ifdef FEAT_HANGULIN
12924 "hangul_input",
12925#endif
12926#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12927 "iconv",
12928#endif
12929#ifdef FEAT_INS_EXPAND
12930 "insert_expand",
12931#endif
12932#ifdef FEAT_JUMPLIST
12933 "jumplist",
12934#endif
12935#ifdef FEAT_KEYMAP
12936 "keymap",
12937#endif
12938#ifdef FEAT_LANGMAP
12939 "langmap",
12940#endif
12941#ifdef FEAT_LIBCALL
12942 "libcall",
12943#endif
12944#ifdef FEAT_LINEBREAK
12945 "linebreak",
12946#endif
12947#ifdef FEAT_LISP
12948 "lispindent",
12949#endif
12950#ifdef FEAT_LISTCMDS
12951 "listcmds",
12952#endif
12953#ifdef FEAT_LOCALMAP
12954 "localmap",
12955#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012956#ifdef FEAT_LUA
12957# ifndef DYNAMIC_LUA
12958 "lua",
12959# endif
12960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961#ifdef FEAT_MENU
12962 "menu",
12963#endif
12964#ifdef FEAT_SESSION
12965 "mksession",
12966#endif
12967#ifdef FEAT_MODIFY_FNAME
12968 "modify_fname",
12969#endif
12970#ifdef FEAT_MOUSE
12971 "mouse",
12972#endif
12973#ifdef FEAT_MOUSESHAPE
12974 "mouseshape",
12975#endif
12976#if defined(UNIX) || defined(VMS)
12977# ifdef FEAT_MOUSE_DEC
12978 "mouse_dec",
12979# endif
12980# ifdef FEAT_MOUSE_GPM
12981 "mouse_gpm",
12982# endif
12983# ifdef FEAT_MOUSE_JSB
12984 "mouse_jsbterm",
12985# endif
12986# ifdef FEAT_MOUSE_NET
12987 "mouse_netterm",
12988# endif
12989# ifdef FEAT_MOUSE_PTERM
12990 "mouse_pterm",
12991# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012992# ifdef FEAT_MOUSE_SGR
12993 "mouse_sgr",
12994# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012995# ifdef FEAT_SYSMOUSE
12996 "mouse_sysmouse",
12997# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012998# ifdef FEAT_MOUSE_URXVT
12999 "mouse_urxvt",
13000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001# ifdef FEAT_MOUSE_XTERM
13002 "mouse_xterm",
13003# endif
13004#endif
13005#ifdef FEAT_MBYTE
13006 "multi_byte",
13007#endif
13008#ifdef FEAT_MBYTE_IME
13009 "multi_byte_ime",
13010#endif
13011#ifdef FEAT_MULTI_LANG
13012 "multi_lang",
13013#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013014#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013015#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013016 "mzscheme",
13017#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019#ifdef FEAT_OLE
13020 "ole",
13021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022#ifdef FEAT_PATH_EXTRA
13023 "path_extra",
13024#endif
13025#ifdef FEAT_PERL
13026#ifndef DYNAMIC_PERL
13027 "perl",
13028#endif
13029#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013030#ifdef FEAT_PERSISTENT_UNDO
13031 "persistent_undo",
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef FEAT_PYTHON
13034#ifndef DYNAMIC_PYTHON
13035 "python",
13036#endif
13037#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013038#ifdef FEAT_PYTHON3
13039#ifndef DYNAMIC_PYTHON3
13040 "python3",
13041#endif
13042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043#ifdef FEAT_POSTSCRIPT
13044 "postscript",
13045#endif
13046#ifdef FEAT_PRINTER
13047 "printer",
13048#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013049#ifdef FEAT_PROFILE
13050 "profile",
13051#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013052#ifdef FEAT_RELTIME
13053 "reltime",
13054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#ifdef FEAT_QUICKFIX
13056 "quickfix",
13057#endif
13058#ifdef FEAT_RIGHTLEFT
13059 "rightleft",
13060#endif
13061#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13062 "ruby",
13063#endif
13064#ifdef FEAT_SCROLLBIND
13065 "scrollbind",
13066#endif
13067#ifdef FEAT_CMDL_INFO
13068 "showcmd",
13069 "cmdline_info",
13070#endif
13071#ifdef FEAT_SIGNS
13072 "signs",
13073#endif
13074#ifdef FEAT_SMARTINDENT
13075 "smartindent",
13076#endif
13077#ifdef FEAT_SNIFF
13078 "sniff",
13079#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013080#ifdef STARTUPTIME
13081 "startuptime",
13082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083#ifdef FEAT_STL_OPT
13084 "statusline",
13085#endif
13086#ifdef FEAT_SUN_WORKSHOP
13087 "sun_workshop",
13088#endif
13089#ifdef FEAT_NETBEANS_INTG
13090 "netbeans_intg",
13091#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013092#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013093 "spell",
13094#endif
13095#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 "syntax",
13097#endif
13098#if defined(USE_SYSTEM) || !defined(UNIX)
13099 "system",
13100#endif
13101#ifdef FEAT_TAG_BINS
13102 "tag_binary",
13103#endif
13104#ifdef FEAT_TAG_OLDSTATIC
13105 "tag_old_static",
13106#endif
13107#ifdef FEAT_TAG_ANYWHITE
13108 "tag_any_white",
13109#endif
13110#ifdef FEAT_TCL
13111# ifndef DYNAMIC_TCL
13112 "tcl",
13113# endif
13114#endif
13115#ifdef TERMINFO
13116 "terminfo",
13117#endif
13118#ifdef FEAT_TERMRESPONSE
13119 "termresponse",
13120#endif
13121#ifdef FEAT_TEXTOBJ
13122 "textobjects",
13123#endif
13124#ifdef HAVE_TGETENT
13125 "tgetent",
13126#endif
13127#ifdef FEAT_TITLE
13128 "title",
13129#endif
13130#ifdef FEAT_TOOLBAR
13131 "toolbar",
13132#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013133#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13134 "unnamedplus",
13135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136#ifdef FEAT_USR_CMDS
13137 "user-commands", /* was accidentally included in 5.4 */
13138 "user_commands",
13139#endif
13140#ifdef FEAT_VIMINFO
13141 "viminfo",
13142#endif
13143#ifdef FEAT_VERTSPLIT
13144 "vertsplit",
13145#endif
13146#ifdef FEAT_VIRTUALEDIT
13147 "virtualedit",
13148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef FEAT_VISUALEXTRA
13151 "visualextra",
13152#endif
13153#ifdef FEAT_VREPLACE
13154 "vreplace",
13155#endif
13156#ifdef FEAT_WILDIGN
13157 "wildignore",
13158#endif
13159#ifdef FEAT_WILDMENU
13160 "wildmenu",
13161#endif
13162#ifdef FEAT_WINDOWS
13163 "windows",
13164#endif
13165#ifdef FEAT_WAK
13166 "winaltkeys",
13167#endif
13168#ifdef FEAT_WRITEBACKUP
13169 "writebackup",
13170#endif
13171#ifdef FEAT_XIM
13172 "xim",
13173#endif
13174#ifdef FEAT_XFONTSET
13175 "xfontset",
13176#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013177#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013178 "xpm",
13179 "xpm_w32", /* for backward compatibility */
13180#else
13181# if defined(HAVE_XPM)
13182 "xpm",
13183# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185#ifdef USE_XSMP
13186 "xsmp",
13187#endif
13188#ifdef USE_XSMP_INTERACT
13189 "xsmp_interact",
13190#endif
13191#ifdef FEAT_XCLIPBOARD
13192 "xterm_clipboard",
13193#endif
13194#ifdef FEAT_XTERM_SAVE
13195 "xterm_save",
13196#endif
13197#if defined(UNIX) && defined(FEAT_X11)
13198 "X11",
13199#endif
13200 NULL
13201 };
13202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 for (i = 0; has_list[i] != NULL; ++i)
13205 if (STRICMP(name, has_list[i]) == 0)
13206 {
13207 n = TRUE;
13208 break;
13209 }
13210
13211 if (n == FALSE)
13212 {
13213 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013214 {
13215 if (name[5] == '-'
13216 && STRLEN(name) > 11
13217 && vim_isdigit(name[6])
13218 && vim_isdigit(name[8])
13219 && vim_isdigit(name[10]))
13220 {
13221 int major = atoi((char *)name + 6);
13222 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013223
13224 /* Expect "patch-9.9.01234". */
13225 n = (major < VIM_VERSION_MAJOR
13226 || (major == VIM_VERSION_MAJOR
13227 && (minor < VIM_VERSION_MINOR
13228 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013229 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013230 }
13231 else
13232 n = has_patch(atoi((char *)name + 5));
13233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 else if (STRICMP(name, "vim_starting") == 0)
13235 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013236#ifdef FEAT_MBYTE
13237 else if (STRICMP(name, "multi_byte_encoding") == 0)
13238 n = has_mbyte;
13239#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013240#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13241 else if (STRICMP(name, "balloon_multiline") == 0)
13242 n = multiline_balloon_available();
13243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244#ifdef DYNAMIC_TCL
13245 else if (STRICMP(name, "tcl") == 0)
13246 n = tcl_enabled(FALSE);
13247#endif
13248#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13249 else if (STRICMP(name, "iconv") == 0)
13250 n = iconv_enabled(FALSE);
13251#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013252#ifdef DYNAMIC_LUA
13253 else if (STRICMP(name, "lua") == 0)
13254 n = lua_enabled(FALSE);
13255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013256#ifdef DYNAMIC_MZSCHEME
13257 else if (STRICMP(name, "mzscheme") == 0)
13258 n = mzscheme_enabled(FALSE);
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef DYNAMIC_RUBY
13261 else if (STRICMP(name, "ruby") == 0)
13262 n = ruby_enabled(FALSE);
13263#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013264#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265#ifdef DYNAMIC_PYTHON
13266 else if (STRICMP(name, "python") == 0)
13267 n = python_enabled(FALSE);
13268#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013269#endif
13270#ifdef FEAT_PYTHON3
13271#ifdef DYNAMIC_PYTHON3
13272 else if (STRICMP(name, "python3") == 0)
13273 n = python3_enabled(FALSE);
13274#endif
13275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276#ifdef DYNAMIC_PERL
13277 else if (STRICMP(name, "perl") == 0)
13278 n = perl_enabled(FALSE);
13279#endif
13280#ifdef FEAT_GUI
13281 else if (STRICMP(name, "gui_running") == 0)
13282 n = (gui.in_use || gui.starting);
13283# ifdef FEAT_GUI_W32
13284 else if (STRICMP(name, "gui_win32s") == 0)
13285 n = gui_is_win32s();
13286# endif
13287# ifdef FEAT_BROWSE
13288 else if (STRICMP(name, "browse") == 0)
13289 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13290# endif
13291#endif
13292#ifdef FEAT_SYN_HL
13293 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013294 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295#endif
13296#if defined(WIN3264)
13297 else if (STRICMP(name, "win95") == 0)
13298 n = mch_windows95();
13299#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013300#ifdef FEAT_NETBEANS_INTG
13301 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013302 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 }
13305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307}
13308
13309/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013310 * "has_key()" function
13311 */
13312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013313f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013314{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013315 if (argvars[0].v_type != VAR_DICT)
13316 {
13317 EMSG(_(e_dictreq));
13318 return;
13319 }
13320 if (argvars[0].vval.v_dict == NULL)
13321 return;
13322
13323 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013324 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013325}
13326
13327/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013328 * "haslocaldir()" function
13329 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013331f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013332{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013333 win_T *wp = NULL;
13334
13335 wp = find_tabwin(&argvars[0], &argvars[1]);
13336 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013337}
13338
13339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 * "hasmapto()" function
13341 */
13342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013343f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344{
13345 char_u *name;
13346 char_u *mode;
13347 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013348 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013351 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 mode = (char_u *)"nvo";
13353 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013356 if (argvars[2].v_type != VAR_UNKNOWN)
13357 abbr = get_tv_number(&argvars[2]);
13358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359
Bram Moolenaar2c932302006-03-18 21:42:09 +000013360 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
13366/*
13367 * "histadd()" function
13368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013370f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371{
13372#ifdef FEAT_CMDHIST
13373 int histype;
13374 char_u *str;
13375 char_u buf[NUMBUFLEN];
13376#endif
13377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 if (check_restricted() || check_secure())
13380 return;
13381#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13383 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 if (histype >= 0)
13385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 if (*str != NUL)
13388 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013389 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 return;
13393 }
13394 }
13395#endif
13396}
13397
13398/*
13399 * "histdel()" function
13400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013402f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403{
13404#ifdef FEAT_CMDHIST
13405 int n;
13406 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13410 if (str == NULL)
13411 n = 0;
13412 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013415 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 else
13420 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 get_tv_string_buf(&argvars[1], buf));
13423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424#endif
13425}
13426
13427/*
13428 * "histget()" function
13429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013431f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432{
13433#ifdef FEAT_CMDHIST
13434 int type;
13435 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13439 if (str == NULL)
13440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 {
13443 type = get_histtype(str);
13444 if (argvars[1].v_type == VAR_UNKNOWN)
13445 idx = get_history_idx(type);
13446 else
13447 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13448 /* -1 on type error */
13449 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455}
13456
13457/*
13458 * "histnr()" function
13459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013461f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462{
13463 int i;
13464
13465#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 char_u *history = get_tv_string_chk(&argvars[0]);
13467
13468 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 if (i >= HIST_CMD && i < HIST_COUNT)
13470 i = get_history_idx(i);
13471 else
13472#endif
13473 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475}
13476
13477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 * "highlightID(name)" function
13479 */
13480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013481f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484}
13485
13486/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013487 * "highlight_exists()" function
13488 */
13489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013490f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491{
13492 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13493}
13494
13495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 * "hostname()" function
13497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013499f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500{
13501 char_u hostname[256];
13502
13503 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->v_type = VAR_STRING;
13505 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506}
13507
13508/*
13509 * iconv() function
13510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013512f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513{
13514#ifdef FEAT_MBYTE
13515 char_u buf1[NUMBUFLEN];
13516 char_u buf2[NUMBUFLEN];
13517 char_u *from, *to, *str;
13518 vimconv_T vimconv;
13519#endif
13520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->v_type = VAR_STRING;
13522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523
13524#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525 str = get_tv_string(&argvars[0]);
13526 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13527 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 vimconv.vc_type = CONV_NONE;
13529 convert_setup(&vimconv, from, to);
13530
13531 /* If the encodings are equal, no conversion needed. */
13532 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
13537 convert_setup(&vimconv, NULL, NULL);
13538 vim_free(from);
13539 vim_free(to);
13540#endif
13541}
13542
13543/*
13544 * "indent()" function
13545 */
13546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013547f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548{
13549 linenr_T lnum;
13550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556}
13557
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013558/*
13559 * "index()" function
13560 */
13561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013562f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013563{
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 list_T *l;
13565 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013566 long idx = 0;
13567 int ic = FALSE;
13568
13569 rettv->vval.v_number = -1;
13570 if (argvars[0].v_type != VAR_LIST)
13571 {
13572 EMSG(_(e_listreq));
13573 return;
13574 }
13575 l = argvars[0].vval.v_list;
13576 if (l != NULL)
13577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013578 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013579 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 int error = FALSE;
13582
Bram Moolenaar758711c2005-02-02 23:11:38 +000013583 /* Start at specified item. Use the cached index that list_find()
13584 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013586 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 ic = get_tv_number_chk(&argvars[3], &error);
13589 if (error)
13590 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013591 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013592
Bram Moolenaar758711c2005-02-02 23:11:38 +000013593 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013594 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013595 {
13596 rettv->vval.v_number = idx;
13597 break;
13598 }
13599 }
13600}
13601
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602static int inputsecret_flag = 0;
13603
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013604static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013605
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013607 * This function is used by f_input() and f_inputdialog() functions. The third
13608 * argument to f_input() specifies the type of completion to use at the
13609 * prompt. The third argument to f_inputdialog() specifies the value to return
13610 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 */
13612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013613get_user_input(
13614 typval_T *argvars,
13615 typval_T *rettv,
13616 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 char_u *p = NULL;
13620 int c;
13621 char_u buf[NUMBUFLEN];
13622 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013624 int xp_type = EXPAND_NOTHING;
13625 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
13630#ifdef NO_CONSOLE_INPUT
13631 /* While starting up, there is no place to enter text. */
13632 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634#endif
13635
13636 cmd_silent = FALSE; /* Want to see the prompt. */
13637 if (prompt != NULL)
13638 {
13639 /* Only the part of the message after the last NL is considered as
13640 * prompt for the command line */
13641 p = vim_strrchr(prompt, '\n');
13642 if (p == NULL)
13643 p = prompt;
13644 else
13645 {
13646 ++p;
13647 c = *p;
13648 *p = NUL;
13649 msg_start();
13650 msg_clr_eos();
13651 msg_puts_attr(prompt, echo_attr);
13652 msg_didout = FALSE;
13653 msg_starthere();
13654 *p = c;
13655 }
13656 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 if (argvars[1].v_type != VAR_UNKNOWN)
13659 {
13660 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13661 if (defstr != NULL)
13662 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013664 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013665 {
13666 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013667 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013668 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013669
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013670 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013671 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013672
Bram Moolenaar4463f292005-09-25 22:20:24 +000013673 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13674 if (xp_name == NULL)
13675 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013676
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013677 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013678
Bram Moolenaar4463f292005-09-25 22:20:24 +000013679 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13680 &xp_arg) == FAIL)
13681 return;
13682 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013683 }
13684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013686 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013687 int save_ex_normal_busy = ex_normal_busy;
13688 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013690 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13691 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013692 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013693 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013694 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013695 && argvars[1].v_type != VAR_UNKNOWN
13696 && argvars[2].v_type != VAR_UNKNOWN)
13697 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13698 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013699
13700 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 /* since the user typed this, no need to wait for return */
13703 need_wait_return = FALSE;
13704 msg_didout = FALSE;
13705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 cmd_silent = cmd_silent_save;
13707}
13708
13709/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013710 * "input()" function
13711 * Also handles inputsecret() when inputsecret is set.
13712 */
13713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013714f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013715{
13716 get_user_input(argvars, rettv, FALSE);
13717}
13718
13719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 * "inputdialog()" function
13721 */
13722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013723f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724{
13725#if defined(FEAT_GUI_TEXTDIALOG)
13726 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13727 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13728 {
13729 char_u *message;
13730 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013731 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013733 message = get_tv_string_chk(&argvars[0]);
13734 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013735 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013736 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 else
13738 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013739 if (message != NULL && defstr != NULL
13740 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013741 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 else
13744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013745 if (message != NULL && defstr != NULL
13746 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013747 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_string = vim_strsave(
13749 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 }
13755 else
13756#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013757 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758}
13759
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013760/*
13761 * "inputlist()" function
13762 */
13763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013764f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013765{
13766 listitem_T *li;
13767 int selected;
13768 int mouse_used;
13769
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013770#ifdef NO_CONSOLE_INPUT
13771 /* While starting up, there is no place to enter text. */
13772 if (no_console_input())
13773 return;
13774#endif
13775 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13776 {
13777 EMSG2(_(e_listarg), "inputlist()");
13778 return;
13779 }
13780
13781 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013782 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013783 lines_left = Rows; /* avoid more prompt */
13784 msg_scroll = TRUE;
13785 msg_clr_eos();
13786
13787 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13788 {
13789 msg_puts(get_tv_string(&li->li_tv));
13790 msg_putchar('\n');
13791 }
13792
13793 /* Ask for choice. */
13794 selected = prompt_for_number(&mouse_used);
13795 if (mouse_used)
13796 selected -= lines_left;
13797
13798 rettv->vval.v_number = selected;
13799}
13800
13801
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13803
13804/*
13805 * "inputrestore()" function
13806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013808f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
13810 if (ga_userinput.ga_len > 0)
13811 {
13812 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13814 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013815 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 }
13817 else if (p_verbose > 1)
13818 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013819 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 }
13822}
13823
13824/*
13825 * "inputsave()" function
13826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013828f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013830 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 if (ga_grow(&ga_userinput, 1) == OK)
13832 {
13833 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13834 + ga_userinput.ga_len);
13835 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013836 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 }
13838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013839 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840}
13841
13842/*
13843 * "inputsecret()" function
13844 */
13845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013846f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847{
13848 ++cmdline_star;
13849 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013850 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 --cmdline_star;
13852 --inputsecret_flag;
13853}
13854
13855/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013856 * "insert()" function
13857 */
13858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013859f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860{
13861 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013862 listitem_T *item;
13863 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013865
13866 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013867 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013868 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013869 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013870 {
13871 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013872 before = get_tv_number_chk(&argvars[2], &error);
13873 if (error)
13874 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013875
Bram Moolenaar758711c2005-02-02 23:11:38 +000013876 if (before == l->lv_len)
13877 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013878 else
13879 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013880 item = list_find(l, before);
13881 if (item == NULL)
13882 {
13883 EMSGN(_(e_listidx), before);
13884 l = NULL;
13885 }
13886 }
13887 if (l != NULL)
13888 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013889 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013890 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013891 }
13892 }
13893}
13894
13895/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013896 * "invert(expr)" function
13897 */
13898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013899f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013900{
13901 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13902}
13903
13904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 * "isdirectory()" function
13906 */
13907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013908f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911}
13912
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013913/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013914 * "islocked()" function
13915 */
13916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013917f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013918{
13919 lval_T lv;
13920 char_u *end;
13921 dictitem_T *di;
13922
13923 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013924 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13925 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013926 if (end != NULL && lv.ll_name != NULL)
13927 {
13928 if (*end != NUL)
13929 EMSG(_(e_trailing));
13930 else
13931 {
13932 if (lv.ll_tv == NULL)
13933 {
13934 if (check_changedtick(lv.ll_name))
13935 rettv->vval.v_number = 1; /* always locked */
13936 else
13937 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013938 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013939 if (di != NULL)
13940 {
13941 /* Consider a variable locked when:
13942 * 1. the variable itself is locked
13943 * 2. the value of the variable is locked.
13944 * 3. the List or Dict value is locked.
13945 */
13946 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13947 || tv_islocked(&di->di_tv));
13948 }
13949 }
13950 }
13951 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013952 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013953 else if (lv.ll_newkey != NULL)
13954 EMSG2(_(e_dictkey), lv.ll_newkey);
13955 else if (lv.ll_list != NULL)
13956 /* List item. */
13957 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13958 else
13959 /* Dictionary item. */
13960 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13961 }
13962 }
13963
13964 clear_lval(&lv);
13965}
13966
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013967static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013968
13969/*
13970 * Turn a dict into a list:
13971 * "what" == 0: list of keys
13972 * "what" == 1: list of values
13973 * "what" == 2: list of items
13974 */
13975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013976dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013977{
Bram Moolenaar33570922005-01-25 22:26:29 +000013978 list_T *l2;
13979 dictitem_T *di;
13980 hashitem_T *hi;
13981 listitem_T *li;
13982 listitem_T *li2;
13983 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013984 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013985
Bram Moolenaar8c711452005-01-14 21:53:12 +000013986 if (argvars[0].v_type != VAR_DICT)
13987 {
13988 EMSG(_(e_dictreq));
13989 return;
13990 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013991 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013992 return;
13993
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013994 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013995 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013996
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013997 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013998 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014000 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014001 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014002 --todo;
14003 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014004
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014005 li = listitem_alloc();
14006 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014007 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014008 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014009
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014010 if (what == 0)
14011 {
14012 /* keys() */
14013 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014014 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014015 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14016 }
14017 else if (what == 1)
14018 {
14019 /* values() */
14020 copy_tv(&di->di_tv, &li->li_tv);
14021 }
14022 else
14023 {
14024 /* items() */
14025 l2 = list_alloc();
14026 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014027 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014028 li->li_tv.vval.v_list = l2;
14029 if (l2 == NULL)
14030 break;
14031 ++l2->lv_refcount;
14032
14033 li2 = listitem_alloc();
14034 if (li2 == NULL)
14035 break;
14036 list_append(l2, li2);
14037 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014038 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014039 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14040
14041 li2 = listitem_alloc();
14042 if (li2 == NULL)
14043 break;
14044 list_append(l2, li2);
14045 copy_tv(&di->di_tv, &li2->li_tv);
14046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014047 }
14048 }
14049}
14050
14051/*
14052 * "items(dict)" function
14053 */
14054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014056{
14057 dict_list(argvars, rettv, 2);
14058}
14059
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014061 * "join()" function
14062 */
14063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014064f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014065{
14066 garray_T ga;
14067 char_u *sep;
14068
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014069 if (argvars[0].v_type != VAR_LIST)
14070 {
14071 EMSG(_(e_listreq));
14072 return;
14073 }
14074 if (argvars[0].vval.v_list == NULL)
14075 return;
14076 if (argvars[1].v_type == VAR_UNKNOWN)
14077 sep = (char_u *)" ";
14078 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014080
14081 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082
14083 if (sep != NULL)
14084 {
14085 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014086 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 ga_append(&ga, NUL);
14088 rettv->vval.v_string = (char_u *)ga.ga_data;
14089 }
14090 else
14091 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014092}
14093
14094/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014095 * "jsondecode()" function
14096 */
14097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014098f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014099{
14100 js_read_T reader;
14101
14102 reader.js_buf = get_tv_string(&argvars[0]);
14103 reader.js_eof = TRUE;
14104 reader.js_used = 0;
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014105 if (json_decode(&reader, rettv) == FAIL)
14106 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014107}
14108
14109/*
14110 * "jsonencode()" function
14111 */
14112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014113f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014114{
14115 rettv->v_type = VAR_STRING;
14116 rettv->vval.v_string = json_encode(&argvars[0]);
14117}
14118
14119/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014120 * "keys()" function
14121 */
14122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014123f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014124{
14125 dict_list(argvars, rettv, 0);
14126}
14127
14128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 * "last_buffer_nr()" function.
14130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014132f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133{
14134 int n = 0;
14135 buf_T *buf;
14136
14137 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14138 if (n < buf->b_fnum)
14139 n = buf->b_fnum;
14140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014141 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014142}
14143
14144/*
14145 * "len()" function
14146 */
14147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014148f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014149{
14150 switch (argvars[0].v_type)
14151 {
14152 case VAR_STRING:
14153 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 rettv->vval.v_number = (varnumber_T)STRLEN(
14155 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014156 break;
14157 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014158 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014159 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014160 case VAR_DICT:
14161 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14162 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014163 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014164 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014165 break;
14166 }
14167}
14168
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014169static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014170
14171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014172libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014173{
14174#ifdef FEAT_LIBCALL
14175 char_u *string_in;
14176 char_u **string_result;
14177 int nr_result;
14178#endif
14179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014180 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014181 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014183
14184 if (check_restricted() || check_secure())
14185 return;
14186
14187#ifdef FEAT_LIBCALL
14188 /* The first two args must be strings, otherwise its meaningless */
14189 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14190 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014191 string_in = NULL;
14192 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014193 string_in = argvars[2].vval.v_string;
14194 if (type == VAR_NUMBER)
14195 string_result = NULL;
14196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014197 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014198 if (mch_libcall(argvars[0].vval.v_string,
14199 argvars[1].vval.v_string,
14200 string_in,
14201 argvars[2].vval.v_number,
14202 string_result,
14203 &nr_result) == OK
14204 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014205 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014206 }
14207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208}
14209
14210/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014211 * "libcall()" function
14212 */
14213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014214f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014215{
14216 libcall_common(argvars, rettv, VAR_STRING);
14217}
14218
14219/*
14220 * "libcallnr()" function
14221 */
14222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014223f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224{
14225 libcall_common(argvars, rettv, VAR_NUMBER);
14226}
14227
14228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 * "line(string)" function
14230 */
14231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014232f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233{
14234 linenr_T lnum = 0;
14235 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014236 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014238 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 if (fp != NULL)
14240 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242}
14243
14244/*
14245 * "line2byte(lnum)" function
14246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014248f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249{
14250#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252#else
14253 linenr_T lnum;
14254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014257 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014259 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14260 if (rettv->vval.v_number >= 0)
14261 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262#endif
14263}
14264
14265/*
14266 * "lispindent(lnum)" function
14267 */
14268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014269f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270{
14271#ifdef FEAT_LISP
14272 pos_T pos;
14273 linenr_T lnum;
14274
14275 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14278 {
14279 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014280 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281 curwin->w_cursor = pos;
14282 }
14283 else
14284#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286}
14287
14288/*
14289 * "localtime()" function
14290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014292f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014294 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295}
14296
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014297static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298
14299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014300get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301{
14302 char_u *keys;
14303 char_u *which;
14304 char_u buf[NUMBUFLEN];
14305 char_u *keys_buf = NULL;
14306 char_u *rhs;
14307 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014308 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014309 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014310 mapblock_T *mp;
14311 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312
14313 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->v_type = VAR_STRING;
14315 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 if (*keys == NUL)
14319 return;
14320
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014321 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014323 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014324 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014325 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014326 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014327 if (argvars[3].v_type != VAR_UNKNOWN)
14328 get_dict = get_tv_number(&argvars[3]);
14329 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 else
14332 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014333 if (which == NULL)
14334 return;
14335
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 mode = get_map_mode(&which, 0);
14337
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014338 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014339 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014341
14342 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014344 /* Return a string. */
14345 if (rhs != NULL)
14346 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347
Bram Moolenaarbd743252010-10-20 21:23:33 +020014348 }
14349 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14350 {
14351 /* Return a dictionary. */
14352 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14353 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14354 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355
Bram Moolenaarbd743252010-10-20 21:23:33 +020014356 dict_add_nr_str(dict, "lhs", 0L, lhs);
14357 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14358 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14359 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14360 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14361 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14362 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014363 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014364 dict_add_nr_str(dict, "mode", 0L, mapmode);
14365
14366 vim_free(lhs);
14367 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 }
14369}
14370
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014371#ifdef FEAT_FLOAT
14372/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014373 * "log()" function
14374 */
14375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014376f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014377{
14378 float_T f;
14379
14380 rettv->v_type = VAR_FLOAT;
14381 if (get_float_arg(argvars, &f) == OK)
14382 rettv->vval.v_float = log(f);
14383 else
14384 rettv->vval.v_float = 0.0;
14385}
14386
14387/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014388 * "log10()" function
14389 */
14390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014391f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014392{
14393 float_T f;
14394
14395 rettv->v_type = VAR_FLOAT;
14396 if (get_float_arg(argvars, &f) == OK)
14397 rettv->vval.v_float = log10(f);
14398 else
14399 rettv->vval.v_float = 0.0;
14400}
14401#endif
14402
Bram Moolenaar1dced572012-04-05 16:54:08 +020014403#ifdef FEAT_LUA
14404/*
14405 * "luaeval()" function
14406 */
14407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014408f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014409{
14410 char_u *str;
14411 char_u buf[NUMBUFLEN];
14412
14413 str = get_tv_string_buf(&argvars[0], buf);
14414 do_luaeval(str, argvars + 1, rettv);
14415}
14416#endif
14417
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014419 * "map()" function
14420 */
14421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014422f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014423{
14424 filter_map(argvars, rettv, TRUE);
14425}
14426
14427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 */
14430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014431f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014433 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434}
14435
14436/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 */
14439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014440f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014442 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443}
14444
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014445static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446
14447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014448find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014450 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014451 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014452 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 char_u *pat;
14454 regmatch_T regmatch;
14455 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014456 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 char_u *save_cpo;
14458 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014459 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014460 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014461 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014462 list_T *l = NULL;
14463 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014464 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014465 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466
14467 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14468 save_cpo = p_cpo;
14469 p_cpo = (char_u *)"";
14470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014471 rettv->vval.v_number = -1;
14472 if (type == 3)
14473 {
14474 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014475 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014476 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014477 }
14478 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 rettv->v_type = VAR_STRING;
14481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014484 if (argvars[0].v_type == VAR_LIST)
14485 {
14486 if ((l = argvars[0].vval.v_list) == NULL)
14487 goto theend;
14488 li = l->lv_first;
14489 }
14490 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014491 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014492 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014493 len = (long)STRLEN(str);
14494 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14497 if (pat == NULL)
14498 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014500 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014502 int error = FALSE;
14503
14504 start = get_tv_number_chk(&argvars[2], &error);
14505 if (error)
14506 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014507 if (l != NULL)
14508 {
14509 li = list_find(l, start);
14510 if (li == NULL)
14511 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014512 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014513 }
14514 else
14515 {
14516 if (start < 0)
14517 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014518 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014519 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014520 /* When "count" argument is there ignore matches before "start",
14521 * otherwise skip part of the string. Differs when pattern is "^"
14522 * or "\<". */
14523 if (argvars[3].v_type != VAR_UNKNOWN)
14524 startcol = start;
14525 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014526 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014527 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014528 len -= start;
14529 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014530 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014531
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014532 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 nth = get_tv_number_chk(&argvars[3], &error);
14534 if (error)
14535 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 }
14537
14538 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14539 if (regmatch.regprog != NULL)
14540 {
14541 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014542
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014543 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014544 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014545 if (l != NULL)
14546 {
14547 if (li == NULL)
14548 {
14549 match = FALSE;
14550 break;
14551 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014552 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014553 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014554 if (str == NULL)
14555 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014556 }
14557
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014558 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014559
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014560 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014561 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562 if (l == NULL && !match)
14563 break;
14564
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014565 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014566 if (l != NULL)
14567 {
14568 li = li->li_next;
14569 ++idx;
14570 }
14571 else
14572 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014573#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014574 startcol = (colnr_T)(regmatch.startp[0]
14575 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014576#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014577 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014578#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014579 if (startcol > (colnr_T)len
14580 || str + startcol <= regmatch.startp[0])
14581 {
14582 match = FALSE;
14583 break;
14584 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014585 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014586 }
14587
14588 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014590 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014591 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014592 int i;
14593
14594 /* return list with matched string and submatches */
14595 for (i = 0; i < NSUBEXP; ++i)
14596 {
14597 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014598 {
14599 if (list_append_string(rettv->vval.v_list,
14600 (char_u *)"", 0) == FAIL)
14601 break;
14602 }
14603 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014604 regmatch.startp[i],
14605 (int)(regmatch.endp[i] - regmatch.startp[i]))
14606 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014607 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014608 }
14609 }
14610 else if (type == 2)
14611 {
14612 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014613 if (l != NULL)
14614 copy_tv(&li->li_tv, rettv);
14615 else
14616 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014618 }
14619 else if (l != NULL)
14620 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 else
14622 {
14623 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 (varnumber_T)(regmatch.startp[0] - str);
14626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014629 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 }
14631 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014632 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 }
14634
14635theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014636 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 p_cpo = save_cpo;
14638}
14639
14640/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641 * "match()" function
14642 */
14643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014644f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645{
14646 find_some_match(argvars, rettv, 1);
14647}
14648
14649/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014650 * "matchadd()" function
14651 */
14652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014653f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014654{
14655#ifdef FEAT_SEARCH_EXTRA
14656 char_u buf[NUMBUFLEN];
14657 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14658 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14659 int prio = 10; /* default priority */
14660 int id = -1;
14661 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014662 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014663
14664 rettv->vval.v_number = -1;
14665
14666 if (grp == NULL || pat == NULL)
14667 return;
14668 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014669 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014670 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014671 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014672 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014673 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014674 if (argvars[4].v_type != VAR_UNKNOWN)
14675 {
14676 if (argvars[4].v_type != VAR_DICT)
14677 {
14678 EMSG(_(e_dictreq));
14679 return;
14680 }
14681 if (dict_find(argvars[4].vval.v_dict,
14682 (char_u *)"conceal", -1) != NULL)
14683 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14684 (char_u *)"conceal", FALSE);
14685 }
14686 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014687 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014688 if (error == TRUE)
14689 return;
14690 if (id >= 1 && id <= 3)
14691 {
14692 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14693 return;
14694 }
14695
Bram Moolenaar6561d522015-07-21 15:48:27 +020014696 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14697 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014698#endif
14699}
14700
14701/*
14702 * "matchaddpos()" function
14703 */
14704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014705f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014706{
14707#ifdef FEAT_SEARCH_EXTRA
14708 char_u buf[NUMBUFLEN];
14709 char_u *group;
14710 int prio = 10;
14711 int id = -1;
14712 int error = FALSE;
14713 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014714 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014715
14716 rettv->vval.v_number = -1;
14717
14718 group = get_tv_string_buf_chk(&argvars[0], buf);
14719 if (group == NULL)
14720 return;
14721
14722 if (argvars[1].v_type != VAR_LIST)
14723 {
14724 EMSG2(_(e_listarg), "matchaddpos()");
14725 return;
14726 }
14727 l = argvars[1].vval.v_list;
14728 if (l == NULL)
14729 return;
14730
14731 if (argvars[2].v_type != VAR_UNKNOWN)
14732 {
14733 prio = get_tv_number_chk(&argvars[2], &error);
14734 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014735 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014736 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014737 if (argvars[4].v_type != VAR_UNKNOWN)
14738 {
14739 if (argvars[4].v_type != VAR_DICT)
14740 {
14741 EMSG(_(e_dictreq));
14742 return;
14743 }
14744 if (dict_find(argvars[4].vval.v_dict,
14745 (char_u *)"conceal", -1) != NULL)
14746 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14747 (char_u *)"conceal", FALSE);
14748 }
14749 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014750 }
14751 if (error == TRUE)
14752 return;
14753
14754 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14755 if (id == 1 || id == 2)
14756 {
14757 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14758 return;
14759 }
14760
Bram Moolenaar6561d522015-07-21 15:48:27 +020014761 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14762 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014763#endif
14764}
14765
14766/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014767 * "matcharg()" function
14768 */
14769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014770f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014771{
14772 if (rettv_list_alloc(rettv) == OK)
14773 {
14774#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014775 int id = get_tv_number(&argvars[0]);
14776 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014777
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014778 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014779 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014780 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14781 {
14782 list_append_string(rettv->vval.v_list,
14783 syn_id2name(m->hlg_id), -1);
14784 list_append_string(rettv->vval.v_list, m->pattern, -1);
14785 }
14786 else
14787 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014788 list_append_string(rettv->vval.v_list, NULL, -1);
14789 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014790 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014791 }
14792#endif
14793 }
14794}
14795
14796/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014797 * "matchdelete()" function
14798 */
14799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014800f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014801{
14802#ifdef FEAT_SEARCH_EXTRA
14803 rettv->vval.v_number = match_delete(curwin,
14804 (int)get_tv_number(&argvars[0]), TRUE);
14805#endif
14806}
14807
14808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014809 * "matchend()" function
14810 */
14811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014812f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014813{
14814 find_some_match(argvars, rettv, 0);
14815}
14816
14817/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014818 * "matchlist()" function
14819 */
14820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014822{
14823 find_some_match(argvars, rettv, 3);
14824}
14825
14826/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014827 * "matchstr()" function
14828 */
14829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014830f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014831{
14832 find_some_match(argvars, rettv, 2);
14833}
14834
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014835static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014836
14837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014838max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014839{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014840 long n = 0;
14841 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014842 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014843
14844 if (argvars[0].v_type == VAR_LIST)
14845 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 list_T *l;
14847 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014848
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014849 l = argvars[0].vval.v_list;
14850 if (l != NULL)
14851 {
14852 li = l->lv_first;
14853 if (li != NULL)
14854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014855 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014856 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014857 {
14858 li = li->li_next;
14859 if (li == NULL)
14860 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014861 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014862 if (domax ? i > n : i < n)
14863 n = i;
14864 }
14865 }
14866 }
14867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014868 else if (argvars[0].v_type == VAR_DICT)
14869 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014870 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014871 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014872 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014873 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014874
14875 d = argvars[0].vval.v_dict;
14876 if (d != NULL)
14877 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014878 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014880 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014881 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014882 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014883 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014884 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014885 if (first)
14886 {
14887 n = i;
14888 first = FALSE;
14889 }
14890 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014891 n = i;
14892 }
14893 }
14894 }
14895 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014896 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014897 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014898 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014899}
14900
14901/*
14902 * "max()" function
14903 */
14904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014905f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014906{
14907 max_min(argvars, rettv, TRUE);
14908}
14909
14910/*
14911 * "min()" function
14912 */
14913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014914f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014915{
14916 max_min(argvars, rettv, FALSE);
14917}
14918
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014919static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014920
14921/*
14922 * Create the directory in which "dir" is located, and higher levels when
14923 * needed.
14924 */
14925 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010014926mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014927{
14928 char_u *p;
14929 char_u *updir;
14930 int r = FAIL;
14931
14932 /* Get end of directory name in "dir".
14933 * We're done when it's "/" or "c:/". */
14934 p = gettail_sep(dir);
14935 if (p <= get_past_head(dir))
14936 return OK;
14937
14938 /* If the directory exists we're done. Otherwise: create it.*/
14939 updir = vim_strnsave(dir, (int)(p - dir));
14940 if (updir == NULL)
14941 return FAIL;
14942 if (mch_isdir(updir))
14943 r = OK;
14944 else if (mkdir_recurse(updir, prot) == OK)
14945 r = vim_mkdir_emsg(updir, prot);
14946 vim_free(updir);
14947 return r;
14948}
14949
14950#ifdef vim_mkdir
14951/*
14952 * "mkdir()" function
14953 */
14954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014955f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014956{
14957 char_u *dir;
14958 char_u buf[NUMBUFLEN];
14959 int prot = 0755;
14960
14961 rettv->vval.v_number = FAIL;
14962 if (check_restricted() || check_secure())
14963 return;
14964
14965 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014966 if (*dir == NUL)
14967 rettv->vval.v_number = FAIL;
14968 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014969 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014970 if (*gettail(dir) == NUL)
14971 /* remove trailing slashes */
14972 *gettail_sep(dir) = NUL;
14973
14974 if (argvars[1].v_type != VAR_UNKNOWN)
14975 {
14976 if (argvars[2].v_type != VAR_UNKNOWN)
14977 prot = get_tv_number_chk(&argvars[2], NULL);
14978 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14979 mkdir_recurse(dir, prot);
14980 }
14981 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014982 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014983}
14984#endif
14985
Bram Moolenaar0d660222005-01-07 21:51:51 +000014986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 * "mode()" function
14988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014990f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014992 char_u buf[3];
14993
14994 buf[1] = NUL;
14995 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 if (VIsual_active)
14998 {
14999 if (VIsual_select)
15000 buf[0] = VIsual_mode + 's' - 'v';
15001 else
15002 buf[0] = VIsual_mode;
15003 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015004 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015005 || State == CONFIRM)
15006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015008 if (State == ASKMORE)
15009 buf[1] = 'm';
15010 else if (State == CONFIRM)
15011 buf[1] = '?';
15012 }
15013 else if (State == EXTERNCMD)
15014 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 else if (State & INSERT)
15016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015017#ifdef FEAT_VREPLACE
15018 if (State & VREPLACE_FLAG)
15019 {
15020 buf[0] = 'R';
15021 buf[1] = 'v';
15022 }
15023 else
15024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 if (State & REPLACE_FLAG)
15026 buf[0] = 'R';
15027 else
15028 buf[0] = 'i';
15029 }
15030 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015033 if (exmode_active)
15034 buf[1] = 'v';
15035 }
15036 else if (exmode_active)
15037 {
15038 buf[0] = 'c';
15039 buf[1] = 'e';
15040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015044 if (finish_op)
15045 buf[1] = 'o';
15046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015048 /* Clear out the minor mode when the argument is not a non-zero number or
15049 * non-empty string. */
15050 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015051 buf[1] = NUL;
15052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->vval.v_string = vim_strsave(buf);
15054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055}
15056
Bram Moolenaar429fa852013-04-15 12:27:36 +020015057#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015058/*
15059 * "mzeval()" function
15060 */
15061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015062f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015063{
15064 char_u *str;
15065 char_u buf[NUMBUFLEN];
15066
15067 str = get_tv_string_buf(&argvars[0], buf);
15068 do_mzeval(str, rettv);
15069}
Bram Moolenaar75676462013-01-30 14:55:42 +010015070
15071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015072mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015073{
15074 typval_T argvars[3];
15075
15076 argvars[0].v_type = VAR_STRING;
15077 argvars[0].vval.v_string = name;
15078 copy_tv(args, &argvars[1]);
15079 argvars[2].v_type = VAR_UNKNOWN;
15080 f_call(argvars, rettv);
15081 clear_tv(&argvars[1]);
15082}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015083#endif
15084
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015086 * "nextnonblank()" function
15087 */
15088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015089f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090{
15091 linenr_T lnum;
15092
15093 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015095 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015096 {
15097 lnum = 0;
15098 break;
15099 }
15100 if (*skipwhite(ml_get(lnum)) != NUL)
15101 break;
15102 }
15103 rettv->vval.v_number = lnum;
15104}
15105
15106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 * "nr2char()" function
15108 */
15109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015110f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111{
15112 char_u buf[NUMBUFLEN];
15113
15114#ifdef FEAT_MBYTE
15115 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015116 {
15117 int utf8 = 0;
15118
15119 if (argvars[1].v_type != VAR_UNKNOWN)
15120 utf8 = get_tv_number_chk(&argvars[1], NULL);
15121 if (utf8)
15122 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15123 else
15124 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 else
15127#endif
15128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130 buf[1] = NUL;
15131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 rettv->v_type = VAR_STRING;
15133 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015134}
15135
15136/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015137 * "or(expr, expr)" function
15138 */
15139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015140f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015141{
15142 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15143 | get_tv_number_chk(&argvars[1], NULL);
15144}
15145
15146/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015147 * "pathshorten()" function
15148 */
15149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015150f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015151{
15152 char_u *p;
15153
15154 rettv->v_type = VAR_STRING;
15155 p = get_tv_string_chk(&argvars[0]);
15156 if (p == NULL)
15157 rettv->vval.v_string = NULL;
15158 else
15159 {
15160 p = vim_strsave(p);
15161 rettv->vval.v_string = p;
15162 if (p != NULL)
15163 shorten_dir(p);
15164 }
15165}
15166
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015167#ifdef FEAT_PERL
15168/*
15169 * "perleval()" function
15170 */
15171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015172f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015173{
15174 char_u *str;
15175 char_u buf[NUMBUFLEN];
15176
15177 str = get_tv_string_buf(&argvars[0], buf);
15178 do_perleval(str, rettv);
15179}
15180#endif
15181
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015182#ifdef FEAT_FLOAT
15183/*
15184 * "pow()" function
15185 */
15186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015187f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015188{
15189 float_T fx, fy;
15190
15191 rettv->v_type = VAR_FLOAT;
15192 if (get_float_arg(argvars, &fx) == OK
15193 && get_float_arg(&argvars[1], &fy) == OK)
15194 rettv->vval.v_float = pow(fx, fy);
15195 else
15196 rettv->vval.v_float = 0.0;
15197}
15198#endif
15199
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 * "prevnonblank()" function
15202 */
15203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015204f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015205{
15206 linenr_T lnum;
15207
15208 lnum = get_tv_lnum(argvars);
15209 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15210 lnum = 0;
15211 else
15212 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15213 --lnum;
15214 rettv->vval.v_number = lnum;
15215}
15216
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015217/* This dummy va_list is here because:
15218 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15219 * - locally in the function results in a "used before set" warning
15220 * - using va_start() to initialize it gives "function with fixed args" error */
15221static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015222
Bram Moolenaar8c711452005-01-14 21:53:12 +000015223/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015224 * "printf()" function
15225 */
15226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015227f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015228{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015229 char_u buf[NUMBUFLEN];
15230 int len;
15231 char_u *s;
15232 int saved_did_emsg = did_emsg;
15233 char *fmt;
15234
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015235 rettv->v_type = VAR_STRING;
15236 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015237
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015238 /* Get the required length, allocate the buffer and do it for real. */
15239 did_emsg = FALSE;
15240 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15241 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15242 if (!did_emsg)
15243 {
15244 s = alloc(len + 1);
15245 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015246 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015247 rettv->vval.v_string = s;
15248 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015249 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015250 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015251 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015252}
15253
15254/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015255 * "pumvisible()" function
15256 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015258f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015259{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015260#ifdef FEAT_INS_EXPAND
15261 if (pum_visible())
15262 rettv->vval.v_number = 1;
15263#endif
15264}
15265
Bram Moolenaardb913952012-06-29 12:54:53 +020015266#ifdef FEAT_PYTHON3
15267/*
15268 * "py3eval()" function
15269 */
15270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015271f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015272{
15273 char_u *str;
15274 char_u buf[NUMBUFLEN];
15275
15276 str = get_tv_string_buf(&argvars[0], buf);
15277 do_py3eval(str, rettv);
15278}
15279#endif
15280
15281#ifdef FEAT_PYTHON
15282/*
15283 * "pyeval()" function
15284 */
15285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015286f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015287{
15288 char_u *str;
15289 char_u buf[NUMBUFLEN];
15290
15291 str = get_tv_string_buf(&argvars[0], buf);
15292 do_pyeval(str, rettv);
15293}
15294#endif
15295
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015296/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015297 * "range()" function
15298 */
15299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015300f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015301{
15302 long start;
15303 long end;
15304 long stride = 1;
15305 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015306 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015308 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015309 if (argvars[1].v_type == VAR_UNKNOWN)
15310 {
15311 end = start - 1;
15312 start = 0;
15313 }
15314 else
15315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015317 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015319 }
15320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 if (error)
15322 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015323 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015324 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015325 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015326 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015327 else
15328 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015329 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015331 if (list_append_number(rettv->vval.v_list,
15332 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015334 }
15335}
15336
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015337/*
15338 * "readfile()" function
15339 */
15340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015341f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015342{
15343 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015344 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015345 char_u *fname;
15346 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015347 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15348 int io_size = sizeof(buf);
15349 int readlen; /* size of last fread() */
15350 char_u *prev = NULL; /* previously read bytes, if any */
15351 long prevlen = 0; /* length of data in prev */
15352 long prevsize = 0; /* size of prev buffer */
15353 long maxline = MAXLNUM;
15354 long cnt = 0;
15355 char_u *p; /* position in buf */
15356 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015357
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015358 if (argvars[1].v_type != VAR_UNKNOWN)
15359 {
15360 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15361 binary = TRUE;
15362 if (argvars[2].v_type != VAR_UNKNOWN)
15363 maxline = get_tv_number(&argvars[2]);
15364 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015366 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015367 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015368
15369 /* Always open the file in binary mode, library functions have a mind of
15370 * their own about CR-LF conversion. */
15371 fname = get_tv_string(&argvars[0]);
15372 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15373 {
15374 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15375 return;
15376 }
15377
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015378 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015379 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015380 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015381
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015382 /* This for loop processes what was read, but is also entered at end
15383 * of file so that either:
15384 * - an incomplete line gets written
15385 * - a "binary" file gets an empty line at the end if it ends in a
15386 * newline. */
15387 for (p = buf, start = buf;
15388 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15389 ++p)
15390 {
15391 if (*p == '\n' || readlen <= 0)
15392 {
15393 listitem_T *li;
15394 char_u *s = NULL;
15395 long_u len = p - start;
15396
15397 /* Finished a line. Remove CRs before NL. */
15398 if (readlen > 0 && !binary)
15399 {
15400 while (len > 0 && start[len - 1] == '\r')
15401 --len;
15402 /* removal may cross back to the "prev" string */
15403 if (len == 0)
15404 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15405 --prevlen;
15406 }
15407 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015408 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015409 else
15410 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015411 /* Change "prev" buffer to be the right size. This way
15412 * the bytes are only copied once, and very long lines are
15413 * allocated only once. */
15414 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015415 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015416 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015417 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015418 prev = NULL; /* the list will own the string */
15419 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015420 }
15421 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015422 if (s == NULL)
15423 {
15424 do_outofmem_msg((long_u) prevlen + len + 1);
15425 failed = TRUE;
15426 break;
15427 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015428
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015429 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015430 {
15431 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015432 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433 break;
15434 }
15435 li->li_tv.v_type = VAR_STRING;
15436 li->li_tv.v_lock = 0;
15437 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015438 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015439
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015440 start = p + 1; /* step over newline */
15441 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015442 break;
15443 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015444 else if (*p == NUL)
15445 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015446#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015447 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15448 * when finding the BF and check the previous two bytes. */
15449 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015450 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015451 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15452 * + 1, these may be in the "prev" string. */
15453 char_u back1 = p >= buf + 1 ? p[-1]
15454 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15455 char_u back2 = p >= buf + 2 ? p[-2]
15456 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15457 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015458
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015459 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015460 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015461 char_u *dest = p - 2;
15462
15463 /* Usually a BOM is at the beginning of a file, and so at
15464 * the beginning of a line; then we can just step over it.
15465 */
15466 if (start == dest)
15467 start = p + 1;
15468 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015469 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015470 /* have to shuffle buf to close gap */
15471 int adjust_prevlen = 0;
15472
15473 if (dest < buf)
15474 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015475 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015476 dest = buf;
15477 }
15478 if (readlen > p - buf + 1)
15479 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15480 readlen -= 3 - adjust_prevlen;
15481 prevlen -= adjust_prevlen;
15482 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015483 }
15484 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015485 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015486#endif
15487 } /* for */
15488
15489 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15490 break;
15491 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015492 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015493 /* There's part of a line in buf, store it in "prev". */
15494 if (p - start + prevlen >= prevsize)
15495 {
15496 /* need bigger "prev" buffer */
15497 char_u *newprev;
15498
15499 /* A common use case is ordinary text files and "prev" gets a
15500 * fragment of a line, so the first allocation is made
15501 * small, to avoid repeatedly 'allocing' large and
15502 * 'reallocing' small. */
15503 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015504 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015505 else
15506 {
15507 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015508 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015509 prevsize = grow50pc > growmin ? grow50pc : growmin;
15510 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015511 newprev = prev == NULL ? alloc(prevsize)
15512 : vim_realloc(prev, prevsize);
15513 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 {
15515 do_outofmem_msg((long_u)prevsize);
15516 failed = TRUE;
15517 break;
15518 }
15519 prev = newprev;
15520 }
15521 /* Add the line part to end of "prev". */
15522 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015523 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015524 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015525 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015526
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015527 /*
15528 * For a negative line count use only the lines at the end of the file,
15529 * free the rest.
15530 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015531 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015532 while (cnt > -maxline)
15533 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015534 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015535 --cnt;
15536 }
15537
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015538 if (failed)
15539 {
15540 list_free(rettv->vval.v_list, TRUE);
15541 /* readfile doc says an empty list is returned on error */
15542 rettv->vval.v_list = list_alloc();
15543 }
15544
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015545 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546 fclose(fd);
15547}
15548
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015549#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015550static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015551
15552/*
15553 * Convert a List to proftime_T.
15554 * Return FAIL when there is something wrong.
15555 */
15556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015557list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015558{
15559 long n1, n2;
15560 int error = FALSE;
15561
15562 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15563 || arg->vval.v_list->lv_len != 2)
15564 return FAIL;
15565 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15566 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15567# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015568 tm->HighPart = n1;
15569 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015570# else
15571 tm->tv_sec = n1;
15572 tm->tv_usec = n2;
15573# endif
15574 return error ? FAIL : OK;
15575}
15576#endif /* FEAT_RELTIME */
15577
15578/*
15579 * "reltime()" function
15580 */
15581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015582f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015583{
15584#ifdef FEAT_RELTIME
15585 proftime_T res;
15586 proftime_T start;
15587
15588 if (argvars[0].v_type == VAR_UNKNOWN)
15589 {
15590 /* No arguments: get current time. */
15591 profile_start(&res);
15592 }
15593 else if (argvars[1].v_type == VAR_UNKNOWN)
15594 {
15595 if (list2proftime(&argvars[0], &res) == FAIL)
15596 return;
15597 profile_end(&res);
15598 }
15599 else
15600 {
15601 /* Two arguments: compute the difference. */
15602 if (list2proftime(&argvars[0], &start) == FAIL
15603 || list2proftime(&argvars[1], &res) == FAIL)
15604 return;
15605 profile_sub(&res, &start);
15606 }
15607
15608 if (rettv_list_alloc(rettv) == OK)
15609 {
15610 long n1, n2;
15611
15612# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015613 n1 = res.HighPart;
15614 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015615# else
15616 n1 = res.tv_sec;
15617 n2 = res.tv_usec;
15618# endif
15619 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15620 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15621 }
15622#endif
15623}
15624
15625/*
15626 * "reltimestr()" function
15627 */
15628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015629f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015630{
15631#ifdef FEAT_RELTIME
15632 proftime_T tm;
15633#endif
15634
15635 rettv->v_type = VAR_STRING;
15636 rettv->vval.v_string = NULL;
15637#ifdef FEAT_RELTIME
15638 if (list2proftime(&argvars[0], &tm) == OK)
15639 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15640#endif
15641}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015642
Bram Moolenaar0d660222005-01-07 21:51:51 +000015643#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015644static void make_connection(void);
15645static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015646
15647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015648make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015649{
15650 if (X_DISPLAY == NULL
15651# ifdef FEAT_GUI
15652 && !gui.in_use
15653# endif
15654 )
15655 {
15656 x_force_connect = TRUE;
15657 setup_term_clip();
15658 x_force_connect = FALSE;
15659 }
15660}
15661
15662 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015663check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015664{
15665 make_connection();
15666 if (X_DISPLAY == NULL)
15667 {
15668 EMSG(_("E240: No connection to Vim server"));
15669 return FAIL;
15670 }
15671 return OK;
15672}
15673#endif
15674
15675#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015676static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677
15678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015679remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680{
15681 char_u *server_name;
15682 char_u *keys;
15683 char_u *r = NULL;
15684 char_u buf[NUMBUFLEN];
15685# ifdef WIN32
15686 HWND w;
15687# else
15688 Window w;
15689# endif
15690
15691 if (check_restricted() || check_secure())
15692 return;
15693
15694# ifdef FEAT_X11
15695 if (check_connection() == FAIL)
15696 return;
15697# endif
15698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015699 server_name = get_tv_string_chk(&argvars[0]);
15700 if (server_name == NULL)
15701 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702 keys = get_tv_string_buf(&argvars[1], buf);
15703# ifdef WIN32
15704 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15705# else
15706 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15707 < 0)
15708# endif
15709 {
15710 if (r != NULL)
15711 EMSG(r); /* sending worked but evaluation failed */
15712 else
15713 EMSG2(_("E241: Unable to send to %s"), server_name);
15714 return;
15715 }
15716
15717 rettv->vval.v_string = r;
15718
15719 if (argvars[2].v_type != VAR_UNKNOWN)
15720 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015722 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015723 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015725 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015726 v.di_tv.v_type = VAR_STRING;
15727 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 idvar = get_tv_string_chk(&argvars[2]);
15729 if (idvar != NULL)
15730 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015731 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732 }
15733}
15734#endif
15735
15736/*
15737 * "remote_expr()" function
15738 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015740f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015741{
15742 rettv->v_type = VAR_STRING;
15743 rettv->vval.v_string = NULL;
15744#ifdef FEAT_CLIENTSERVER
15745 remote_common(argvars, rettv, TRUE);
15746#endif
15747}
15748
15749/*
15750 * "remote_foreground()" function
15751 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015753f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755#ifdef FEAT_CLIENTSERVER
15756# ifdef WIN32
15757 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015758 {
15759 char_u *server_name = get_tv_string_chk(&argvars[0]);
15760
15761 if (server_name != NULL)
15762 serverForeground(server_name);
15763 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015764# else
15765 /* Send a foreground() expression to the server. */
15766 argvars[1].v_type = VAR_STRING;
15767 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15768 argvars[2].v_type = VAR_UNKNOWN;
15769 remote_common(argvars, rettv, TRUE);
15770 vim_free(argvars[1].vval.v_string);
15771# endif
15772#endif
15773}
15774
Bram Moolenaar0d660222005-01-07 21:51:51 +000015775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015776f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777{
15778#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015779 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780 char_u *s = NULL;
15781# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015782 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015784 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785
15786 if (check_restricted() || check_secure())
15787 {
15788 rettv->vval.v_number = -1;
15789 return;
15790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015791 serverid = get_tv_string_chk(&argvars[0]);
15792 if (serverid == NULL)
15793 {
15794 rettv->vval.v_number = -1;
15795 return; /* type error; errmsg already given */
15796 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015798 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015799 if (n == 0)
15800 rettv->vval.v_number = -1;
15801 else
15802 {
15803 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15804 rettv->vval.v_number = (s != NULL);
15805 }
15806# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807 if (check_connection() == FAIL)
15808 return;
15809
15810 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812# endif
15813
15814 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015816 char_u *retvar;
15817
Bram Moolenaar33570922005-01-25 22:26:29 +000015818 v.di_tv.v_type = VAR_STRING;
15819 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015820 retvar = get_tv_string_chk(&argvars[1]);
15821 if (retvar != NULL)
15822 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015823 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824 }
15825#else
15826 rettv->vval.v_number = -1;
15827#endif
15828}
15829
Bram Moolenaar0d660222005-01-07 21:51:51 +000015830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015831f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832{
15833 char_u *r = NULL;
15834
15835#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015836 char_u *serverid = get_tv_string_chk(&argvars[0]);
15837
15838 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 {
15840# ifdef WIN32
15841 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015842 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015843
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015844 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845 if (n != 0)
15846 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15847 if (r == NULL)
15848# else
15849 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015851# endif
15852 EMSG(_("E277: Unable to read a server reply"));
15853 }
15854#endif
15855 rettv->v_type = VAR_STRING;
15856 rettv->vval.v_string = r;
15857}
15858
15859/*
15860 * "remote_send()" function
15861 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015863f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864{
15865 rettv->v_type = VAR_STRING;
15866 rettv->vval.v_string = NULL;
15867#ifdef FEAT_CLIENTSERVER
15868 remote_common(argvars, rettv, FALSE);
15869#endif
15870}
15871
15872/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015873 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015874 */
15875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015876f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015877{
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 list_T *l;
15879 listitem_T *item, *item2;
15880 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015881 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015882 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015883 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015884 dict_T *d;
15885 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015886 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015887
Bram Moolenaar8c711452005-01-14 21:53:12 +000015888 if (argvars[0].v_type == VAR_DICT)
15889 {
15890 if (argvars[2].v_type != VAR_UNKNOWN)
15891 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015892 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015893 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 key = get_tv_string_chk(&argvars[1]);
15896 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 di = dict_find(d, key, -1);
15899 if (di == NULL)
15900 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015901 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15902 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015903 {
15904 *rettv = di->di_tv;
15905 init_tv(&di->di_tv);
15906 dictitem_remove(d, di);
15907 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015908 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015909 }
15910 }
15911 else if (argvars[0].v_type != VAR_LIST)
15912 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015913 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015914 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015916 int error = FALSE;
15917
15918 idx = get_tv_number_chk(&argvars[1], &error);
15919 if (error)
15920 ; /* type error: do nothing, errmsg already given */
15921 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015922 EMSGN(_(e_listidx), idx);
15923 else
15924 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015925 if (argvars[2].v_type == VAR_UNKNOWN)
15926 {
15927 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015928 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015929 *rettv = item->li_tv;
15930 vim_free(item);
15931 }
15932 else
15933 {
15934 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015935 end = get_tv_number_chk(&argvars[2], &error);
15936 if (error)
15937 ; /* type error: do nothing */
15938 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015939 EMSGN(_(e_listidx), end);
15940 else
15941 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015942 int cnt = 0;
15943
15944 for (li = item; li != NULL; li = li->li_next)
15945 {
15946 ++cnt;
15947 if (li == item2)
15948 break;
15949 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015950 if (li == NULL) /* didn't find "item2" after "item" */
15951 EMSG(_(e_invrange));
15952 else
15953 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015954 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015955 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015956 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015957 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015958 l->lv_first = item;
15959 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015960 item->li_prev = NULL;
15961 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015962 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015963 }
15964 }
15965 }
15966 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015967 }
15968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969}
15970
15971/*
15972 * "rename({from}, {to})" function
15973 */
15974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015975f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976{
15977 char_u buf[NUMBUFLEN];
15978
15979 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015980 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015982 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15983 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984}
15985
15986/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015987 * "repeat()" function
15988 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015990f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015991{
15992 char_u *p;
15993 int n;
15994 int slen;
15995 int len;
15996 char_u *r;
15997 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015998
15999 n = get_tv_number(&argvars[1]);
16000 if (argvars[0].v_type == VAR_LIST)
16001 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016002 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016003 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016004 if (list_extend(rettv->vval.v_list,
16005 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016006 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016007 }
16008 else
16009 {
16010 p = get_tv_string(&argvars[0]);
16011 rettv->v_type = VAR_STRING;
16012 rettv->vval.v_string = NULL;
16013
16014 slen = (int)STRLEN(p);
16015 len = slen * n;
16016 if (len <= 0)
16017 return;
16018
16019 r = alloc(len + 1);
16020 if (r != NULL)
16021 {
16022 for (i = 0; i < n; i++)
16023 mch_memmove(r + i * slen, p, (size_t)slen);
16024 r[len] = NUL;
16025 }
16026
16027 rettv->vval.v_string = r;
16028 }
16029}
16030
16031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 * "resolve()" function
16033 */
16034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016035f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036{
16037 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016038#ifdef HAVE_READLINK
16039 char_u *buf = NULL;
16040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016042 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043#ifdef FEAT_SHORTCUT
16044 {
16045 char_u *v = NULL;
16046
16047 v = mch_resolve_shortcut(p);
16048 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016049 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016051 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 }
16053#else
16054# ifdef HAVE_READLINK
16055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 char_u *cpy;
16057 int len;
16058 char_u *remain = NULL;
16059 char_u *q;
16060 int is_relative_to_current = FALSE;
16061 int has_trailing_pathsep = FALSE;
16062 int limit = 100;
16063
16064 p = vim_strsave(p);
16065
16066 if (p[0] == '.' && (vim_ispathsep(p[1])
16067 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16068 is_relative_to_current = TRUE;
16069
16070 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016071 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016074 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076
16077 q = getnextcomp(p);
16078 if (*q != NUL)
16079 {
16080 /* Separate the first path component in "p", and keep the
16081 * remainder (beginning with the path separator). */
16082 remain = vim_strsave(q - 1);
16083 q[-1] = NUL;
16084 }
16085
Bram Moolenaard9462e32011-04-11 21:35:11 +020016086 buf = alloc(MAXPATHL + 1);
16087 if (buf == NULL)
16088 goto fail;
16089
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 for (;;)
16091 {
16092 for (;;)
16093 {
16094 len = readlink((char *)p, (char *)buf, MAXPATHL);
16095 if (len <= 0)
16096 break;
16097 buf[len] = NUL;
16098
16099 if (limit-- == 0)
16100 {
16101 vim_free(p);
16102 vim_free(remain);
16103 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016104 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 goto fail;
16106 }
16107
16108 /* Ensure that the result will have a trailing path separator
16109 * if the argument has one. */
16110 if (remain == NULL && has_trailing_pathsep)
16111 add_pathsep(buf);
16112
16113 /* Separate the first path component in the link value and
16114 * concatenate the remainders. */
16115 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16116 if (*q != NUL)
16117 {
16118 if (remain == NULL)
16119 remain = vim_strsave(q - 1);
16120 else
16121 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016122 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 if (cpy != NULL)
16124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 vim_free(remain);
16126 remain = cpy;
16127 }
16128 }
16129 q[-1] = NUL;
16130 }
16131
16132 q = gettail(p);
16133 if (q > p && *q == NUL)
16134 {
16135 /* Ignore trailing path separator. */
16136 q[-1] = NUL;
16137 q = gettail(p);
16138 }
16139 if (q > p && !mch_isFullName(buf))
16140 {
16141 /* symlink is relative to directory of argument */
16142 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16143 if (cpy != NULL)
16144 {
16145 STRCPY(cpy, p);
16146 STRCPY(gettail(cpy), buf);
16147 vim_free(p);
16148 p = cpy;
16149 }
16150 }
16151 else
16152 {
16153 vim_free(p);
16154 p = vim_strsave(buf);
16155 }
16156 }
16157
16158 if (remain == NULL)
16159 break;
16160
16161 /* Append the first path component of "remain" to "p". */
16162 q = getnextcomp(remain + 1);
16163 len = q - remain - (*q != NUL);
16164 cpy = vim_strnsave(p, STRLEN(p) + len);
16165 if (cpy != NULL)
16166 {
16167 STRNCAT(cpy, remain, len);
16168 vim_free(p);
16169 p = cpy;
16170 }
16171 /* Shorten "remain". */
16172 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016173 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 else
16175 {
16176 vim_free(remain);
16177 remain = NULL;
16178 }
16179 }
16180
16181 /* If the result is a relative path name, make it explicitly relative to
16182 * the current directory if and only if the argument had this form. */
16183 if (!vim_ispathsep(*p))
16184 {
16185 if (is_relative_to_current
16186 && *p != NUL
16187 && !(p[0] == '.'
16188 && (p[1] == NUL
16189 || vim_ispathsep(p[1])
16190 || (p[1] == '.'
16191 && (p[2] == NUL
16192 || vim_ispathsep(p[2]))))))
16193 {
16194 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016195 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 if (cpy != NULL)
16197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 vim_free(p);
16199 p = cpy;
16200 }
16201 }
16202 else if (!is_relative_to_current)
16203 {
16204 /* Strip leading "./". */
16205 q = p;
16206 while (q[0] == '.' && vim_ispathsep(q[1]))
16207 q += 2;
16208 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016209 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 }
16211 }
16212
16213 /* Ensure that the result will have no trailing path separator
16214 * if the argument had none. But keep "/" or "//". */
16215 if (!has_trailing_pathsep)
16216 {
16217 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016218 if (after_pathsep(p, q))
16219 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 }
16221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 }
16224# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226# endif
16227#endif
16228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016229 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230
16231#ifdef HAVE_READLINK
16232fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016233 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236}
16237
16238/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016239 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 */
16241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016242f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243{
Bram Moolenaar33570922005-01-25 22:26:29 +000016244 list_T *l;
16245 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 if (argvars[0].v_type != VAR_LIST)
16248 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016249 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016250 && !tv_check_lock(l->lv_lock,
16251 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 {
16253 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016254 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016255 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256 while (li != NULL)
16257 {
16258 ni = li->li_prev;
16259 list_append(l, li);
16260 li = ni;
16261 }
16262 rettv->vval.v_list = l;
16263 rettv->v_type = VAR_LIST;
16264 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016265 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267}
16268
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016269#define SP_NOMOVE 0x01 /* don't move cursor */
16270#define SP_REPEAT 0x02 /* repeat to find outer pair */
16271#define SP_RETCOUNT 0x04 /* return matchcount */
16272#define SP_SETPCMARK 0x08 /* set previous context mark */
16273#define SP_START 0x10 /* accept match at start position */
16274#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16275#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016276#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016277
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016278static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279
16280/*
16281 * Get flags for a search function.
16282 * Possibly sets "p_ws".
16283 * Returns BACKWARD, FORWARD or zero (for an error).
16284 */
16285 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016286get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287{
16288 int dir = FORWARD;
16289 char_u *flags;
16290 char_u nbuf[NUMBUFLEN];
16291 int mask;
16292
16293 if (varp->v_type != VAR_UNKNOWN)
16294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016295 flags = get_tv_string_buf_chk(varp, nbuf);
16296 if (flags == NULL)
16297 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016298 while (*flags != NUL)
16299 {
16300 switch (*flags)
16301 {
16302 case 'b': dir = BACKWARD; break;
16303 case 'w': p_ws = TRUE; break;
16304 case 'W': p_ws = FALSE; break;
16305 default: mask = 0;
16306 if (flagsp != NULL)
16307 switch (*flags)
16308 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016309 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016310 case 'e': mask = SP_END; break;
16311 case 'm': mask = SP_RETCOUNT; break;
16312 case 'n': mask = SP_NOMOVE; break;
16313 case 'p': mask = SP_SUBPAT; break;
16314 case 'r': mask = SP_REPEAT; break;
16315 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016316 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 }
16318 if (mask == 0)
16319 {
16320 EMSG2(_(e_invarg2), flags);
16321 dir = 0;
16322 }
16323 else
16324 *flagsp |= mask;
16325 }
16326 if (dir == 0)
16327 break;
16328 ++flags;
16329 }
16330 }
16331 return dir;
16332}
16333
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016335 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016337 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016338search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016340 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 char_u *pat;
16342 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016343 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 int save_p_ws = p_ws;
16345 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016346 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016347 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016348 proftime_T tm;
16349#ifdef FEAT_RELTIME
16350 long time_limit = 0;
16351#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016352 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016353 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016356 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016357 if (dir == 0)
16358 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016359 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016360 if (flags & SP_START)
16361 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016362 if (flags & SP_END)
16363 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016364 if (flags & SP_COLUMN)
16365 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016366
Bram Moolenaar76929292008-01-06 19:07:36 +000016367 /* Optional arguments: line number to stop searching and timeout. */
16368 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016369 {
16370 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16371 if (lnum_stop < 0)
16372 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016373#ifdef FEAT_RELTIME
16374 if (argvars[3].v_type != VAR_UNKNOWN)
16375 {
16376 time_limit = get_tv_number_chk(&argvars[3], NULL);
16377 if (time_limit < 0)
16378 goto theend;
16379 }
16380#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016381 }
16382
Bram Moolenaar76929292008-01-06 19:07:36 +000016383#ifdef FEAT_RELTIME
16384 /* Set the time limit, if there is one. */
16385 profile_setlimit(time_limit, &tm);
16386#endif
16387
Bram Moolenaar231334e2005-07-25 20:46:57 +000016388 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016389 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016390 * Check to make sure only those flags are set.
16391 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16392 * flags cannot be set. Check for that condition also.
16393 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016394 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016395 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016397 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016398 goto theend;
16399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016401 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016402 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016403 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016404 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016406 if (flags & SP_SUBPAT)
16407 retval = subpatnum;
16408 else
16409 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016410 if (flags & SP_SETPCMARK)
16411 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016413 if (match_pos != NULL)
16414 {
16415 /* Store the match cursor position */
16416 match_pos->lnum = pos.lnum;
16417 match_pos->col = pos.col + 1;
16418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 /* "/$" will put the cursor after the end of the line, may need to
16420 * correct that here */
16421 check_cursor();
16422 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016423
16424 /* If 'n' flag is used: restore cursor position. */
16425 if (flags & SP_NOMOVE)
16426 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016427 else
16428 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016429theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016431
16432 return retval;
16433}
16434
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016435#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016436
16437/*
16438 * round() is not in C90, use ceil() or floor() instead.
16439 */
16440 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016441vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016442{
16443 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16444}
16445
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016446/*
16447 * "round({float})" function
16448 */
16449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016450f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016451{
16452 float_T f;
16453
16454 rettv->v_type = VAR_FLOAT;
16455 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016456 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016457 else
16458 rettv->vval.v_float = 0.0;
16459}
16460#endif
16461
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016462/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016463 * "screenattr()" function
16464 */
16465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016466f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016467{
16468 int row;
16469 int col;
16470 int c;
16471
16472 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16473 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16474 if (row < 0 || row >= screen_Rows
16475 || col < 0 || col >= screen_Columns)
16476 c = -1;
16477 else
16478 c = ScreenAttrs[LineOffset[row] + col];
16479 rettv->vval.v_number = c;
16480}
16481
16482/*
16483 * "screenchar()" function
16484 */
16485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016486f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016487{
16488 int row;
16489 int col;
16490 int off;
16491 int c;
16492
16493 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16494 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16495 if (row < 0 || row >= screen_Rows
16496 || col < 0 || col >= screen_Columns)
16497 c = -1;
16498 else
16499 {
16500 off = LineOffset[row] + col;
16501#ifdef FEAT_MBYTE
16502 if (enc_utf8 && ScreenLinesUC[off] != 0)
16503 c = ScreenLinesUC[off];
16504 else
16505#endif
16506 c = ScreenLines[off];
16507 }
16508 rettv->vval.v_number = c;
16509}
16510
16511/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016512 * "screencol()" function
16513 *
16514 * First column is 1 to be consistent with virtcol().
16515 */
16516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016517f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016518{
16519 rettv->vval.v_number = screen_screencol() + 1;
16520}
16521
16522/*
16523 * "screenrow()" function
16524 */
16525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016526f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016527{
16528 rettv->vval.v_number = screen_screenrow() + 1;
16529}
16530
16531/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016532 * "search()" function
16533 */
16534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016535f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016536{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016537 int flags = 0;
16538
16539 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540}
16541
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016543 * "searchdecl()" function
16544 */
16545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016546f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016547{
16548 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016549 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016550 int error = FALSE;
16551 char_u *name;
16552
16553 rettv->vval.v_number = 1; /* default: FAIL */
16554
16555 name = get_tv_string_chk(&argvars[0]);
16556 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016557 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016558 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016559 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16560 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16561 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016562 if (!error && name != NULL)
16563 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016564 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016565}
16566
16567/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016568 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016571searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572{
16573 char_u *spat, *mpat, *epat;
16574 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 int dir;
16577 int flags = 0;
16578 char_u nbuf1[NUMBUFLEN];
16579 char_u nbuf2[NUMBUFLEN];
16580 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016581 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016582 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016583 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016586 spat = get_tv_string_chk(&argvars[0]);
16587 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16588 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16589 if (spat == NULL || mpat == NULL || epat == NULL)
16590 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 /* Handle the optional fourth argument: flags */
16593 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016594 if (dir == 0)
16595 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016596
16597 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016598 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16599 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016600 if ((flags & (SP_END | SP_SUBPAT)) != 0
16601 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016602 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016603 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016604 goto theend;
16605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016607 /* Using 'r' implies 'W', otherwise it doesn't work. */
16608 if (flags & SP_REPEAT)
16609 p_ws = FALSE;
16610
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016611 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016612 if (argvars[3].v_type == VAR_UNKNOWN
16613 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 skip = (char_u *)"";
16615 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016618 if (argvars[5].v_type != VAR_UNKNOWN)
16619 {
16620 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16621 if (lnum_stop < 0)
16622 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016623#ifdef FEAT_RELTIME
16624 if (argvars[6].v_type != VAR_UNKNOWN)
16625 {
16626 time_limit = get_tv_number_chk(&argvars[6], NULL);
16627 if (time_limit < 0)
16628 goto theend;
16629 }
16630#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016631 }
16632 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016633 if (skip == NULL)
16634 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016636 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016637 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016638
16639theend:
16640 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016641
16642 return retval;
16643}
16644
16645/*
16646 * "searchpair()" function
16647 */
16648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016649f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016650{
16651 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16652}
16653
16654/*
16655 * "searchpairpos()" function
16656 */
16657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016658f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016659{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016660 pos_T match_pos;
16661 int lnum = 0;
16662 int col = 0;
16663
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016664 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016665 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016666
16667 if (searchpair_cmn(argvars, &match_pos) > 0)
16668 {
16669 lnum = match_pos.lnum;
16670 col = match_pos.col;
16671 }
16672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016673 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16674 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016675}
16676
16677/*
16678 * Search for a start/middle/end thing.
16679 * Used by searchpair(), see its documentation for the details.
16680 * Returns 0 or -1 for no match,
16681 */
16682 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016683do_searchpair(
16684 char_u *spat, /* start pattern */
16685 char_u *mpat, /* middle pattern */
16686 char_u *epat, /* end pattern */
16687 int dir, /* BACKWARD or FORWARD */
16688 char_u *skip, /* skip expression */
16689 int flags, /* SP_SETPCMARK and other SP_ values */
16690 pos_T *match_pos,
16691 linenr_T lnum_stop, /* stop at this line if not zero */
16692 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016693{
16694 char_u *save_cpo;
16695 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16696 long retval = 0;
16697 pos_T pos;
16698 pos_T firstpos;
16699 pos_T foundpos;
16700 pos_T save_cursor;
16701 pos_T save_pos;
16702 int n;
16703 int r;
16704 int nest = 1;
16705 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016706 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016707 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016708
16709 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16710 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016711 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016712
Bram Moolenaar76929292008-01-06 19:07:36 +000016713#ifdef FEAT_RELTIME
16714 /* Set the time limit, if there is one. */
16715 profile_setlimit(time_limit, &tm);
16716#endif
16717
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016718 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16719 * start/middle/end (pat3, for the top pair). */
16720 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16721 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16722 if (pat2 == NULL || pat3 == NULL)
16723 goto theend;
16724 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16725 if (*mpat == NUL)
16726 STRCPY(pat3, pat2);
16727 else
16728 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16729 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016730 if (flags & SP_START)
16731 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016732
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 save_cursor = curwin->w_cursor;
16734 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016735 clearpos(&firstpos);
16736 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 pat = pat3;
16738 for (;;)
16739 {
16740 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016741 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16743 /* didn't find it or found the first match again: FAIL */
16744 break;
16745
16746 if (firstpos.lnum == 0)
16747 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016748 if (equalpos(pos, foundpos))
16749 {
16750 /* Found the same position again. Can happen with a pattern that
16751 * has "\zs" at the end and searching backwards. Advance one
16752 * character and try again. */
16753 if (dir == BACKWARD)
16754 decl(&pos);
16755 else
16756 incl(&pos);
16757 }
16758 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016760 /* clear the start flag to avoid getting stuck here */
16761 options &= ~SEARCH_START;
16762
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 /* If the skip pattern matches, ignore this match. */
16764 if (*skip != NUL)
16765 {
16766 save_pos = curwin->w_cursor;
16767 curwin->w_cursor = pos;
16768 r = eval_to_bool(skip, &err, NULL, FALSE);
16769 curwin->w_cursor = save_pos;
16770 if (err)
16771 {
16772 /* Evaluating {skip} caused an error, break here. */
16773 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016774 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 break;
16776 }
16777 if (r)
16778 continue;
16779 }
16780
16781 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16782 {
16783 /* Found end when searching backwards or start when searching
16784 * forward: nested pair. */
16785 ++nest;
16786 pat = pat2; /* nested, don't search for middle */
16787 }
16788 else
16789 {
16790 /* Found end when searching forward or start when searching
16791 * backward: end of (nested) pair; or found middle in outer pair. */
16792 if (--nest == 1)
16793 pat = pat3; /* outer level, search for middle */
16794 }
16795
16796 if (nest == 0)
16797 {
16798 /* Found the match: return matchcount or line number. */
16799 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016800 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016802 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016803 if (flags & SP_SETPCMARK)
16804 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 curwin->w_cursor = pos;
16806 if (!(flags & SP_REPEAT))
16807 break;
16808 nest = 1; /* search for next unmatched */
16809 }
16810 }
16811
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016812 if (match_pos != NULL)
16813 {
16814 /* Store the match cursor position */
16815 match_pos->lnum = curwin->w_cursor.lnum;
16816 match_pos->col = curwin->w_cursor.col + 1;
16817 }
16818
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016820 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 curwin->w_cursor = save_cursor;
16822
16823theend:
16824 vim_free(pat2);
16825 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016826 if (p_cpo == empty_option)
16827 p_cpo = save_cpo;
16828 else
16829 /* Darn, evaluating the {skip} expression changed the value. */
16830 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016831
16832 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833}
16834
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016835/*
16836 * "searchpos()" function
16837 */
16838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016839f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016840{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016841 pos_T match_pos;
16842 int lnum = 0;
16843 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016844 int n;
16845 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016846
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016847 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016848 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016849
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016850 n = search_cmn(argvars, &match_pos, &flags);
16851 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016852 {
16853 lnum = match_pos.lnum;
16854 col = match_pos.col;
16855 }
16856
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016857 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16858 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016859 if (flags & SP_SUBPAT)
16860 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016861}
16862
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016863#ifdef FEAT_CHANNEL
16864/*
16865 * common for "sendexpr()" and "sendraw()"
16866 * Returns the channel index if the caller should read the response.
16867 * Otherwise returns -1.
16868 */
16869 static int
16870send_common(typval_T *argvars, char_u *text, char *fun)
16871{
16872 int ch_idx;
16873 char_u *callback = NULL;
16874
16875 ch_idx = get_channel_arg(&argvars[0]);
16876 if (ch_idx < 0)
16877 return -1;
16878
16879 if (argvars[2].v_type != VAR_UNKNOWN)
16880 {
16881 callback = get_callback(&argvars[2]);
16882 if (callback == NULL)
16883 return -1;
16884 }
16885 /* Set the callback or clear it. An empty callback means no callback and
16886 * not reading the response. */
16887 channel_set_req_callback(ch_idx,
16888 callback != NULL && *callback == NUL ? NULL : callback);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016889
16890 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
16891 return ch_idx;
16892 return -1;
16893}
16894
16895/*
16896 * "sendexpr()" function
16897 */
16898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016899f_sendexpr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016900{
16901 char_u *text;
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010016902 typval_T *listtv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016903 int ch_idx;
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010016904 int id;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016905
16906 /* return an empty string by default */
16907 rettv->v_type = VAR_STRING;
16908 rettv->vval.v_string = NULL;
16909
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010016910 id = channel_get_id();
16911 text = json_encode_nr_expr(id, &argvars[1]);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010016912 if (text == NULL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016913 return;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016914
16915 ch_idx = send_common(argvars, text, "sendexpr");
16916 if (ch_idx >= 0)
16917 {
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010016918 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016919 {
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010016920 if (listtv->v_type == VAR_LIST)
16921 {
16922 list_T *list = listtv->vval.v_list;
16923
16924 if (list->lv_len == 2)
16925 {
16926 /* Move the item from the list and then change the type to
16927 * avoid the value being freed. */
16928 *rettv = list->lv_last->li_tv;
16929 list->lv_last->li_tv.v_type = VAR_NUMBER;
16930 }
16931 }
16932 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016933 }
16934 }
16935}
16936
16937/*
16938 * "sendraw()" function
16939 */
16940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016941f_sendraw(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016942{
16943 char_u buf[NUMBUFLEN];
16944 char_u *text;
16945 int ch_idx;
16946
16947 /* return an empty string by default */
16948 rettv->v_type = VAR_STRING;
16949 rettv->vval.v_string = NULL;
16950
16951 text = get_tv_string_buf(&argvars[1], buf);
16952 ch_idx = send_common(argvars, text, "sendraw");
16953 if (ch_idx >= 0)
16954 rettv->vval.v_string = channel_read_block(ch_idx);
16955}
16956#endif
16957
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016958
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016960f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962#ifdef FEAT_CLIENTSERVER
16963 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016964 char_u *server = get_tv_string_chk(&argvars[0]);
16965 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016968 if (server == NULL || reply == NULL)
16969 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 if (check_restricted() || check_secure())
16971 return;
16972# ifdef FEAT_X11
16973 if (check_connection() == FAIL)
16974 return;
16975# endif
16976
16977 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 EMSG(_("E258: Unable to send to client"));
16980 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 rettv->vval.v_number = 0;
16983#else
16984 rettv->vval.v_number = -1;
16985#endif
16986}
16987
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016989f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990{
16991 char_u *r = NULL;
16992
16993#ifdef FEAT_CLIENTSERVER
16994# ifdef WIN32
16995 r = serverGetVimNames();
16996# else
16997 make_connection();
16998 if (X_DISPLAY != NULL)
16999 r = serverGetVimNames(X_DISPLAY);
17000# endif
17001#endif
17002 rettv->v_type = VAR_STRING;
17003 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004}
17005
17006/*
17007 * "setbufvar()" function
17008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017010f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011{
17012 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 char_u nbuf[NUMBUFLEN];
17017
17018 if (check_restricted() || check_secure())
17019 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017020 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17021 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017022 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 varp = &argvars[2];
17024
17025 if (buf != NULL && varname != NULL && varp != NULL)
17026 {
17027 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029
17030 if (*varname == '&')
17031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017032 long numval;
17033 char_u *strval;
17034 int error = FALSE;
17035
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 numval = get_tv_number_chk(varp, &error);
17038 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017039 if (!error && strval != NULL)
17040 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 }
17042 else
17043 {
17044 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17045 if (bufvarname != NULL)
17046 {
17047 STRCPY(bufvarname, "b:");
17048 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017049 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 vim_free(bufvarname);
17051 }
17052 }
17053
17054 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057}
17058
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017060f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017061{
17062 dict_T *d;
17063 dictitem_T *di;
17064 char_u *csearch;
17065
17066 if (argvars[0].v_type != VAR_DICT)
17067 {
17068 EMSG(_(e_dictreq));
17069 return;
17070 }
17071
17072 if ((d = argvars[0].vval.v_dict) != NULL)
17073 {
17074 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17075 if (csearch != NULL)
17076 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017077#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017078 if (enc_utf8)
17079 {
17080 int pcc[MAX_MCO];
17081 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017082
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017083 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17084 }
17085 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017086#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017087 set_last_csearch(PTR2CHAR(csearch),
17088 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017089 }
17090
17091 di = dict_find(d, (char_u *)"forward", -1);
17092 if (di != NULL)
17093 set_csearch_direction(get_tv_number(&di->di_tv)
17094 ? FORWARD : BACKWARD);
17095
17096 di = dict_find(d, (char_u *)"until", -1);
17097 if (di != NULL)
17098 set_csearch_until(!!get_tv_number(&di->di_tv));
17099 }
17100}
17101
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102/*
17103 * "setcmdpos()" function
17104 */
17105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017106f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017108 int pos = (int)get_tv_number(&argvars[0]) - 1;
17109
17110 if (pos >= 0)
17111 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112}
17113
17114/*
17115 * "setline()" function
17116 */
17117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017118f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
17120 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017121 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017122 list_T *l = NULL;
17123 listitem_T *li = NULL;
17124 long added = 0;
17125 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017127 lnum = get_tv_lnum(&argvars[0]);
17128 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017130 l = argvars[1].vval.v_list;
17131 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017133 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017134 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017135
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017136 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017137 for (;;)
17138 {
17139 if (l != NULL)
17140 {
17141 /* list argument, get next string */
17142 if (li == NULL)
17143 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017145 li = li->li_next;
17146 }
17147
17148 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017150 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017151
17152 /* When coming here from Insert mode, sync undo, so that this can be
17153 * undone separately from what was previously inserted. */
17154 if (u_sync_once == 2)
17155 {
17156 u_sync_once = 1; /* notify that u_sync() was called */
17157 u_sync(TRUE);
17158 }
17159
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017160 if (lnum <= curbuf->b_ml.ml_line_count)
17161 {
17162 /* existing line, replace it */
17163 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17164 {
17165 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017166 if (lnum == curwin->w_cursor.lnum)
17167 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017168 rettv->vval.v_number = 0; /* OK */
17169 }
17170 }
17171 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17172 {
17173 /* lnum is one past the last line, append the line */
17174 ++added;
17175 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17176 rettv->vval.v_number = 0; /* OK */
17177 }
17178
17179 if (l == NULL) /* only one string argument */
17180 break;
17181 ++lnum;
17182 }
17183
17184 if (added > 0)
17185 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186}
17187
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017188static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv);
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017189
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017191 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017192 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017194set_qf_ll_list(
17195 win_T *wp UNUSED,
17196 typval_T *list_arg UNUSED,
17197 typval_T *action_arg UNUSED,
17198 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017199{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017200#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017201 char_u *act;
17202 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017203#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017204
Bram Moolenaar2641f772005-03-25 21:58:17 +000017205 rettv->vval.v_number = -1;
17206
17207#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017208 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017209 EMSG(_(e_listreq));
17210 else
17211 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017212 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017213
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017214 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017215 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017216 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017217 if (act == NULL)
17218 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017219 if (*act == 'a' || *act == 'r')
17220 action = *act;
17221 }
17222
Bram Moolenaar81484f42012-12-05 15:16:47 +010017223 if (l != NULL && set_errorlist(wp, l, action,
17224 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017225 rettv->vval.v_number = 0;
17226 }
17227#endif
17228}
17229
17230/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017231 * "setloclist()" function
17232 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017234f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017235{
17236 win_T *win;
17237
17238 rettv->vval.v_number = -1;
17239
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017240 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017241 if (win != NULL)
17242 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17243}
17244
17245/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017246 * "setmatches()" function
17247 */
17248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017249f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017250{
17251#ifdef FEAT_SEARCH_EXTRA
17252 list_T *l;
17253 listitem_T *li;
17254 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017255 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017256
17257 rettv->vval.v_number = -1;
17258 if (argvars[0].v_type != VAR_LIST)
17259 {
17260 EMSG(_(e_listreq));
17261 return;
17262 }
17263 if ((l = argvars[0].vval.v_list) != NULL)
17264 {
17265
17266 /* To some extent make sure that we are dealing with a list from
17267 * "getmatches()". */
17268 li = l->lv_first;
17269 while (li != NULL)
17270 {
17271 if (li->li_tv.v_type != VAR_DICT
17272 || (d = li->li_tv.vval.v_dict) == NULL)
17273 {
17274 EMSG(_(e_invarg));
17275 return;
17276 }
17277 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017278 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17279 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017280 && dict_find(d, (char_u *)"priority", -1) != NULL
17281 && dict_find(d, (char_u *)"id", -1) != NULL))
17282 {
17283 EMSG(_(e_invarg));
17284 return;
17285 }
17286 li = li->li_next;
17287 }
17288
17289 clear_matches(curwin);
17290 li = l->lv_first;
17291 while (li != NULL)
17292 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017293 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017294 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017295 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017296 char_u *group;
17297 int priority;
17298 int id;
17299 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017300
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017301 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017302 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17303 {
17304 if (s == NULL)
17305 {
17306 s = list_alloc();
17307 if (s == NULL)
17308 return;
17309 }
17310
17311 /* match from matchaddpos() */
17312 for (i = 1; i < 9; i++)
17313 {
17314 sprintf((char *)buf, (char *)"pos%d", i);
17315 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17316 {
17317 if (di->di_tv.v_type != VAR_LIST)
17318 return;
17319
17320 list_append_tv(s, &di->di_tv);
17321 s->lv_refcount++;
17322 }
17323 else
17324 break;
17325 }
17326 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017327
17328 group = get_dict_string(d, (char_u *)"group", FALSE);
17329 priority = (int)get_dict_number(d, (char_u *)"priority");
17330 id = (int)get_dict_number(d, (char_u *)"id");
17331 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17332 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17333 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017334 if (i == 0)
17335 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017336 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017337 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017338 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017339 }
17340 else
17341 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017342 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017343 list_unref(s);
17344 s = NULL;
17345 }
17346
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017347 li = li->li_next;
17348 }
17349 rettv->vval.v_number = 0;
17350 }
17351#endif
17352}
17353
17354/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017355 * "setpos()" function
17356 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017358f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017359{
17360 pos_T pos;
17361 int fnum;
17362 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017363 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017364
Bram Moolenaar08250432008-02-13 11:42:46 +000017365 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017366 name = get_tv_string_chk(argvars);
17367 if (name != NULL)
17368 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017369 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017370 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017371 if (--pos.col < 0)
17372 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017373 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017374 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017375 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017376 if (fnum == curbuf->b_fnum)
17377 {
17378 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017379 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017380 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017381 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017382 curwin->w_set_curswant = FALSE;
17383 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017384 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017385 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017386 }
17387 else
17388 EMSG(_(e_invarg));
17389 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017390 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17391 {
17392 /* set mark */
17393 if (setmark_pos(name[1], &pos, fnum) == OK)
17394 rettv->vval.v_number = 0;
17395 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017396 else
17397 EMSG(_(e_invarg));
17398 }
17399 }
17400}
17401
17402/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017403 * "setqflist()" function
17404 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017406f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017407{
17408 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17409}
17410
17411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 * "setreg()" function
17413 */
17414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017415f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416{
17417 int regname;
17418 char_u *strregname;
17419 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017420 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421 int append;
17422 char_u yank_type;
17423 long block_len;
17424
17425 block_len = -1;
17426 yank_type = MAUTO;
17427 append = FALSE;
17428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017429 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017430 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017432 if (strregname == NULL)
17433 return; /* type error; errmsg already given */
17434 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 if (regname == 0 || regname == '@')
17436 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017440 stropt = get_tv_string_chk(&argvars[2]);
17441 if (stropt == NULL)
17442 return; /* type error */
17443 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 switch (*stropt)
17445 {
17446 case 'a': case 'A': /* append */
17447 append = TRUE;
17448 break;
17449 case 'v': case 'c': /* character-wise selection */
17450 yank_type = MCHAR;
17451 break;
17452 case 'V': case 'l': /* line-wise selection */
17453 yank_type = MLINE;
17454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 case 'b': case Ctrl_V: /* block-wise selection */
17456 yank_type = MBLOCK;
17457 if (VIM_ISDIGIT(stropt[1]))
17458 {
17459 ++stropt;
17460 block_len = getdigits(&stropt) - 1;
17461 --stropt;
17462 }
17463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 }
17465 }
17466
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017467 if (argvars[1].v_type == VAR_LIST)
17468 {
17469 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017470 char_u **allocval;
17471 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017472 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017473 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017474 int len = argvars[1].vval.v_list->lv_len;
17475 listitem_T *li;
17476
Bram Moolenaar7d647822014-04-05 21:28:56 +020017477 /* First half: use for pointers to result lines; second half: use for
17478 * pointers to allocated copies. */
17479 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017480 if (lstval == NULL)
17481 return;
17482 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017483 allocval = lstval + len + 2;
17484 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017485
17486 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17487 li = li->li_next)
17488 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017489 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017490 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017491 goto free_lstval;
17492 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017493 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017494 /* Need to make a copy, next get_tv_string_buf_chk() will
17495 * overwrite the string. */
17496 strval = vim_strsave(buf);
17497 if (strval == NULL)
17498 goto free_lstval;
17499 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017500 }
17501 *curval++ = strval;
17502 }
17503 *curval++ = NULL;
17504
17505 write_reg_contents_lst(regname, lstval, -1,
17506 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017507free_lstval:
17508 while (curallocval > allocval)
17509 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017510 vim_free(lstval);
17511 }
17512 else
17513 {
17514 strval = get_tv_string_chk(&argvars[1]);
17515 if (strval == NULL)
17516 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017517 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521}
17522
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017523/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017524 * "settabvar()" function
17525 */
17526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017527f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017528{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017529#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017530 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017531 tabpage_T *tp;
17532#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017533 char_u *varname, *tabvarname;
17534 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017535
17536 rettv->vval.v_number = 0;
17537
17538 if (check_restricted() || check_secure())
17539 return;
17540
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017541#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017542 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017543#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017544 varname = get_tv_string_chk(&argvars[1]);
17545 varp = &argvars[2];
17546
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017547 if (varname != NULL && varp != NULL
17548#ifdef FEAT_WINDOWS
17549 && tp != NULL
17550#endif
17551 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017552 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017553#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017554 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017555 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017556#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017557
17558 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17559 if (tabvarname != NULL)
17560 {
17561 STRCPY(tabvarname, "t:");
17562 STRCPY(tabvarname + 2, varname);
17563 set_var(tabvarname, varp, TRUE);
17564 vim_free(tabvarname);
17565 }
17566
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017567#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017568 /* Restore current tabpage */
17569 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017570 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017571#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017572 }
17573}
17574
17575/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017576 * "settabwinvar()" function
17577 */
17578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017579f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017580{
17581 setwinvar(argvars, rettv, 1);
17582}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583
17584/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017585 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017588f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017590 setwinvar(argvars, rettv, 0);
17591}
17592
17593/*
17594 * "setwinvar()" and "settabwinvar()" functions
17595 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017596
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017598setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017599{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 win_T *win;
17601#ifdef FEAT_WINDOWS
17602 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017603 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017604 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605#endif
17606 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017607 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017609 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610
17611 if (check_restricted() || check_secure())
17612 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017613
17614#ifdef FEAT_WINDOWS
17615 if (off == 1)
17616 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17617 else
17618 tp = curtab;
17619#endif
17620 win = find_win_by_nr(&argvars[off], tp);
17621 varname = get_tv_string_chk(&argvars[off + 1]);
17622 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623
17624 if (win != NULL && varname != NULL && varp != NULL)
17625 {
17626#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017627 need_switch_win = !(tp == curtab && win == curwin);
17628 if (!need_switch_win
17629 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017632 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017634 long numval;
17635 char_u *strval;
17636 int error = FALSE;
17637
17638 ++varname;
17639 numval = get_tv_number_chk(varp, &error);
17640 strval = get_tv_string_buf_chk(varp, nbuf);
17641 if (!error && strval != NULL)
17642 set_option_value(varname, numval, strval, OPT_LOCAL);
17643 }
17644 else
17645 {
17646 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17647 if (winvarname != NULL)
17648 {
17649 STRCPY(winvarname, "w:");
17650 STRCPY(winvarname + 2, varname);
17651 set_var(winvarname, varp, TRUE);
17652 vim_free(winvarname);
17653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 }
17655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017657 if (need_switch_win)
17658 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659#endif
17660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661}
17662
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017663#ifdef FEAT_CRYPT
17664/*
17665 * "sha256({string})" function
17666 */
17667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017668f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017669{
17670 char_u *p;
17671
17672 p = get_tv_string(&argvars[0]);
17673 rettv->vval.v_string = vim_strsave(
17674 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17675 rettv->v_type = VAR_STRING;
17676}
17677#endif /* FEAT_CRYPT */
17678
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017680 * "shellescape({string})" function
17681 */
17682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017683f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017684{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017685 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017686 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017687 rettv->v_type = VAR_STRING;
17688}
17689
17690/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017691 * shiftwidth() function
17692 */
17693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017694f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017695{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017696 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017697}
17698
17699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017700 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 */
17702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017703f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017705 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706
Bram Moolenaar0d660222005-01-07 21:51:51 +000017707 p = get_tv_string(&argvars[0]);
17708 rettv->vval.v_string = vim_strsave(p);
17709 simplify_filename(rettv->vval.v_string); /* simplify in place */
17710 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711}
17712
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017713#ifdef FEAT_FLOAT
17714/*
17715 * "sin()" function
17716 */
17717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017718f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017719{
17720 float_T f;
17721
17722 rettv->v_type = VAR_FLOAT;
17723 if (get_float_arg(argvars, &f) == OK)
17724 rettv->vval.v_float = sin(f);
17725 else
17726 rettv->vval.v_float = 0.0;
17727}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017728
17729/*
17730 * "sinh()" function
17731 */
17732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017733f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017734{
17735 float_T f;
17736
17737 rettv->v_type = VAR_FLOAT;
17738 if (get_float_arg(argvars, &f) == OK)
17739 rettv->vval.v_float = sinh(f);
17740 else
17741 rettv->vval.v_float = 0.0;
17742}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017743#endif
17744
Bram Moolenaar0d660222005-01-07 21:51:51 +000017745static int
17746#ifdef __BORLANDC__
17747 _RTLENTRYF
17748#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017749 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017750static int
17751#ifdef __BORLANDC__
17752 _RTLENTRYF
17753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017754 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017755
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017756/* struct used in the array that's given to qsort() */
17757typedef struct
17758{
17759 listitem_T *item;
17760 int idx;
17761} sortItem_T;
17762
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017764static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017765static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017766#ifdef FEAT_FLOAT
17767static int item_compare_float;
17768#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017769static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017770static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017772static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017773static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017774#define ITEM_COMPARE_FAIL 999
17775
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017777 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017779 static int
17780#ifdef __BORLANDC__
17781_RTLENTRYF
17782#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017783item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017785 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017786 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017787 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017788 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017789 int res;
17790 char_u numbuf1[NUMBUFLEN];
17791 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017793 si1 = (sortItem_T *)s1;
17794 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017795 tv1 = &si1->item->li_tv;
17796 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017797
17798 if (item_compare_numbers)
17799 {
17800 long v1 = get_tv_number(tv1);
17801 long v2 = get_tv_number(tv2);
17802
17803 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17804 }
17805
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017806#ifdef FEAT_FLOAT
17807 if (item_compare_float)
17808 {
17809 float_T v1 = get_tv_float(tv1);
17810 float_T v2 = get_tv_float(tv2);
17811
17812 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17813 }
17814#endif
17815
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017816 /* tv2string() puts quotes around a string and allocates memory. Don't do
17817 * that for string variables. Use a single quote when comparing with a
17818 * non-string to do what the docs promise. */
17819 if (tv1->v_type == VAR_STRING)
17820 {
17821 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17822 p1 = (char_u *)"'";
17823 else
17824 p1 = tv1->vval.v_string;
17825 }
17826 else
17827 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17828 if (tv2->v_type == VAR_STRING)
17829 {
17830 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17831 p2 = (char_u *)"'";
17832 else
17833 p2 = tv2->vval.v_string;
17834 }
17835 else
17836 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017837 if (p1 == NULL)
17838 p1 = (char_u *)"";
17839 if (p2 == NULL)
17840 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017841 if (!item_compare_numeric)
17842 {
17843 if (item_compare_ic)
17844 res = STRICMP(p1, p2);
17845 else
17846 res = STRCMP(p1, p2);
17847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017849 {
17850 double n1, n2;
17851 n1 = strtod((char *)p1, (char **)&p1);
17852 n2 = strtod((char *)p2, (char **)&p2);
17853 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17854 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017855
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017856 /* When the result would be zero, compare the item indexes. Makes the
17857 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017858 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017859 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017860
Bram Moolenaar0d660222005-01-07 21:51:51 +000017861 vim_free(tofree1);
17862 vim_free(tofree2);
17863 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864}
17865
17866 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017867#ifdef __BORLANDC__
17868_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017870item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017871{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017872 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017873 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017874 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017875 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017876 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017878 /* shortcut after failure in previous call; compare all items equal */
17879 if (item_compare_func_err)
17880 return 0;
17881
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017882 si1 = (sortItem_T *)s1;
17883 si2 = (sortItem_T *)s2;
17884
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017885 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017886 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017887 copy_tv(&si1->item->li_tv, &argv[0]);
17888 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017889
17890 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017891 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017892 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17893 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017894 clear_tv(&argv[0]);
17895 clear_tv(&argv[1]);
17896
17897 if (res == FAIL)
17898 res = ITEM_COMPARE_FAIL;
17899 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017900 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17901 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017902 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017903 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017904
17905 /* When the result would be zero, compare the pointers themselves. Makes
17906 * the sort stable. */
17907 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017908 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017909
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 return res;
17911}
17912
17913/*
17914 * "sort({list})" function
17915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017917do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918{
Bram Moolenaar33570922005-01-25 22:26:29 +000017919 list_T *l;
17920 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017921 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017922 long len;
17923 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924
Bram Moolenaar0d660222005-01-07 21:51:51 +000017925 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017926 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 else
17928 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017930 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017931 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17932 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017933 return;
17934 rettv->vval.v_list = l;
17935 rettv->v_type = VAR_LIST;
17936 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938 len = list_len(l);
17939 if (len <= 1)
17940 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941
Bram Moolenaar0d660222005-01-07 21:51:51 +000017942 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017943 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017944 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017945#ifdef FEAT_FLOAT
17946 item_compare_float = FALSE;
17947#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017948 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017949 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017950 if (argvars[1].v_type != VAR_UNKNOWN)
17951 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017952 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017954 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017955 else
17956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017957 int error = FALSE;
17958
17959 i = get_tv_number_chk(&argvars[1], &error);
17960 if (error)
17961 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017962 if (i == 1)
17963 item_compare_ic = TRUE;
17964 else
17965 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017966 if (item_compare_func != NULL)
17967 {
17968 if (STRCMP(item_compare_func, "n") == 0)
17969 {
17970 item_compare_func = NULL;
17971 item_compare_numeric = TRUE;
17972 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017973 else if (STRCMP(item_compare_func, "N") == 0)
17974 {
17975 item_compare_func = NULL;
17976 item_compare_numbers = TRUE;
17977 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017978#ifdef FEAT_FLOAT
17979 else if (STRCMP(item_compare_func, "f") == 0)
17980 {
17981 item_compare_func = NULL;
17982 item_compare_float = TRUE;
17983 }
17984#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017985 else if (STRCMP(item_compare_func, "i") == 0)
17986 {
17987 item_compare_func = NULL;
17988 item_compare_ic = TRUE;
17989 }
17990 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017991 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017992
17993 if (argvars[2].v_type != VAR_UNKNOWN)
17994 {
17995 /* optional third argument: {dict} */
17996 if (argvars[2].v_type != VAR_DICT)
17997 {
17998 EMSG(_(e_dictreq));
17999 return;
18000 }
18001 item_compare_selfdict = argvars[2].vval.v_dict;
18002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018006 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018007 if (ptrs == NULL)
18008 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009
Bram Moolenaar327aa022014-03-25 18:24:23 +010018010 i = 0;
18011 if (sort)
18012 {
18013 /* sort(): ptrs will be the list to sort */
18014 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018015 {
18016 ptrs[i].item = li;
18017 ptrs[i].idx = i;
18018 ++i;
18019 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018020
18021 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018022 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018023 /* test the compare function */
18024 if (item_compare_func != NULL
18025 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018026 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018027 EMSG(_("E702: Sort compare function failed"));
18028 else
18029 {
18030 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018031 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018032 item_compare_func == NULL ? item_compare : item_compare2);
18033
18034 if (!item_compare_func_err)
18035 {
18036 /* Clear the List and append the items in sorted order. */
18037 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18038 l->lv_len = 0;
18039 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018040 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018041 }
18042 }
18043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018045 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018046 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018047
18048 /* f_uniq(): ptrs will be a stack of items to remove */
18049 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018050 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018051 item_compare_func_ptr = item_compare_func
18052 ? item_compare2 : item_compare;
18053
18054 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18055 li = li->li_next)
18056 {
18057 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18058 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018059 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018060 if (item_compare_func_err)
18061 {
18062 EMSG(_("E882: Uniq compare function failed"));
18063 break;
18064 }
18065 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018067 if (!item_compare_func_err)
18068 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018069 while (--i >= 0)
18070 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018071 li = ptrs[i].item->li_next;
18072 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018073 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018074 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018075 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018076 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018077 list_fix_watch(l, li);
18078 listitem_free(li);
18079 l->lv_len--;
18080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018081 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018082 }
18083
18084 vim_free(ptrs);
18085 }
18086}
18087
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018088/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018089 * "sort({list})" function
18090 */
18091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018092f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018093{
18094 do_sort_uniq(argvars, rettv, TRUE);
18095}
18096
18097/*
18098 * "uniq({list})" function
18099 */
18100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018101f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018102{
18103 do_sort_uniq(argvars, rettv, FALSE);
18104}
18105
18106/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018107 * "soundfold({word})" function
18108 */
18109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018110f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018111{
18112 char_u *s;
18113
18114 rettv->v_type = VAR_STRING;
18115 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018116#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018117 rettv->vval.v_string = eval_soundfold(s);
18118#else
18119 rettv->vval.v_string = vim_strsave(s);
18120#endif
18121}
18122
18123/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018124 * "spellbadword()" function
18125 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018127f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018128{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018129 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018130 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018131 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018132
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018133 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018134 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018135
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018136#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018137 if (argvars[0].v_type == VAR_UNKNOWN)
18138 {
18139 /* Find the start and length of the badly spelled word. */
18140 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18141 if (len != 0)
18142 word = ml_get_cursor();
18143 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018144 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018145 {
18146 char_u *str = get_tv_string_chk(&argvars[0]);
18147 int capcol = -1;
18148
18149 if (str != NULL)
18150 {
18151 /* Check the argument for spelling. */
18152 while (*str != NUL)
18153 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018154 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018155 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018156 {
18157 word = str;
18158 break;
18159 }
18160 str += len;
18161 }
18162 }
18163 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018164#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018165
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018166 list_append_string(rettv->vval.v_list, word, len);
18167 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018168 attr == HLF_SPB ? "bad" :
18169 attr == HLF_SPR ? "rare" :
18170 attr == HLF_SPL ? "local" :
18171 attr == HLF_SPC ? "caps" :
18172 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018173}
18174
18175/*
18176 * "spellsuggest()" function
18177 */
18178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018179f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018180{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018181#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018182 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018183 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018184 int maxcount;
18185 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018186 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018187 listitem_T *li;
18188 int need_capital = FALSE;
18189#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018190
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018191 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018192 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018193
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018194#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018195 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018196 {
18197 str = get_tv_string(&argvars[0]);
18198 if (argvars[1].v_type != VAR_UNKNOWN)
18199 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018200 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018201 if (maxcount <= 0)
18202 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018203 if (argvars[2].v_type != VAR_UNKNOWN)
18204 {
18205 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18206 if (typeerr)
18207 return;
18208 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018209 }
18210 else
18211 maxcount = 25;
18212
Bram Moolenaar4770d092006-01-12 23:22:24 +000018213 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018214
18215 for (i = 0; i < ga.ga_len; ++i)
18216 {
18217 str = ((char_u **)ga.ga_data)[i];
18218
18219 li = listitem_alloc();
18220 if (li == NULL)
18221 vim_free(str);
18222 else
18223 {
18224 li->li_tv.v_type = VAR_STRING;
18225 li->li_tv.v_lock = 0;
18226 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018227 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018228 }
18229 }
18230 ga_clear(&ga);
18231 }
18232#endif
18233}
18234
Bram Moolenaar0d660222005-01-07 21:51:51 +000018235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018236f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237{
18238 char_u *str;
18239 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018240 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018241 regmatch_T regmatch;
18242 char_u patbuf[NUMBUFLEN];
18243 char_u *save_cpo;
18244 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018246 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018247 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248
18249 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18250 save_cpo = p_cpo;
18251 p_cpo = (char_u *)"";
18252
18253 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018254 if (argvars[1].v_type != VAR_UNKNOWN)
18255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018256 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18257 if (pat == NULL)
18258 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018259 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018260 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018261 }
18262 if (pat == NULL || *pat == NUL)
18263 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018265 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018267 if (typeerr)
18268 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269
Bram Moolenaar0d660222005-01-07 21:51:51 +000018270 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18271 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018274 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018275 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018276 if (*str == NUL)
18277 match = FALSE; /* empty item at the end */
18278 else
18279 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280 if (match)
18281 end = regmatch.startp[0];
18282 else
18283 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018284 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18285 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018286 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018287 if (list_append_string(rettv->vval.v_list, str,
18288 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290 }
18291 if (!match)
18292 break;
18293 /* Advance to just after the match. */
18294 if (regmatch.endp[0] > str)
18295 col = 0;
18296 else
18297 {
18298 /* Don't get stuck at the same match. */
18299#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018300 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018301#else
18302 col = 1;
18303#endif
18304 }
18305 str = regmatch.endp[0];
18306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307
Bram Moolenaar473de612013-06-08 18:19:48 +020018308 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310
Bram Moolenaar0d660222005-01-07 21:51:51 +000018311 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312}
18313
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018314#ifdef FEAT_FLOAT
18315/*
18316 * "sqrt()" function
18317 */
18318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018319f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018320{
18321 float_T f;
18322
18323 rettv->v_type = VAR_FLOAT;
18324 if (get_float_arg(argvars, &f) == OK)
18325 rettv->vval.v_float = sqrt(f);
18326 else
18327 rettv->vval.v_float = 0.0;
18328}
18329
18330/*
18331 * "str2float()" function
18332 */
18333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018334f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018335{
18336 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18337
18338 if (*p == '+')
18339 p = skipwhite(p + 1);
18340 (void)string2float(p, &rettv->vval.v_float);
18341 rettv->v_type = VAR_FLOAT;
18342}
18343#endif
18344
Bram Moolenaar2c932302006-03-18 21:42:09 +000018345/*
18346 * "str2nr()" function
18347 */
18348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018349f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018350{
18351 int base = 10;
18352 char_u *p;
18353 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018354 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018355
18356 if (argvars[1].v_type != VAR_UNKNOWN)
18357 {
18358 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018359 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018360 {
18361 EMSG(_(e_invarg));
18362 return;
18363 }
18364 }
18365
18366 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018367 if (*p == '+')
18368 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018369 switch (base)
18370 {
18371 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18372 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18373 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18374 default: what = 0;
18375 }
18376 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018377 rettv->vval.v_number = n;
18378}
18379
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380#ifdef HAVE_STRFTIME
18381/*
18382 * "strftime({format}[, {time}])" function
18383 */
18384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386{
18387 char_u result_buf[256];
18388 struct tm *curtime;
18389 time_t seconds;
18390 char_u *p;
18391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018394 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018395 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 seconds = time(NULL);
18397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018398 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 curtime = localtime(&seconds);
18400 /* MSVC returns NULL for an invalid value of seconds. */
18401 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018402 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 else
18404 {
18405# ifdef FEAT_MBYTE
18406 vimconv_T conv;
18407 char_u *enc;
18408
18409 conv.vc_type = CONV_NONE;
18410 enc = enc_locale();
18411 convert_setup(&conv, p_enc, enc);
18412 if (conv.vc_type != CONV_NONE)
18413 p = string_convert(&conv, p, NULL);
18414# endif
18415 if (p != NULL)
18416 (void)strftime((char *)result_buf, sizeof(result_buf),
18417 (char *)p, curtime);
18418 else
18419 result_buf[0] = NUL;
18420
18421# ifdef FEAT_MBYTE
18422 if (conv.vc_type != CONV_NONE)
18423 vim_free(p);
18424 convert_setup(&conv, enc, p_enc);
18425 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 else
18428# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430
18431# ifdef FEAT_MBYTE
18432 /* Release conversion descriptors */
18433 convert_setup(&conv, NULL, NULL);
18434 vim_free(enc);
18435# endif
18436 }
18437}
18438#endif
18439
18440/*
18441 * "stridx()" function
18442 */
18443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018444f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445{
18446 char_u buf[NUMBUFLEN];
18447 char_u *needle;
18448 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018449 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018453 needle = get_tv_string_chk(&argvars[1]);
18454 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018455 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018456 if (needle == NULL || haystack == NULL)
18457 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458
Bram Moolenaar33570922005-01-25 22:26:29 +000018459 if (argvars[2].v_type != VAR_UNKNOWN)
18460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018461 int error = FALSE;
18462
18463 start_idx = get_tv_number_chk(&argvars[2], &error);
18464 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018466 if (start_idx >= 0)
18467 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018468 }
18469
18470 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18471 if (pos != NULL)
18472 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473}
18474
18475/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018476 * "string()" function
18477 */
18478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018479f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018480{
18481 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018482 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018485 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018486 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018487 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018488 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489}
18490
18491/*
18492 * "strlen()" function
18493 */
18494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018495f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018497 rettv->vval.v_number = (varnumber_T)(STRLEN(
18498 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499}
18500
18501/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018502 * "strchars()" function
18503 */
18504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018505f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018506{
18507 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018508 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018509#ifdef FEAT_MBYTE
18510 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018511 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018512#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018513
18514 if (argvars[1].v_type != VAR_UNKNOWN)
18515 skipcc = get_tv_number_chk(&argvars[1], NULL);
18516 if (skipcc < 0 || skipcc > 1)
18517 EMSG(_(e_invarg));
18518 else
18519 {
18520#ifdef FEAT_MBYTE
18521 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18522 while (*s != NUL)
18523 {
18524 func_mb_ptr2char_adv(&s);
18525 ++len;
18526 }
18527 rettv->vval.v_number = len;
18528#else
18529 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18530#endif
18531 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018532}
18533
18534/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018535 * "strdisplaywidth()" function
18536 */
18537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018538f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018539{
18540 char_u *s = get_tv_string(&argvars[0]);
18541 int col = 0;
18542
18543 if (argvars[1].v_type != VAR_UNKNOWN)
18544 col = get_tv_number(&argvars[1]);
18545
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018546 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018547}
18548
18549/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018550 * "strwidth()" function
18551 */
18552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018553f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018554{
18555 char_u *s = get_tv_string(&argvars[0]);
18556
18557 rettv->vval.v_number = (varnumber_T)(
18558#ifdef FEAT_MBYTE
18559 mb_string2cells(s, -1)
18560#else
18561 STRLEN(s)
18562#endif
18563 );
18564}
18565
18566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 * "strpart()" function
18568 */
18569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018570f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571{
18572 char_u *p;
18573 int n;
18574 int len;
18575 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018576 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018578 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 slen = (int)STRLEN(p);
18580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018581 n = get_tv_number_chk(&argvars[1], &error);
18582 if (error)
18583 len = 0;
18584 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018585 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 else
18587 len = slen - n; /* default len: all bytes that are available. */
18588
18589 /*
18590 * Only return the overlap between the specified part and the actual
18591 * string.
18592 */
18593 if (n < 0)
18594 {
18595 len += n;
18596 n = 0;
18597 }
18598 else if (n > slen)
18599 n = slen;
18600 if (len < 0)
18601 len = 0;
18602 else if (n + len > slen)
18603 len = slen - n;
18604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018605 rettv->v_type = VAR_STRING;
18606 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607}
18608
18609/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610 * "strridx()" function
18611 */
18612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018613f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614{
18615 char_u buf[NUMBUFLEN];
18616 char_u *needle;
18617 char_u *haystack;
18618 char_u *rest;
18619 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018620 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018622 needle = get_tv_string_chk(&argvars[1]);
18623 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018624
18625 rettv->vval.v_number = -1;
18626 if (needle == NULL || haystack == NULL)
18627 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018628
18629 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018630 if (argvars[2].v_type != VAR_UNKNOWN)
18631 {
18632 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018633 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018634 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018635 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018636 }
18637 else
18638 end_idx = haystack_len;
18639
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018641 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018642 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018643 lastmatch = haystack + end_idx;
18644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018646 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647 for (rest = haystack; *rest != '\0'; ++rest)
18648 {
18649 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018650 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018651 break;
18652 lastmatch = rest;
18653 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018655
18656 if (lastmatch == NULL)
18657 rettv->vval.v_number = -1;
18658 else
18659 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18660}
18661
18662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 * "strtrans()" function
18664 */
18665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018666f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018668 rettv->v_type = VAR_STRING;
18669 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670}
18671
18672/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018673 * "submatch()" function
18674 */
18675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018676f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677{
Bram Moolenaar41571762014-04-02 19:00:58 +020018678 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018679 int no;
18680 int retList = 0;
18681
18682 no = (int)get_tv_number_chk(&argvars[0], &error);
18683 if (error)
18684 return;
18685 error = FALSE;
18686 if (argvars[1].v_type != VAR_UNKNOWN)
18687 retList = get_tv_number_chk(&argvars[1], &error);
18688 if (error)
18689 return;
18690
18691 if (retList == 0)
18692 {
18693 rettv->v_type = VAR_STRING;
18694 rettv->vval.v_string = reg_submatch(no);
18695 }
18696 else
18697 {
18698 rettv->v_type = VAR_LIST;
18699 rettv->vval.v_list = reg_submatch_list(no);
18700 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018701}
18702
18703/*
18704 * "substitute()" function
18705 */
18706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018707f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018708{
18709 char_u patbuf[NUMBUFLEN];
18710 char_u subbuf[NUMBUFLEN];
18711 char_u flagsbuf[NUMBUFLEN];
18712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018713 char_u *str = get_tv_string_chk(&argvars[0]);
18714 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18715 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18716 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18717
Bram Moolenaar0d660222005-01-07 21:51:51 +000018718 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018719 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18720 rettv->vval.v_string = NULL;
18721 else
18722 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018723}
18724
18725/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018726 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018729f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730{
18731 int id = 0;
18732#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018733 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 long col;
18735 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018736 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018738 lnum = get_tv_lnum(argvars); /* -1 on type error */
18739 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18740 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018742 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018743 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018744 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745#endif
18746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748}
18749
18750/*
18751 * "synIDattr(id, what [, mode])" function
18752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018754f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755{
18756 char_u *p = NULL;
18757#ifdef FEAT_SYN_HL
18758 int id;
18759 char_u *what;
18760 char_u *mode;
18761 char_u modebuf[NUMBUFLEN];
18762 int modec;
18763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018764 id = get_tv_number(&argvars[0]);
18765 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018766 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018770 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 modec = 0; /* replace invalid with current */
18772 }
18773 else
18774 {
18775#ifdef FEAT_GUI
18776 if (gui.in_use)
18777 modec = 'g';
18778 else
18779#endif
18780 if (t_colors > 1)
18781 modec = 'c';
18782 else
18783 modec = 't';
18784 }
18785
18786
18787 switch (TOLOWER_ASC(what[0]))
18788 {
18789 case 'b':
18790 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18791 p = highlight_color(id, what, modec);
18792 else /* bold */
18793 p = highlight_has_attr(id, HL_BOLD, modec);
18794 break;
18795
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018796 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 p = highlight_color(id, what, modec);
18798 break;
18799
18800 case 'i':
18801 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18802 p = highlight_has_attr(id, HL_INVERSE, modec);
18803 else /* italic */
18804 p = highlight_has_attr(id, HL_ITALIC, modec);
18805 break;
18806
18807 case 'n': /* name */
18808 p = get_highlight_name(NULL, id - 1);
18809 break;
18810
18811 case 'r': /* reverse */
18812 p = highlight_has_attr(id, HL_INVERSE, modec);
18813 break;
18814
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018815 case 's':
18816 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18817 p = highlight_color(id, what, modec);
18818 else /* standout */
18819 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 break;
18821
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018822 case 'u':
18823 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18824 /* underline */
18825 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18826 else
18827 /* undercurl */
18828 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 break;
18830 }
18831
18832 if (p != NULL)
18833 p = vim_strsave(p);
18834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018835 rettv->v_type = VAR_STRING;
18836 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837}
18838
18839/*
18840 * "synIDtrans(id)" function
18841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018843f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844{
18845 int id;
18846
18847#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849
18850 if (id > 0)
18851 id = syn_get_final_id(id);
18852 else
18853#endif
18854 id = 0;
18855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018856 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857}
18858
18859/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018860 * "synconcealed(lnum, col)" function
18861 */
18862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018863f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018864{
18865#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18866 long lnum;
18867 long col;
18868 int syntax_flags = 0;
18869 int cchar;
18870 int matchid = 0;
18871 char_u str[NUMBUFLEN];
18872#endif
18873
18874 rettv->v_type = VAR_LIST;
18875 rettv->vval.v_list = NULL;
18876
18877#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18878 lnum = get_tv_lnum(argvars); /* -1 on type error */
18879 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18880
18881 vim_memset(str, NUL, sizeof(str));
18882
18883 if (rettv_list_alloc(rettv) != FAIL)
18884 {
18885 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18886 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18887 && curwin->w_p_cole > 0)
18888 {
18889 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18890 syntax_flags = get_syntax_info(&matchid);
18891
18892 /* get the conceal character */
18893 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18894 {
18895 cchar = syn_get_sub_char();
18896 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18897 cchar = lcs_conceal;
18898 if (cchar != NUL)
18899 {
18900# ifdef FEAT_MBYTE
18901 if (has_mbyte)
18902 (*mb_char2bytes)(cchar, str);
18903 else
18904# endif
18905 str[0] = cchar;
18906 }
18907 }
18908 }
18909
18910 list_append_number(rettv->vval.v_list,
18911 (syntax_flags & HL_CONCEAL) != 0);
18912 /* -1 to auto-determine strlen */
18913 list_append_string(rettv->vval.v_list, str, -1);
18914 list_append_number(rettv->vval.v_list, matchid);
18915 }
18916#endif
18917}
18918
18919/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018920 * "synstack(lnum, col)" function
18921 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018923f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018924{
18925#ifdef FEAT_SYN_HL
18926 long lnum;
18927 long col;
18928 int i;
18929 int id;
18930#endif
18931
18932 rettv->v_type = VAR_LIST;
18933 rettv->vval.v_list = NULL;
18934
18935#ifdef FEAT_SYN_HL
18936 lnum = get_tv_lnum(argvars); /* -1 on type error */
18937 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18938
18939 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018940 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018941 && rettv_list_alloc(rettv) != FAIL)
18942 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018943 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018944 for (i = 0; ; ++i)
18945 {
18946 id = syn_get_stack_item(i);
18947 if (id < 0)
18948 break;
18949 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18950 break;
18951 }
18952 }
18953#endif
18954}
18955
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018957get_cmd_output_as_rettv(
18958 typval_T *argvars,
18959 typval_T *rettv,
18960 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018962 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018964 char_u *infile = NULL;
18965 char_u buf[NUMBUFLEN];
18966 int err = FALSE;
18967 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018968 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018969 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018971 rettv->v_type = VAR_STRING;
18972 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018973 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018974 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018975
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018976 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018977 {
18978 /*
18979 * Write the string to a temp file, to be used for input of the shell
18980 * command.
18981 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018982 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018983 {
18984 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018985 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018986 }
18987
18988 fd = mch_fopen((char *)infile, WRITEBIN);
18989 if (fd == NULL)
18990 {
18991 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018992 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018993 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018994 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018995 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018996 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18997 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018998 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018999 else
19000 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019001 size_t len;
19002
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019003 p = get_tv_string_buf_chk(&argvars[1], buf);
19004 if (p == NULL)
19005 {
19006 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019007 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019008 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019009 len = STRLEN(p);
19010 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019011 err = TRUE;
19012 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019013 if (fclose(fd) != 0)
19014 err = TRUE;
19015 if (err)
19016 {
19017 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019018 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019019 }
19020 }
19021
Bram Moolenaar52a72462014-08-29 15:53:52 +020019022 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19023 * echoes typeahead, that messes up the display. */
19024 if (!msg_silent)
19025 flags += SHELL_COOKED;
19026
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019027 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019029 int len;
19030 listitem_T *li;
19031 char_u *s = NULL;
19032 char_u *start;
19033 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019034 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035
Bram Moolenaar52a72462014-08-29 15:53:52 +020019036 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019037 if (res == NULL)
19038 goto errret;
19039
19040 list = list_alloc();
19041 if (list == NULL)
19042 goto errret;
19043
19044 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019046 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019047 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019048 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019049 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019050
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019051 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019052 if (s == NULL)
19053 goto errret;
19054
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019055 for (p = s; start < end; ++p, ++start)
19056 *p = *start == NUL ? NL : *start;
19057 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019058
19059 li = listitem_alloc();
19060 if (li == NULL)
19061 {
19062 vim_free(s);
19063 goto errret;
19064 }
19065 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019066 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019067 li->li_tv.vval.v_string = s;
19068 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019070
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019071 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019072 rettv->v_type = VAR_LIST;
19073 rettv->vval.v_list = list;
19074 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019076 else
19077 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019078 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019079#ifdef USE_CR
19080 /* translate <CR> into <NL> */
19081 if (res != NULL)
19082 {
19083 char_u *s;
19084
19085 for (s = res; *s; ++s)
19086 {
19087 if (*s == CAR)
19088 *s = NL;
19089 }
19090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091#else
19092# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019093 /* translate <CR><NL> into <NL> */
19094 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019096 char_u *s, *d;
19097
19098 d = res;
19099 for (s = res; *s; ++s)
19100 {
19101 if (s[0] == CAR && s[1] == NL)
19102 ++s;
19103 *d++ = *s;
19104 }
19105 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107# endif
19108#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019109 rettv->vval.v_string = res;
19110 res = NULL;
19111 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019112
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019113errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019114 if (infile != NULL)
19115 {
19116 mch_remove(infile);
19117 vim_free(infile);
19118 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019119 if (res != NULL)
19120 vim_free(res);
19121 if (list != NULL)
19122 list_free(list, TRUE);
19123}
19124
19125/*
19126 * "system()" function
19127 */
19128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019129f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019130{
19131 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19132}
19133
19134/*
19135 * "systemlist()" function
19136 */
19137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019138f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019139{
19140 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141}
19142
19143/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019144 * "tabpagebuflist()" function
19145 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019147f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019148{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019149#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019150 tabpage_T *tp;
19151 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019152
19153 if (argvars[0].v_type == VAR_UNKNOWN)
19154 wp = firstwin;
19155 else
19156 {
19157 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19158 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019159 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019160 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019161 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019162 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019163 for (; wp != NULL; wp = wp->w_next)
19164 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019165 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019166 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019167 }
19168#endif
19169}
19170
19171
19172/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019173 * "tabpagenr()" function
19174 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019176f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019177{
19178 int nr = 1;
19179#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019180 char_u *arg;
19181
19182 if (argvars[0].v_type != VAR_UNKNOWN)
19183 {
19184 arg = get_tv_string_chk(&argvars[0]);
19185 nr = 0;
19186 if (arg != NULL)
19187 {
19188 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019189 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019190 else
19191 EMSG2(_(e_invexpr2), arg);
19192 }
19193 }
19194 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019195 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019196#endif
19197 rettv->vval.v_number = nr;
19198}
19199
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019200
19201#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019202static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019203
19204/*
19205 * Common code for tabpagewinnr() and winnr().
19206 */
19207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019208get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019209{
19210 win_T *twin;
19211 int nr = 1;
19212 win_T *wp;
19213 char_u *arg;
19214
19215 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19216 if (argvar->v_type != VAR_UNKNOWN)
19217 {
19218 arg = get_tv_string_chk(argvar);
19219 if (arg == NULL)
19220 nr = 0; /* type error; errmsg already given */
19221 else if (STRCMP(arg, "$") == 0)
19222 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19223 else if (STRCMP(arg, "#") == 0)
19224 {
19225 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19226 if (twin == NULL)
19227 nr = 0;
19228 }
19229 else
19230 {
19231 EMSG2(_(e_invexpr2), arg);
19232 nr = 0;
19233 }
19234 }
19235
19236 if (nr > 0)
19237 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19238 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019239 {
19240 if (wp == NULL)
19241 {
19242 /* didn't find it in this tabpage */
19243 nr = 0;
19244 break;
19245 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019246 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019247 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019248 return nr;
19249}
19250#endif
19251
19252/*
19253 * "tabpagewinnr()" function
19254 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019256f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019257{
19258 int nr = 1;
19259#ifdef FEAT_WINDOWS
19260 tabpage_T *tp;
19261
19262 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19263 if (tp == NULL)
19264 nr = 0;
19265 else
19266 nr = get_winnr(tp, &argvars[1]);
19267#endif
19268 rettv->vval.v_number = nr;
19269}
19270
19271
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019272/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019273 * "tagfiles()" function
19274 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019276f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019277{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019278 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019279 tagname_T tn;
19280 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019281
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019282 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019283 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019284 fname = alloc(MAXPATHL);
19285 if (fname == NULL)
19286 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019287
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019288 for (first = TRUE; ; first = FALSE)
19289 if (get_tagfname(&tn, first, fname) == FAIL
19290 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019291 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019292 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019293 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019294}
19295
19296/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019297 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019298 */
19299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019300f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019301{
19302 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019303
19304 tag_pattern = get_tv_string(&argvars[0]);
19305
19306 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019307 if (*tag_pattern == NUL)
19308 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019310 if (rettv_list_alloc(rettv) == OK)
19311 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019312}
19313
19314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 * "tempname()" function
19316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019318f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319{
19320 static int x = 'A';
19321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019323 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324
19325 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19326 * names. Skip 'I' and 'O', they are used for shell redirection. */
19327 do
19328 {
19329 if (x == 'Z')
19330 x = '0';
19331 else if (x == '9')
19332 x = 'A';
19333 else
19334 {
19335#ifdef EBCDIC
19336 if (x == 'I')
19337 x = 'J';
19338 else if (x == 'R')
19339 x = 'S';
19340 else
19341#endif
19342 ++x;
19343 }
19344 } while (x == 'I' || x == 'O');
19345}
19346
19347/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019348 * "test(list)" function: Just checking the walls...
19349 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019351f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019352{
19353 /* Used for unit testing. Change the code below to your liking. */
19354#if 0
19355 listitem_T *li;
19356 list_T *l;
19357 char_u *bad, *good;
19358
19359 if (argvars[0].v_type != VAR_LIST)
19360 return;
19361 l = argvars[0].vval.v_list;
19362 if (l == NULL)
19363 return;
19364 li = l->lv_first;
19365 if (li == NULL)
19366 return;
19367 bad = get_tv_string(&li->li_tv);
19368 li = li->li_next;
19369 if (li == NULL)
19370 return;
19371 good = get_tv_string(&li->li_tv);
19372 rettv->vval.v_number = test_edit_score(bad, good);
19373#endif
19374}
19375
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019376#ifdef FEAT_FLOAT
19377/*
19378 * "tan()" function
19379 */
19380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019381f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019382{
19383 float_T f;
19384
19385 rettv->v_type = VAR_FLOAT;
19386 if (get_float_arg(argvars, &f) == OK)
19387 rettv->vval.v_float = tan(f);
19388 else
19389 rettv->vval.v_float = 0.0;
19390}
19391
19392/*
19393 * "tanh()" function
19394 */
19395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019396f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019397{
19398 float_T f;
19399
19400 rettv->v_type = VAR_FLOAT;
19401 if (get_float_arg(argvars, &f) == OK)
19402 rettv->vval.v_float = tanh(f);
19403 else
19404 rettv->vval.v_float = 0.0;
19405}
19406#endif
19407
Bram Moolenaard52d9742005-08-21 22:20:28 +000019408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 * "tolower(string)" function
19410 */
19411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019412f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413{
19414 char_u *p;
19415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019416 p = vim_strsave(get_tv_string(&argvars[0]));
19417 rettv->v_type = VAR_STRING;
19418 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419
19420 if (p != NULL)
19421 while (*p != NUL)
19422 {
19423#ifdef FEAT_MBYTE
19424 int l;
19425
19426 if (enc_utf8)
19427 {
19428 int c, lc;
19429
19430 c = utf_ptr2char(p);
19431 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019432 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 /* TODO: reallocate string when byte count changes. */
19434 if (utf_char2len(lc) == l)
19435 utf_char2bytes(lc, p);
19436 p += l;
19437 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019438 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 p += l; /* skip multi-byte character */
19440 else
19441#endif
19442 {
19443 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19444 ++p;
19445 }
19446 }
19447}
19448
19449/*
19450 * "toupper(string)" function
19451 */
19452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019453f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019455 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019456 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457}
19458
19459/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019460 * "tr(string, fromstr, tostr)" function
19461 */
19462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019463f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019464{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019465 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019466 char_u *fromstr;
19467 char_u *tostr;
19468 char_u *p;
19469#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019470 int inlen;
19471 int fromlen;
19472 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019473 int idx;
19474 char_u *cpstr;
19475 int cplen;
19476 int first = TRUE;
19477#endif
19478 char_u buf[NUMBUFLEN];
19479 char_u buf2[NUMBUFLEN];
19480 garray_T ga;
19481
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019482 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019483 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19484 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019485
19486 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019487 rettv->v_type = VAR_STRING;
19488 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019489 if (fromstr == NULL || tostr == NULL)
19490 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019491 ga_init2(&ga, (int)sizeof(char), 80);
19492
19493#ifdef FEAT_MBYTE
19494 if (!has_mbyte)
19495#endif
19496 /* not multi-byte: fromstr and tostr must be the same length */
19497 if (STRLEN(fromstr) != STRLEN(tostr))
19498 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019499#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019500error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019501#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019502 EMSG2(_(e_invarg2), fromstr);
19503 ga_clear(&ga);
19504 return;
19505 }
19506
19507 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019508 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019509 {
19510#ifdef FEAT_MBYTE
19511 if (has_mbyte)
19512 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019513 inlen = (*mb_ptr2len)(in_str);
19514 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019515 cplen = inlen;
19516 idx = 0;
19517 for (p = fromstr; *p != NUL; p += fromlen)
19518 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019519 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019520 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019521 {
19522 for (p = tostr; *p != NUL; p += tolen)
19523 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019524 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019525 if (idx-- == 0)
19526 {
19527 cplen = tolen;
19528 cpstr = p;
19529 break;
19530 }
19531 }
19532 if (*p == NUL) /* tostr is shorter than fromstr */
19533 goto error;
19534 break;
19535 }
19536 ++idx;
19537 }
19538
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019539 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019540 {
19541 /* Check that fromstr and tostr have the same number of
19542 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019543 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019544 first = FALSE;
19545 for (p = tostr; *p != NUL; p += tolen)
19546 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019547 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019548 --idx;
19549 }
19550 if (idx != 0)
19551 goto error;
19552 }
19553
Bram Moolenaarcde88542015-08-11 19:14:00 +020019554 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019555 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019556 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019557
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019558 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019559 }
19560 else
19561#endif
19562 {
19563 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019564 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019565 if (p != NULL)
19566 ga_append(&ga, tostr[p - fromstr]);
19567 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019568 ga_append(&ga, *in_str);
19569 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019570 }
19571 }
19572
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019573 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019574 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019575 ga_append(&ga, NUL);
19576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019578}
19579
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019580#ifdef FEAT_FLOAT
19581/*
19582 * "trunc({float})" function
19583 */
19584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019585f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019586{
19587 float_T f;
19588
19589 rettv->v_type = VAR_FLOAT;
19590 if (get_float_arg(argvars, &f) == OK)
19591 /* trunc() is not in C90, use floor() or ceil() instead. */
19592 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19593 else
19594 rettv->vval.v_float = 0.0;
19595}
19596#endif
19597
Bram Moolenaar8299df92004-07-10 09:47:34 +000019598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 * "type(expr)" function
19600 */
19601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019602f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019604 int n;
19605
19606 switch (argvars[0].v_type)
19607 {
19608 case VAR_NUMBER: n = 0; break;
19609 case VAR_STRING: n = 1; break;
19610 case VAR_FUNC: n = 2; break;
19611 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019612 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019613#ifdef FEAT_FLOAT
19614 case VAR_FLOAT: n = 5; break;
19615#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019616 case VAR_SPECIAL:
19617 if (argvars[0].vval.v_number == VVAL_FALSE
19618 || argvars[0].vval.v_number == VVAL_TRUE)
19619 n = 6;
19620 else
19621 n = 7;
19622 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019623 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19624 }
19625 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626}
19627
19628/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019629 * "undofile(name)" function
19630 */
19631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019632f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019633{
19634 rettv->v_type = VAR_STRING;
19635#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019636 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019637 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019638
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019639 if (*fname == NUL)
19640 {
19641 /* If there is no file name there will be no undo file. */
19642 rettv->vval.v_string = NULL;
19643 }
19644 else
19645 {
19646 char_u *ffname = FullName_save(fname, FALSE);
19647
19648 if (ffname != NULL)
19649 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19650 vim_free(ffname);
19651 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019652 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019653#else
19654 rettv->vval.v_string = NULL;
19655#endif
19656}
19657
19658/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019659 * "undotree()" function
19660 */
19661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019662f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019663{
19664 if (rettv_dict_alloc(rettv) == OK)
19665 {
19666 dict_T *dict = rettv->vval.v_dict;
19667 list_T *list;
19668
Bram Moolenaar730cde92010-06-27 05:18:54 +020019669 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019670 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019671 dict_add_nr_str(dict, "save_last",
19672 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019673 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19674 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019675 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019676
19677 list = list_alloc();
19678 if (list != NULL)
19679 {
19680 u_eval_tree(curbuf->b_u_oldhead, list);
19681 dict_add_list(dict, "entries", list);
19682 }
19683 }
19684}
19685
19686/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019687 * "values(dict)" function
19688 */
19689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019690f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019691{
19692 dict_list(argvars, rettv, 1);
19693}
19694
19695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 * "virtcol(string)" function
19697 */
19698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019699f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700{
19701 colnr_T vcol = 0;
19702 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019703 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019705 fp = var2fpos(&argvars[0], FALSE, &fnum);
19706 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19707 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 {
19709 getvvcol(curwin, fp, NULL, NULL, &vcol);
19710 ++vcol;
19711 }
19712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019713 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714}
19715
19716/*
19717 * "visualmode()" function
19718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019720f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 char_u str[2];
19723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 str[0] = curbuf->b_visual_mode_eval;
19726 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019727 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728
19729 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019730 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732}
19733
19734/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019735 * "wildmenumode()" function
19736 */
19737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019738f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019739{
19740#ifdef FEAT_WILDMENU
19741 if (wild_menu_showing)
19742 rettv->vval.v_number = 1;
19743#endif
19744}
19745
19746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 * "winbufnr(nr)" function
19748 */
19749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019750f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751{
19752 win_T *wp;
19753
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019754 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019758 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759}
19760
19761/*
19762 * "wincol()" function
19763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019765f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766{
19767 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769}
19770
19771/*
19772 * "winheight(nr)" function
19773 */
19774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019775f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776{
19777 win_T *wp;
19778
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019779 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019781 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019783 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784}
19785
19786/*
19787 * "winline()" function
19788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019790f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791{
19792 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794}
19795
19796/*
19797 * "winnr()" function
19798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019800f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801{
19802 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019803
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019805 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019807 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808}
19809
19810/*
19811 * "winrestcmd()" function
19812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019814f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815{
19816#ifdef FEAT_WINDOWS
19817 win_T *wp;
19818 int winnr = 1;
19819 garray_T ga;
19820 char_u buf[50];
19821
19822 ga_init2(&ga, (int)sizeof(char), 70);
19823 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19824 {
19825 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19826 ga_concat(&ga, buf);
19827# ifdef FEAT_VERTSPLIT
19828 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19829 ga_concat(&ga, buf);
19830# endif
19831 ++winnr;
19832 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019833 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019839 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840}
19841
19842/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019843 * "winrestview()" function
19844 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019846f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019847{
19848 dict_T *dict;
19849
19850 if (argvars[0].v_type != VAR_DICT
19851 || (dict = argvars[0].vval.v_dict) == NULL)
19852 EMSG(_(e_invarg));
19853 else
19854 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019855 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19856 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19857 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19858 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019859#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019860 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19861 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019862#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019863 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19864 {
19865 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19866 curwin->w_set_curswant = FALSE;
19867 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019868
Bram Moolenaar82c25852014-05-28 16:47:16 +020019869 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19870 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019871#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019872 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19873 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019874#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019875 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19876 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19877 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19878 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019879
19880 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019881 win_new_height(curwin, curwin->w_height);
19882# ifdef FEAT_VERTSPLIT
19883 win_new_width(curwin, W_WIDTH(curwin));
19884# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019885 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019886
Bram Moolenaarb851a962014-10-31 15:45:52 +010019887 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019888 curwin->w_topline = 1;
19889 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19890 curwin->w_topline = curbuf->b_ml.ml_line_count;
19891#ifdef FEAT_DIFF
19892 check_topfill(curwin, TRUE);
19893#endif
19894 }
19895}
19896
19897/*
19898 * "winsaveview()" function
19899 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019901f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019902{
19903 dict_T *dict;
19904
Bram Moolenaara800b422010-06-27 01:15:55 +020019905 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019906 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019907 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019908
19909 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19910 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19911#ifdef FEAT_VIRTUALEDIT
19912 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19913#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019914 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019915 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19916
19917 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19918#ifdef FEAT_DIFF
19919 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19920#endif
19921 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19922 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19923}
19924
19925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 * "winwidth(nr)" function
19927 */
19928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019929f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930{
19931 win_T *wp;
19932
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019933 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 else
19937#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019938 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019940 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941#endif
19942}
19943
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019945 * "wordcount()" function
19946 */
19947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019948f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019949{
19950 if (rettv_dict_alloc(rettv) == FAIL)
19951 return;
19952 cursor_pos_info(rettv->vval.v_dict);
19953}
19954
19955/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019956 * Write list of strings to file
19957 */
19958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019959write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019960{
19961 listitem_T *li;
19962 int c;
19963 int ret = OK;
19964 char_u *s;
19965
19966 for (li = list->lv_first; li != NULL; li = li->li_next)
19967 {
19968 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19969 {
19970 if (*s == '\n')
19971 c = putc(NUL, fd);
19972 else
19973 c = putc(*s, fd);
19974 if (c == EOF)
19975 {
19976 ret = FAIL;
19977 break;
19978 }
19979 }
19980 if (!binary || li->li_next != NULL)
19981 if (putc('\n', fd) == EOF)
19982 {
19983 ret = FAIL;
19984 break;
19985 }
19986 if (ret == FAIL)
19987 {
19988 EMSG(_(e_write));
19989 break;
19990 }
19991 }
19992 return ret;
19993}
19994
19995/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019996 * "writefile()" function
19997 */
19998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019999f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020000{
20001 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020002 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020003 char_u *fname;
20004 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020005 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020006
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020007 if (check_restricted() || check_secure())
20008 return;
20009
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020010 if (argvars[0].v_type != VAR_LIST)
20011 {
20012 EMSG2(_(e_listarg), "writefile()");
20013 return;
20014 }
20015 if (argvars[0].vval.v_list == NULL)
20016 return;
20017
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020018 if (argvars[2].v_type != VAR_UNKNOWN)
20019 {
20020 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20021 binary = TRUE;
20022 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20023 append = TRUE;
20024 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020025
20026 /* Always open the file in binary mode, library functions have a mind of
20027 * their own about CR-LF conversion. */
20028 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020029 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20030 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020031 {
20032 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20033 ret = -1;
20034 }
20035 else
20036 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020037 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20038 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020039 fclose(fd);
20040 }
20041
20042 rettv->vval.v_number = ret;
20043}
20044
20045/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020046 * "xor(expr, expr)" function
20047 */
20048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020049f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020050{
20051 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20052 ^ get_tv_number_chk(&argvars[1], NULL);
20053}
20054
20055
20056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020058 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 */
20060 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020061var2fpos(
20062 typval_T *varp,
20063 int dollar_lnum, /* TRUE when $ is last line */
20064 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020066 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020068 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069
Bram Moolenaara5525202006-03-02 22:52:09 +000020070 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020071 if (varp->v_type == VAR_LIST)
20072 {
20073 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020074 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020075 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020076 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020077
20078 l = varp->vval.v_list;
20079 if (l == NULL)
20080 return NULL;
20081
20082 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020083 pos.lnum = list_find_nr(l, 0L, &error);
20084 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020085 return NULL; /* invalid line number */
20086
20087 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020088 pos.col = list_find_nr(l, 1L, &error);
20089 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020090 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020091 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020092
20093 /* We accept "$" for the column number: last column. */
20094 li = list_find(l, 1L);
20095 if (li != NULL && li->li_tv.v_type == VAR_STRING
20096 && li->li_tv.vval.v_string != NULL
20097 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20098 pos.col = len + 1;
20099
Bram Moolenaara5525202006-03-02 22:52:09 +000020100 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020101 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020102 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020103 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020104
Bram Moolenaara5525202006-03-02 22:52:09 +000020105#ifdef FEAT_VIRTUALEDIT
20106 /* Get the virtual offset. Defaults to zero. */
20107 pos.coladd = list_find_nr(l, 2L, &error);
20108 if (error)
20109 pos.coladd = 0;
20110#endif
20111
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020112 return &pos;
20113 }
20114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020115 name = get_tv_string_chk(varp);
20116 if (name == NULL)
20117 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020118 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020120 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20121 {
20122 if (VIsual_active)
20123 return &VIsual;
20124 return &curwin->w_cursor;
20125 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020126 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020128 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20130 return NULL;
20131 return pp;
20132 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020133
20134#ifdef FEAT_VIRTUALEDIT
20135 pos.coladd = 0;
20136#endif
20137
Bram Moolenaar477933c2007-07-17 14:32:23 +000020138 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020139 {
20140 pos.col = 0;
20141 if (name[1] == '0') /* "w0": first visible line */
20142 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020143 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020144 pos.lnum = curwin->w_topline;
20145 return &pos;
20146 }
20147 else if (name[1] == '$') /* "w$": last visible line */
20148 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020149 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020150 pos.lnum = curwin->w_botline - 1;
20151 return &pos;
20152 }
20153 }
20154 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020156 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 {
20158 pos.lnum = curbuf->b_ml.ml_line_count;
20159 pos.col = 0;
20160 }
20161 else
20162 {
20163 pos.lnum = curwin->w_cursor.lnum;
20164 pos.col = (colnr_T)STRLEN(ml_get_curline());
20165 }
20166 return &pos;
20167 }
20168 return NULL;
20169}
20170
20171/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020172 * Convert list in "arg" into a position and optional file number.
20173 * When "fnump" is NULL there is no file number, only 3 items.
20174 * Note that the column is passed on as-is, the caller may want to decrement
20175 * it to use 1 for the first column.
20176 * Return FAIL when conversion is not possible, doesn't check the position for
20177 * validity.
20178 */
20179 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020180list2fpos(
20181 typval_T *arg,
20182 pos_T *posp,
20183 int *fnump,
20184 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020185{
20186 list_T *l = arg->vval.v_list;
20187 long i = 0;
20188 long n;
20189
Bram Moolenaar493c1782014-05-28 14:34:46 +020020190 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20191 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020192 if (arg->v_type != VAR_LIST
20193 || l == NULL
20194 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020195 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020196 return FAIL;
20197
20198 if (fnump != NULL)
20199 {
20200 n = list_find_nr(l, i++, NULL); /* fnum */
20201 if (n < 0)
20202 return FAIL;
20203 if (n == 0)
20204 n = curbuf->b_fnum; /* current buffer */
20205 *fnump = n;
20206 }
20207
20208 n = list_find_nr(l, i++, NULL); /* lnum */
20209 if (n < 0)
20210 return FAIL;
20211 posp->lnum = n;
20212
20213 n = list_find_nr(l, i++, NULL); /* col */
20214 if (n < 0)
20215 return FAIL;
20216 posp->col = n;
20217
20218#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020219 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020220 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020221 posp->coladd = 0;
20222 else
20223 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020224#endif
20225
Bram Moolenaar493c1782014-05-28 14:34:46 +020020226 if (curswantp != NULL)
20227 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20228
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020229 return OK;
20230}
20231
20232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 * Get the length of an environment variable name.
20234 * Advance "arg" to the first character after the name.
20235 * Return 0 for error.
20236 */
20237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020238get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239{
20240 char_u *p;
20241 int len;
20242
20243 for (p = *arg; vim_isIDc(*p); ++p)
20244 ;
20245 if (p == *arg) /* no name found */
20246 return 0;
20247
20248 len = (int)(p - *arg);
20249 *arg = p;
20250 return len;
20251}
20252
20253/*
20254 * Get the length of the name of a function or internal variable.
20255 * "arg" is advanced to the first non-white character after the name.
20256 * Return 0 if something is wrong.
20257 */
20258 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020259get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260{
20261 char_u *p;
20262 int len;
20263
20264 /* Find the end of the name. */
20265 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020266 {
20267 if (*p == ':')
20268 {
20269 /* "s:" is start of "s:var", but "n:" is not and can be used in
20270 * slice "[n:]". Also "xx:" is not a namespace. */
20271 len = (int)(p - *arg);
20272 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20273 || len > 1)
20274 break;
20275 }
20276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 if (p == *arg) /* no name found */
20278 return 0;
20279
20280 len = (int)(p - *arg);
20281 *arg = skipwhite(p);
20282
20283 return len;
20284}
20285
20286/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020287 * Get the length of the name of a variable or function.
20288 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020290 * Return -1 if curly braces expansion failed.
20291 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 * If the name contains 'magic' {}'s, expand them and return the
20293 * expanded name in an allocated string via 'alias' - caller must free.
20294 */
20295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020296get_name_len(
20297 char_u **arg,
20298 char_u **alias,
20299 int evaluate,
20300 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301{
20302 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 char_u *p;
20304 char_u *expr_start;
20305 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306
20307 *alias = NULL; /* default to no alias */
20308
20309 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20310 && (*arg)[2] == (int)KE_SNR)
20311 {
20312 /* hard coded <SNR>, already translated */
20313 *arg += 3;
20314 return get_id_len(arg) + 3;
20315 }
20316 len = eval_fname_script(*arg);
20317 if (len > 0)
20318 {
20319 /* literal "<SID>", "s:" or "<SNR>" */
20320 *arg += len;
20321 }
20322
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020324 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020326 p = find_name_end(*arg, &expr_start, &expr_end,
20327 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 if (expr_start != NULL)
20329 {
20330 char_u *temp_string;
20331
20332 if (!evaluate)
20333 {
20334 len += (int)(p - *arg);
20335 *arg = skipwhite(p);
20336 return len;
20337 }
20338
20339 /*
20340 * Include any <SID> etc in the expanded string:
20341 * Thus the -len here.
20342 */
20343 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20344 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020345 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 *alias = temp_string;
20347 *arg = skipwhite(p);
20348 return (int)STRLEN(temp_string);
20349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350
20351 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020352 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 EMSG2(_(e_invexpr2), *arg);
20354
20355 return len;
20356}
20357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020358/*
20359 * Find the end of a variable or function name, taking care of magic braces.
20360 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20361 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020362 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020363 * Return a pointer to just after the name. Equal to "arg" if there is no
20364 * valid name.
20365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020367find_name_end(
20368 char_u *arg,
20369 char_u **expr_start,
20370 char_u **expr_end,
20371 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020373 int mb_nest = 0;
20374 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020376 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020378 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380 *expr_start = NULL;
20381 *expr_end = NULL;
20382 }
20383
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020384 /* Quick check for valid starting character. */
20385 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20386 return arg;
20387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020388 for (p = arg; *p != NUL
20389 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020390 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020391 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020393 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020394 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020395 if (*p == '\'')
20396 {
20397 /* skip over 'string' to avoid counting [ and ] inside it. */
20398 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20399 ;
20400 if (*p == NUL)
20401 break;
20402 }
20403 else if (*p == '"')
20404 {
20405 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20406 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20407 if (*p == '\\' && p[1] != NUL)
20408 ++p;
20409 if (*p == NUL)
20410 break;
20411 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020412 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20413 {
20414 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020415 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020416 len = (int)(p - arg);
20417 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020418 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020419 break;
20420 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020422 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 if (*p == '[')
20425 ++br_nest;
20426 else if (*p == ']')
20427 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020430 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020432 if (*p == '{')
20433 {
20434 mb_nest++;
20435 if (expr_start != NULL && *expr_start == NULL)
20436 *expr_start = p;
20437 }
20438 else if (*p == '}')
20439 {
20440 mb_nest--;
20441 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20442 *expr_end = p;
20443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 }
20446
20447 return p;
20448}
20449
20450/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020451 * Expands out the 'magic' {}'s in a variable/function name.
20452 * Note that this can call itself recursively, to deal with
20453 * constructs like foo{bar}{baz}{bam}
20454 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20455 * "in_start" ^
20456 * "expr_start" ^
20457 * "expr_end" ^
20458 * "in_end" ^
20459 *
20460 * Returns a new allocated string, which the caller must free.
20461 * Returns NULL for failure.
20462 */
20463 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020464make_expanded_name(
20465 char_u *in_start,
20466 char_u *expr_start,
20467 char_u *expr_end,
20468 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020469{
20470 char_u c1;
20471 char_u *retval = NULL;
20472 char_u *temp_result;
20473 char_u *nextcmd = NULL;
20474
20475 if (expr_end == NULL || in_end == NULL)
20476 return NULL;
20477 *expr_start = NUL;
20478 *expr_end = NUL;
20479 c1 = *in_end;
20480 *in_end = NUL;
20481
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020482 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020483 if (temp_result != NULL && nextcmd == NULL)
20484 {
20485 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20486 + (in_end - expr_end) + 1));
20487 if (retval != NULL)
20488 {
20489 STRCPY(retval, in_start);
20490 STRCAT(retval, temp_result);
20491 STRCAT(retval, expr_end + 1);
20492 }
20493 }
20494 vim_free(temp_result);
20495
20496 *in_end = c1; /* put char back for error messages */
20497 *expr_start = '{';
20498 *expr_end = '}';
20499
20500 if (retval != NULL)
20501 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020502 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020503 if (expr_start != NULL)
20504 {
20505 /* Further expansion! */
20506 temp_result = make_expanded_name(retval, expr_start,
20507 expr_end, temp_result);
20508 vim_free(retval);
20509 retval = temp_result;
20510 }
20511 }
20512
20513 return retval;
20514}
20515
20516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020518 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 */
20520 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020521eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020523 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20524}
20525
20526/*
20527 * Return TRUE if character "c" can be used as the first character in a
20528 * variable or function name (excluding '{' and '}').
20529 */
20530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020531eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020532{
20533 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534}
20535
20536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 * Set number v: variable to "val".
20538 */
20539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020540set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020542 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543}
20544
20545/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020546 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 */
20548 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020549get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020551 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552}
20553
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020554/*
20555 * Get string v: variable value. Uses a static buffer, can only be used once.
20556 */
20557 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020558get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020559{
20560 return get_tv_string(&vimvars[idx].vv_tv);
20561}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020562
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020564 * Get List v: variable value. Caller must take care of reference count when
20565 * needed.
20566 */
20567 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020568get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020569{
20570 return vimvars[idx].vv_list;
20571}
20572
20573/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020574 * Set v:char to character "c".
20575 */
20576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020577set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020578{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020579 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020580
20581#ifdef FEAT_MBYTE
20582 if (has_mbyte)
20583 buf[(*mb_char2bytes)(c, buf)] = NUL;
20584 else
20585#endif
20586 {
20587 buf[0] = c;
20588 buf[1] = NUL;
20589 }
20590 set_vim_var_string(VV_CHAR, buf, -1);
20591}
20592
20593/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020594 * Set v:count to "count" and v:count1 to "count1".
20595 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 */
20597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020598set_vcount(
20599 long count,
20600 long count1,
20601 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020603 if (set_prevcount)
20604 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020605 vimvars[VV_COUNT].vv_nr = count;
20606 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607}
20608
20609/*
20610 * Set string v: variable to a copy of "val".
20611 */
20612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020613set_vim_var_string(
20614 int idx,
20615 char_u *val,
20616 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617{
Bram Moolenaara542c682016-01-31 16:28:04 +010020618 clear_tv(&vimvars[idx].vv_di.di_tv);
20619 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020621 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020623 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020625 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626}
20627
20628/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020629 * Set List v: variable to "val".
20630 */
20631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020633{
Bram Moolenaara542c682016-01-31 16:28:04 +010020634 clear_tv(&vimvars[idx].vv_di.di_tv);
20635 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020636 vimvars[idx].vv_list = val;
20637 if (val != NULL)
20638 ++val->lv_refcount;
20639}
20640
20641/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020642 * Set Dictionary v: variable to "val".
20643 */
20644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020645set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020646{
20647 int todo;
20648 hashitem_T *hi;
20649
Bram Moolenaara542c682016-01-31 16:28:04 +010020650 clear_tv(&vimvars[idx].vv_di.di_tv);
20651 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020652 vimvars[idx].vv_dict = val;
20653 if (val != NULL)
20654 {
20655 ++val->dv_refcount;
20656
20657 /* Set readonly */
20658 todo = (int)val->dv_hashtab.ht_used;
20659 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20660 {
20661 if (HASHITEM_EMPTY(hi))
20662 continue;
20663 --todo;
20664 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20665 }
20666 }
20667}
20668
20669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670 * Set v:register if needed.
20671 */
20672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020673set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674{
20675 char_u regname;
20676
20677 if (c == 0 || c == ' ')
20678 regname = '"';
20679 else
20680 regname = c;
20681 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020682 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 set_vim_var_string(VV_REG, &regname, 1);
20684}
20685
20686/*
20687 * Get or set v:exception. If "oldval" == NULL, return the current value.
20688 * Otherwise, restore the value to "oldval" and return NULL.
20689 * Must always be called in pairs to save and restore v:exception! Does not
20690 * take care of memory allocations.
20691 */
20692 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020693v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694{
20695 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020696 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697
Bram Moolenaare9a41262005-01-15 22:18:47 +000020698 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 return NULL;
20700}
20701
20702/*
20703 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20704 * Otherwise, restore the value to "oldval" and return NULL.
20705 * Must always be called in pairs to save and restore v:throwpoint! Does not
20706 * take care of memory allocations.
20707 */
20708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020709v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710{
20711 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020712 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713
Bram Moolenaare9a41262005-01-15 22:18:47 +000020714 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 return NULL;
20716}
20717
20718#if defined(FEAT_AUTOCMD) || defined(PROTO)
20719/*
20720 * Set v:cmdarg.
20721 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20722 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20723 * Must always be called in pairs!
20724 */
20725 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020726set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727{
20728 char_u *oldval;
20729 char_u *newval;
20730 unsigned len;
20731
Bram Moolenaare9a41262005-01-15 22:18:47 +000020732 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020733 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020735 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020736 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020737 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 }
20739
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020740 if (eap->force_bin == FORCE_BIN)
20741 len = 6;
20742 else if (eap->force_bin == FORCE_NOBIN)
20743 len = 8;
20744 else
20745 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020746
20747 if (eap->read_edit)
20748 len += 7;
20749
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020750 if (eap->force_ff != 0)
20751 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20752# ifdef FEAT_MBYTE
20753 if (eap->force_enc != 0)
20754 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020755 if (eap->bad_char != 0)
20756 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020757# endif
20758
20759 newval = alloc(len + 1);
20760 if (newval == NULL)
20761 return NULL;
20762
20763 if (eap->force_bin == FORCE_BIN)
20764 sprintf((char *)newval, " ++bin");
20765 else if (eap->force_bin == FORCE_NOBIN)
20766 sprintf((char *)newval, " ++nobin");
20767 else
20768 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020769
20770 if (eap->read_edit)
20771 STRCAT(newval, " ++edit");
20772
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020773 if (eap->force_ff != 0)
20774 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20775 eap->cmd + eap->force_ff);
20776# ifdef FEAT_MBYTE
20777 if (eap->force_enc != 0)
20778 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20779 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020780 if (eap->bad_char == BAD_KEEP)
20781 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20782 else if (eap->bad_char == BAD_DROP)
20783 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20784 else if (eap->bad_char != 0)
20785 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020786# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020787 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020788 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789}
20790#endif
20791
20792/*
20793 * Get the value of internal variable "name".
20794 * Return OK or FAIL.
20795 */
20796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020797get_var_tv(
20798 char_u *name,
20799 int len, /* length of "name" */
20800 typval_T *rettv, /* NULL when only checking existence */
20801 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20802 int verbose, /* may give error message */
20803 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804{
20805 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020806 typval_T *tv = NULL;
20807 typval_T atv;
20808 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810
20811 /* truncate the name, so that we can use strcmp() */
20812 cc = name[len];
20813 name[len] = NUL;
20814
20815 /*
20816 * Check for "b:changedtick".
20817 */
20818 if (STRCMP(name, "b:changedtick") == 0)
20819 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020820 atv.v_type = VAR_NUMBER;
20821 atv.vval.v_number = curbuf->b_changedtick;
20822 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 }
20824
20825 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 * Check for user-defined variables.
20827 */
20828 else
20829 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020830 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020833 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020834 if (dip != NULL)
20835 *dip = v;
20836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 }
20838
Bram Moolenaare9a41262005-01-15 22:18:47 +000020839 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020841 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020842 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 ret = FAIL;
20844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020845 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020846 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847
20848 name[len] = cc;
20849
20850 return ret;
20851}
20852
20853/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020854 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20855 * Also handle function call with Funcref variable: func(expr)
20856 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20857 */
20858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020859handle_subscript(
20860 char_u **arg,
20861 typval_T *rettv,
20862 int evaluate, /* do more than finding the end */
20863 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020864{
20865 int ret = OK;
20866 dict_T *selfdict = NULL;
20867 char_u *s;
20868 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020869 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020870
20871 while (ret == OK
20872 && (**arg == '['
20873 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020874 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020875 && !vim_iswhite(*(*arg - 1)))
20876 {
20877 if (**arg == '(')
20878 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020879 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020880 if (evaluate)
20881 {
20882 functv = *rettv;
20883 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020884
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020885 /* Invoke the function. Recursive! */
20886 s = functv.vval.v_string;
20887 }
20888 else
20889 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020890 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020891 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20892 &len, evaluate, selfdict);
20893
20894 /* Clear the funcref afterwards, so that deleting it while
20895 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020896 if (evaluate)
20897 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020898
20899 /* Stop the expression evaluation when immediately aborting on
20900 * error, or when an interrupt occurred or an exception was thrown
20901 * but not caught. */
20902 if (aborting())
20903 {
20904 if (ret == OK)
20905 clear_tv(rettv);
20906 ret = FAIL;
20907 }
20908 dict_unref(selfdict);
20909 selfdict = NULL;
20910 }
20911 else /* **arg == '[' || **arg == '.' */
20912 {
20913 dict_unref(selfdict);
20914 if (rettv->v_type == VAR_DICT)
20915 {
20916 selfdict = rettv->vval.v_dict;
20917 if (selfdict != NULL)
20918 ++selfdict->dv_refcount;
20919 }
20920 else
20921 selfdict = NULL;
20922 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20923 {
20924 clear_tv(rettv);
20925 ret = FAIL;
20926 }
20927 }
20928 }
20929 dict_unref(selfdict);
20930 return ret;
20931}
20932
20933/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020934 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020935 * value).
20936 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010020937 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020938alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020939{
Bram Moolenaar33570922005-01-25 22:26:29 +000020940 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020941}
20942
20943/*
20944 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 * The string "s" must have been allocated, it is consumed.
20946 * Return NULL for out of memory, the variable otherwise.
20947 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020948 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020949alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950{
Bram Moolenaar33570922005-01-25 22:26:29 +000020951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020953 rettv = alloc_tv();
20954 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020956 rettv->v_type = VAR_STRING;
20957 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 }
20959 else
20960 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020961 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962}
20963
20964/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020965 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020968free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969{
20970 if (varp != NULL)
20971 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020972 switch (varp->v_type)
20973 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020974 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020975 func_unref(varp->vval.v_string);
20976 /*FALLTHROUGH*/
20977 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020978 vim_free(varp->vval.v_string);
20979 break;
20980 case VAR_LIST:
20981 list_unref(varp->vval.v_list);
20982 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983 case VAR_DICT:
20984 dict_unref(varp->vval.v_dict);
20985 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020986 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020987#ifdef FEAT_FLOAT
20988 case VAR_FLOAT:
20989#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020990 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010020991 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020992 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020993 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020994 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020995 break;
20996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 vim_free(varp);
20998 }
20999}
21000
21001/*
21002 * Free the memory for a variable value and set the value to NULL or 0.
21003 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021005clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006{
21007 if (varp != NULL)
21008 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021009 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021011 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021012 func_unref(varp->vval.v_string);
21013 /*FALLTHROUGH*/
21014 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021015 vim_free(varp->vval.v_string);
21016 varp->vval.v_string = NULL;
21017 break;
21018 case VAR_LIST:
21019 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021020 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021021 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021022 case VAR_DICT:
21023 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021024 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021025 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021026 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021027 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021028 varp->vval.v_number = 0;
21029 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021030#ifdef FEAT_FLOAT
21031 case VAR_FLOAT:
21032 varp->vval.v_float = 0.0;
21033 break;
21034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035 case VAR_UNKNOWN:
21036 break;
21037 default:
21038 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021040 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 }
21042}
21043
21044/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021045 * Set the value of a variable to NULL without freeing items.
21046 */
21047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021048init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021049{
21050 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021051 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021052}
21053
21054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 * Get the number value of a variable.
21056 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021057 * For incompatible types, return 0.
21058 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21059 * caller of incompatible types: it sets *denote to TRUE if "denote"
21060 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 */
21062 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021063get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021065 int error = FALSE;
21066
21067 return get_tv_number_chk(varp, &error); /* return 0L on error */
21068}
21069
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021070 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021071get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021072{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021073 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021075 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021077 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021078 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021079#ifdef FEAT_FLOAT
21080 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021081 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021082 break;
21083#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021084 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021085 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021086 break;
21087 case VAR_STRING:
21088 if (varp->vval.v_string != NULL)
21089 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021090 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021091 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021092 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021093 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021094 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021095 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021096 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021097 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021098 case VAR_SPECIAL:
21099 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21100 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021101 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021102 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021105 if (denote == NULL) /* useful for values that must be unsigned */
21106 n = -1;
21107 else
21108 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021109 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110}
21111
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021112#ifdef FEAT_FLOAT
21113 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021114get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021115{
21116 switch (varp->v_type)
21117 {
21118 case VAR_NUMBER:
21119 return (float_T)(varp->vval.v_number);
21120#ifdef FEAT_FLOAT
21121 case VAR_FLOAT:
21122 return varp->vval.v_float;
21123 break;
21124#endif
21125 case VAR_FUNC:
21126 EMSG(_("E891: Using a Funcref as a Float"));
21127 break;
21128 case VAR_STRING:
21129 EMSG(_("E892: Using a String as a Float"));
21130 break;
21131 case VAR_LIST:
21132 EMSG(_("E893: Using a List as a Float"));
21133 break;
21134 case VAR_DICT:
21135 EMSG(_("E894: Using a Dictionary as a Float"));
21136 break;
21137 default:
21138 EMSG2(_(e_intern2), "get_tv_float()");
21139 break;
21140 }
21141 return 0;
21142}
21143#endif
21144
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021146 * Get the lnum from the first argument.
21147 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021148 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 */
21150 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021151get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
Bram Moolenaar33570922005-01-25 22:26:29 +000021153 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 linenr_T lnum;
21155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021156 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 if (lnum == 0) /* no valid number, try using line() */
21158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021159 rettv.v_type = VAR_NUMBER;
21160 f_line(argvars, &rettv);
21161 lnum = rettv.vval.v_number;
21162 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 }
21164 return lnum;
21165}
21166
21167/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021168 * Get the lnum from the first argument.
21169 * Also accepts "$", then "buf" is used.
21170 * Returns 0 on error.
21171 */
21172 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021173get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021174{
21175 if (argvars[0].v_type == VAR_STRING
21176 && argvars[0].vval.v_string != NULL
21177 && argvars[0].vval.v_string[0] == '$'
21178 && buf != NULL)
21179 return buf->b_ml.ml_line_count;
21180 return get_tv_number_chk(&argvars[0], NULL);
21181}
21182
21183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 * Get the string value of a variable.
21185 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021186 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21187 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 * If the String variable has never been set, return an empty string.
21189 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021190 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21191 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 */
21193 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021194get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195{
21196 static char_u mybuf[NUMBUFLEN];
21197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021198 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199}
21200
21201 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021204 char_u *res = get_tv_string_buf_chk(varp, buf);
21205
21206 return res != NULL ? res : (char_u *)"";
21207}
21208
Bram Moolenaar7d647822014-04-05 21:28:56 +020021209/*
21210 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21211 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021212 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021213get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021214{
21215 static char_u mybuf[NUMBUFLEN];
21216
21217 return get_tv_string_buf_chk(varp, mybuf);
21218}
21219
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021220 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021222{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021223 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021225 case VAR_NUMBER:
21226 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21227 return buf;
21228 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021229 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021230 break;
21231 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021232 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021233 break;
21234 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021235 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021236 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021237#ifdef FEAT_FLOAT
21238 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021239 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021240 break;
21241#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021242 case VAR_STRING:
21243 if (varp->vval.v_string != NULL)
21244 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021245 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021246 case VAR_SPECIAL:
21247 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21248 return buf;
21249
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021250 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021251 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021252 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021254 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255}
21256
21257/*
21258 * Find variable "name" in the list of variables.
21259 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021260 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021261 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021262 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021264 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021265find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021268 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269
Bram Moolenaara7043832005-01-21 11:56:39 +000021270 ht = find_var_ht(name, &varname);
21271 if (htp != NULL)
21272 *htp = ht;
21273 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021275 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276}
21277
21278/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021279 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021280 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021282 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021283find_var_in_ht(
21284 hashtab_T *ht,
21285 int htname,
21286 char_u *varname,
21287 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021288{
Bram Moolenaar33570922005-01-25 22:26:29 +000021289 hashitem_T *hi;
21290
21291 if (*varname == NUL)
21292 {
21293 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021294 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021295 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021296 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021297 case 'g': return &globvars_var;
21298 case 'v': return &vimvars_var;
21299 case 'b': return &curbuf->b_bufvar;
21300 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021301#ifdef FEAT_WINDOWS
21302 case 't': return &curtab->tp_winvar;
21303#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021304 case 'l': return current_funccal == NULL
21305 ? NULL : &current_funccal->l_vars_var;
21306 case 'a': return current_funccal == NULL
21307 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021308 }
21309 return NULL;
21310 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021311
21312 hi = hash_find(ht, varname);
21313 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021314 {
21315 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021316 * worked find the variable again. Don't auto-load a script if it was
21317 * loaded already, otherwise it would be loaded every time when
21318 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021319 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021320 {
21321 /* Note: script_autoload() may make "hi" invalid. It must either
21322 * be obtained again or not used. */
21323 if (!script_autoload(varname, FALSE) || aborting())
21324 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021325 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021326 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021327 if (HASHITEM_EMPTY(hi))
21328 return NULL;
21329 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021330 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021331}
21332
21333/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021335 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021336 * Set "varname" to the start of name without ':'.
21337 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021338 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021339find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021341 hashitem_T *hi;
21342
Bram Moolenaar73627d02015-08-11 15:46:09 +020021343 if (name[0] == NUL)
21344 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 if (name[1] != ':')
21346 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021347 /* The name must not start with a colon or #. */
21348 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 return NULL;
21350 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021351
21352 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021353 hi = hash_find(&compat_hashtab, name);
21354 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021355 return &compat_hashtab;
21356
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021358 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021359 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 }
21361 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021362 if (*name == 'g') /* global variable */
21363 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021364 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21365 */
21366 if (vim_strchr(name + 2, ':') != NULL
21367 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021368 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021370 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021372 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021373#ifdef FEAT_WINDOWS
21374 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021375 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021376#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 if (*name == 'v') /* v: variable */
21378 return &vimvarht;
21379 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021380 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021381 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021382 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 if (*name == 's' /* script variable */
21384 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21385 return &SCRIPT_VARS(current_SID);
21386 return NULL;
21387}
21388
21389/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021390 * Get function call environment based on bactrace debug level
21391 */
21392 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021393get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021394{
21395 int i;
21396 funccall_T *funccal;
21397 funccall_T *temp_funccal;
21398
21399 funccal = current_funccal;
21400 if (debug_backtrace_level > 0)
21401 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021402 for (i = 0; i < debug_backtrace_level; i++)
21403 {
21404 temp_funccal = funccal->caller;
21405 if (temp_funccal)
21406 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021407 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021408 /* backtrace level overflow. reset to max */
21409 debug_backtrace_level = i;
21410 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021411 }
21412 return funccal;
21413}
21414
21415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021417 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 * Returns NULL when it doesn't exist.
21419 */
21420 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021421get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422{
Bram Moolenaar33570922005-01-25 22:26:29 +000021423 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021425 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 if (v == NULL)
21427 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021428 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429}
21430
21431/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021432 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 * sourcing this script and when executing functions defined in the script.
21434 */
21435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021436new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437{
Bram Moolenaara7043832005-01-21 11:56:39 +000021438 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 hashtab_T *ht;
21440 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021441
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21443 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021444 /* Re-allocating ga_data means that an ht_array pointing to
21445 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021446 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021447 for (i = 1; i <= ga_scripts.ga_len; ++i)
21448 {
21449 ht = &SCRIPT_VARS(i);
21450 if (ht->ht_mask == HT_INIT_SIZE - 1)
21451 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021452 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021453 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021454 }
21455
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 while (ga_scripts.ga_len < id)
21457 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021458 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021459 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021460 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 }
21463 }
21464}
21465
21466/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21468 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 */
21470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021471init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472{
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021474 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021475 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021476 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021477 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021478 dict_var->di_tv.vval.v_dict = dict;
21479 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021480 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021481 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21482 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483}
21484
21485/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021486 * Unreference a dictionary initialized by init_var_dict().
21487 */
21488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021489unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021490{
21491 /* Now the dict needs to be freed if no one else is using it, go back to
21492 * normal reference counting. */
21493 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21494 dict_unref(dict);
21495}
21496
21497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021499 * Frees all allocated variables and the value they contain.
21500 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 */
21502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021503vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021504{
21505 vars_clear_ext(ht, TRUE);
21506}
21507
21508/*
21509 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21510 */
21511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021512vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513{
Bram Moolenaara7043832005-01-21 11:56:39 +000021514 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021515 hashitem_T *hi;
21516 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021519 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021520 for (hi = ht->ht_array; todo > 0; ++hi)
21521 {
21522 if (!HASHITEM_EMPTY(hi))
21523 {
21524 --todo;
21525
Bram Moolenaar33570922005-01-25 22:26:29 +000021526 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021527 * ht_array might change then. hash_clear() takes care of it
21528 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021529 v = HI2DI(hi);
21530 if (free_val)
21531 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021532 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021534 }
21535 }
21536 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021537 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538}
21539
Bram Moolenaara7043832005-01-21 11:56:39 +000021540/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021541 * Delete a variable from hashtab "ht" at item "hi".
21542 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021545delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546{
Bram Moolenaar33570922005-01-25 22:26:29 +000021547 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021548
21549 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021550 clear_tv(&di->di_tv);
21551 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552}
21553
21554/*
21555 * List the value of one internal variable.
21556 */
21557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021558list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021560 char_u *tofree;
21561 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021562 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021563
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021564 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021565 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021566 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568}
21569
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021571list_one_var_a(
21572 char_u *prefix,
21573 char_u *name,
21574 int type,
21575 char_u *string,
21576 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577{
Bram Moolenaar31859182007-08-14 20:41:13 +000021578 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21579 msg_start();
21580 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 if (name != NULL) /* "a:" vars don't have a name stored */
21582 msg_puts(name);
21583 msg_putchar(' ');
21584 msg_advance(22);
21585 if (type == VAR_NUMBER)
21586 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021587 else if (type == VAR_FUNC)
21588 msg_putchar('*');
21589 else if (type == VAR_LIST)
21590 {
21591 msg_putchar('[');
21592 if (*string == '[')
21593 ++string;
21594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021595 else if (type == VAR_DICT)
21596 {
21597 msg_putchar('{');
21598 if (*string == '{')
21599 ++string;
21600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 else
21602 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021603
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021605
21606 if (type == VAR_FUNC)
21607 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021608 if (*first)
21609 {
21610 msg_clr_eos();
21611 *first = FALSE;
21612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613}
21614
21615/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021616 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 * If the variable already exists, the value is updated.
21618 * Otherwise the variable is created.
21619 */
21620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021621set_var(
21622 char_u *name,
21623 typval_T *tv,
21624 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625{
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021630 ht = find_var_ht(name, &varname);
21631 if (ht == NULL || *varname == NUL)
21632 {
21633 EMSG2(_(e_illvar), name);
21634 return;
21635 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021636 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021637
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021638 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21639 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640
Bram Moolenaar33570922005-01-25 22:26:29 +000021641 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021643 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021644 if (var_check_ro(v->di_flags, name, FALSE)
21645 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021646 return;
21647 if (v->di_tv.v_type != tv->v_type
21648 && !((v->di_tv.v_type == VAR_STRING
21649 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021650 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021651 || tv->v_type == VAR_NUMBER))
21652#ifdef FEAT_FLOAT
21653 && !((v->di_tv.v_type == VAR_NUMBER
21654 || v->di_tv.v_type == VAR_FLOAT)
21655 && (tv->v_type == VAR_NUMBER
21656 || tv->v_type == VAR_FLOAT))
21657#endif
21658 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021659 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021660 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021661 return;
21662 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021663
21664 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021665 * Handle setting internal v: variables separately where needed to
21666 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 */
21668 if (ht == &vimvarht)
21669 {
21670 if (v->di_tv.v_type == VAR_STRING)
21671 {
21672 vim_free(v->di_tv.vval.v_string);
21673 if (copy || tv->v_type != VAR_STRING)
21674 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21675 else
21676 {
21677 /* Take over the string to avoid an extra alloc/free. */
21678 v->di_tv.vval.v_string = tv->vval.v_string;
21679 tv->vval.v_string = NULL;
21680 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021681 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021682 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021683 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021684 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021685 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021686 if (STRCMP(varname, "searchforward") == 0)
21687 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021688#ifdef FEAT_SEARCH_EXTRA
21689 else if (STRCMP(varname, "hlsearch") == 0)
21690 {
21691 no_hlsearch = !v->di_tv.vval.v_number;
21692 redraw_all_later(SOME_VALID);
21693 }
21694#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021695 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021696 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021697 else if (v->di_tv.v_type != tv->v_type)
21698 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021699 }
21700
21701 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 }
21703 else /* add a new variable */
21704 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021705 /* Can't add "v:" variable. */
21706 if (ht == &vimvarht)
21707 {
21708 EMSG2(_(e_illvar), name);
21709 return;
21710 }
21711
Bram Moolenaar92124a32005-06-17 22:03:40 +000021712 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021713 if (!valid_varname(varname))
21714 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021715
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021716 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21717 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021718 if (v == NULL)
21719 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021723 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 return;
21725 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021726 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021728
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021729 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021730 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021731 else
21732 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021734 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021735 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737}
21738
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021739/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021740 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021741 * Also give an error message.
21742 */
21743 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021744var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021745{
21746 if (flags & DI_FLAGS_RO)
21747 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021748 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021749 return TRUE;
21750 }
21751 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21752 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021753 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 return TRUE;
21755 }
21756 return FALSE;
21757}
21758
21759/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021760 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21761 * Also give an error message.
21762 */
21763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021764var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021765{
21766 if (flags & DI_FLAGS_FIX)
21767 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021768 EMSG2(_("E795: Cannot delete variable %s"),
21769 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021770 return TRUE;
21771 }
21772 return FALSE;
21773}
21774
21775/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021776 * Check if a funcref is assigned to a valid variable name.
21777 * Return TRUE and give an error if not.
21778 */
21779 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021780var_check_func_name(
21781 char_u *name, /* points to start of variable name */
21782 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021783{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021784 /* Allow for w: b: s: and t:. */
21785 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021786 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21787 ? name[2] : name[0]))
21788 {
21789 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21790 name);
21791 return TRUE;
21792 }
21793 /* Don't allow hiding a function. When "v" is not NULL we might be
21794 * assigning another function to the same var, the type is checked
21795 * below. */
21796 if (new_var && function_exists(name))
21797 {
21798 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21799 name);
21800 return TRUE;
21801 }
21802 return FALSE;
21803}
21804
21805/*
21806 * Check if a variable name is valid.
21807 * Return FALSE and give an error if not.
21808 */
21809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021810valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021811{
21812 char_u *p;
21813
21814 for (p = varname; *p != NUL; ++p)
21815 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21816 && *p != AUTOLOAD_CHAR)
21817 {
21818 EMSG2(_(e_illvar), varname);
21819 return FALSE;
21820 }
21821 return TRUE;
21822}
21823
21824/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021825 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021826 * Also give an error message, using "name" or _("name") when use_gettext is
21827 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021828 */
21829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021830tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021831{
21832 if (lock & VAR_LOCKED)
21833 {
21834 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021835 name == NULL ? (char_u *)_("Unknown")
21836 : use_gettext ? (char_u *)_(name)
21837 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021838 return TRUE;
21839 }
21840 if (lock & VAR_FIXED)
21841 {
21842 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021843 name == NULL ? (char_u *)_("Unknown")
21844 : use_gettext ? (char_u *)_(name)
21845 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021846 return TRUE;
21847 }
21848 return FALSE;
21849}
21850
21851/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021852 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021853 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021854 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021855 * It is OK for "from" and "to" to point to the same item. This is used to
21856 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021857 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021858 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021859copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021861 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021862 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021863 switch (from->v_type)
21864 {
21865 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021866 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021867 to->vval.v_number = from->vval.v_number;
21868 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021869#ifdef FEAT_FLOAT
21870 case VAR_FLOAT:
21871 to->vval.v_float = from->vval.v_float;
21872 break;
21873#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021874 case VAR_STRING:
21875 case VAR_FUNC:
21876 if (from->vval.v_string == NULL)
21877 to->vval.v_string = NULL;
21878 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021879 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021880 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021881 if (from->v_type == VAR_FUNC)
21882 func_ref(to->vval.v_string);
21883 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021884 break;
21885 case VAR_LIST:
21886 if (from->vval.v_list == NULL)
21887 to->vval.v_list = NULL;
21888 else
21889 {
21890 to->vval.v_list = from->vval.v_list;
21891 ++to->vval.v_list->lv_refcount;
21892 }
21893 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021894 case VAR_DICT:
21895 if (from->vval.v_dict == NULL)
21896 to->vval.v_dict = NULL;
21897 else
21898 {
21899 to->vval.v_dict = from->vval.v_dict;
21900 ++to->vval.v_dict->dv_refcount;
21901 }
21902 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021903 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021904 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021905 break;
21906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907}
21908
21909/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021910 * Make a copy of an item.
21911 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021912 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21913 * reference to an already copied list/dict can be used.
21914 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021915 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021917item_copy(
21918 typval_T *from,
21919 typval_T *to,
21920 int deep,
21921 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021922{
21923 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021924 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021925
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021927 {
21928 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021929 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021930 }
21931 ++recurse;
21932
21933 switch (from->v_type)
21934 {
21935 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021936#ifdef FEAT_FLOAT
21937 case VAR_FLOAT:
21938#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021939 case VAR_STRING:
21940 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010021941 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000021942 copy_tv(from, to);
21943 break;
21944 case VAR_LIST:
21945 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021946 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021947 if (from->vval.v_list == NULL)
21948 to->vval.v_list = NULL;
21949 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21950 {
21951 /* use the copy made earlier */
21952 to->vval.v_list = from->vval.v_list->lv_copylist;
21953 ++to->vval.v_list->lv_refcount;
21954 }
21955 else
21956 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21957 if (to->vval.v_list == NULL)
21958 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021959 break;
21960 case VAR_DICT:
21961 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021962 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021963 if (from->vval.v_dict == NULL)
21964 to->vval.v_dict = NULL;
21965 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21966 {
21967 /* use the copy made earlier */
21968 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21969 ++to->vval.v_dict->dv_refcount;
21970 }
21971 else
21972 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21973 if (to->vval.v_dict == NULL)
21974 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021975 break;
21976 default:
21977 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021978 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021979 }
21980 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021981 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021982}
21983
21984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 * ":echo expr1 ..." print each argument separated with a space, add a
21986 * newline at the end.
21987 * ":echon expr1 ..." print each argument plain.
21988 */
21989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021990ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991{
21992 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021994 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 char_u *p;
21996 int needclr = TRUE;
21997 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021998 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999
22000 if (eap->skip)
22001 ++emsg_skip;
22002 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022004 /* If eval1() causes an error message the text from the command may
22005 * still need to be cleared. E.g., "echo 22,44". */
22006 need_clr_eos = needclr;
22007
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022009 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 {
22011 /*
22012 * Report the invalid expression unless the expression evaluation
22013 * has been cancelled due to an aborting error, an interrupt, or an
22014 * exception.
22015 */
22016 if (!aborting())
22017 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022018 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 break;
22020 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022021 need_clr_eos = FALSE;
22022
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 if (!eap->skip)
22024 {
22025 if (atstart)
22026 {
22027 atstart = FALSE;
22028 /* Call msg_start() after eval1(), evaluating the expression
22029 * may cause a message to appear. */
22030 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022031 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022032 /* Mark the saved text as finishing the line, so that what
22033 * follows is displayed on a new line when scrolling back
22034 * at the more prompt. */
22035 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 }
22039 else if (eap->cmdidx == CMD_echo)
22040 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022041 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022042 if (p != NULL)
22043 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022045 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022047 if (*p != TAB && needclr)
22048 {
22049 /* remove any text still there from the command */
22050 msg_clr_eos();
22051 needclr = FALSE;
22052 }
22053 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 }
22055 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022056 {
22057#ifdef FEAT_MBYTE
22058 if (has_mbyte)
22059 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022060 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022061
22062 (void)msg_outtrans_len_attr(p, i, echo_attr);
22063 p += i - 1;
22064 }
22065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022067 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022070 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022072 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 arg = skipwhite(arg);
22074 }
22075 eap->nextcmd = check_nextcmd(arg);
22076
22077 if (eap->skip)
22078 --emsg_skip;
22079 else
22080 {
22081 /* remove text that may still be there from the command */
22082 if (needclr)
22083 msg_clr_eos();
22084 if (eap->cmdidx == CMD_echo)
22085 msg_end();
22086 }
22087}
22088
22089/*
22090 * ":echohl {name}".
22091 */
22092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022093ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094{
22095 int id;
22096
22097 id = syn_name2id(eap->arg);
22098 if (id == 0)
22099 echo_attr = 0;
22100 else
22101 echo_attr = syn_id2attr(id);
22102}
22103
22104/*
22105 * ":execute expr1 ..." execute the result of an expression.
22106 * ":echomsg expr1 ..." Print a message
22107 * ":echoerr expr1 ..." Print an error
22108 * Each gets spaces around each argument and a newline at the end for
22109 * echo commands
22110 */
22111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022112ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113{
22114 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022115 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 int ret = OK;
22117 char_u *p;
22118 garray_T ga;
22119 int len;
22120 int save_did_emsg;
22121
22122 ga_init2(&ga, 1, 80);
22123
22124 if (eap->skip)
22125 ++emsg_skip;
22126 while (*arg != NUL && *arg != '|' && *arg != '\n')
22127 {
22128 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022129 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 {
22131 /*
22132 * Report the invalid expression unless the expression evaluation
22133 * has been cancelled due to an aborting error, an interrupt, or an
22134 * exception.
22135 */
22136 if (!aborting())
22137 EMSG2(_(e_invexpr2), p);
22138 ret = FAIL;
22139 break;
22140 }
22141
22142 if (!eap->skip)
22143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022144 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 len = (int)STRLEN(p);
22146 if (ga_grow(&ga, len + 2) == FAIL)
22147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 ret = FAIL;
22150 break;
22151 }
22152 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 ga.ga_len += len;
22156 }
22157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022158 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 arg = skipwhite(arg);
22160 }
22161
22162 if (ret != FAIL && ga.ga_data != NULL)
22163 {
22164 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022167 out_flush();
22168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 else if (eap->cmdidx == CMD_echoerr)
22170 {
22171 /* We don't want to abort following commands, restore did_emsg. */
22172 save_did_emsg = did_emsg;
22173 EMSG((char_u *)ga.ga_data);
22174 if (!force_abort)
22175 did_emsg = save_did_emsg;
22176 }
22177 else if (eap->cmdidx == CMD_execute)
22178 do_cmdline((char_u *)ga.ga_data,
22179 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22180 }
22181
22182 ga_clear(&ga);
22183
22184 if (eap->skip)
22185 --emsg_skip;
22186
22187 eap->nextcmd = check_nextcmd(arg);
22188}
22189
22190/*
22191 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22192 * "arg" points to the "&" or '+' when called, to "option" when returning.
22193 * Returns NULL when no option name found. Otherwise pointer to the char
22194 * after the option name.
22195 */
22196 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022197find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198{
22199 char_u *p = *arg;
22200
22201 ++p;
22202 if (*p == 'g' && p[1] == ':')
22203 {
22204 *opt_flags = OPT_GLOBAL;
22205 p += 2;
22206 }
22207 else if (*p == 'l' && p[1] == ':')
22208 {
22209 *opt_flags = OPT_LOCAL;
22210 p += 2;
22211 }
22212 else
22213 *opt_flags = 0;
22214
22215 if (!ASCII_ISALPHA(*p))
22216 return NULL;
22217 *arg = p;
22218
22219 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22220 p += 4; /* termcap option */
22221 else
22222 while (ASCII_ISALPHA(*p))
22223 ++p;
22224 return p;
22225}
22226
22227/*
22228 * ":function"
22229 */
22230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022231ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232{
22233 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022234 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 int j;
22236 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022238 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 char_u *name = NULL;
22240 char_u *p;
22241 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022242 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 garray_T newargs;
22244 garray_T newlines;
22245 int varargs = FALSE;
22246 int mustend = FALSE;
22247 int flags = 0;
22248 ufunc_T *fp;
22249 int indent;
22250 int nesting;
22251 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 dictitem_T *v;
22253 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022254 static int func_nr = 0; /* number for nameless function */
22255 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022256 hashtab_T *ht;
22257 int todo;
22258 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022259 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260
22261 /*
22262 * ":function" without argument: list functions.
22263 */
22264 if (ends_excmd(*eap->arg))
22265 {
22266 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022267 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022268 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022269 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022270 {
22271 if (!HASHITEM_EMPTY(hi))
22272 {
22273 --todo;
22274 fp = HI2UF(hi);
22275 if (!isdigit(*fp->uf_name))
22276 list_func_head(fp, FALSE);
22277 }
22278 }
22279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 eap->nextcmd = check_nextcmd(eap->arg);
22281 return;
22282 }
22283
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022284 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022285 * ":function /pat": list functions matching pattern.
22286 */
22287 if (*eap->arg == '/')
22288 {
22289 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22290 if (!eap->skip)
22291 {
22292 regmatch_T regmatch;
22293
22294 c = *p;
22295 *p = NUL;
22296 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22297 *p = c;
22298 if (regmatch.regprog != NULL)
22299 {
22300 regmatch.rm_ic = p_ic;
22301
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022302 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022303 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22304 {
22305 if (!HASHITEM_EMPTY(hi))
22306 {
22307 --todo;
22308 fp = HI2UF(hi);
22309 if (!isdigit(*fp->uf_name)
22310 && vim_regexec(&regmatch, fp->uf_name, 0))
22311 list_func_head(fp, FALSE);
22312 }
22313 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022314 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022315 }
22316 }
22317 if (*p == '/')
22318 ++p;
22319 eap->nextcmd = check_nextcmd(p);
22320 return;
22321 }
22322
22323 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022324 * Get the function name. There are these situations:
22325 * func normal function name
22326 * "name" == func, "fudi.fd_dict" == NULL
22327 * dict.func new dictionary entry
22328 * "name" == NULL, "fudi.fd_dict" set,
22329 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22330 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022331 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22333 * dict.func existing dict entry that's not a Funcref
22334 * "name" == NULL, "fudi.fd_dict" set,
22335 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022336 * s:func script-local function name
22337 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 name = trans_function_name(&p, eap->skip, 0, &fudi);
22341 paren = (vim_strchr(p, '(') != NULL);
22342 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 {
22344 /*
22345 * Return on an invalid expression in braces, unless the expression
22346 * evaluation has been cancelled due to an aborting error, an
22347 * interrupt, or an exception.
22348 */
22349 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022351 if (!eap->skip && fudi.fd_newkey != NULL)
22352 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022353 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 else
22357 eap->skip = TRUE;
22358 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022359
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 /* An error in a function call during evaluation of an expression in magic
22361 * braces should not cause the function not to be defined. */
22362 saved_did_emsg = did_emsg;
22363 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364
22365 /*
22366 * ":function func" with only function name: list function.
22367 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022368 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 {
22370 if (!ends_excmd(*skipwhite(p)))
22371 {
22372 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022373 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 }
22375 eap->nextcmd = check_nextcmd(p);
22376 if (eap->nextcmd != NULL)
22377 *p = NUL;
22378 if (!eap->skip && !got_int)
22379 {
22380 fp = find_func(name);
22381 if (fp != NULL)
22382 {
22383 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022384 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022386 if (FUNCLINE(fp, j) == NULL)
22387 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 msg_putchar('\n');
22389 msg_outnum((long)(j + 1));
22390 if (j < 9)
22391 msg_putchar(' ');
22392 if (j < 99)
22393 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022394 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 out_flush(); /* show a line at a time */
22396 ui_breakcheck();
22397 }
22398 if (!got_int)
22399 {
22400 msg_putchar('\n');
22401 msg_puts((char_u *)" endfunction");
22402 }
22403 }
22404 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022405 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022407 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 }
22409
22410 /*
22411 * ":function name(arg1, arg2)" Define function.
22412 */
22413 p = skipwhite(p);
22414 if (*p != '(')
22415 {
22416 if (!eap->skip)
22417 {
22418 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 }
22421 /* attempt to continue by skipping some text */
22422 if (vim_strchr(p, '(') != NULL)
22423 p = vim_strchr(p, '(');
22424 }
22425 p = skipwhite(p + 1);
22426
22427 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22428 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22429
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022430 if (!eap->skip)
22431 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022432 /* Check the name of the function. Unless it's a dictionary function
22433 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022434 if (name != NULL)
22435 arg = name;
22436 else
22437 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022438 if (arg != NULL && (fudi.fd_di == NULL
22439 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022440 {
22441 if (*arg == K_SPECIAL)
22442 j = 3;
22443 else
22444 j = 0;
22445 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22446 : eval_isnamec(arg[j])))
22447 ++j;
22448 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022449 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022450 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022451 /* Disallow using the g: dict. */
22452 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22453 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022454 }
22455
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 /*
22457 * Isolate the arguments: "arg1, arg2, ...)"
22458 */
22459 while (*p != ')')
22460 {
22461 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22462 {
22463 varargs = TRUE;
22464 p += 3;
22465 mustend = TRUE;
22466 }
22467 else
22468 {
22469 arg = p;
22470 while (ASCII_ISALNUM(*p) || *p == '_')
22471 ++p;
22472 if (arg == p || isdigit(*arg)
22473 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22474 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22475 {
22476 if (!eap->skip)
22477 EMSG2(_("E125: Illegal argument: %s"), arg);
22478 break;
22479 }
22480 if (ga_grow(&newargs, 1) == FAIL)
22481 goto erret;
22482 c = *p;
22483 *p = NUL;
22484 arg = vim_strsave(arg);
22485 if (arg == NULL)
22486 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022487
22488 /* Check for duplicate argument name. */
22489 for (i = 0; i < newargs.ga_len; ++i)
22490 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22491 {
22492 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022493 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022494 goto erret;
22495 }
22496
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22498 *p = c;
22499 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 if (*p == ',')
22501 ++p;
22502 else
22503 mustend = TRUE;
22504 }
22505 p = skipwhite(p);
22506 if (mustend && *p != ')')
22507 {
22508 if (!eap->skip)
22509 EMSG2(_(e_invarg2), eap->arg);
22510 break;
22511 }
22512 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022513 if (*p != ')')
22514 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 ++p; /* skip the ')' */
22516
Bram Moolenaare9a41262005-01-15 22:18:47 +000022517 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 for (;;)
22519 {
22520 p = skipwhite(p);
22521 if (STRNCMP(p, "range", 5) == 0)
22522 {
22523 flags |= FC_RANGE;
22524 p += 5;
22525 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022526 else if (STRNCMP(p, "dict", 4) == 0)
22527 {
22528 flags |= FC_DICT;
22529 p += 4;
22530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 else if (STRNCMP(p, "abort", 5) == 0)
22532 {
22533 flags |= FC_ABORT;
22534 p += 5;
22535 }
22536 else
22537 break;
22538 }
22539
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022540 /* When there is a line break use what follows for the function body.
22541 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22542 if (*p == '\n')
22543 line_arg = p + 1;
22544 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 EMSG(_(e_trailing));
22546
22547 /*
22548 * Read the body of the function, until ":endfunction" is found.
22549 */
22550 if (KeyTyped)
22551 {
22552 /* Check if the function already exists, don't let the user type the
22553 * whole function before telling him it doesn't work! For a script we
22554 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022555 if (!eap->skip && !eap->forceit)
22556 {
22557 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22558 EMSG(_(e_funcdict));
22559 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022560 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022563 if (!eap->skip && did_emsg)
22564 goto erret;
22565
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 msg_putchar('\n'); /* don't overwrite the function name */
22567 cmdline_row = msg_row;
22568 }
22569
22570 indent = 2;
22571 nesting = 0;
22572 for (;;)
22573 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022574 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022575 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022576 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022577 saved_wait_return = FALSE;
22578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022580 sourcing_lnum_off = sourcing_lnum;
22581
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022582 if (line_arg != NULL)
22583 {
22584 /* Use eap->arg, split up in parts by line breaks. */
22585 theline = line_arg;
22586 p = vim_strchr(theline, '\n');
22587 if (p == NULL)
22588 line_arg += STRLEN(line_arg);
22589 else
22590 {
22591 *p = NUL;
22592 line_arg = p + 1;
22593 }
22594 }
22595 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 theline = getcmdline(':', 0L, indent);
22597 else
22598 theline = eap->getline(':', eap->cookie, indent);
22599 if (KeyTyped)
22600 lines_left = Rows - 1;
22601 if (theline == NULL)
22602 {
22603 EMSG(_("E126: Missing :endfunction"));
22604 goto erret;
22605 }
22606
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022607 /* Detect line continuation: sourcing_lnum increased more than one. */
22608 if (sourcing_lnum > sourcing_lnum_off + 1)
22609 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22610 else
22611 sourcing_lnum_off = 0;
22612
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 if (skip_until != NULL)
22614 {
22615 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22616 * don't check for ":endfunc". */
22617 if (STRCMP(theline, skip_until) == 0)
22618 {
22619 vim_free(skip_until);
22620 skip_until = NULL;
22621 }
22622 }
22623 else
22624 {
22625 /* skip ':' and blanks*/
22626 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22627 ;
22628
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022629 /* Check for "endfunction". */
22630 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022632 if (line_arg == NULL)
22633 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 break;
22635 }
22636
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022637 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 * at "end". */
22639 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22640 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022641 else if (STRNCMP(p, "if", 2) == 0
22642 || STRNCMP(p, "wh", 2) == 0
22643 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 || STRNCMP(p, "try", 3) == 0)
22645 indent += 2;
22646
22647 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022648 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022650 if (*p == '!')
22651 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022653 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22654 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022656 ++nesting;
22657 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 }
22659 }
22660
22661 /* Check for ":append" or ":insert". */
22662 p = skip_range(p, NULL);
22663 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22664 || (p[0] == 'i'
22665 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22666 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22667 skip_until = vim_strsave((char_u *)".");
22668
22669 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22670 arg = skipwhite(skiptowhite(p));
22671 if (arg[0] == '<' && arg[1] =='<'
22672 && ((p[0] == 'p' && p[1] == 'y'
22673 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22674 || (p[0] == 'p' && p[1] == 'e'
22675 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22676 || (p[0] == 't' && p[1] == 'c'
22677 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022678 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22679 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22681 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022682 || (p[0] == 'm' && p[1] == 'z'
22683 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 ))
22685 {
22686 /* ":python <<" continues until a dot, like ":append" */
22687 p = skipwhite(arg + 2);
22688 if (*p == NUL)
22689 skip_until = vim_strsave((char_u *)".");
22690 else
22691 skip_until = vim_strsave(p);
22692 }
22693 }
22694
22695 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022696 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022697 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022698 if (line_arg == NULL)
22699 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022701 }
22702
22703 /* Copy the line to newly allocated memory. get_one_sourceline()
22704 * allocates 250 bytes per line, this saves 80% on average. The cost
22705 * is an extra alloc/free. */
22706 p = vim_strsave(theline);
22707 if (p != NULL)
22708 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022709 if (line_arg == NULL)
22710 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022711 theline = p;
22712 }
22713
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022714 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22715
22716 /* Add NULL lines for continuation lines, so that the line count is
22717 * equal to the index in the growarray. */
22718 while (sourcing_lnum_off-- > 0)
22719 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022720
22721 /* Check for end of eap->arg. */
22722 if (line_arg != NULL && *line_arg == NUL)
22723 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 }
22725
22726 /* Don't define the function when skipping commands or when an error was
22727 * detected. */
22728 if (eap->skip || did_emsg)
22729 goto erret;
22730
22731 /*
22732 * If there are no errors, add the function
22733 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022734 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022735 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022736 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022737 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022738 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022739 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022740 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022741 goto erret;
22742 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022743
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022744 fp = find_func(name);
22745 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022747 if (!eap->forceit)
22748 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022749 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022750 goto erret;
22751 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022752 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022753 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022754 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022755 name);
22756 goto erret;
22757 }
22758 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022759 ga_clear_strings(&(fp->uf_args));
22760 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022761 vim_free(name);
22762 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 }
22765 else
22766 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022767 char numbuf[20];
22768
22769 fp = NULL;
22770 if (fudi.fd_newkey == NULL && !eap->forceit)
22771 {
22772 EMSG(_(e_funcdict));
22773 goto erret;
22774 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022775 if (fudi.fd_di == NULL)
22776 {
22777 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022778 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022779 goto erret;
22780 }
22781 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022782 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022783 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784
22785 /* Give the function a sequential number. Can only be used with a
22786 * Funcref! */
22787 vim_free(name);
22788 sprintf(numbuf, "%d", ++func_nr);
22789 name = vim_strsave((char_u *)numbuf);
22790 if (name == NULL)
22791 goto erret;
22792 }
22793
22794 if (fp == NULL)
22795 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022796 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022797 {
22798 int slen, plen;
22799 char_u *scriptname;
22800
22801 /* Check that the autoload name matches the script name. */
22802 j = FAIL;
22803 if (sourcing_name != NULL)
22804 {
22805 scriptname = autoload_name(name);
22806 if (scriptname != NULL)
22807 {
22808 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022809 plen = (int)STRLEN(p);
22810 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 if (slen > plen && fnamecmp(p,
22812 sourcing_name + slen - plen) == 0)
22813 j = OK;
22814 vim_free(scriptname);
22815 }
22816 }
22817 if (j == FAIL)
22818 {
22819 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22820 goto erret;
22821 }
22822 }
22823
22824 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 if (fp == NULL)
22826 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022827
22828 if (fudi.fd_dict != NULL)
22829 {
22830 if (fudi.fd_di == NULL)
22831 {
22832 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022833 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834 if (fudi.fd_di == NULL)
22835 {
22836 vim_free(fp);
22837 goto erret;
22838 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022839 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22840 {
22841 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022842 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022843 goto erret;
22844 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022845 }
22846 else
22847 /* overwrite existing dict entry */
22848 clear_tv(&fudi.fd_di->di_tv);
22849 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022850 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022851 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022852 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022853
22854 /* behave like "dict" was used */
22855 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022856 }
22857
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022859 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022860 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22861 {
22862 vim_free(fp);
22863 goto erret;
22864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022866 fp->uf_args = newargs;
22867 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022868#ifdef FEAT_PROFILE
22869 fp->uf_tml_count = NULL;
22870 fp->uf_tml_total = NULL;
22871 fp->uf_tml_self = NULL;
22872 fp->uf_profiling = FALSE;
22873 if (prof_def_func())
22874 func_do_profile(fp);
22875#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022876 fp->uf_varargs = varargs;
22877 fp->uf_flags = flags;
22878 fp->uf_calls = 0;
22879 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022880 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881
22882erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 ga_clear_strings(&newargs);
22884 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022885ret_free:
22886 vim_free(skip_until);
22887 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022890 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891}
22892
22893/*
22894 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022895 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022897 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022898 * TFN_INT: internal function name OK
22899 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022900 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 * Advances "pp" to just after the function name (if no error).
22902 */
22903 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022904trans_function_name(
22905 char_u **pp,
22906 int skip, /* only find the end, don't evaluate */
22907 int flags,
22908 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022910 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 char_u *start;
22912 char_u *end;
22913 int lead;
22914 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022916 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022917
22918 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022919 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022921
22922 /* Check for hard coded <SNR>: already translated function ID (from a user
22923 * command). */
22924 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22925 && (*pp)[2] == (int)KE_SNR)
22926 {
22927 *pp += 3;
22928 len = get_id_len(pp) + 3;
22929 return vim_strnsave(start, len);
22930 }
22931
22932 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22933 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022935 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022937
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022938 /* Note that TFN_ flags use the same values as GLV_ flags. */
22939 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022940 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022941 if (end == start)
22942 {
22943 if (!skip)
22944 EMSG(_("E129: Function name required"));
22945 goto theend;
22946 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022947 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022948 {
22949 /*
22950 * Report an invalid expression in braces, unless the expression
22951 * evaluation has been cancelled due to an aborting error, an
22952 * interrupt, or an exception.
22953 */
22954 if (!aborting())
22955 {
22956 if (end != NULL)
22957 EMSG2(_(e_invarg2), start);
22958 }
22959 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022960 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022961 goto theend;
22962 }
22963
22964 if (lv.ll_tv != NULL)
22965 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022966 if (fdp != NULL)
22967 {
22968 fdp->fd_dict = lv.ll_dict;
22969 fdp->fd_newkey = lv.ll_newkey;
22970 lv.ll_newkey = NULL;
22971 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022973 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22974 {
22975 name = vim_strsave(lv.ll_tv->vval.v_string);
22976 *pp = end;
22977 }
22978 else
22979 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022980 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22981 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022982 EMSG(_(e_funcref));
22983 else
22984 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022985 name = NULL;
22986 }
22987 goto theend;
22988 }
22989
22990 if (lv.ll_name == NULL)
22991 {
22992 /* Error found, but continue after the function name. */
22993 *pp = end;
22994 goto theend;
22995 }
22996
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022997 /* Check if the name is a Funcref. If so, use the value. */
22998 if (lv.ll_exp_name != NULL)
22999 {
23000 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023001 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023002 if (name == lv.ll_exp_name)
23003 name = NULL;
23004 }
23005 else
23006 {
23007 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023008 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023009 if (name == *pp)
23010 name = NULL;
23011 }
23012 if (name != NULL)
23013 {
23014 name = vim_strsave(name);
23015 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023016 if (STRNCMP(name, "<SNR>", 5) == 0)
23017 {
23018 /* Change "<SNR>" to the byte sequence. */
23019 name[0] = K_SPECIAL;
23020 name[1] = KS_EXTRA;
23021 name[2] = (int)KE_SNR;
23022 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23023 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023024 goto theend;
23025 }
23026
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023027 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023028 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023029 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023030 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23031 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23032 {
23033 /* When there was "s:" already or the name expanded to get a
23034 * leading "s:" then remove it. */
23035 lv.ll_name += 2;
23036 len -= 2;
23037 lead = 2;
23038 }
23039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023040 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023041 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023042 /* skip over "s:" and "g:" */
23043 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023044 lv.ll_name += 2;
23045 len = (int)(end - lv.ll_name);
23046 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023047
23048 /*
23049 * Copy the function name to allocated memory.
23050 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23051 * Accept <SNR>123_name() outside a script.
23052 */
23053 if (skip)
23054 lead = 0; /* do nothing */
23055 else if (lead > 0)
23056 {
23057 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023058 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23059 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023060 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023061 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023062 if (current_SID <= 0)
23063 {
23064 EMSG(_(e_usingsid));
23065 goto theend;
23066 }
23067 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23068 lead += (int)STRLEN(sid_buf);
23069 }
23070 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023071 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023072 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023073 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023074 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023075 goto theend;
23076 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023077 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023078 {
23079 char_u *cp = vim_strchr(lv.ll_name, ':');
23080
23081 if (cp != NULL && cp < end)
23082 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023083 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023084 goto theend;
23085 }
23086 }
23087
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023088 name = alloc((unsigned)(len + lead + 1));
23089 if (name != NULL)
23090 {
23091 if (lead > 0)
23092 {
23093 name[0] = K_SPECIAL;
23094 name[1] = KS_EXTRA;
23095 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023096 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023097 STRCPY(name + 3, sid_buf);
23098 }
23099 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023100 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023101 }
23102 *pp = end;
23103
23104theend:
23105 clear_lval(&lv);
23106 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107}
23108
23109/*
23110 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23111 * Return 2 if "p" starts with "s:".
23112 * Return 0 otherwise.
23113 */
23114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023115eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023117 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23118 * the standard library function. */
23119 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23120 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 return 5;
23122 if (p[0] == 's' && p[1] == ':')
23123 return 2;
23124 return 0;
23125}
23126
23127/*
23128 * Return TRUE if "p" starts with "<SID>" or "s:".
23129 * Only works if eval_fname_script() returned non-zero for "p"!
23130 */
23131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023132eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133{
23134 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23135}
23136
23137/*
23138 * List the head of the function: "name(arg1, arg2)".
23139 */
23140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023141list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142{
23143 int j;
23144
23145 msg_start();
23146 if (indent)
23147 MSG_PUTS(" ");
23148 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023149 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 {
23151 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023152 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 }
23154 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023155 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023157 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 {
23159 if (j)
23160 MSG_PUTS(", ");
23161 msg_puts(FUNCARG(fp, j));
23162 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023163 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 {
23165 if (j)
23166 MSG_PUTS(", ");
23167 MSG_PUTS("...");
23168 }
23169 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023170 if (fp->uf_flags & FC_ABORT)
23171 MSG_PUTS(" abort");
23172 if (fp->uf_flags & FC_RANGE)
23173 MSG_PUTS(" range");
23174 if (fp->uf_flags & FC_DICT)
23175 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023176 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023177 if (p_verbose > 0)
23178 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179}
23180
23181/*
23182 * Find a function by name, return pointer to it in ufuncs.
23183 * Return NULL for unknown function.
23184 */
23185 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023186find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023188 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023190 hi = hash_find(&func_hashtab, name);
23191 if (!HASHITEM_EMPTY(hi))
23192 return HI2UF(hi);
23193 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194}
23195
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023196#if defined(EXITFREE) || defined(PROTO)
23197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023198free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023199{
23200 hashitem_T *hi;
23201
23202 /* Need to start all over every time, because func_free() may change the
23203 * hash table. */
23204 while (func_hashtab.ht_used > 0)
23205 for (hi = func_hashtab.ht_array; ; ++hi)
23206 if (!HASHITEM_EMPTY(hi))
23207 {
23208 func_free(HI2UF(hi));
23209 break;
23210 }
23211}
23212#endif
23213
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023214 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023215translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023216{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023217 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023218 return find_internal_func(name) >= 0;
23219 return find_func(name) != NULL;
23220}
23221
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023222/*
23223 * Return TRUE if a function "name" exists.
23224 */
23225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023226function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023227{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023228 char_u *nm = name;
23229 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023230 int n = FALSE;
23231
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023232 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23233 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023234 nm = skipwhite(nm);
23235
23236 /* Only accept "funcname", "funcname ", "funcname (..." and
23237 * "funcname(...", not "funcname!...". */
23238 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023239 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023240 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023241 return n;
23242}
23243
Bram Moolenaara1544c02013-05-30 12:35:52 +020023244 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023245get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023246{
23247 char_u *nm = name;
23248 char_u *p;
23249
23250 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23251
23252 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023253 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023254 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023255
Bram Moolenaara1544c02013-05-30 12:35:52 +020023256 vim_free(p);
23257 return NULL;
23258}
23259
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023260/*
23261 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023262 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23263 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023264 */
23265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023266builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023267{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023268 char_u *p;
23269
23270 if (!ASCII_ISLOWER(name[0]))
23271 return FALSE;
23272 p = vim_strchr(name, AUTOLOAD_CHAR);
23273 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023274}
23275
Bram Moolenaar05159a02005-02-26 23:04:13 +000023276#if defined(FEAT_PROFILE) || defined(PROTO)
23277/*
23278 * Start profiling function "fp".
23279 */
23280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023281func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023282{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023283 int len = fp->uf_lines.ga_len;
23284
23285 if (len == 0)
23286 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023287 fp->uf_tm_count = 0;
23288 profile_zero(&fp->uf_tm_self);
23289 profile_zero(&fp->uf_tm_total);
23290 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023291 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023292 if (fp->uf_tml_total == NULL)
23293 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023294 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023295 if (fp->uf_tml_self == NULL)
23296 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023297 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023298 fp->uf_tml_idx = -1;
23299 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23300 || fp->uf_tml_self == NULL)
23301 return; /* out of memory */
23302
23303 fp->uf_profiling = TRUE;
23304}
23305
23306/*
23307 * Dump the profiling results for all functions in file "fd".
23308 */
23309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023310func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023311{
23312 hashitem_T *hi;
23313 int todo;
23314 ufunc_T *fp;
23315 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023316 ufunc_T **sorttab;
23317 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023319 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023320 if (todo == 0)
23321 return; /* nothing to dump */
23322
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023323 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023324
Bram Moolenaar05159a02005-02-26 23:04:13 +000023325 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23326 {
23327 if (!HASHITEM_EMPTY(hi))
23328 {
23329 --todo;
23330 fp = HI2UF(hi);
23331 if (fp->uf_profiling)
23332 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023333 if (sorttab != NULL)
23334 sorttab[st_len++] = fp;
23335
Bram Moolenaar05159a02005-02-26 23:04:13 +000023336 if (fp->uf_name[0] == K_SPECIAL)
23337 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23338 else
23339 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23340 if (fp->uf_tm_count == 1)
23341 fprintf(fd, "Called 1 time\n");
23342 else
23343 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23344 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23345 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23346 fprintf(fd, "\n");
23347 fprintf(fd, "count total (s) self (s)\n");
23348
23349 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23350 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023351 if (FUNCLINE(fp, i) == NULL)
23352 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023353 prof_func_line(fd, fp->uf_tml_count[i],
23354 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023355 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23356 }
23357 fprintf(fd, "\n");
23358 }
23359 }
23360 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023361
23362 if (sorttab != NULL && st_len > 0)
23363 {
23364 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23365 prof_total_cmp);
23366 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23367 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23368 prof_self_cmp);
23369 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23370 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023371
23372 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023373}
Bram Moolenaar73830342005-02-28 22:48:19 +000023374
23375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023376prof_sort_list(
23377 FILE *fd,
23378 ufunc_T **sorttab,
23379 int st_len,
23380 char *title,
23381 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023382{
23383 int i;
23384 ufunc_T *fp;
23385
23386 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23387 fprintf(fd, "count total (s) self (s) function\n");
23388 for (i = 0; i < 20 && i < st_len; ++i)
23389 {
23390 fp = sorttab[i];
23391 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23392 prefer_self);
23393 if (fp->uf_name[0] == K_SPECIAL)
23394 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23395 else
23396 fprintf(fd, " %s()\n", fp->uf_name);
23397 }
23398 fprintf(fd, "\n");
23399}
23400
23401/*
23402 * Print the count and times for one function or function line.
23403 */
23404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023405prof_func_line(
23406 FILE *fd,
23407 int count,
23408 proftime_T *total,
23409 proftime_T *self,
23410 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023411{
23412 if (count > 0)
23413 {
23414 fprintf(fd, "%5d ", count);
23415 if (prefer_self && profile_equal(total, self))
23416 fprintf(fd, " ");
23417 else
23418 fprintf(fd, "%s ", profile_msg(total));
23419 if (!prefer_self && profile_equal(total, self))
23420 fprintf(fd, " ");
23421 else
23422 fprintf(fd, "%s ", profile_msg(self));
23423 }
23424 else
23425 fprintf(fd, " ");
23426}
23427
23428/*
23429 * Compare function for total time sorting.
23430 */
23431 static int
23432#ifdef __BORLANDC__
23433_RTLENTRYF
23434#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023435prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023436{
23437 ufunc_T *p1, *p2;
23438
23439 p1 = *(ufunc_T **)s1;
23440 p2 = *(ufunc_T **)s2;
23441 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23442}
23443
23444/*
23445 * Compare function for self time sorting.
23446 */
23447 static int
23448#ifdef __BORLANDC__
23449_RTLENTRYF
23450#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023451prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023452{
23453 ufunc_T *p1, *p2;
23454
23455 p1 = *(ufunc_T **)s1;
23456 p2 = *(ufunc_T **)s2;
23457 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23458}
23459
Bram Moolenaar05159a02005-02-26 23:04:13 +000023460#endif
23461
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023462/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023463 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023464 * Return TRUE if a package was loaded.
23465 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023466 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023467script_autoload(
23468 char_u *name,
23469 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023470{
23471 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023472 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023473 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023474 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023475
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023476 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023477 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023478 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023479 return FALSE;
23480
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023481 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023482
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023483 /* Find the name in the list of previously loaded package names. Skip
23484 * "autoload/", it's always the same. */
23485 for (i = 0; i < ga_loaded.ga_len; ++i)
23486 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23487 break;
23488 if (!reload && i < ga_loaded.ga_len)
23489 ret = FALSE; /* was loaded already */
23490 else
23491 {
23492 /* Remember the name if it wasn't loaded already. */
23493 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23494 {
23495 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23496 tofree = NULL;
23497 }
23498
23499 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023500 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023501 ret = TRUE;
23502 }
23503
23504 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023505 return ret;
23506}
23507
23508/*
23509 * Return the autoload script name for a function or variable name.
23510 * Returns NULL when out of memory.
23511 */
23512 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023513autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023514{
23515 char_u *p;
23516 char_u *scriptname;
23517
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023518 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023519 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23520 if (scriptname == NULL)
23521 return FALSE;
23522 STRCPY(scriptname, "autoload/");
23523 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023524 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023525 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023526 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023527 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023528 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023529}
23530
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23532
23533/*
23534 * Function given to ExpandGeneric() to obtain the list of user defined
23535 * function names.
23536 */
23537 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023538get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023540 static long_u done;
23541 static hashitem_T *hi;
23542 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543
23544 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023546 done = 0;
23547 hi = func_hashtab.ht_array;
23548 }
23549 if (done < func_hashtab.ht_used)
23550 {
23551 if (done++ > 0)
23552 ++hi;
23553 while (HASHITEM_EMPTY(hi))
23554 ++hi;
23555 fp = HI2UF(hi);
23556
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023557 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023558 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023559
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023560 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23561 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562
23563 cat_func_name(IObuff, fp);
23564 if (xp->xp_context != EXPAND_USER_FUNC)
23565 {
23566 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023567 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 STRCAT(IObuff, ")");
23569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 return IObuff;
23571 }
23572 return NULL;
23573}
23574
23575#endif /* FEAT_CMDL_COMPL */
23576
23577/*
23578 * Copy the function name of "fp" to buffer "buf".
23579 * "buf" must be able to hold the function name plus three bytes.
23580 * Takes care of script-local function names.
23581 */
23582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023583cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023585 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 {
23587 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023588 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 }
23590 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023591 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592}
23593
23594/*
23595 * ":delfunction {name}"
23596 */
23597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023598ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023600 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 char_u *p;
23602 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023603 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604
23605 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023606 name = trans_function_name(&p, eap->skip, 0, &fudi);
23607 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023609 {
23610 if (fudi.fd_dict != NULL && !eap->skip)
23611 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 if (!ends_excmd(*skipwhite(p)))
23615 {
23616 vim_free(name);
23617 EMSG(_(e_trailing));
23618 return;
23619 }
23620 eap->nextcmd = check_nextcmd(p);
23621 if (eap->nextcmd != NULL)
23622 *p = NUL;
23623
23624 if (!eap->skip)
23625 fp = find_func(name);
23626 vim_free(name);
23627
23628 if (!eap->skip)
23629 {
23630 if (fp == NULL)
23631 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023632 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 return;
23634 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023635 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 {
23637 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23638 return;
23639 }
23640
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023641 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023643 /* Delete the dict item that refers to the function, it will
23644 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023645 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023647 else
23648 func_free(fp);
23649 }
23650}
23651
23652/*
23653 * Free a function and remove it from the list of functions.
23654 */
23655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023656func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023658 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023659
23660 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023661 ga_clear_strings(&(fp->uf_args));
23662 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023663#ifdef FEAT_PROFILE
23664 vim_free(fp->uf_tml_count);
23665 vim_free(fp->uf_tml_total);
23666 vim_free(fp->uf_tml_self);
23667#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023668
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023669 /* remove the function from the function hashtable */
23670 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23671 if (HASHITEM_EMPTY(hi))
23672 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023673 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023674 hash_remove(&func_hashtab, hi);
23675
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023676 vim_free(fp);
23677}
23678
23679/*
23680 * Unreference a Function: decrement the reference count and free it when it
23681 * becomes zero. Only for numbered functions.
23682 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023684func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023685{
23686 ufunc_T *fp;
23687
23688 if (name != NULL && isdigit(*name))
23689 {
23690 fp = find_func(name);
23691 if (fp == NULL)
23692 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023693 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023694 {
23695 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023696 * when "uf_calls" becomes zero. */
23697 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023698 func_free(fp);
23699 }
23700 }
23701}
23702
23703/*
23704 * Count a reference to a Function.
23705 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023707func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023708{
23709 ufunc_T *fp;
23710
23711 if (name != NULL && isdigit(*name))
23712 {
23713 fp = find_func(name);
23714 if (fp == NULL)
23715 EMSG2(_(e_intern2), "func_ref()");
23716 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023717 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 }
23719}
23720
23721/*
23722 * Call a user function.
23723 */
23724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023725call_user_func(
23726 ufunc_T *fp, /* pointer to function */
23727 int argcount, /* nr of args */
23728 typval_T *argvars, /* arguments */
23729 typval_T *rettv, /* return value */
23730 linenr_T firstline, /* first line of range */
23731 linenr_T lastline, /* last line of range */
23732 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733{
Bram Moolenaar33570922005-01-25 22:26:29 +000023734 char_u *save_sourcing_name;
23735 linenr_T save_sourcing_lnum;
23736 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023737 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023738 int save_did_emsg;
23739 static int depth = 0;
23740 dictitem_T *v;
23741 int fixvar_idx = 0; /* index in fixvar[] */
23742 int i;
23743 int ai;
23744 char_u numbuf[NUMBUFLEN];
23745 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023746 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023747#ifdef FEAT_PROFILE
23748 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023749 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751
23752 /* If depth of calling is getting too high, don't execute the function */
23753 if (depth >= p_mfd)
23754 {
23755 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023756 rettv->v_type = VAR_NUMBER;
23757 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758 return;
23759 }
23760 ++depth;
23761
23762 line_breakcheck(); /* check for CTRL-C hit */
23763
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023764 fc = (funccall_T *)alloc(sizeof(funccall_T));
23765 fc->caller = current_funccal;
23766 current_funccal = fc;
23767 fc->func = fp;
23768 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023769 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023770 fc->linenr = 0;
23771 fc->returned = FALSE;
23772 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023774 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23775 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776
Bram Moolenaar33570922005-01-25 22:26:29 +000023777 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023778 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023779 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23780 * each argument variable and saves a lot of time.
23781 */
23782 /*
23783 * Init l: variables.
23784 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023785 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023786 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023787 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023788 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23789 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023790 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023791 name = v->di_key;
23792 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023793 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023794 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023795 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023796 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023797 v->di_tv.vval.v_dict = selfdict;
23798 ++selfdict->dv_refcount;
23799 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023800
Bram Moolenaar33570922005-01-25 22:26:29 +000023801 /*
23802 * Init a: variables.
23803 * Set a:0 to "argcount".
23804 * Set a:000 to a list with room for the "..." arguments.
23805 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023806 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023807 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023808 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023809 /* Use "name" to avoid a warning from some compiler that checks the
23810 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023811 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023812 name = v->di_key;
23813 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023814 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023815 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023816 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023817 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023818 v->di_tv.vval.v_list = &fc->l_varlist;
23819 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23820 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23821 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023822
23823 /*
23824 * Set a:firstline to "firstline" and a:lastline to "lastline".
23825 * Set a:name to named arguments.
23826 * Set a:N to the "..." arguments.
23827 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023828 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023829 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023830 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023831 (varnumber_T)lastline);
23832 for (i = 0; i < argcount; ++i)
23833 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023834 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023835 if (ai < 0)
23836 /* named argument a:name */
23837 name = FUNCARG(fp, i);
23838 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023839 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023840 /* "..." argument a:1, a:2, etc. */
23841 sprintf((char *)numbuf, "%d", ai + 1);
23842 name = numbuf;
23843 }
23844 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23845 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023846 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023847 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23848 }
23849 else
23850 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023851 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23852 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023853 if (v == NULL)
23854 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023855 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023856 }
23857 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023858 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023859
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023860 /* Note: the values are copied directly to avoid alloc/free.
23861 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023862 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023863 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023864
23865 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23866 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023867 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23868 fc->l_listitems[ai].li_tv = argvars[i];
23869 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023870 }
23871 }
23872
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 /* Don't redraw while executing the function. */
23874 ++RedrawingDisabled;
23875 save_sourcing_name = sourcing_name;
23876 save_sourcing_lnum = sourcing_lnum;
23877 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023878 /* need space for function name + ("function " + 3) or "[number]" */
23879 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23880 + STRLEN(fp->uf_name) + 20;
23881 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 if (sourcing_name != NULL)
23883 {
23884 if (save_sourcing_name != NULL
23885 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023886 sprintf((char *)sourcing_name, "%s[%d]..",
23887 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 else
23889 STRCPY(sourcing_name, "function ");
23890 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23891
23892 if (p_verbose >= 12)
23893 {
23894 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023895 verbose_enter_scroll();
23896
Bram Moolenaar555b2802005-05-19 21:08:39 +000023897 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 if (p_verbose >= 14)
23899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023901 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023902 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023903 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904
23905 msg_puts((char_u *)"(");
23906 for (i = 0; i < argcount; ++i)
23907 {
23908 if (i > 0)
23909 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023910 if (argvars[i].v_type == VAR_NUMBER)
23911 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 else
23913 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023914 /* Do not want errors such as E724 here. */
23915 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023916 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023917 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023918 if (s != NULL)
23919 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023920 if (vim_strsize(s) > MSG_BUF_CLEN)
23921 {
23922 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23923 s = buf;
23924 }
23925 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023926 vim_free(tofree);
23927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 }
23929 }
23930 msg_puts((char_u *)")");
23931 }
23932 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023933
23934 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 --no_wait_return;
23936 }
23937 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023938#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023939 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023940 {
23941 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23942 func_do_profile(fp);
23943 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023944 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023945 {
23946 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023947 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023948 profile_zero(&fp->uf_tm_children);
23949 }
23950 script_prof_save(&wait_start);
23951 }
23952#endif
23953
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023955 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 save_did_emsg = did_emsg;
23957 did_emsg = FALSE;
23958
23959 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023960 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23962
23963 --RedrawingDisabled;
23964
23965 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023966 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023968 clear_tv(rettv);
23969 rettv->v_type = VAR_NUMBER;
23970 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 }
23972
Bram Moolenaar05159a02005-02-26 23:04:13 +000023973#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023974 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023975 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023976 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023977 profile_end(&call_start);
23978 profile_sub_wait(&wait_start, &call_start);
23979 profile_add(&fp->uf_tm_total, &call_start);
23980 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023981 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023982 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023983 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23984 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023985 }
23986 }
23987#endif
23988
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989 /* when being verbose, mention the return value */
23990 if (p_verbose >= 12)
23991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023993 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023996 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023997 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023998 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023999 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024002 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024003 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024004 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024005 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024006
Bram Moolenaar555b2802005-05-19 21:08:39 +000024007 /* The value may be very long. Skip the middle part, so that we
24008 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024009 * truncate it at the end. Don't want errors such as E724 here. */
24010 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024011 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024012 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024013 if (s != NULL)
24014 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024015 if (vim_strsize(s) > MSG_BUF_CLEN)
24016 {
24017 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24018 s = buf;
24019 }
24020 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024021 vim_free(tofree);
24022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 }
24024 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024025
24026 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 --no_wait_return;
24028 }
24029
24030 vim_free(sourcing_name);
24031 sourcing_name = save_sourcing_name;
24032 sourcing_lnum = save_sourcing_lnum;
24033 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024034#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024035 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024036 script_prof_restore(&wait_start);
24037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024038
24039 if (p_verbose >= 12 && sourcing_name != NULL)
24040 {
24041 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024042 verbose_enter_scroll();
24043
Bram Moolenaar555b2802005-05-19 21:08:39 +000024044 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024046
24047 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 --no_wait_return;
24049 }
24050
24051 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024052 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024054
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024055 /* If the a:000 list and the l: and a: dicts are not referenced we can
24056 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024057 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24058 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24059 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24060 {
24061 free_funccal(fc, FALSE);
24062 }
24063 else
24064 {
24065 hashitem_T *hi;
24066 listitem_T *li;
24067 int todo;
24068
24069 /* "fc" is still in use. This can happen when returning "a:000" or
24070 * assigning "l:" to a global variable.
24071 * Link "fc" in the list for garbage collection later. */
24072 fc->caller = previous_funccal;
24073 previous_funccal = fc;
24074
24075 /* Make a copy of the a: variables, since we didn't do that above. */
24076 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24077 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24078 {
24079 if (!HASHITEM_EMPTY(hi))
24080 {
24081 --todo;
24082 v = HI2DI(hi);
24083 copy_tv(&v->di_tv, &v->di_tv);
24084 }
24085 }
24086
24087 /* Make a copy of the a:000 items, since we didn't do that above. */
24088 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24089 copy_tv(&li->li_tv, &li->li_tv);
24090 }
24091}
24092
24093/*
24094 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024095 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024096 */
24097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024098can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024099{
24100 return (fc->l_varlist.lv_copyID != copyID
24101 && fc->l_vars.dv_copyID != copyID
24102 && fc->l_avars.dv_copyID != copyID);
24103}
24104
24105/*
24106 * Free "fc" and what it contains.
24107 */
24108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024109free_funccal(
24110 funccall_T *fc,
24111 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024112{
24113 listitem_T *li;
24114
24115 /* The a: variables typevals may not have been allocated, only free the
24116 * allocated variables. */
24117 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24118
24119 /* free all l: variables */
24120 vars_clear(&fc->l_vars.dv_hashtab);
24121
24122 /* Free the a:000 variables if they were allocated. */
24123 if (free_val)
24124 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24125 clear_tv(&li->li_tv);
24126
24127 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128}
24129
24130/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024131 * Add a number variable "name" to dict "dp" with value "nr".
24132 */
24133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024134add_nr_var(
24135 dict_T *dp,
24136 dictitem_T *v,
24137 char *name,
24138 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024139{
24140 STRCPY(v->di_key, name);
24141 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24142 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24143 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024144 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024145 v->di_tv.vval.v_number = nr;
24146}
24147
24148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 * ":return [expr]"
24150 */
24151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024152ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153{
24154 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024155 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 int returning = FALSE;
24157
24158 if (current_funccal == NULL)
24159 {
24160 EMSG(_("E133: :return not inside a function"));
24161 return;
24162 }
24163
24164 if (eap->skip)
24165 ++emsg_skip;
24166
24167 eap->nextcmd = NULL;
24168 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024169 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 {
24171 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024172 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024174 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 }
24176 /* It's safer to return also on error. */
24177 else if (!eap->skip)
24178 {
24179 /*
24180 * Return unless the expression evaluation has been cancelled due to an
24181 * aborting error, an interrupt, or an exception.
24182 */
24183 if (!aborting())
24184 returning = do_return(eap, FALSE, TRUE, NULL);
24185 }
24186
24187 /* When skipping or the return gets pending, advance to the next command
24188 * in this line (!returning). Otherwise, ignore the rest of the line.
24189 * Following lines will be ignored by get_func_line(). */
24190 if (returning)
24191 eap->nextcmd = NULL;
24192 else if (eap->nextcmd == NULL) /* no argument */
24193 eap->nextcmd = check_nextcmd(arg);
24194
24195 if (eap->skip)
24196 --emsg_skip;
24197}
24198
24199/*
24200 * Return from a function. Possibly makes the return pending. Also called
24201 * for a pending return at the ":endtry" or after returning from an extra
24202 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024203 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024204 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 * FALSE when the return gets pending.
24206 */
24207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024208do_return(
24209 exarg_T *eap,
24210 int reanimate,
24211 int is_cmd,
24212 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213{
24214 int idx;
24215 struct condstack *cstack = eap->cstack;
24216
24217 if (reanimate)
24218 /* Undo the return. */
24219 current_funccal->returned = FALSE;
24220
24221 /*
24222 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24223 * not in its finally clause (which then is to be executed next) is found.
24224 * In this case, make the ":return" pending for execution at the ":endtry".
24225 * Otherwise, return normally.
24226 */
24227 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24228 if (idx >= 0)
24229 {
24230 cstack->cs_pending[idx] = CSTP_RETURN;
24231
24232 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024233 /* A pending return again gets pending. "rettv" points to an
24234 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024236 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 else
24238 {
24239 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024240 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024242 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024244 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 {
24246 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024247 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024248 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 else
24250 EMSG(_(e_outofmem));
24251 }
24252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024253 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254
24255 if (reanimate)
24256 {
24257 /* The pending return value could be overwritten by a ":return"
24258 * without argument in a finally clause; reset the default
24259 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024260 current_funccal->rettv->v_type = VAR_NUMBER;
24261 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 }
24263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024264 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 }
24266 else
24267 {
24268 current_funccal->returned = TRUE;
24269
24270 /* If the return is carried out now, store the return value. For
24271 * a return immediately after reanimation, the value is already
24272 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024273 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024275 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024276 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024278 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 }
24280 }
24281
24282 return idx < 0;
24283}
24284
24285/*
24286 * Free the variable with a pending return value.
24287 */
24288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024289discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290{
Bram Moolenaar33570922005-01-25 22:26:29 +000024291 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292}
24293
24294/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024295 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 * is an allocated string. Used by report_pending() for verbose messages.
24297 */
24298 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024299get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024301 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024302 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024303 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024305 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024306 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024307 if (s == NULL)
24308 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024309
24310 STRCPY(IObuff, ":return ");
24311 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24312 if (STRLEN(s) + 8 >= IOSIZE)
24313 STRCPY(IObuff + IOSIZE - 4, "...");
24314 vim_free(tofree);
24315 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316}
24317
24318/*
24319 * Get next function line.
24320 * Called by do_cmdline() to get the next line.
24321 * Returns allocated string, or NULL for end of function.
24322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024324get_func_line(
24325 int c UNUSED,
24326 void *cookie,
24327 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328{
Bram Moolenaar33570922005-01-25 22:26:29 +000024329 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024330 ufunc_T *fp = fcp->func;
24331 char_u *retval;
24332 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333
24334 /* If breakpoints have been added/deleted need to check for it. */
24335 if (fcp->dbg_tick != debug_tick)
24336 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024337 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 sourcing_lnum);
24339 fcp->dbg_tick = debug_tick;
24340 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024341#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024342 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024343 func_line_end(cookie);
24344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345
Bram Moolenaar05159a02005-02-26 23:04:13 +000024346 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024347 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24348 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 retval = NULL;
24350 else
24351 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024352 /* Skip NULL lines (continuation lines). */
24353 while (fcp->linenr < gap->ga_len
24354 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24355 ++fcp->linenr;
24356 if (fcp->linenr >= gap->ga_len)
24357 retval = NULL;
24358 else
24359 {
24360 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24361 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024362#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024363 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024364 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024365#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 }
24368
24369 /* Did we encounter a breakpoint? */
24370 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24371 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024372 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024374 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 sourcing_lnum);
24376 fcp->dbg_tick = debug_tick;
24377 }
24378
24379 return retval;
24380}
24381
Bram Moolenaar05159a02005-02-26 23:04:13 +000024382#if defined(FEAT_PROFILE) || defined(PROTO)
24383/*
24384 * Called when starting to read a function line.
24385 * "sourcing_lnum" must be correct!
24386 * When skipping lines it may not actually be executed, but we won't find out
24387 * until later and we need to store the time now.
24388 */
24389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024390func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024391{
24392 funccall_T *fcp = (funccall_T *)cookie;
24393 ufunc_T *fp = fcp->func;
24394
24395 if (fp->uf_profiling && sourcing_lnum >= 1
24396 && sourcing_lnum <= fp->uf_lines.ga_len)
24397 {
24398 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024399 /* Skip continuation lines. */
24400 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24401 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402 fp->uf_tml_execed = FALSE;
24403 profile_start(&fp->uf_tml_start);
24404 profile_zero(&fp->uf_tml_children);
24405 profile_get_wait(&fp->uf_tml_wait);
24406 }
24407}
24408
24409/*
24410 * Called when actually executing a function line.
24411 */
24412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024413func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024414{
24415 funccall_T *fcp = (funccall_T *)cookie;
24416 ufunc_T *fp = fcp->func;
24417
24418 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24419 fp->uf_tml_execed = TRUE;
24420}
24421
24422/*
24423 * Called when done with a function line.
24424 */
24425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024426func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024427{
24428 funccall_T *fcp = (funccall_T *)cookie;
24429 ufunc_T *fp = fcp->func;
24430
24431 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24432 {
24433 if (fp->uf_tml_execed)
24434 {
24435 ++fp->uf_tml_count[fp->uf_tml_idx];
24436 profile_end(&fp->uf_tml_start);
24437 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024438 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024439 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24440 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024441 }
24442 fp->uf_tml_idx = -1;
24443 }
24444}
24445#endif
24446
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447/*
24448 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024449 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450 */
24451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024452func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024453{
Bram Moolenaar33570922005-01-25 22:26:29 +000024454 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455
24456 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24457 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024458 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459 || fcp->returned);
24460}
24461
24462/*
24463 * return TRUE if cookie indicates a function which "abort"s on errors.
24464 */
24465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024466func_has_abort(
24467 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024469 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470}
24471
24472#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24473typedef enum
24474{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024475 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24476 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24477 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024478} var_flavour_T;
24479
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024480static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481
24482 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024483var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484{
24485 char_u *p = varname;
24486
24487 if (ASCII_ISUPPER(*p))
24488 {
24489 while (*(++p))
24490 if (ASCII_ISLOWER(*p))
24491 return VAR_FLAVOUR_SESSION;
24492 return VAR_FLAVOUR_VIMINFO;
24493 }
24494 else
24495 return VAR_FLAVOUR_DEFAULT;
24496}
24497#endif
24498
24499#if defined(FEAT_VIMINFO) || defined(PROTO)
24500/*
24501 * Restore global vars that start with a capital from the viminfo file
24502 */
24503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024504read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505{
24506 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024507 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024508 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024509 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510
24511 if (!writing && (find_viminfo_parameter('!') != NULL))
24512 {
24513 tab = vim_strchr(virp->vir_line + 1, '\t');
24514 if (tab != NULL)
24515 {
24516 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024517 switch (*tab)
24518 {
24519 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024520#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024521 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024522#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024523 case 'D': type = VAR_DICT; break;
24524 case 'L': type = VAR_LIST; break;
24525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024526
24527 tab = vim_strchr(tab, '\t');
24528 if (tab != NULL)
24529 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024530 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024531 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024532 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024534#ifdef FEAT_FLOAT
24535 else if (type == VAR_FLOAT)
24536 (void)string2float(tab + 1, &tv.vval.v_float);
24537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024539 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024540 if (type == VAR_DICT || type == VAR_LIST)
24541 {
24542 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24543
24544 if (etv == NULL)
24545 /* Failed to parse back the dict or list, use it as a
24546 * string. */
24547 tv.v_type = VAR_STRING;
24548 else
24549 {
24550 vim_free(tv.vval.v_string);
24551 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024552 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024553 }
24554 }
24555
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024556 /* when in a function use global variables */
24557 save_funccal = current_funccal;
24558 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024559 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024560 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024561
24562 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024563 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024564 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24565 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 }
24567 }
24568 }
24569
24570 return viminfo_readline(virp);
24571}
24572
24573/*
24574 * Write global vars that start with a capital to the viminfo file
24575 */
24576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024577write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578{
Bram Moolenaar33570922005-01-25 22:26:29 +000024579 hashitem_T *hi;
24580 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024581 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024582 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024583 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024584 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024585 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586
24587 if (find_viminfo_parameter('!') == NULL)
24588 return;
24589
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024590 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024592 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024593 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024595 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024597 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024598 this_var = HI2DI(hi);
24599 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024600 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024601 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024602 {
24603 case VAR_STRING: s = "STR"; break;
24604 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024605#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024606 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024607#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024608 case VAR_DICT: s = "DIC"; break;
24609 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024610 default: continue;
24611 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024612 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024613 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024614 if (p != NULL)
24615 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024616 vim_free(tofree);
24617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 }
24619 }
24620}
24621#endif
24622
24623#if defined(FEAT_SESSION) || defined(PROTO)
24624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024625store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626{
Bram Moolenaar33570922005-01-25 22:26:29 +000024627 hashitem_T *hi;
24628 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024629 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 char_u *p, *t;
24631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024632 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024633 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024635 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024637 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024638 this_var = HI2DI(hi);
24639 if ((this_var->di_tv.v_type == VAR_NUMBER
24640 || this_var->di_tv.v_type == VAR_STRING)
24641 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024642 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024643 /* Escape special characters with a backslash. Turn a LF and
24644 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024645 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024646 (char_u *)"\\\"\n\r");
24647 if (p == NULL) /* out of memory */
24648 break;
24649 for (t = p; *t != NUL; ++t)
24650 if (*t == '\n')
24651 *t = 'n';
24652 else if (*t == '\r')
24653 *t = 'r';
24654 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024655 this_var->di_key,
24656 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24657 : ' ',
24658 p,
24659 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24660 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024661 || put_eol(fd) == FAIL)
24662 {
24663 vim_free(p);
24664 return FAIL;
24665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666 vim_free(p);
24667 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024668#ifdef FEAT_FLOAT
24669 else if (this_var->di_tv.v_type == VAR_FLOAT
24670 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24671 {
24672 float_T f = this_var->di_tv.vval.v_float;
24673 int sign = ' ';
24674
24675 if (f < 0)
24676 {
24677 f = -f;
24678 sign = '-';
24679 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024680 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024681 this_var->di_key, sign, f) < 0)
24682 || put_eol(fd) == FAIL)
24683 return FAIL;
24684 }
24685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686 }
24687 }
24688 return OK;
24689}
24690#endif
24691
Bram Moolenaar661b1822005-07-28 22:36:45 +000024692/*
24693 * Display script name where an item was last set.
24694 * Should only be invoked when 'verbose' is non-zero.
24695 */
24696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024697last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024698{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024699 char_u *p;
24700
Bram Moolenaar661b1822005-07-28 22:36:45 +000024701 if (scriptID != 0)
24702 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024703 p = home_replace_save(NULL, get_scriptname(scriptID));
24704 if (p != NULL)
24705 {
24706 verbose_enter();
24707 MSG_PUTS(_("\n\tLast set from "));
24708 MSG_PUTS(p);
24709 vim_free(p);
24710 verbose_leave();
24711 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024712 }
24713}
24714
Bram Moolenaard812df62008-11-09 12:46:09 +000024715/*
24716 * List v:oldfiles in a nice way.
24717 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024719ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024720{
24721 list_T *l = vimvars[VV_OLDFILES].vv_list;
24722 listitem_T *li;
24723 int nr = 0;
24724
24725 if (l == NULL)
24726 msg((char_u *)_("No old files"));
24727 else
24728 {
24729 msg_start();
24730 msg_scroll = TRUE;
24731 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24732 {
24733 msg_outnum((long)++nr);
24734 MSG_PUTS(": ");
24735 msg_outtrans(get_tv_string(&li->li_tv));
24736 msg_putchar('\n');
24737 out_flush(); /* output one line at a time */
24738 ui_breakcheck();
24739 }
24740 /* Assume "got_int" was set to truncate the listing. */
24741 got_int = FALSE;
24742
24743#ifdef FEAT_BROWSE_CMD
24744 if (cmdmod.browse)
24745 {
24746 quit_more = FALSE;
24747 nr = prompt_for_number(FALSE);
24748 msg_starthere();
24749 if (nr > 0)
24750 {
24751 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24752 (long)nr);
24753
24754 if (p != NULL)
24755 {
24756 p = expand_env_save(p);
24757 eap->arg = p;
24758 eap->cmdidx = CMD_edit;
24759 cmdmod.browse = FALSE;
24760 do_exedit(eap, NULL);
24761 vim_free(p);
24762 }
24763 }
24764 }
24765#endif
24766 }
24767}
24768
Bram Moolenaar53744302015-07-17 17:38:22 +020024769/* reset v:option_new, v:option_old and v:option_type */
24770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024771reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024772{
24773 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24774 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24775 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24776}
24777
24778
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779#endif /* FEAT_EVAL */
24780
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024782#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783
24784#ifdef WIN3264
24785/*
24786 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24787 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024788static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24789static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24790static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791
24792/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024793 * Get the short path (8.3) for the filename in "fnamep".
24794 * Only works for a valid file name.
24795 * When the path gets longer "fnamep" is changed and the allocated buffer
24796 * is put in "bufp".
24797 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24798 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799 */
24800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024801get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024803 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 char_u *newbuf;
24805
24806 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 l = GetShortPathName(*fnamep, *fnamep, len);
24808 if (l > len - 1)
24809 {
24810 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024811 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812 newbuf = vim_strnsave(*fnamep, l);
24813 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024814 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815
24816 vim_free(*bufp);
24817 *fnamep = *bufp = newbuf;
24818
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024819 /* Really should always succeed, as the buffer is big enough. */
24820 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821 }
24822
24823 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024824 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024825}
24826
24827/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024828 * Get the short path (8.3) for the filename in "fname". The converted
24829 * path is returned in "bufp".
24830 *
24831 * Some of the directories specified in "fname" may not exist. This function
24832 * will shorten the existing directories at the beginning of the path and then
24833 * append the remaining non-existing path.
24834 *
24835 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024836 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024837 * bufp - Pointer to an allocated buffer for the filename.
24838 * fnamelen - Length of the filename pointed to by fname
24839 *
24840 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 */
24842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024843shortpath_for_invalid_fname(
24844 char_u **fname,
24845 char_u **bufp,
24846 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024848 char_u *short_fname, *save_fname, *pbuf_unused;
24849 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024851 int old_len, len;
24852 int new_len, sfx_len;
24853 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854
24855 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024856 old_len = *fnamelen;
24857 save_fname = vim_strnsave(*fname, old_len);
24858 pbuf_unused = NULL;
24859 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024861 endp = save_fname + old_len - 1; /* Find the end of the copy */
24862 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024864 /*
24865 * Try shortening the supplied path till it succeeds by removing one
24866 * directory at a time from the tail of the path.
24867 */
24868 len = 0;
24869 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024871 /* go back one path-separator */
24872 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24873 --endp;
24874 if (endp <= save_fname)
24875 break; /* processed the complete path */
24876
24877 /*
24878 * Replace the path separator with a NUL and try to shorten the
24879 * resulting path.
24880 */
24881 ch = *endp;
24882 *endp = 0;
24883 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024884 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024885 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24886 {
24887 retval = FAIL;
24888 goto theend;
24889 }
24890 *endp = ch; /* preserve the string */
24891
24892 if (len > 0)
24893 break; /* successfully shortened the path */
24894
24895 /* failed to shorten the path. Skip the path separator */
24896 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 }
24898
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024899 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024901 /*
24902 * Succeeded in shortening the path. Now concatenate the shortened
24903 * path with the remaining path at the tail.
24904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024906 /* Compute the length of the new path. */
24907 sfx_len = (int)(save_endp - endp) + 1;
24908 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024910 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024912 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024914 /* There is not enough space in the currently allocated string,
24915 * copy it to a buffer big enough. */
24916 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024918 {
24919 retval = FAIL;
24920 goto theend;
24921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 }
24923 else
24924 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024925 /* Transfer short_fname to the main buffer (it's big enough),
24926 * unless get_short_pathname() did its work in-place. */
24927 *fname = *bufp = save_fname;
24928 if (short_fname != save_fname)
24929 vim_strncpy(save_fname, short_fname, len);
24930 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024932
24933 /* concat the not-shortened part of the path */
24934 vim_strncpy(*fname + len, endp, sfx_len);
24935 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024937
24938theend:
24939 vim_free(pbuf_unused);
24940 vim_free(save_fname);
24941
24942 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943}
24944
24945/*
24946 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024947 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948 */
24949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024950shortpath_for_partial(
24951 char_u **fnamep,
24952 char_u **bufp,
24953 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954{
24955 int sepcount, len, tflen;
24956 char_u *p;
24957 char_u *pbuf, *tfname;
24958 int hasTilde;
24959
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024960 /* Count up the path separators from the RHS.. so we know which part
24961 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024962 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024963 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 if (vim_ispathsep(*p))
24965 ++sepcount;
24966
24967 /* Need full path first (use expand_env() to remove a "~/") */
24968 hasTilde = (**fnamep == '~');
24969 if (hasTilde)
24970 pbuf = tfname = expand_env_save(*fnamep);
24971 else
24972 pbuf = tfname = FullName_save(*fnamep, FALSE);
24973
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024974 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024976 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978
24979 if (len == 0)
24980 {
24981 /* Don't have a valid filename, so shorten the rest of the
24982 * path if we can. This CAN give us invalid 8.3 filenames, but
24983 * there's not a lot of point in guessing what it might be.
24984 */
24985 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024986 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24987 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 }
24989
24990 /* Count the paths backward to find the beginning of the desired string. */
24991 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024992 {
24993#ifdef FEAT_MBYTE
24994 if (has_mbyte)
24995 p -= mb_head_off(tfname, p);
24996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 if (vim_ispathsep(*p))
24998 {
24999 if (sepcount == 0 || (hasTilde && sepcount == 1))
25000 break;
25001 else
25002 sepcount --;
25003 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025005 if (hasTilde)
25006 {
25007 --p;
25008 if (p >= tfname)
25009 *p = '~';
25010 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025011 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 }
25013 else
25014 ++p;
25015
25016 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25017 vim_free(*bufp);
25018 *fnamelen = (int)STRLEN(p);
25019 *bufp = pbuf;
25020 *fnamep = p;
25021
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025022 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023}
25024#endif /* WIN3264 */
25025
25026/*
25027 * Adjust a filename, according to a string of modifiers.
25028 * *fnamep must be NUL terminated when called. When returning, the length is
25029 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025030 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 * When there is an error, *fnamep is set to NULL.
25032 */
25033 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025034modify_fname(
25035 char_u *src, /* string with modifiers */
25036 int *usedlen, /* characters after src that are used */
25037 char_u **fnamep, /* file name so far */
25038 char_u **bufp, /* buffer for allocated file name or NULL */
25039 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040{
25041 int valid = 0;
25042 char_u *tail;
25043 char_u *s, *p, *pbuf;
25044 char_u dirname[MAXPATHL];
25045 int c;
25046 int has_fullname = 0;
25047#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025048 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 int has_shortname = 0;
25050#endif
25051
25052repeat:
25053 /* ":p" - full path/file_name */
25054 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25055 {
25056 has_fullname = 1;
25057
25058 valid |= VALID_PATH;
25059 *usedlen += 2;
25060
25061 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25062 if ((*fnamep)[0] == '~'
25063#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25064 && ((*fnamep)[1] == '/'
25065# ifdef BACKSLASH_IN_FILENAME
25066 || (*fnamep)[1] == '\\'
25067# endif
25068 || (*fnamep)[1] == NUL)
25069
25070#endif
25071 )
25072 {
25073 *fnamep = expand_env_save(*fnamep);
25074 vim_free(*bufp); /* free any allocated file name */
25075 *bufp = *fnamep;
25076 if (*fnamep == NULL)
25077 return -1;
25078 }
25079
25080 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025081 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 {
25083 if (vim_ispathsep(*p)
25084 && p[1] == '.'
25085 && (p[2] == NUL
25086 || vim_ispathsep(p[2])
25087 || (p[2] == '.'
25088 && (p[3] == NUL || vim_ispathsep(p[3])))))
25089 break;
25090 }
25091
25092 /* FullName_save() is slow, don't use it when not needed. */
25093 if (*p != NUL || !vim_isAbsName(*fnamep))
25094 {
25095 *fnamep = FullName_save(*fnamep, *p != NUL);
25096 vim_free(*bufp); /* free any allocated file name */
25097 *bufp = *fnamep;
25098 if (*fnamep == NULL)
25099 return -1;
25100 }
25101
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025102#ifdef WIN3264
25103# if _WIN32_WINNT >= 0x0500
25104 if (vim_strchr(*fnamep, '~') != NULL)
25105 {
25106 /* Expand 8.3 filename to full path. Needed to make sure the same
25107 * file does not have two different names.
25108 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25109 p = alloc(_MAX_PATH + 1);
25110 if (p != NULL)
25111 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025112 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025113 {
25114 vim_free(*bufp);
25115 *bufp = *fnamep = p;
25116 }
25117 else
25118 vim_free(p);
25119 }
25120 }
25121# endif
25122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025123 /* Append a path separator to a directory. */
25124 if (mch_isdir(*fnamep))
25125 {
25126 /* Make room for one or two extra characters. */
25127 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25128 vim_free(*bufp); /* free any allocated file name */
25129 *bufp = *fnamep;
25130 if (*fnamep == NULL)
25131 return -1;
25132 add_pathsep(*fnamep);
25133 }
25134 }
25135
25136 /* ":." - path relative to the current directory */
25137 /* ":~" - path relative to the home directory */
25138 /* ":8" - shortname path - postponed till after */
25139 while (src[*usedlen] == ':'
25140 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25141 {
25142 *usedlen += 2;
25143 if (c == '8')
25144 {
25145#ifdef WIN3264
25146 has_shortname = 1; /* Postpone this. */
25147#endif
25148 continue;
25149 }
25150 pbuf = NULL;
25151 /* Need full path first (use expand_env() to remove a "~/") */
25152 if (!has_fullname)
25153 {
25154 if (c == '.' && **fnamep == '~')
25155 p = pbuf = expand_env_save(*fnamep);
25156 else
25157 p = pbuf = FullName_save(*fnamep, FALSE);
25158 }
25159 else
25160 p = *fnamep;
25161
25162 has_fullname = 0;
25163
25164 if (p != NULL)
25165 {
25166 if (c == '.')
25167 {
25168 mch_dirname(dirname, MAXPATHL);
25169 s = shorten_fname(p, dirname);
25170 if (s != NULL)
25171 {
25172 *fnamep = s;
25173 if (pbuf != NULL)
25174 {
25175 vim_free(*bufp); /* free any allocated file name */
25176 *bufp = pbuf;
25177 pbuf = NULL;
25178 }
25179 }
25180 }
25181 else
25182 {
25183 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25184 /* Only replace it when it starts with '~' */
25185 if (*dirname == '~')
25186 {
25187 s = vim_strsave(dirname);
25188 if (s != NULL)
25189 {
25190 *fnamep = s;
25191 vim_free(*bufp);
25192 *bufp = s;
25193 }
25194 }
25195 }
25196 vim_free(pbuf);
25197 }
25198 }
25199
25200 tail = gettail(*fnamep);
25201 *fnamelen = (int)STRLEN(*fnamep);
25202
25203 /* ":h" - head, remove "/file_name", can be repeated */
25204 /* Don't remove the first "/" or "c:\" */
25205 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25206 {
25207 valid |= VALID_HEAD;
25208 *usedlen += 2;
25209 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025210 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025211 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 *fnamelen = (int)(tail - *fnamep);
25213#ifdef VMS
25214 if (*fnamelen > 0)
25215 *fnamelen += 1; /* the path separator is part of the path */
25216#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025217 if (*fnamelen == 0)
25218 {
25219 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25220 p = vim_strsave((char_u *)".");
25221 if (p == NULL)
25222 return -1;
25223 vim_free(*bufp);
25224 *bufp = *fnamep = tail = p;
25225 *fnamelen = 1;
25226 }
25227 else
25228 {
25229 while (tail > s && !after_pathsep(s, tail))
25230 mb_ptr_back(*fnamep, tail);
25231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232 }
25233
25234 /* ":8" - shortname */
25235 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25236 {
25237 *usedlen += 2;
25238#ifdef WIN3264
25239 has_shortname = 1;
25240#endif
25241 }
25242
25243#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025244 /*
25245 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 */
25247 if (has_shortname)
25248 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025249 /* Copy the string if it is shortened by :h and when it wasn't copied
25250 * yet, because we are going to change it in place. Avoids changing
25251 * the buffer name for "%:8". */
25252 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 {
25254 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025255 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 return -1;
25257 vim_free(*bufp);
25258 *bufp = *fnamep = p;
25259 }
25260
25261 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025262 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263 if (!has_fullname && !vim_isAbsName(*fnamep))
25264 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025265 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 return -1;
25267 }
25268 else
25269 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025270 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271
Bram Moolenaardc935552011-08-17 15:23:23 +020025272 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025274 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 return -1;
25276
25277 if (l == 0)
25278 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025279 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025281 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 return -1;
25283 }
25284 *fnamelen = l;
25285 }
25286 }
25287#endif /* WIN3264 */
25288
25289 /* ":t" - tail, just the basename */
25290 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25291 {
25292 *usedlen += 2;
25293 *fnamelen -= (int)(tail - *fnamep);
25294 *fnamep = tail;
25295 }
25296
25297 /* ":e" - extension, can be repeated */
25298 /* ":r" - root, without extension, can be repeated */
25299 while (src[*usedlen] == ':'
25300 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25301 {
25302 /* find a '.' in the tail:
25303 * - for second :e: before the current fname
25304 * - otherwise: The last '.'
25305 */
25306 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25307 s = *fnamep - 2;
25308 else
25309 s = *fnamep + *fnamelen - 1;
25310 for ( ; s > tail; --s)
25311 if (s[0] == '.')
25312 break;
25313 if (src[*usedlen + 1] == 'e') /* :e */
25314 {
25315 if (s > tail)
25316 {
25317 *fnamelen += (int)(*fnamep - (s + 1));
25318 *fnamep = s + 1;
25319#ifdef VMS
25320 /* cut version from the extension */
25321 s = *fnamep + *fnamelen - 1;
25322 for ( ; s > *fnamep; --s)
25323 if (s[0] == ';')
25324 break;
25325 if (s > *fnamep)
25326 *fnamelen = s - *fnamep;
25327#endif
25328 }
25329 else if (*fnamep <= tail)
25330 *fnamelen = 0;
25331 }
25332 else /* :r */
25333 {
25334 if (s > tail) /* remove one extension */
25335 *fnamelen = (int)(s - *fnamep);
25336 }
25337 *usedlen += 2;
25338 }
25339
25340 /* ":s?pat?foo?" - substitute */
25341 /* ":gs?pat?foo?" - global substitute */
25342 if (src[*usedlen] == ':'
25343 && (src[*usedlen + 1] == 's'
25344 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25345 {
25346 char_u *str;
25347 char_u *pat;
25348 char_u *sub;
25349 int sep;
25350 char_u *flags;
25351 int didit = FALSE;
25352
25353 flags = (char_u *)"";
25354 s = src + *usedlen + 2;
25355 if (src[*usedlen + 1] == 'g')
25356 {
25357 flags = (char_u *)"g";
25358 ++s;
25359 }
25360
25361 sep = *s++;
25362 if (sep)
25363 {
25364 /* find end of pattern */
25365 p = vim_strchr(s, sep);
25366 if (p != NULL)
25367 {
25368 pat = vim_strnsave(s, (int)(p - s));
25369 if (pat != NULL)
25370 {
25371 s = p + 1;
25372 /* find end of substitution */
25373 p = vim_strchr(s, sep);
25374 if (p != NULL)
25375 {
25376 sub = vim_strnsave(s, (int)(p - s));
25377 str = vim_strnsave(*fnamep, *fnamelen);
25378 if (sub != NULL && str != NULL)
25379 {
25380 *usedlen = (int)(p + 1 - src);
25381 s = do_string_sub(str, pat, sub, flags);
25382 if (s != NULL)
25383 {
25384 *fnamep = s;
25385 *fnamelen = (int)STRLEN(s);
25386 vim_free(*bufp);
25387 *bufp = s;
25388 didit = TRUE;
25389 }
25390 }
25391 vim_free(sub);
25392 vim_free(str);
25393 }
25394 vim_free(pat);
25395 }
25396 }
25397 /* after using ":s", repeat all the modifiers */
25398 if (didit)
25399 goto repeat;
25400 }
25401 }
25402
Bram Moolenaar26df0922014-02-23 23:39:13 +010025403 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25404 {
25405 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25406 if (p == NULL)
25407 return -1;
25408 vim_free(*bufp);
25409 *bufp = *fnamep = p;
25410 *fnamelen = (int)STRLEN(p);
25411 *usedlen += 2;
25412 }
25413
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414 return valid;
25415}
25416
25417/*
25418 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25419 * "flags" can be "g" to do a global substitute.
25420 * Returns an allocated string, NULL for error.
25421 */
25422 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025423do_string_sub(
25424 char_u *str,
25425 char_u *pat,
25426 char_u *sub,
25427 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428{
25429 int sublen;
25430 regmatch_T regmatch;
25431 int i;
25432 int do_all;
25433 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025434 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 garray_T ga;
25436 char_u *ret;
25437 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025438 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439
25440 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25441 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025442 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443
25444 ga_init2(&ga, 1, 200);
25445
25446 do_all = (flags[0] == 'g');
25447
25448 regmatch.rm_ic = p_ic;
25449 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25450 if (regmatch.regprog != NULL)
25451 {
25452 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025453 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25455 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025456 /* Skip empty match except for first match. */
25457 if (regmatch.startp[0] == regmatch.endp[0])
25458 {
25459 if (zero_width == regmatch.startp[0])
25460 {
25461 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025462 i = MB_PTR2LEN(tail);
25463 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25464 (size_t)i);
25465 ga.ga_len += i;
25466 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025467 continue;
25468 }
25469 zero_width = regmatch.startp[0];
25470 }
25471
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472 /*
25473 * Get some space for a temporary buffer to do the substitution
25474 * into. It will contain:
25475 * - The text up to where the match is.
25476 * - The substituted text.
25477 * - The text after the match.
25478 */
25479 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025480 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25482 {
25483 ga_clear(&ga);
25484 break;
25485 }
25486
25487 /* copy the text up to where the match is */
25488 i = (int)(regmatch.startp[0] - tail);
25489 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25490 /* add the substituted text */
25491 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25492 + ga.ga_len + i, TRUE, TRUE, FALSE);
25493 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025494 tail = regmatch.endp[0];
25495 if (*tail == NUL)
25496 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497 if (!do_all)
25498 break;
25499 }
25500
25501 if (ga.ga_data != NULL)
25502 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25503
Bram Moolenaar473de612013-06-08 18:19:48 +020025504 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 }
25506
25507 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25508 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025509 if (p_cpo == empty_option)
25510 p_cpo = save_cpo;
25511 else
25512 /* Darn, evaluating {sub} expression changed the value. */
25513 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514
25515 return ret;
25516}
25517
25518#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */