blob: fc512aba93f38e3581fe977e55ba0299182d61ac [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);
817static typval_T *alloc_tv(void);
818static typval_T *alloc_string_tv(char_u *string);
819static void init_tv(typval_T *varp);
820static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100821#ifdef FEAT_FLOAT
822static float_T get_tv_float(typval_T *varp);
823#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static linenr_T get_tv_lnum(typval_T *argvars);
825static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
826static char_u *get_tv_string(typval_T *varp);
827static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
828static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
829static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
830static hashtab_T *find_var_ht(char_u *name, char_u **varname);
831static funccall_T *get_funccal(void);
832static void vars_clear_ext(hashtab_T *ht, int free_val);
833static void delete_var(hashtab_T *ht, hashitem_T *hi);
834static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
835static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
836static void set_var(char_u *name, typval_T *varp, int copy);
837static int var_check_ro(int flags, char_u *name, int use_gettext);
838static int var_check_fixed(int flags, char_u *name, int use_gettext);
839static int var_check_func_name(char_u *name, int new_var);
840static int valid_varname(char_u *varname);
841static int tv_check_lock(int lock, char_u *name, int use_gettext);
842static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
843static char_u *find_option_end(char_u **arg, int *opt_flags);
844static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
845static int eval_fname_script(char_u *p);
846static int eval_fname_sid(char_u *p);
847static void list_func_head(ufunc_T *fp, int indent);
848static ufunc_T *find_func(char_u *name);
849static int function_exists(char_u *name);
850static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static void func_do_profile(ufunc_T *fp);
853static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
854static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000855static int
856# ifdef __BORLANDC__
857 _RTLENTRYF
858# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000860static int
861# ifdef __BORLANDC__
862 _RTLENTRYF
863# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100866static int script_autoload(char_u *name, int reload);
867static char_u *autoload_name(char_u *name);
868static void cat_func_name(char_u *buf, ufunc_T *fp);
869static void func_free(ufunc_T *fp);
870static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
871static int can_free_funccal(funccall_T *fc, int copyID) ;
872static void free_funccal(funccall_T *fc, int free_val);
873static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
874static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
875static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
876static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
877static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
878static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
879static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
880static int write_list(FILE *fd, list_T *list, int binary);
881static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883
884#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static int compare_func_name(const void *s1, const void *s2);
886static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200887#endif
888
Bram Moolenaar33570922005-01-25 22:26:29 +0000889/*
890 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000891 */
892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000894{
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 int i;
896 struct vimvar *p;
897
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200898 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
899 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200900 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000901 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000902 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903
904 for (i = 0; i < VV_LEN; ++i)
905 {
906 p = &vimvars[i];
907 STRCPY(p->vv_di.di_key, p->vv_name);
908 if (p->vv_flags & VV_RO)
909 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
910 else if (p->vv_flags & VV_RO_SBX)
911 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
912 else
913 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000914
915 /* add to v: scope dict, unless the value is not always available */
916 if (p->vv_type != VAR_UNKNOWN)
917 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000919 /* add to compat scope dict */
920 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000922 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100923 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200924 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100925 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100926
927 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
928 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
929 set_vim_var_nr(VV_NONE, VVAL_NONE);
930 set_vim_var_nr(VV_NULL, VVAL_NULL);
931
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200932 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200933
934#ifdef EBCDIC
935 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100936 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200937 */
938 sortFunctions();
939#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000940}
941
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942#if defined(EXITFREE) || defined(PROTO)
943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100944eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945{
946 int i;
947 struct vimvar *p;
948
949 for (i = 0; i < VV_LEN; ++i)
950 {
951 p = &vimvars[i];
952 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000953 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000954 vim_free(p->vv_str);
955 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000956 }
957 else if (p->vv_di.di_tv.v_type == VAR_LIST)
958 {
959 list_unref(p->vv_list);
960 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000961 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962 }
963 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000964 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000965 hash_clear(&compat_hashtab);
966
Bram Moolenaard9fba312005-06-26 22:34:35 +0000967 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100968# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200969 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100970# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000971
972 /* global variables */
973 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000975 /* autoloaded script names */
976 ga_clear_strings(&ga_loaded);
977
Bram Moolenaarcca74132013-09-25 21:00:28 +0200978 /* Script-local variables. First clear all the variables and in a second
979 * loop free the scriptvar_T, because a variable in one script might hold
980 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200981 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200983 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200984 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200985 ga_clear(&ga_scripts);
986
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000987 /* unreferenced lists and dicts */
988 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000989
990 /* functions */
991 free_all_functions();
992 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993}
994#endif
995
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Return the name of the executed function.
998 */
999 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001000func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003}
1004
1005/*
1006 * Return the address holding the next breakpoint line for a funccall cookie.
1007 */
1008 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001009func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
Bram Moolenaar33570922005-01-25 22:26:29 +00001011 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012}
1013
1014/*
1015 * Return the address holding the debug tick for a funccall cookie.
1016 */
1017 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001018func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
Bram Moolenaar33570922005-01-25 22:26:29 +00001020 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021}
1022
1023/*
1024 * Return the nesting level for a funccall cookie.
1025 */
1026 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001027func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaar33570922005-01-25 22:26:29 +00001029 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001033funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001035/* pointer to list of previously used funccal, still around because some
1036 * item in it is still being used. */
1037funccall_T *previous_funccal = NULL;
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039/*
1040 * Return TRUE when a function was ended by a ":return" command.
1041 */
1042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001043current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
1045 return current_funccal->returned;
1046}
1047
1048
1049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 * Set an internal variable to a string value. Creates the variable if it does
1051 * not already exist.
1052 */
1053 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001054set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001056 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001057 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 val = vim_strsave(value);
1060 if (val != NULL)
1061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001062 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001063 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001065 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001066 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
1068 }
1069}
1070
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001072static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073static char_u *redir_endp = NULL;
1074static char_u *redir_varname = NULL;
1075
1076/*
1077 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 * Returns OK if successfully completed the setup. FAIL otherwise.
1080 */
1081 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001082var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083{
1084 int save_emsg;
1085 int err;
1086 typval_T tv;
1087
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001088 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001089 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 {
1091 EMSG(_(e_invarg));
1092 return FAIL;
1093 }
1094
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 redir_varname = vim_strsave(name);
1097 if (redir_varname == NULL)
1098 return FAIL;
1099
1100 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1101 if (redir_lval == NULL)
1102 {
1103 var_redir_stop();
1104 return FAIL;
1105 }
1106
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 /* The output is stored in growarray "redir_ga" until redirection ends. */
1108 ga_init2(&redir_ga, (int)sizeof(char), 500);
1109
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001111 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001112 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1114 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001115 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 if (redir_endp != NULL && *redir_endp != NUL)
1117 /* Trailing characters are present after the variable name */
1118 EMSG(_(e_trailing));
1119 else
1120 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001121 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 var_redir_stop();
1123 return FAIL;
1124 }
1125
1126 /* check if we can write to the variable: set it to or append an empty
1127 * string */
1128 save_emsg = did_emsg;
1129 did_emsg = FALSE;
1130 tv.v_type = VAR_STRING;
1131 tv.vval.v_string = (char_u *)"";
1132 if (append)
1133 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1134 else
1135 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001136 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001138 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (err)
1140 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 var_redir_stop();
1143 return FAIL;
1144 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145
1146 return OK;
1147}
1148
1149/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001150 * Append "value[value_len]" to the variable set by var_redir_start().
1151 * The actual appending is postponed until redirection ends, because the value
1152 * appended may in fact be the string we write to, changing it may cause freed
1153 * memory to be used:
1154 * :redir => foo
1155 * :let foo
1156 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 */
1158 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001159var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001161 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162
1163 if (redir_lval == NULL)
1164 return;
1165
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001167 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001169 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 {
1173 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001174 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001175 }
1176 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178}
1179
1180/*
1181 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001182 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 */
1184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001185var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187 typval_T tv;
1188
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 if (redir_lval != NULL)
1190 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 /* If there was no error: assign the text to the variable. */
1192 if (redir_endp != NULL)
1193 {
1194 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1195 tv.v_type = VAR_STRING;
1196 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001197 /* Call get_lval() again, if it's inside a Dict or List it may
1198 * have changed. */
1199 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001200 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001201 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1202 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1203 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001204 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 /* free the collected output */
1207 vim_free(redir_ga.ga_data);
1208 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 vim_free(redir_lval);
1211 redir_lval = NULL;
1212 }
1213 vim_free(redir_varname);
1214 redir_varname = NULL;
1215}
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217# if defined(FEAT_MBYTE) || defined(PROTO)
1218 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001219eval_charconvert(
1220 char_u *enc_from,
1221 char_u *enc_to,
1222 char_u *fname_from,
1223 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
1225 int err = FALSE;
1226
1227 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1228 set_vim_var_string(VV_CC_TO, enc_to, -1);
1229 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1230 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1231 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1232 err = TRUE;
1233 set_vim_var_string(VV_CC_FROM, NULL, -1);
1234 set_vim_var_string(VV_CC_TO, NULL, -1);
1235 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1236 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1237
1238 if (err)
1239 return FAIL;
1240 return OK;
1241}
1242# endif
1243
1244# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001246eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 int err = FALSE;
1249
1250 set_vim_var_string(VV_FNAME_IN, fname, -1);
1251 set_vim_var_string(VV_CMDARG, args, -1);
1252 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1253 err = TRUE;
1254 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1255 set_vim_var_string(VV_CMDARG, NULL, -1);
1256
1257 if (err)
1258 {
1259 mch_remove(fname);
1260 return FAIL;
1261 }
1262 return OK;
1263}
1264# endif
1265
1266# if defined(FEAT_DIFF) || defined(PROTO)
1267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268eval_diff(
1269 char_u *origfile,
1270 char_u *newfile,
1271 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 int err = FALSE;
1274
1275 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1276 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1277 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1278 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1281 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1282}
1283
1284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285eval_patch(
1286 char_u *origfile,
1287 char_u *difffile,
1288 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 int err;
1291
1292 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1293 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1294 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1295 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1296 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1297 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1298 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1299}
1300# endif
1301
1302/*
1303 * Top level evaluation function, returning a boolean.
1304 * Sets "error" to TRUE if there was an error.
1305 * Return TRUE or FALSE.
1306 */
1307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308eval_to_bool(
1309 char_u *arg,
1310 int *error,
1311 char_u **nextcmd,
1312 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
Bram Moolenaar33570922005-01-25 22:26:29 +00001314 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 int retval = FALSE;
1316
1317 if (skip)
1318 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 else
1322 {
1323 *error = FALSE;
1324 if (!skip)
1325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001326 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 }
1330 if (skip)
1331 --emsg_skip;
1332
1333 return retval;
1334}
1335
1336/*
1337 * Top level evaluation function, returning a string. If "skip" is TRUE,
1338 * only parsing to "nextcmd" is done, without reporting errors. Return
1339 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1340 */
1341 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001342eval_to_string_skip(
1343 char_u *arg,
1344 char_u **nextcmd,
1345 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346{
Bram Moolenaar33570922005-01-25 22:26:29 +00001347 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 char_u *retval;
1349
1350 if (skip)
1351 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 retval = vim_strsave(get_tv_string(&tv));
1357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 if (skip)
1360 --emsg_skip;
1361
1362 return retval;
1363}
1364
1365/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001366 * Skip over an expression at "*pp".
1367 * Return FAIL for an error, OK otherwise.
1368 */
1369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001370skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001371{
Bram Moolenaar33570922005-01-25 22:26:29 +00001372 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373
1374 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001376}
1377
1378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380 * When "convert" is TRUE convert a List into a sequence of lines and convert
1381 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 * Return pointer to allocated memory, or NULL for failure.
1383 */
1384 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001385eval_to_string(
1386 char_u *arg,
1387 char_u **nextcmd,
1388 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001393#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 retval = NULL;
1399 else
1400 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001401 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001402 {
1403 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001404 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001405 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001406 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001407 if (tv.vval.v_list->lv_len > 0)
1408 ga_append(&ga, NL);
1409 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001410 ga_append(&ga, NUL);
1411 retval = (char_u *)ga.ga_data;
1412 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001413#ifdef FEAT_FLOAT
1414 else if (convert && tv.v_type == VAR_FLOAT)
1415 {
1416 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1417 retval = vim_strsave(numbuf);
1418 }
1419#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 else
1421 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424
1425 return retval;
1426}
1427
1428/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 * Call eval_to_string() without using current local variables and using
1430 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 */
1432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001433eval_to_string_safe(
1434 char_u *arg,
1435 char_u **nextcmd,
1436 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 char_u *retval;
1439 void *save_funccalp;
1440
1441 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001442 if (use_sandbox)
1443 ++sandbox;
1444 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001445 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001446 if (use_sandbox)
1447 --sandbox;
1448 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 restore_funccal(save_funccalp);
1450 return retval;
1451}
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453/*
1454 * Top level evaluation function, returning a number.
1455 * Evaluates "expr" silently.
1456 * Returns -1 for an error.
1457 */
1458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001459eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaar33570922005-01-25 22:26:29 +00001461 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001463 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
1465 ++emsg_off;
1466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 retval = -1;
1469 else
1470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001471 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474 --emsg_off;
1475
1476 return retval;
1477}
1478
Bram Moolenaara40058a2005-07-11 22:42:07 +00001479/*
1480 * Prepare v: variable "idx" to be used.
1481 * Save the current typeval in "save_tv".
1482 * When not used yet add the variable to the v: hashtable.
1483 */
1484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001486{
1487 *save_tv = vimvars[idx].vv_tv;
1488 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1489 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1490}
1491
1492/*
1493 * Restore v: variable "idx" to typeval "save_tv".
1494 * When no longer defined, remove the variable from the v: hashtable.
1495 */
1496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001497restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498{
1499 hashitem_T *hi;
1500
Bram Moolenaara40058a2005-07-11 22:42:07 +00001501 vimvars[idx].vv_tv = *save_tv;
1502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1503 {
1504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1505 if (HASHITEM_EMPTY(hi))
1506 EMSG2(_(e_intern2), "restore_vimvar()");
1507 else
1508 hash_remove(&vimvarht, hi);
1509 }
1510}
1511
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001512#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001513/*
1514 * Evaluate an expression to a list with suggestions.
1515 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001516 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517 */
1518 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001520{
1521 typval_T save_val;
1522 typval_T rettv;
1523 list_T *list = NULL;
1524 char_u *p = skipwhite(expr);
1525
1526 /* Set "v:val" to the bad word. */
1527 prepare_vimvar(VV_VAL, &save_val);
1528 vimvars[VV_VAL].vv_type = VAR_STRING;
1529 vimvars[VV_VAL].vv_str = badword;
1530 if (p_verbose == 0)
1531 ++emsg_off;
1532
1533 if (eval1(&p, &rettv, TRUE) == OK)
1534 {
1535 if (rettv.v_type != VAR_LIST)
1536 clear_tv(&rettv);
1537 else
1538 list = rettv.vval.v_list;
1539 }
1540
1541 if (p_verbose == 0)
1542 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001543 restore_vimvar(VV_VAL, &save_val);
1544
1545 return list;
1546}
1547
1548/*
1549 * "list" is supposed to contain two items: a word and a number. Return the
1550 * word in "pp" and the number as the return value.
1551 * Return -1 if anything isn't right.
1552 * Used to get the good word and score from the eval_spell_expr() result.
1553 */
1554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001555get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001556{
1557 listitem_T *li;
1558
1559 li = list->lv_first;
1560 if (li == NULL)
1561 return -1;
1562 *pp = get_tv_string(&li->li_tv);
1563
1564 li = li->li_next;
1565 if (li == NULL)
1566 return -1;
1567 return get_tv_number(&li->li_tv);
1568}
1569#endif
1570
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001572 * Top level evaluation function.
1573 * Returns an allocated typval_T with the result.
1574 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575 */
1576 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001577eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001578{
1579 typval_T *tv;
1580
1581 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583 {
1584 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001585 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001586 }
1587
1588 return tv;
1589}
1590
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001594 * Uses argv[argc] for the function arguments. Only Number and String
1595 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001596 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001599call_vim_function(
1600 char_u *func,
1601 int argc,
1602 char_u **argv,
1603 int safe, /* use the sandbox */
1604 int str_arg_only, /* all arguments are strings */
1605 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
Bram Moolenaar33570922005-01-25 22:26:29 +00001607 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 long n;
1609 int len;
1610 int i;
1611 int doesrange;
1612 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001615 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 for (i = 0; i < argc; i++)
1620 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001621 /* Pass a NULL or empty argument as an empty string */
1622 if (argv[i] == NULL || *argv[i] == NUL)
1623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001624 argvars[i].v_type = VAR_STRING;
1625 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001626 continue;
1627 }
1628
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001629 if (str_arg_only)
1630 len = 0;
1631 else
1632 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001633 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (len != 0 && len == (int)STRLEN(argv[i]))
1635 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001636 argvars[i].v_type = VAR_NUMBER;
1637 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
1639 else
1640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001641 argvars[i].v_type = VAR_STRING;
1642 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 }
1645
1646 if (safe)
1647 {
1648 save_funccalp = save_funccal();
1649 ++sandbox;
1650 }
1651
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1653 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (safe)
1657 {
1658 --sandbox;
1659 restore_funccal(save_funccalp);
1660 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 vim_free(argvars);
1662
1663 if (ret == FAIL)
1664 clear_tv(rettv);
1665
1666 return ret;
1667}
1668
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001669/*
1670 * Call vimL function "func" and return the result as a number.
1671 * Returns -1 when calling the function fails.
1672 * Uses argv[argc] for the function arguments.
1673 */
1674 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675call_func_retnr(
1676 char_u *func,
1677 int argc,
1678 char_u **argv,
1679 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001680{
1681 typval_T rettv;
1682 long retval;
1683
1684 /* All arguments are passed as strings, no conversion to number. */
1685 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1686 return -1;
1687
1688 retval = get_tv_number_chk(&rettv, NULL);
1689 clear_tv(&rettv);
1690 return retval;
1691}
1692
1693#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1694 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1695
Bram Moolenaar4f688582007-07-24 12:34:30 +00001696# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001698 * Call vimL function "func" and return the result as a string.
1699 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 * Uses argv[argc] for the function arguments.
1701 */
1702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703call_func_retstr(
1704 char_u *func,
1705 int argc,
1706 char_u **argv,
1707 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708{
1709 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001710 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001712 /* All arguments are passed as strings, no conversion to number. */
1713 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 return NULL;
1715
1716 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 return retval;
1719}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001722/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001723 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001725 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726 */
1727 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728call_func_retlist(
1729 char_u *func,
1730 int argc,
1731 char_u **argv,
1732 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733{
1734 typval_T rettv;
1735
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001736 /* All arguments are passed as strings, no conversion to number. */
1737 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 return NULL;
1739
1740 if (rettv.v_type != VAR_LIST)
1741 {
1742 clear_tv(&rettv);
1743 return NULL;
1744 }
1745
1746 return rettv.vval.v_list;
1747}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#endif
1749
1750/*
1751 * Save the current function call pointer, and set it to NULL.
1752 * Used when executing autocommands and for ":source".
1753 */
1754 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001757 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 current_funccal = NULL;
1760 return (void *)fc;
1761}
1762
1763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001764restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001766 funccall_T *fc = (funccall_T *)vfc;
1767
1768 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
Bram Moolenaar05159a02005-02-26 23:04:13 +00001771#if defined(FEAT_PROFILE) || defined(PROTO)
1772/*
1773 * Prepare profiling for entering a child or something else that is not
1774 * counted for the script/function itself.
1775 * Should always be called in pair with prof_child_exit().
1776 */
1777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778prof_child_enter(
1779 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001780{
1781 funccall_T *fc = current_funccal;
1782
1783 if (fc != NULL && fc->func->uf_profiling)
1784 profile_start(&fc->prof_child);
1785 script_prof_save(tm);
1786}
1787
1788/*
1789 * Take care of time spent in a child.
1790 * Should always be called after prof_child_enter().
1791 */
1792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001793prof_child_exit(
1794 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795{
1796 funccall_T *fc = current_funccal;
1797
1798 if (fc != NULL && fc->func->uf_profiling)
1799 {
1800 profile_end(&fc->prof_child);
1801 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1802 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1803 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1804 }
1805 script_prof_restore(tm);
1806}
1807#endif
1808
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#ifdef FEAT_FOLDING
1811/*
1812 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1813 * it in "*cp". Doesn't give error messages.
1814 */
1815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
Bram Moolenaar33570922005-01-25 22:26:29 +00001818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 int retval;
1820 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001821 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1822 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001825 if (use_sandbox)
1826 ++sandbox;
1827 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 retval = 0;
1831 else
1832 {
1833 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 if (tv.v_type == VAR_NUMBER)
1835 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001836 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 retval = 0;
1838 else
1839 {
1840 /* If the result is a string, check if there is a non-digit before
1841 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (!VIM_ISDIGIT(*s) && *s != '-')
1844 *cp = *s++;
1845 retval = atol((char *)s);
1846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001850 if (use_sandbox)
1851 --sandbox;
1852 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 return retval;
1855}
1856#endif
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 * ":let" list all variable values
1860 * ":let var1 var2" list variable values
1861 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 * ":let var += expr" assignment command.
1863 * ":let var -= expr" assignment command.
1864 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 */
1867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001868ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869{
1870 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 int var_count = 0;
1875 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 argend = skip_var_list(arg, &var_count, &semicolon);
1881 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001883 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1884 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001885 expr = skipwhite(argend);
1886 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1887 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001889 /*
1890 * ":let" without "=": list variables
1891 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 if (*arg == '[')
1893 EMSG(_(e_invarg));
1894 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001896 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_glob_vars(&first);
1901 list_buf_vars(&first);
1902 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001904 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_script_vars(&first);
1907 list_func_vars(&first);
1908 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 eap->nextcmd = check_nextcmd(arg);
1911 }
1912 else
1913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 op[0] = '=';
1915 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1919 op[0] = *expr; /* +=, -= or .= */
1920 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001922 else
1923 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (eap->skip)
1929 {
1930 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 --emsg_skip;
1933 }
1934 else if (i != FAIL)
1935 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 }
1941}
1942
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943/*
1944 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1945 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 * When "nextchars" is not NULL it points to a string with characters that
1947 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1948 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 * Returns OK or FAIL;
1950 */
1951 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001952ex_let_vars(
1953 char_u *arg_start,
1954 typval_T *tv,
1955 int copy, /* copy values from "tv", don't move */
1956 int semicolon, /* from skip_var_list() */
1957 int var_count, /* from skip_var_list() */
1958 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959{
1960 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 listitem_T *item;
1964 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965
1966 if (*arg != '[')
1967 {
1968 /*
1969 * ":let var = expr" or ":for var in list"
1970 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 return FAIL;
1973 return OK;
1974 }
1975
1976 /*
1977 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1978 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001979 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 {
1981 EMSG(_(e_listreq));
1982 return FAIL;
1983 }
1984
1985 i = list_len(l);
1986 if (semicolon == 0 && var_count < i)
1987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001988 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 }
1991 if (var_count - semicolon > i)
1992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001993 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 }
1996
1997 item = l->lv_first;
1998 while (*arg != ']')
1999 {
2000 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 item = item->li_next;
2003 if (arg == NULL)
2004 return FAIL;
2005
2006 arg = skipwhite(arg);
2007 if (*arg == ';')
2008 {
2009 /* Put the rest of the list (may be empty) in the var after ';'.
2010 * Create a new list for this. */
2011 l = list_alloc();
2012 if (l == NULL)
2013 return FAIL;
2014 while (item != NULL)
2015 {
2016 list_append_tv(l, &item->li_tv);
2017 item = item->li_next;
2018 }
2019
2020 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002021 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 ltv.vval.v_list = l;
2023 l->lv_refcount = 1;
2024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2026 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 clear_tv(&ltv);
2028 if (arg == NULL)
2029 return FAIL;
2030 break;
2031 }
2032 else if (*arg != ',' && *arg != ']')
2033 {
2034 EMSG2(_(e_intern2), "ex_let_vars()");
2035 return FAIL;
2036 }
2037 }
2038
2039 return OK;
2040}
2041
2042/*
2043 * Skip over assignable variable "var" or list of variables "[var, var]".
2044 * Used for ":let varvar = expr" and ":for varvar in expr".
2045 * For "[var, var]" increment "*var_count" for each variable.
2046 * for "[var, var; var]" set "semicolon".
2047 * Return NULL for an error.
2048 */
2049 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002050skip_var_list(
2051 char_u *arg,
2052 int *var_count,
2053 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054{
2055 char_u *p, *s;
2056
2057 if (*arg == '[')
2058 {
2059 /* "[var, var]": find the matching ']'. */
2060 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002061 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 {
2063 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2064 s = skip_var_one(p);
2065 if (s == p)
2066 {
2067 EMSG2(_(e_invarg2), p);
2068 return NULL;
2069 }
2070 ++*var_count;
2071
2072 p = skipwhite(s);
2073 if (*p == ']')
2074 break;
2075 else if (*p == ';')
2076 {
2077 if (*semicolon == 1)
2078 {
2079 EMSG(_("Double ; in list of variables"));
2080 return NULL;
2081 }
2082 *semicolon = 1;
2083 }
2084 else if (*p != ',')
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 }
2090 return p + 1;
2091 }
2092 else
2093 return skip_var_one(arg);
2094}
2095
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002097 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002101skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002103 if (*arg == '@' && arg[1] != NUL)
2104 return arg + 2;
2105 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2106 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107}
2108
Bram Moolenaara7043832005-01-21 11:56:39 +00002109/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 * List variables for hashtab "ht" with prefix "prefix".
2111 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002114list_hashtable_vars(
2115 hashtab_T *ht,
2116 char_u *prefix,
2117 int empty,
2118 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 hashitem_T *hi;
2121 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 int todo;
2123
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002124 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2126 {
2127 if (!HASHITEM_EMPTY(hi))
2128 {
2129 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 di = HI2DI(hi);
2131 if (empty || di->di_tv.v_type != VAR_STRING
2132 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134 }
2135 }
2136}
2137
2138/*
2139 * List global variables.
2140 */
2141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002142list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
2147/*
2148 * List buffer variables.
2149 */
2150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002152{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002153 char_u numbuf[NUMBUFLEN];
2154
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002157
2158 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2160 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
2163/*
2164 * List window variables.
2165 */
2166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002168{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002169 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002171}
2172
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173#ifdef FEAT_WINDOWS
2174/*
2175 * List tab page variables.
2176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002180 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182}
2183#endif
2184
Bram Moolenaara7043832005-01-21 11:56:39 +00002185/*
2186 * List Vim variables.
2187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192}
2193
2194/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195 * List script-local variables, if there is a script.
2196 */
2197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002198list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002199{
2200 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2202 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203}
2204
2205/*
2206 * List function variables, if there is a function.
2207 */
2208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210{
2211 if (current_funccal != NULL)
2212 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214}
2215
2216/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 * List variables in "arg".
2218 */
2219 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002220list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221{
2222 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 char_u *name_start;
2226 char_u *arg_subsc;
2227 char_u *tofree;
2228 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229
2230 while (!ends_excmd(*arg) && !got_int)
2231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002234 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2236 {
2237 emsg_severe = TRUE;
2238 EMSG(_(e_trailing));
2239 break;
2240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 /* get_name_len() takes care of expanding curly braces */
2245 name_start = name = arg;
2246 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2247 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* This is mainly to keep test 49 working: when expanding
2250 * curly braces fails overrule the exception error message. */
2251 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 emsg_severe = TRUE;
2254 EMSG2(_(e_invarg2), arg);
2255 break;
2256 }
2257 error = TRUE;
2258 }
2259 else
2260 {
2261 if (tofree != NULL)
2262 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002263 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 else
2266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 /* handle d.key, l[idx], f(expr) */
2268 arg_subsc = arg;
2269 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 case 'g': list_glob_vars(first); break;
2278 case 'b': list_buf_vars(first); break;
2279 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002280#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002281 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002282#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 case 'v': list_vim_vars(first); break;
2284 case 's': list_script_vars(first); break;
2285 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 default:
2287 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 }
2290 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 {
2292 char_u numbuf[NUMBUFLEN];
2293 char_u *tf;
2294 int c;
2295 char_u *s;
2296
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002297 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 c = *arg;
2299 *arg = NUL;
2300 list_one_var_a((char_u *)"",
2301 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 tv.v_type,
2303 s == NULL ? (char_u *)"" : s,
2304 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 *arg = c;
2306 vim_free(tf);
2307 }
2308 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 }
2311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315
2316 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318
2319 return arg;
2320}
2321
2322/*
2323 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2324 * Returns a pointer to the char just after the var name.
2325 * Returns NULL if there is an error.
2326 */
2327 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328ex_let_one(
2329 char_u *arg, /* points to variable name */
2330 typval_T *tv, /* value to assign to variable */
2331 int copy, /* copy value from "tv" */
2332 char_u *endchars, /* valid chars after variable name or NULL */
2333 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334{
2335 int c1;
2336 char_u *name;
2337 char_u *p;
2338 char_u *arg_end = NULL;
2339 int len;
2340 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342
2343 /*
2344 * ":let $VAR = expr": Set environment variable.
2345 */
2346 if (*arg == '$')
2347 {
2348 /* Find the end of the name. */
2349 ++arg;
2350 name = arg;
2351 len = get_env_len(&arg);
2352 if (len == 0)
2353 EMSG2(_(e_invarg2), name - 1);
2354 else
2355 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 if (op != NULL && (*op == '+' || *op == '-'))
2357 EMSG2(_(e_letwrong), op);
2358 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002359 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002361 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 {
2363 c1 = name[len];
2364 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 p = get_tv_string_chk(tv);
2366 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 {
2368 int mustfree = FALSE;
2369 char_u *s = vim_getenv(name, &mustfree);
2370
2371 if (s != NULL)
2372 {
2373 p = tofree = concat_str(s, p);
2374 if (mustfree)
2375 vim_free(s);
2376 }
2377 }
2378 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002381 if (STRICMP(name, "HOME") == 0)
2382 init_homedir();
2383 else if (didset_vim && STRICMP(name, "VIM") == 0)
2384 didset_vim = FALSE;
2385 else if (didset_vimruntime
2386 && STRICMP(name, "VIMRUNTIME") == 0)
2387 didset_vimruntime = FALSE;
2388 arg_end = arg;
2389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 }
2393 }
2394 }
2395
2396 /*
2397 * ":let &option = expr": Set option value.
2398 * ":let &l:option = expr": Set local option value.
2399 * ":let &g:option = expr": Set global option value.
2400 */
2401 else if (*arg == '&')
2402 {
2403 /* Find the end of the name. */
2404 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002405 if (p == NULL || (endchars != NULL
2406 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 EMSG(_(e_letunexp));
2408 else
2409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 long n;
2411 int opt_type;
2412 long numval;
2413 char_u *stringval = NULL;
2414 char_u *s;
2415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 c1 = *p;
2417 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418
2419 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 s = get_tv_string_chk(tv); /* != NULL if number or string */
2421 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 {
2423 opt_type = get_option_value(arg, &numval,
2424 &stringval, opt_flags);
2425 if ((opt_type == 1 && *op == '.')
2426 || (opt_type == 0 && *op != '.'))
2427 EMSG2(_(e_letwrong), op);
2428 else
2429 {
2430 if (opt_type == 1) /* number */
2431 {
2432 if (*op == '+')
2433 n = numval + n;
2434 else
2435 n = numval - n;
2436 }
2437 else if (opt_type == 0 && stringval != NULL) /* string */
2438 {
2439 s = concat_str(stringval, s);
2440 vim_free(stringval);
2441 stringval = s;
2442 }
2443 }
2444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 if (s != NULL)
2446 {
2447 set_option_value(arg, n, s, opt_flags);
2448 arg_end = p;
2449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454
2455 /*
2456 * ":let @r = expr": Set register contents.
2457 */
2458 else if (*arg == '@')
2459 {
2460 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 if (op != NULL && (*op == '+' || *op == '-'))
2462 EMSG2(_(e_letwrong), op);
2463 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 EMSG(_(e_letunexp));
2466 else
2467 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002468 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 char_u *s;
2470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 p = get_tv_string_chk(tv);
2472 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002474 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 if (s != NULL)
2476 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002477 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 vim_free(s);
2479 }
2480 }
2481 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 arg_end = arg + 1;
2485 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002486 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
2488 }
2489
2490 /*
2491 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002494 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002498 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2502 EMSG(_(e_letunexp));
2503 else
2504 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 arg_end = p;
2507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 }
2511
2512 else
2513 EMSG2(_(e_invarg2), arg);
2514
2515 return arg_end;
2516}
2517
2518/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2520 */
2521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002522check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523{
2524 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2525 {
2526 EMSG2(_(e_readonlyvar), arg);
2527 return TRUE;
2528 }
2529 return FALSE;
2530}
2531
2532/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 * Get an lval: variable, Dict item or List item that can be assigned a value
2534 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2535 * "name.key", "name.key[expr]" etc.
2536 * Indexing only works if "name" is an existing List or Dictionary.
2537 * "name" points to the start of the name.
2538 * If "rettv" is not NULL it points to the value to be assigned.
2539 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2540 * wrong; must end in space or cmd separator.
2541 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542 * flags:
2543 * GLV_QUIET: do not give error messages
2544 * GLV_NO_AUTOLOAD: do not use script autoloading
2545 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002547 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 */
2550 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002551get_lval(
2552 char_u *name,
2553 typval_T *rettv,
2554 lval_T *lp,
2555 int unlet,
2556 int skip,
2557 int flags, /* GLV_ values */
2558 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 char_u *expr_start, *expr_end;
2562 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 dictitem_T *v;
2564 typval_T var1;
2565 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002571 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575
2576 if (skip)
2577 {
2578 /* When skipping just find the end of the name. */
2579 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002580 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 }
2582
2583 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002584 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (expr_start != NULL)
2586 {
2587 /* Don't expand the name when we already know there is an error. */
2588 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2589 && *p != '[' && *p != '.')
2590 {
2591 EMSG(_(e_trailing));
2592 return NULL;
2593 }
2594
2595 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2596 if (lp->ll_exp_name == NULL)
2597 {
2598 /* Report an invalid expression in braces, unless the
2599 * expression evaluation has been cancelled due to an
2600 * aborting error, an interrupt, or an exception. */
2601 if (!aborting() && !quiet)
2602 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002603 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 EMSG2(_(e_invarg2), name);
2605 return NULL;
2606 }
2607 }
2608 lp->ll_name = lp->ll_exp_name;
2609 }
2610 else
2611 lp->ll_name = name;
2612
2613 /* Without [idx] or .key we are done. */
2614 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2615 return p;
2616
2617 cc = *p;
2618 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002619 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (v == NULL && !quiet)
2621 EMSG2(_(e_undefvar), lp->ll_name);
2622 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 if (v == NULL)
2624 return NULL;
2625
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 /*
2627 * Loop until no more [idx] or .key is following.
2628 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2633 && !(lp->ll_tv->v_type == VAR_DICT
2634 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (!quiet)
2637 EMSG(_("E689: Can only index a List or Dictionary"));
2638 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_("E708: [:] must come last"));
2644 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 len = -1;
2648 if (*p == '.')
2649 {
2650 key = p + 1;
2651 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2652 ;
2653 if (len == 0)
2654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_(e_emptykey));
2657 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659 p = key + len;
2660 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 else
2662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (*p == ':')
2666 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 else
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 empty1 = FALSE;
2670 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002672 if (get_tv_string_chk(&var1) == NULL)
2673 {
2674 /* not a number or string */
2675 clear_tv(&var1);
2676 return NULL;
2677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
2679
2680 /* Optionally get the second index [ :expr]. */
2681 if (*p == ':')
2682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (rettv != NULL && (rettv->v_type != VAR_LIST
2692 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (!quiet)
2695 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (!empty1)
2697 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700 p = skipwhite(p + 1);
2701 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 else
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2707 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (!empty1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002712 if (get_tv_string_chk(&var2) == NULL)
2713 {
2714 /* not a number or string */
2715 if (!empty1)
2716 clear_tv(&var1);
2717 clear_tv(&var2);
2718 return NULL;
2719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (!quiet)
2729 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (!empty1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736
2737 /* Skip to past ']'. */
2738 ++p;
2739 }
2740
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 {
2743 if (len == -1)
2744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002746 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (*key == NUL)
2748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!quiet)
2750 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 }
2754 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755 lp->ll_list = NULL;
2756 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002757 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002759 /* When assigning to a scope dictionary check that a function and
2760 * variable name is valid (only variable name unless it is l: or
2761 * g: dictionary). Disallow overwriting a builtin function. */
2762 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002764 int prevval;
2765 int wrong;
2766
2767 if (len != -1)
2768 {
2769 prevval = key[len];
2770 key[len] = NUL;
2771 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002772 else
2773 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2775 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002776 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002777 || !valid_varname(key);
2778 if (len != -1)
2779 key[len] = prevval;
2780 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781 return NULL;
2782 }
2783
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 /* Can't add "v:" variable. */
2787 if (lp->ll_dict == &vimvardict)
2788 {
2789 EMSG2(_(e_illvar), name);
2790 return NULL;
2791 }
2792
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002793 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002797 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 if (len == -1)
2807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 p = NULL;
2810 break;
2811 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002812 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002813 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002814 return NULL;
2815
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 }
2820 else
2821 {
2822 /*
2823 * Get the number and item for the only or first index of the List.
2824 */
2825 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 else
2828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002829 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 clear_tv(&var1);
2831 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002832 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_list = lp->ll_tv->vval.v_list;
2834 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2835 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002837 if (lp->ll_n1 < 0)
2838 {
2839 lp->ll_n1 = 0;
2840 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2841 }
2842 }
2843 if (lp->ll_li == NULL)
2844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 if (!quiet)
2848 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 }
2851
2852 /*
2853 * May need to find the item or absolute index for the second
2854 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 * When no index given: "lp->ll_empty2" is TRUE.
2856 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002860 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 {
2867 if (!quiet)
2868 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 }
2873
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2875 if (lp->ll_n1 < 0)
2876 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2877 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002878 {
2879 if (!quiet)
2880 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002887 }
2888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 return p;
2890}
2891
2892/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 */
2895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002896clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897{
2898 vim_free(lp->ll_exp_name);
2899 vim_free(lp->ll_newkey);
2900}
2901
2902/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002903 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 */
2907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002908set_var_lval(
2909 lval_T *lp,
2910 char_u *endp,
2911 typval_T *rettv,
2912 int copy,
2913 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914{
2915 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 listitem_T *ri;
2917 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918
2919 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 cc = *endp;
2924 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 if (op != NULL && *op != '=')
2926 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002929 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002930 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002931 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002932 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002934 if ((di == NULL
2935 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2936 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2937 FALSE)))
2938 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939 set_var(lp->ll_name, &tv, FALSE);
2940 clear_tv(&tv);
2941 }
2942 }
2943 else
2944 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 else if (tv_check_lock(lp->ll_newkey == NULL
2949 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002950 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 else if (lp->ll_range)
2953 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002954 listitem_T *ll_li = lp->ll_li;
2955 int ll_n1 = lp->ll_n1;
2956
2957 /*
2958 * Check whether any of the list items is locked
2959 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002960 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002962 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002963 return;
2964 ri = ri->li_next;
2965 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2966 break;
2967 ll_li = ll_li->li_next;
2968 ++ll_n1;
2969 }
2970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 /*
2972 * Assign the List values to the list items.
2973 */
2974 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 if (op != NULL && *op != '=')
2977 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2978 else
2979 {
2980 clear_tv(&lp->ll_li->li_tv);
2981 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2982 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 ri = ri->li_next;
2984 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2985 break;
2986 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002989 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 ri = NULL;
2992 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002993 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 lp->ll_li = lp->ll_li->li_next;
2996 ++lp->ll_n1;
2997 }
2998 if (ri != NULL)
2999 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 else if (lp->ll_empty2
3001 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 : lp->ll_n1 != lp->ll_n2)
3003 EMSG(_("E711: List value has not enough items"));
3004 }
3005 else
3006 {
3007 /*
3008 * Assign to a List or Dictionary item.
3009 */
3010 if (lp->ll_newkey != NULL)
3011 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 if (op != NULL && *op != '=')
3013 {
3014 EMSG2(_(e_letwrong), op);
3015 return;
3016 }
3017
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 if (di == NULL)
3021 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003022 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3023 {
3024 vim_free(di);
3025 return;
3026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003028 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 else if (op != NULL && *op != '=')
3030 {
3031 tv_op(lp->ll_tv, rettv, op);
3032 return;
3033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003034 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 /*
3038 * Assign the value to the variable or list item.
3039 */
3040 if (copy)
3041 copy_tv(rettv, lp->ll_tv);
3042 else
3043 {
3044 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003045 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003047 }
3048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049}
3050
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3053 * Returns OK or FAIL.
3054 */
3055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003056tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057{
3058 long n;
3059 char_u numbuf[NUMBUFLEN];
3060 char_u *s;
3061
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003062 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3063 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3064 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003065 {
3066 switch (tv1->v_type)
3067 {
3068 case VAR_DICT:
3069 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003070 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 break;
3072
3073 case VAR_LIST:
3074 if (*op != '+' || tv2->v_type != VAR_LIST)
3075 break;
3076 /* List += List */
3077 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3078 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3079 return OK;
3080
3081 case VAR_NUMBER:
3082 case VAR_STRING:
3083 if (tv2->v_type == VAR_LIST)
3084 break;
3085 if (*op == '+' || *op == '-')
3086 {
3087 /* nr += nr or nr -= nr*/
3088 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089#ifdef FEAT_FLOAT
3090 if (tv2->v_type == VAR_FLOAT)
3091 {
3092 float_T f = n;
3093
3094 if (*op == '+')
3095 f += tv2->vval.v_float;
3096 else
3097 f -= tv2->vval.v_float;
3098 clear_tv(tv1);
3099 tv1->v_type = VAR_FLOAT;
3100 tv1->vval.v_float = f;
3101 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#endif
3104 {
3105 if (*op == '+')
3106 n += get_tv_number(tv2);
3107 else
3108 n -= get_tv_number(tv2);
3109 clear_tv(tv1);
3110 tv1->v_type = VAR_NUMBER;
3111 tv1->vval.v_number = n;
3112 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 }
3114 else
3115 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116 if (tv2->v_type == VAR_FLOAT)
3117 break;
3118
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 /* str .= str */
3120 s = get_tv_string(tv1);
3121 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_STRING;
3124 tv1->vval.v_string = s;
3125 }
3126 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127
3128#ifdef FEAT_FLOAT
3129 case VAR_FLOAT:
3130 {
3131 float_T f;
3132
3133 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3134 && tv2->v_type != VAR_NUMBER
3135 && tv2->v_type != VAR_STRING))
3136 break;
3137 if (tv2->v_type == VAR_FLOAT)
3138 f = tv2->vval.v_float;
3139 else
3140 f = get_tv_number(tv2);
3141 if (*op == '+')
3142 tv1->vval.v_float += f;
3143 else
3144 tv1->vval.v_float -= f;
3145 }
3146 return OK;
3147#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 }
3149 }
3150
3151 EMSG2(_(e_letwrong), op);
3152 return FAIL;
3153}
3154
3155/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156 * Add a watcher to a list.
3157 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003159list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160{
3161 lw->lw_next = l->lv_watch;
3162 l->lv_watch = lw;
3163}
3164
3165/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003166 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167 * No warning when it isn't found...
3168 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003170list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171{
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
3174 lwp = &l->lv_watch;
3175 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3176 {
3177 if (lw == lwrem)
3178 {
3179 *lwp = lw->lw_next;
3180 break;
3181 }
3182 lwp = &lw->lw_next;
3183 }
3184}
3185
3186/*
3187 * Just before removing an item from a list: advance watchers to the next
3188 * item.
3189 */
3190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003191list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192{
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 if (lw->lw_item == item)
3197 lw->lw_item = item->li_next;
3198}
3199
3200/*
3201 * Evaluate the expression used in a ":for var in expr" command.
3202 * "arg" points to "var".
3203 * Set "*errp" to TRUE for an error, FALSE otherwise;
3204 * Return a pointer that holds the info. Null when there is an error.
3205 */
3206 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003207eval_for_line(
3208 char_u *arg,
3209 int *errp,
3210 char_u **nextcmdp,
3211 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 typval_T tv;
3216 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217
3218 *errp = TRUE; /* default: there is an error */
3219
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 if (fi == NULL)
3222 return NULL;
3223
3224 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3225 if (expr == NULL)
3226 return fi;
3227
3228 expr = skipwhite(expr);
3229 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3230 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003231 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 return fi;
3233 }
3234
3235 if (skip)
3236 ++emsg_skip;
3237 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3238 {
3239 *errp = FALSE;
3240 if (!skip)
3241 {
3242 l = tv.vval.v_list;
3243 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003246 clear_tv(&tv);
3247 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 else
3249 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003250 /* No need to increment the refcount, it's already set for the
3251 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 fi->fi_list = l;
3253 list_add_watch(l, &fi->fi_lw);
3254 fi->fi_lw.lw_item = l->lv_first;
3255 }
3256 }
3257 }
3258 if (skip)
3259 --emsg_skip;
3260
3261 return fi;
3262}
3263
3264/*
3265 * Use the first item in a ":for" list. Advance to the next.
3266 * Assign the values to the variable (list). "arg" points to the first one.
3267 * Return TRUE when a valid item was found, FALSE when at end of list or
3268 * something wrong.
3269 */
3270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003273 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276
3277 item = fi->fi_lw.lw_item;
3278 if (item == NULL)
3279 result = FALSE;
3280 else
3281 {
3282 fi->fi_lw.lw_item = item->li_next;
3283 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3284 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3285 }
3286 return result;
3287}
3288
3289/*
3290 * Free the structure used to store info used by ":for".
3291 */
3292 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294{
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003297 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003298 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003300 list_unref(fi->fi_list);
3301 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 vim_free(fi);
3303}
3304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3306
3307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003308set_context_for_expression(
3309 expand_T *xp,
3310 char_u *arg,
3311 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 int got_eq = FALSE;
3314 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 if (cmdidx == CMD_let)
3318 {
3319 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003320 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 {
3322 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003323 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 {
3325 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 if (vim_iswhite(*p))
3328 break;
3329 }
3330 return;
3331 }
3332 }
3333 else
3334 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3335 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 while ((xp->xp_pattern = vim_strpbrk(arg,
3337 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3338 {
3339 c = *xp->xp_pattern;
3340 if (c == '&')
3341 {
3342 c = xp->xp_pattern[1];
3343 if (c == '&')
3344 {
3345 ++xp->xp_pattern;
3346 xp->xp_context = cmdidx != CMD_let || got_eq
3347 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3348 }
3349 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003352 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3353 xp->xp_pattern += 2;
3354
3355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357 else if (c == '$')
3358 {
3359 /* environment variable */
3360 xp->xp_context = EXPAND_ENV_VARS;
3361 }
3362 else if (c == '=')
3363 {
3364 got_eq = TRUE;
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003367 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 && xp->xp_context == EXPAND_FUNCTIONS
3369 && vim_strchr(xp->xp_pattern, '(') == NULL)
3370 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003371 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 break;
3373 }
3374 else if (cmdidx != CMD_let || got_eq)
3375 {
3376 if (c == '"') /* string */
3377 {
3378 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3379 if (c == '\\' && xp->xp_pattern[1] != NUL)
3380 ++xp->xp_pattern;
3381 xp->xp_context = EXPAND_NOTHING;
3382 }
3383 else if (c == '\'') /* literal string */
3384 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003385 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3387 /* skip */ ;
3388 xp->xp_context = EXPAND_NOTHING;
3389 }
3390 else if (c == '|')
3391 {
3392 if (xp->xp_pattern[1] == '|')
3393 {
3394 ++xp->xp_pattern;
3395 xp->xp_context = EXPAND_EXPRESSION;
3396 }
3397 else
3398 xp->xp_context = EXPAND_COMMANDS;
3399 }
3400 else
3401 xp->xp_context = EXPAND_EXPRESSION;
3402 }
3403 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003404 /* Doesn't look like something valid, expand as an expression
3405 * anyway. */
3406 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 arg = xp->xp_pattern;
3408 if (*arg != NUL)
3409 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3410 /* skip */ ;
3411 }
3412 xp->xp_pattern = arg;
3413}
3414
3415#endif /* FEAT_CMDL_COMPL */
3416
3417/*
3418 * ":1,25call func(arg1, arg2)" function call.
3419 */
3420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003421ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 char_u *arg = eap->arg;
3424 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003426 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003428 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 linenr_T lnum;
3430 int doesrange;
3431 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003432 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003434 if (eap->skip)
3435 {
3436 /* trans_function_name() doesn't work well when skipping, use eval0()
3437 * instead to skip to any following command, e.g. for:
3438 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003439 ++emsg_skip;
3440 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3441 clear_tv(&rettv);
3442 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003443 return;
3444 }
3445
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003446 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003447 if (fudi.fd_newkey != NULL)
3448 {
3449 /* Still need to give an error message for missing key. */
3450 EMSG2(_(e_dictkey), fudi.fd_newkey);
3451 vim_free(fudi.fd_newkey);
3452 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 if (tofree == NULL)
3454 return;
3455
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003456 /* Increase refcount on dictionary, it could get deleted when evaluating
3457 * the arguments. */
3458 if (fudi.fd_dict != NULL)
3459 ++fudi.fd_dict->dv_refcount;
3460
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003461 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003462 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003463 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaar532c7802005-01-27 14:44:31 +00003465 /* Skip white space to allow ":call func ()". Not good, but required for
3466 * backward compatibility. */
3467 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003468 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
3470 if (*startarg != '(')
3471 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003472 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 goto end;
3474 }
3475
3476 /*
3477 * When skipping, evaluate the function once, to find the end of the
3478 * arguments.
3479 * When the function takes a range, this is discovered after the first
3480 * call, and the loop is broken.
3481 */
3482 if (eap->skip)
3483 {
3484 ++emsg_skip;
3485 lnum = eap->line2; /* do it once, also with an invalid range */
3486 }
3487 else
3488 lnum = eap->line1;
3489 for ( ; lnum <= eap->line2; ++lnum)
3490 {
3491 if (!eap->skip && eap->addr_count > 0)
3492 {
3493 curwin->w_cursor.lnum = lnum;
3494 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003495#ifdef FEAT_VIRTUALEDIT
3496 curwin->w_cursor.coladd = 0;
3497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003501 eap->line1, eap->line2, &doesrange,
3502 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 failed = TRUE;
3505 break;
3506 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003507
3508 /* Handle a function returning a Funcref, Dictionary or List. */
3509 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3510 {
3511 failed = TRUE;
3512 break;
3513 }
3514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003515 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (doesrange || eap->skip)
3517 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003520 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003521 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003522 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (aborting())
3524 break;
3525 }
3526 if (eap->skip)
3527 --emsg_skip;
3528
3529 if (!failed)
3530 {
3531 /* Check for trailing illegal characters and a following command. */
3532 if (!ends_excmd(*arg))
3533 {
3534 emsg_severe = TRUE;
3535 EMSG(_(e_trailing));
3536 }
3537 else
3538 eap->nextcmd = check_nextcmd(arg);
3539 }
3540
3541end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003542 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003543 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544}
3545
3546/*
3547 * ":unlet[!] var1 ... " command.
3548 */
3549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003550ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 ex_unletlock(eap, eap->arg, 0);
3553}
3554
3555/*
3556 * ":lockvar" and ":unlockvar" commands
3557 */
3558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003559ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003560{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003562 int deep = 2;
3563
3564 if (eap->forceit)
3565 deep = -1;
3566 else if (vim_isdigit(*arg))
3567 {
3568 deep = getdigits(&arg);
3569 arg = skipwhite(arg);
3570 }
3571
3572 ex_unletlock(eap, arg, deep);
3573}
3574
3575/*
3576 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3577 */
3578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ex_unletlock(
3580 exarg_T *eap,
3581 char_u *argstart,
3582 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583{
3584 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 do
3590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003592 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003593 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003594 if (lv.ll_name == NULL)
3595 error = TRUE; /* error but continue parsing */
3596 if (name_end == NULL || (!vim_iswhite(*name_end)
3597 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 if (name_end != NULL)
3600 {
3601 emsg_severe = TRUE;
3602 EMSG(_(e_trailing));
3603 }
3604 if (!(eap->skip || error))
3605 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 break;
3607 }
3608
3609 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 {
3611 if (eap->cmdidx == CMD_unlet)
3612 {
3613 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3614 error = TRUE;
3615 }
3616 else
3617 {
3618 if (do_lock_var(&lv, name_end, deep,
3619 eap->cmdidx == CMD_lockvar) == FAIL)
3620 error = TRUE;
3621 }
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 if (!eap->skip)
3625 clear_lval(&lv);
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 arg = skipwhite(name_end);
3628 } while (!ends_excmd(*arg));
3629
3630 eap->nextcmd = check_nextcmd(arg);
3631}
3632
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003634do_unlet_var(
3635 lval_T *lp,
3636 char_u *name_end,
3637 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 int ret = OK;
3640 int cc;
3641
3642 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 cc = *name_end;
3645 *name_end = NUL;
3646
3647 /* Normal name or expanded name. */
3648 if (check_changedtick(lp->ll_name))
3649 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003654 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003655 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003656 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003657 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 else if (lp->ll_range)
3660 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003662 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003663 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003664
3665 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3666 {
3667 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003668 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003669 return FAIL;
3670 ll_li = li;
3671 ++ll_n1;
3672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673
3674 /* Delete a range of List items. */
3675 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3676 {
3677 li = lp->ll_li->li_next;
3678 listitem_remove(lp->ll_list, lp->ll_li);
3679 lp->ll_li = li;
3680 ++lp->ll_n1;
3681 }
3682 }
3683 else
3684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* unlet a List item. */
3687 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003690 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691 }
3692
3693 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694}
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696/*
3697 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003698 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
3700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
Bram Moolenaar33570922005-01-25 22:26:29 +00003703 hashtab_T *ht;
3704 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003705 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003706 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003707 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 ht = find_var_ht(name, &varname);
3710 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003712 if (ht == &globvarht)
3713 d = &globvardict;
3714 else if (current_funccal != NULL
3715 && ht == &current_funccal->l_vars.dv_hashtab)
3716 d = &current_funccal->l_vars;
3717 else if (ht == &compat_hashtab)
3718 d = &vimvardict;
3719 else
3720 {
3721 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3722 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3723 }
3724 if (d == NULL)
3725 {
3726 EMSG2(_(e_intern2), "do_unlet()");
3727 return FAIL;
3728 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 hi = hash_find(ht, varname);
3730 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003731 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003732 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003734 || var_check_ro(di->di_flags, name, FALSE)
3735 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003736 return FAIL;
3737
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 delete_var(ht, hi);
3739 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003742 if (forceit)
3743 return OK;
3744 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 return FAIL;
3746}
3747
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003748/*
3749 * Lock or unlock variable indicated by "lp".
3750 * "deep" is the levels to go (-1 for unlimited);
3751 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3752 */
3753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003754do_lock_var(
3755 lval_T *lp,
3756 char_u *name_end,
3757 int deep,
3758 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003759{
3760 int ret = OK;
3761 int cc;
3762 dictitem_T *di;
3763
3764 if (deep == 0) /* nothing to do */
3765 return OK;
3766
3767 if (lp->ll_tv == NULL)
3768 {
3769 cc = *name_end;
3770 *name_end = NUL;
3771
3772 /* Normal name or expanded name. */
3773 if (check_changedtick(lp->ll_name))
3774 ret = FAIL;
3775 else
3776 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003777 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778 if (di == NULL)
3779 ret = FAIL;
3780 else
3781 {
3782 if (lock)
3783 di->di_flags |= DI_FLAGS_LOCK;
3784 else
3785 di->di_flags &= ~DI_FLAGS_LOCK;
3786 item_lock(&di->di_tv, deep, lock);
3787 }
3788 }
3789 *name_end = cc;
3790 }
3791 else if (lp->ll_range)
3792 {
3793 listitem_T *li = lp->ll_li;
3794
3795 /* (un)lock a range of List items. */
3796 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3797 {
3798 item_lock(&li->li_tv, deep, lock);
3799 li = li->li_next;
3800 ++lp->ll_n1;
3801 }
3802 }
3803 else if (lp->ll_list != NULL)
3804 /* (un)lock a List item. */
3805 item_lock(&lp->ll_li->li_tv, deep, lock);
3806 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003807 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808 item_lock(&lp->ll_di->di_tv, deep, lock);
3809
3810 return ret;
3811}
3812
3813/*
3814 * Lock or unlock an item. "deep" is nr of levels to go.
3815 */
3816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818{
3819 static int recurse = 0;
3820 list_T *l;
3821 listitem_T *li;
3822 dict_T *d;
3823 hashitem_T *hi;
3824 int todo;
3825
3826 if (recurse >= DICT_MAXNEST)
3827 {
3828 EMSG(_("E743: variable nested too deep for (un)lock"));
3829 return;
3830 }
3831 if (deep == 0)
3832 return;
3833 ++recurse;
3834
3835 /* lock/unlock the item itself */
3836 if (lock)
3837 tv->v_lock |= VAR_LOCKED;
3838 else
3839 tv->v_lock &= ~VAR_LOCKED;
3840
3841 switch (tv->v_type)
3842 {
3843 case VAR_LIST:
3844 if ((l = tv->vval.v_list) != NULL)
3845 {
3846 if (lock)
3847 l->lv_lock |= VAR_LOCKED;
3848 else
3849 l->lv_lock &= ~VAR_LOCKED;
3850 if (deep < 0 || deep > 1)
3851 /* recursive: lock/unlock the items the List contains */
3852 for (li = l->lv_first; li != NULL; li = li->li_next)
3853 item_lock(&li->li_tv, deep - 1, lock);
3854 }
3855 break;
3856 case VAR_DICT:
3857 if ((d = tv->vval.v_dict) != NULL)
3858 {
3859 if (lock)
3860 d->dv_lock |= VAR_LOCKED;
3861 else
3862 d->dv_lock &= ~VAR_LOCKED;
3863 if (deep < 0 || deep > 1)
3864 {
3865 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003866 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003867 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3868 {
3869 if (!HASHITEM_EMPTY(hi))
3870 {
3871 --todo;
3872 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3873 }
3874 }
3875 }
3876 }
3877 }
3878 --recurse;
3879}
3880
Bram Moolenaara40058a2005-07-11 22:42:07 +00003881/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003882 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3883 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003884 */
3885 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003886tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003887{
3888 return (tv->v_lock & VAR_LOCKED)
3889 || (tv->v_type == VAR_LIST
3890 && tv->vval.v_list != NULL
3891 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3892 || (tv->v_type == VAR_DICT
3893 && tv->vval.v_dict != NULL
3894 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3895}
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3898/*
3899 * Delete all "menutrans_" variables.
3900 */
3901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003902del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003905 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003908 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003909 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 {
3911 if (!HASHITEM_EMPTY(hi))
3912 {
3913 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3915 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003916 }
3917 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003918 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919}
3920#endif
3921
3922#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3923
3924/*
3925 * Local string buffer for the next two functions to store a variable name
3926 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3927 * get_user_var_name().
3928 */
3929
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003930static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932static char_u *varnamebuf = NULL;
3933static int varnamebuflen = 0;
3934
3935/*
3936 * Function to concatenate a prefix and a variable name.
3937 */
3938 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 int len;
3942
3943 len = (int)STRLEN(name) + 3;
3944 if (len > varnamebuflen)
3945 {
3946 vim_free(varnamebuf);
3947 len += 10; /* some additional space */
3948 varnamebuf = alloc(len);
3949 if (varnamebuf == NULL)
3950 {
3951 varnamebuflen = 0;
3952 return NULL;
3953 }
3954 varnamebuflen = len;
3955 }
3956 *varnamebuf = prefix;
3957 varnamebuf[1] = ':';
3958 STRCPY(varnamebuf + 2, name);
3959 return varnamebuf;
3960}
3961
3962/*
3963 * Function given to ExpandGeneric() to obtain the list of user defined
3964 * (global/buffer/window/built-in) variable names.
3965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003967get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003969 static long_u gdone;
3970 static long_u bdone;
3971 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003972#ifdef FEAT_WINDOWS
3973 static long_u tdone;
3974#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 static int vidx;
3976 static hashitem_T *hi;
3977 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003980 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003981 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982#ifdef FEAT_WINDOWS
3983 tdone = 0;
3984#endif
3985 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003986
3987 /* Global variables */
3988 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003990 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003992 else
3993 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003994 while (HASHITEM_EMPTY(hi))
3995 ++hi;
3996 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3997 return cat_prefix_varname('g', hi->hi_key);
3998 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004000
4001 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004002 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004005 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 else
4008 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004009 while (HASHITEM_EMPTY(hi))
4010 ++hi;
4011 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 return (char_u *)"b:changedtick";
4017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018
4019 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004020 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004023 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004025 else
4026 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 while (HASHITEM_EMPTY(hi))
4028 ++hi;
4029 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004031
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032#ifdef FEAT_WINDOWS
4033 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004035 if (tdone < ht->ht_used)
4036 {
4037 if (tdone++ == 0)
4038 hi = ht->ht_array;
4039 else
4040 ++hi;
4041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('t', hi->hi_key);
4044 }
4045#endif
4046
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 /* v: variables */
4048 if (vidx < VV_LEN)
4049 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 vim_free(varnamebuf);
4052 varnamebuf = NULL;
4053 varnamebuflen = 0;
4054 return NULL;
4055}
4056
4057#endif /* FEAT_CMDL_COMPL */
4058
4059/*
4060 * types for expressions.
4061 */
4062typedef enum
4063{
4064 TYPE_UNKNOWN = 0
4065 , TYPE_EQUAL /* == */
4066 , TYPE_NEQUAL /* != */
4067 , TYPE_GREATER /* > */
4068 , TYPE_GEQUAL /* >= */
4069 , TYPE_SMALLER /* < */
4070 , TYPE_SEQUAL /* <= */
4071 , TYPE_MATCH /* =~ */
4072 , TYPE_NOMATCH /* !~ */
4073} exptype_T;
4074
4075/*
4076 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4079 */
4080
4081/*
4082 * Handle zero level expression.
4083 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004085 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 * Return OK or FAIL.
4087 */
4088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004089eval0(
4090 char_u *arg,
4091 typval_T *rettv,
4092 char_u **nextcmd,
4093 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 int ret;
4096 char_u *p;
4097
4098 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (ret == FAIL || !ends_excmd(*p))
4101 {
4102 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 /*
4105 * Report the invalid expression unless the expression evaluation has
4106 * been cancelled due to an aborting error, an interrupt, or an
4107 * exception.
4108 */
4109 if (!aborting())
4110 EMSG2(_(e_invexpr2), arg);
4111 ret = FAIL;
4112 }
4113 if (nextcmd != NULL)
4114 *nextcmd = check_nextcmd(p);
4115
4116 return ret;
4117}
4118
4119/*
4120 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004121 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 *
4123 * "arg" must point to the first non-white of the expression.
4124 * "arg" is advanced to the next non-white after the recognized expression.
4125 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004126 * Note: "rettv.v_lock" is not set.
4127 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * Return OK or FAIL.
4129 */
4130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004131eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 /*
4137 * Get the first variable.
4138 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141
4142 if ((*arg)[0] == '?')
4143 {
4144 result = FALSE;
4145 if (evaluate)
4146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 int error = FALSE;
4148
4149 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 if (error)
4153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155
4156 /*
4157 * Get the second variable.
4158 */
4159 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return FAIL;
4162
4163 /*
4164 * Check for the ":".
4165 */
4166 if ((*arg)[0] != ':')
4167 {
4168 EMSG(_("E109: Missing ':' after '?'"));
4169 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 return FAIL;
4172 }
4173
4174 /*
4175 * Get the third variable.
4176 */
4177 *arg = skipwhite(*arg + 1);
4178 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4179 {
4180 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FAIL;
4183 }
4184 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 return OK;
4189}
4190
4191/*
4192 * Handle first level expression:
4193 * expr2 || expr2 || expr2 logical OR
4194 *
4195 * "arg" must point to the first non-white of the expression.
4196 * "arg" is advanced to the next non-white after the recognized expression.
4197 *
4198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 long result;
4205 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 /*
4209 * Get the first variable.
4210 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Repeat until there is no following "||".
4216 */
4217 first = TRUE;
4218 result = FALSE;
4219 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4220 {
4221 if (evaluate && first)
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 if (error)
4227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 first = FALSE;
4229 }
4230
4231 /*
4232 * Get the second variable.
4233 */
4234 *arg = skipwhite(*arg + 2);
4235 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4236 return FAIL;
4237
4238 /*
4239 * Compute the result.
4240 */
4241 if (evaluate && !result)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 if (evaluate)
4250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle second level expression:
4261 * expr3 && expr3 && expr3 logical AND
4262 *
4263 * "arg" must point to the first non-white of the expression.
4264 * "arg" is advanced to the next non-white after the recognized expression.
4265 *
4266 * Return OK or FAIL.
4267 */
4268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004269eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
Bram Moolenaar33570922005-01-25 22:26:29 +00004271 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 long result;
4273 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 /*
4277 * Get the first variable.
4278 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return FAIL;
4281
4282 /*
4283 * Repeat until there is no following "&&".
4284 */
4285 first = TRUE;
4286 result = TRUE;
4287 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4288 {
4289 if (evaluate && first)
4290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 if (error)
4295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 first = FALSE;
4297 }
4298
4299 /*
4300 * Get the second variable.
4301 */
4302 *arg = skipwhite(*arg + 2);
4303 if (eval4(arg, &var2, evaluate && result) == FAIL)
4304 return FAIL;
4305
4306 /*
4307 * Compute the result.
4308 */
4309 if (evaluate && result)
4310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (error)
4315 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 if (evaluate)
4318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 rettv->v_type = VAR_NUMBER;
4320 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 }
4323
4324 return OK;
4325}
4326
4327/*
4328 * Handle third level expression:
4329 * var1 == var2
4330 * var1 =~ var2
4331 * var1 != var2
4332 * var1 !~ var2
4333 * var1 > var2
4334 * var1 >= var2
4335 * var1 < var2
4336 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004337 * var1 is var2
4338 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 *
4340 * "arg" must point to the first non-white of the expression.
4341 * "arg" is advanced to the next non-white after the recognized expression.
4342 *
4343 * Return OK or FAIL.
4344 */
4345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004346eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
Bram Moolenaar33570922005-01-25 22:26:29 +00004348 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 char_u *p;
4350 int i;
4351 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004352 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 int len = 2;
4354 long n1, n2;
4355 char_u *s1, *s2;
4356 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4357 regmatch_T regmatch;
4358 int ic;
4359 char_u *save_cpo;
4360
4361 /*
4362 * Get the first variable.
4363 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 return FAIL;
4366
4367 p = *arg;
4368 switch (p[0])
4369 {
4370 case '=': if (p[1] == '=')
4371 type = TYPE_EQUAL;
4372 else if (p[1] == '~')
4373 type = TYPE_MATCH;
4374 break;
4375 case '!': if (p[1] == '=')
4376 type = TYPE_NEQUAL;
4377 else if (p[1] == '~')
4378 type = TYPE_NOMATCH;
4379 break;
4380 case '>': if (p[1] != '=')
4381 {
4382 type = TYPE_GREATER;
4383 len = 1;
4384 }
4385 else
4386 type = TYPE_GEQUAL;
4387 break;
4388 case '<': if (p[1] != '=')
4389 {
4390 type = TYPE_SMALLER;
4391 len = 1;
4392 }
4393 else
4394 type = TYPE_SEQUAL;
4395 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 case 'i': if (p[1] == 's')
4397 {
4398 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4399 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004400 i = p[len];
4401 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 {
4403 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4404 type_is = TRUE;
4405 }
4406 }
4407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409
4410 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004411 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 */
4413 if (type != TYPE_UNKNOWN)
4414 {
4415 /* extra question mark appended: ignore case */
4416 if (p[len] == '?')
4417 {
4418 ic = TRUE;
4419 ++len;
4420 }
4421 /* extra '#' appended: match case */
4422 else if (p[len] == '#')
4423 {
4424 ic = FALSE;
4425 ++len;
4426 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004427 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 else
4429 ic = p_ic;
4430
4431 /*
4432 * Get the second variable.
4433 */
4434 *arg = skipwhite(p + len);
4435 if (eval5(arg, &var2, evaluate) == FAIL)
4436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 return FAIL;
4439 }
4440
4441 if (evaluate)
4442 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 if (type_is && rettv->v_type != var2.v_type)
4444 {
4445 /* For "is" a different type always means FALSE, for "notis"
4446 * it means TRUE. */
4447 n1 = (type == TYPE_NEQUAL);
4448 }
4449 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4450 {
4451 if (type_is)
4452 {
4453 n1 = (rettv->v_type == var2.v_type
4454 && rettv->vval.v_list == var2.vval.v_list);
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 else if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004462 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004464 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004472 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4473 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004474 if (type == TYPE_NEQUAL)
4475 n1 = !n1;
4476 }
4477 }
4478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004479 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4480 {
4481 if (type_is)
4482 {
4483 n1 = (rettv->v_type == var2.v_type
4484 && rettv->vval.v_dict == var2.vval.v_dict);
4485 if (type == TYPE_NEQUAL)
4486 n1 = !n1;
4487 }
4488 else if (rettv->v_type != var2.v_type
4489 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4490 {
4491 if (rettv->v_type != var2.v_type)
4492 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4493 else
4494 EMSG(_("E736: Invalid operation for Dictionary"));
4495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 return FAIL;
4498 }
4499 else
4500 {
4501 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004502 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4503 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 }
4508
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4510 {
4511 if (rettv->v_type != var2.v_type
4512 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4513 {
4514 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004515 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004516 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004517 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 return FAIL;
4521 }
4522 else
4523 {
4524 /* Compare two Funcrefs for being equal or unequal. */
4525 if (rettv->vval.v_string == NULL
4526 || var2.vval.v_string == NULL)
4527 n1 = FALSE;
4528 else
4529 n1 = STRCMP(rettv->vval.v_string,
4530 var2.vval.v_string) == 0;
4531 if (type == TYPE_NEQUAL)
4532 n1 = !n1;
4533 }
4534 }
4535
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536#ifdef FEAT_FLOAT
4537 /*
4538 * If one of the two variables is a float, compare as a float.
4539 * When using "=~" or "!~", always compare as string.
4540 */
4541 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4542 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 {
4544 float_T f1, f2;
4545
4546 if (rettv->v_type == VAR_FLOAT)
4547 f1 = rettv->vval.v_float;
4548 else
4549 f1 = get_tv_number(rettv);
4550 if (var2.v_type == VAR_FLOAT)
4551 f2 = var2.vval.v_float;
4552 else
4553 f2 = get_tv_number(&var2);
4554 n1 = FALSE;
4555 switch (type)
4556 {
4557 case TYPE_EQUAL: n1 = (f1 == f2); break;
4558 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4559 case TYPE_GREATER: n1 = (f1 > f2); break;
4560 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4561 case TYPE_SMALLER: n1 = (f1 < f2); break;
4562 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4563 case TYPE_UNKNOWN:
4564 case TYPE_MATCH:
4565 case TYPE_NOMATCH: break; /* avoid gcc warning */
4566 }
4567 }
4568#endif
4569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 /*
4571 * If one of the two variables is a number, compare as a number.
4572 * When using "=~" or "!~", always compare as string.
4573 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004574 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 n1 = get_tv_number(rettv);
4578 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 switch (type)
4580 {
4581 case TYPE_EQUAL: n1 = (n1 == n2); break;
4582 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4583 case TYPE_GREATER: n1 = (n1 > n2); break;
4584 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4585 case TYPE_SMALLER: n1 = (n1 < n2); break;
4586 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4587 case TYPE_UNKNOWN:
4588 case TYPE_MATCH:
4589 case TYPE_NOMATCH: break; /* avoid gcc warning */
4590 }
4591 }
4592 else
4593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594 s1 = get_tv_string_buf(rettv, buf1);
4595 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4597 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4598 else
4599 i = 0;
4600 n1 = FALSE;
4601 switch (type)
4602 {
4603 case TYPE_EQUAL: n1 = (i == 0); break;
4604 case TYPE_NEQUAL: n1 = (i != 0); break;
4605 case TYPE_GREATER: n1 = (i > 0); break;
4606 case TYPE_GEQUAL: n1 = (i >= 0); break;
4607 case TYPE_SMALLER: n1 = (i < 0); break;
4608 case TYPE_SEQUAL: n1 = (i <= 0); break;
4609
4610 case TYPE_MATCH:
4611 case TYPE_NOMATCH:
4612 /* avoid 'l' flag in 'cpoptions' */
4613 save_cpo = p_cpo;
4614 p_cpo = (char_u *)"";
4615 regmatch.regprog = vim_regcomp(s2,
4616 RE_MAGIC + RE_STRING);
4617 regmatch.rm_ic = ic;
4618 if (regmatch.regprog != NULL)
4619 {
4620 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004621 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (type == TYPE_NOMATCH)
4623 n1 = !n1;
4624 }
4625 p_cpo = save_cpo;
4626 break;
4627
4628 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4629 }
4630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 clear_tv(rettv);
4632 clear_tv(&var2);
4633 rettv->v_type = VAR_NUMBER;
4634 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 }
4637
4638 return OK;
4639}
4640
4641/*
4642 * Handle fourth level expression:
4643 * + number addition
4644 * - number subtraction
4645 * . string concatenation
4646 *
4647 * "arg" must point to the first non-white of the expression.
4648 * "arg" is advanced to the next non-white after the recognized expression.
4649 *
4650 * Return OK or FAIL.
4651 */
4652 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004653eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
Bram Moolenaar33570922005-01-25 22:26:29 +00004655 typval_T var2;
4656 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 int op;
4658 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004659#ifdef FEAT_FLOAT
4660 float_T f1 = 0, f2 = 0;
4661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 char_u *s1, *s2;
4663 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4664 char_u *p;
4665
4666 /*
4667 * Get the first variable.
4668 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004669 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return FAIL;
4671
4672 /*
4673 * Repeat computing, until no '+', '-' or '.' is following.
4674 */
4675 for (;;)
4676 {
4677 op = **arg;
4678 if (op != '+' && op != '-' && op != '.')
4679 break;
4680
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004681 if ((op != '+' || rettv->v_type != VAR_LIST)
4682#ifdef FEAT_FLOAT
4683 && (op == '.' || rettv->v_type != VAR_FLOAT)
4684#endif
4685 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686 {
4687 /* For "list + ...", an illegal use of the first operand as
4688 * a number cannot be determined before evaluating the 2nd
4689 * operand: if this is also a list, all is ok.
4690 * For "something . ...", "something - ..." or "non-list + ...",
4691 * we know that the first operand needs to be a string or number
4692 * without evaluating the 2nd operand. So check before to avoid
4693 * side effects after an error. */
4694 if (evaluate && get_tv_string_chk(rettv) == NULL)
4695 {
4696 clear_tv(rettv);
4697 return FAIL;
4698 }
4699 }
4700
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 /*
4702 * Get the second variable.
4703 */
4704 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004705 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return FAIL;
4709 }
4710
4711 if (evaluate)
4712 {
4713 /*
4714 * Compute the result.
4715 */
4716 if (op == '.')
4717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4719 s2 = get_tv_string_buf_chk(&var2, buf2);
4720 if (s2 == NULL) /* type error ? */
4721 {
4722 clear_tv(rettv);
4723 clear_tv(&var2);
4724 return FAIL;
4725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004726 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 clear_tv(rettv);
4728 rettv->v_type = VAR_STRING;
4729 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004731 else if (op == '+' && rettv->v_type == VAR_LIST
4732 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004733 {
4734 /* concatenate Lists */
4735 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4736 &var3) == FAIL)
4737 {
4738 clear_tv(rettv);
4739 clear_tv(&var2);
4740 return FAIL;
4741 }
4742 clear_tv(rettv);
4743 *rettv = var3;
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 else
4746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747 int error = FALSE;
4748
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749#ifdef FEAT_FLOAT
4750 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752 f1 = rettv->vval.v_float;
4753 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 else
4756#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004757 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758 n1 = get_tv_number_chk(rettv, &error);
4759 if (error)
4760 {
4761 /* This can only happen for "list + non-list". For
4762 * "non-list + ..." or "something - ...", we returned
4763 * before evaluating the 2nd operand. */
4764 clear_tv(rettv);
4765 return FAIL;
4766 }
4767#ifdef FEAT_FLOAT
4768 if (var2.v_type == VAR_FLOAT)
4769 f1 = n1;
4770#endif
4771 }
4772#ifdef FEAT_FLOAT
4773 if (var2.v_type == VAR_FLOAT)
4774 {
4775 f2 = var2.vval.v_float;
4776 n2 = 0;
4777 }
4778 else
4779#endif
4780 {
4781 n2 = get_tv_number_chk(&var2, &error);
4782 if (error)
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
4788#ifdef FEAT_FLOAT
4789 if (rettv->v_type == VAR_FLOAT)
4790 f2 = n2;
4791#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794
4795#ifdef FEAT_FLOAT
4796 /* If there is a float on either side the result is a float. */
4797 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4798 {
4799 if (op == '+')
4800 f1 = f1 + f2;
4801 else
4802 f1 = f1 - f2;
4803 rettv->v_type = VAR_FLOAT;
4804 rettv->vval.v_float = f1;
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807#endif
4808 {
4809 if (op == '+')
4810 n1 = n1 + n2;
4811 else
4812 n1 = n1 - n2;
4813 rettv->v_type = VAR_NUMBER;
4814 rettv->vval.v_number = n1;
4815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 }
4820 return OK;
4821}
4822
4823/*
4824 * Handle fifth level expression:
4825 * * number multiplication
4826 * / number division
4827 * % number modulo
4828 *
4829 * "arg" must point to the first non-white of the expression.
4830 * "arg" is advanced to the next non-white after the recognized expression.
4831 *
4832 * Return OK or FAIL.
4833 */
4834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004835eval6(
4836 char_u **arg,
4837 typval_T *rettv,
4838 int evaluate,
4839 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
Bram Moolenaar33570922005-01-25 22:26:29 +00004841 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 int op;
4843 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#ifdef FEAT_FLOAT
4845 int use_float = FALSE;
4846 float_T f1 = 0, f2;
4847#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /*
4851 * Get the first variable.
4852 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004853 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 return FAIL;
4855
4856 /*
4857 * Repeat computing, until no '*', '/' or '%' is following.
4858 */
4859 for (;;)
4860 {
4861 op = **arg;
4862 if (op != '*' && op != '/' && op != '%')
4863 break;
4864
4865 if (evaluate)
4866 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867#ifdef FEAT_FLOAT
4868 if (rettv->v_type == VAR_FLOAT)
4869 {
4870 f1 = rettv->vval.v_float;
4871 use_float = TRUE;
4872 n1 = 0;
4873 }
4874 else
4875#endif
4876 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 if (error)
4879 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 else
4882 n1 = 0;
4883
4884 /*
4885 * Get the second variable.
4886 */
4887 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return FAIL;
4890
4891 if (evaluate)
4892 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893#ifdef FEAT_FLOAT
4894 if (var2.v_type == VAR_FLOAT)
4895 {
4896 if (!use_float)
4897 {
4898 f1 = n1;
4899 use_float = TRUE;
4900 }
4901 f2 = var2.vval.v_float;
4902 n2 = 0;
4903 }
4904 else
4905#endif
4906 {
4907 n2 = get_tv_number_chk(&var2, &error);
4908 clear_tv(&var2);
4909 if (error)
4910 return FAIL;
4911#ifdef FEAT_FLOAT
4912 if (use_float)
4913 f2 = n2;
4914#endif
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 /*
4918 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921#ifdef FEAT_FLOAT
4922 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 if (op == '*')
4925 f1 = f1 * f2;
4926 else if (op == '/')
4927 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004928# ifdef VMS
4929 /* VMS crashes on divide by zero, work around it */
4930 if (f2 == 0.0)
4931 {
4932 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004933 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004934 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004935 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004936 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004937 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004938 }
4939 else
4940 f1 = f1 / f2;
4941# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942 /* We rely on the floating point library to handle divide
4943 * by zero to result in "inf" and not a crash. */
4944 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004945# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004949 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004950 return FAIL;
4951 }
4952 rettv->v_type = VAR_FLOAT;
4953 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 n1 = n1 * n2;
4960 else if (op == '/')
4961 {
4962 if (n2 == 0) /* give an error message? */
4963 {
4964 if (n1 == 0)
4965 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4966 else if (n1 < 0)
4967 n1 = -0x7fffffffL;
4968 else
4969 n1 = 0x7fffffffL;
4970 }
4971 else
4972 n1 = n1 / n2;
4973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 {
4976 if (n2 == 0) /* give an error message? */
4977 n1 = 0;
4978 else
4979 n1 = n1 % n2;
4980 }
4981 rettv->v_type = VAR_NUMBER;
4982 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 }
4986
4987 return OK;
4988}
4989
4990/*
4991 * Handle sixth level expression:
4992 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004993 * "string" string constant
4994 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 * &option-name option value
4996 * @r register contents
4997 * identifier variable value
4998 * function() function call
4999 * $VAR environment variable
5000 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005001 * [expr, expr] List
5002 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 *
5004 * Also handle:
5005 * ! in front logical NOT
5006 * - in front unary minus
5007 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 * trailing [] subscript in String or List
5009 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 *
5011 * "arg" must point to the first non-white of the expression.
5012 * "arg" is advanced to the next non-white after the recognized expression.
5013 *
5014 * Return OK or FAIL.
5015 */
5016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005017eval7(
5018 char_u **arg,
5019 typval_T *rettv,
5020 int evaluate,
5021 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 long n;
5024 int len;
5025 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 char_u *start_leader, *end_leader;
5027 int ret = OK;
5028 char_u *alias;
5029
5030 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005032 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
5036 /*
5037 * Skip '!' and '-' characters. They are handled later.
5038 */
5039 start_leader = *arg;
5040 while (**arg == '!' || **arg == '-' || **arg == '+')
5041 *arg = skipwhite(*arg + 1);
5042 end_leader = *arg;
5043
5044 switch (**arg)
5045 {
5046 /*
5047 * Number constant.
5048 */
5049 case '0':
5050 case '1':
5051 case '2':
5052 case '3':
5053 case '4':
5054 case '5':
5055 case '6':
5056 case '7':
5057 case '8':
5058 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005059 {
5060#ifdef FEAT_FLOAT
5061 char_u *p = skipdigits(*arg + 1);
5062 int get_float = FALSE;
5063
5064 /* We accept a float when the format matches
5065 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005066 * strict to avoid backwards compatibility problems.
5067 * Don't look for a float after the "." operator, so that
5068 * ":let vers = 1.2.3" doesn't fail. */
5069 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 get_float = TRUE;
5072 p = skipdigits(p + 2);
5073 if (*p == 'e' || *p == 'E')
5074 {
5075 ++p;
5076 if (*p == '-' || *p == '+')
5077 ++p;
5078 if (!vim_isdigit(*p))
5079 get_float = FALSE;
5080 else
5081 p = skipdigits(p + 1);
5082 }
5083 if (ASCII_ISALPHA(*p) || *p == '.')
5084 get_float = FALSE;
5085 }
5086 if (get_float)
5087 {
5088 float_T f;
5089
5090 *arg += string2float(*arg, &f);
5091 if (evaluate)
5092 {
5093 rettv->v_type = VAR_FLOAT;
5094 rettv->vval.v_float = f;
5095 }
5096 }
5097 else
5098#endif
5099 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005100 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 *arg += len;
5102 if (evaluate)
5103 {
5104 rettv->v_type = VAR_NUMBER;
5105 rettv->vval.v_number = n;
5106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
5111 /*
5112 * String constant: "string".
5113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 break;
5116
5117 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 break;
5122
5123 /*
5124 * List: [expr, expr]
5125 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 break;
5128
5129 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 * Dictionary: {key: val, key: val}
5131 */
5132 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5133 break;
5134
5135 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005136 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005138 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140
5141 /*
5142 * Environment variable: $VAR.
5143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /*
5148 * Register contents: @r.
5149 */
5150 case '@': ++*arg;
5151 if (evaluate)
5152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005153 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005154 rettv->vval.v_string = get_reg_contents(**arg,
5155 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157 if (**arg != NUL)
5158 ++*arg;
5159 break;
5160
5161 /*
5162 * nested expression: (expression).
5163 */
5164 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (**arg == ')')
5167 ++*arg;
5168 else if (ret == OK)
5169 {
5170 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 ret = FAIL;
5173 }
5174 break;
5175
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179
5180 if (ret == NOTDONE)
5181 {
5182 /*
5183 * Must be a variable or function name.
5184 * Can also be a curly-braces kind of name: {expr}.
5185 */
5186 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 if (alias != NULL)
5189 s = alias;
5190
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005191 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 ret = FAIL;
5193 else
5194 {
5195 if (**arg == '(') /* recursive! */
5196 {
5197 /* If "s" is the name of a variable of type VAR_FUNC
5198 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005199 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200
5201 /* Invoke the function. */
5202 ret = get_func_tv(s, len, rettv, arg,
5203 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005204 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005205
5206 /* If evaluate is FALSE rettv->v_type was not set in
5207 * get_func_tv, but it's needed in handle_subscript() to parse
5208 * what follows. So set it here. */
5209 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5210 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005211 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005212 rettv->v_type = VAR_FUNC;
5213 }
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 /* Stop the expression evaluation when immediately
5216 * aborting on error, or when an interrupt occurred or
5217 * an exception was thrown but not caught. */
5218 if (aborting())
5219 {
5220 if (ret == OK)
5221 clear_tv(rettv);
5222 ret = FAIL;
5223 }
5224 }
5225 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005226 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005227 else
5228 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005230 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 }
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 *arg = skipwhite(*arg);
5234
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005235 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5236 * expr(expr). */
5237 if (ret == OK)
5238 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 /*
5241 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5242 */
5243 if (ret == OK && evaluate && end_leader > start_leader)
5244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 int val = 0;
5247#ifdef FEAT_FLOAT
5248 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005250 if (rettv->v_type == VAR_FLOAT)
5251 f = rettv->vval.v_float;
5252 else
5253#endif
5254 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005255 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 clear_tv(rettv);
5258 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 else
5261 {
5262 while (end_leader > start_leader)
5263 {
5264 --end_leader;
5265 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005266 {
5267#ifdef FEAT_FLOAT
5268 if (rettv->v_type == VAR_FLOAT)
5269 f = !f;
5270 else
5271#endif
5272 val = !val;
5273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005274 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005275 {
5276#ifdef FEAT_FLOAT
5277 if (rettv->v_type == VAR_FLOAT)
5278 f = -f;
5279 else
5280#endif
5281 val = -val;
5282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284#ifdef FEAT_FLOAT
5285 if (rettv->v_type == VAR_FLOAT)
5286 {
5287 clear_tv(rettv);
5288 rettv->vval.v_float = f;
5289 }
5290 else
5291#endif
5292 {
5293 clear_tv(rettv);
5294 rettv->v_type = VAR_NUMBER;
5295 rettv->vval.v_number = val;
5296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299
5300 return ret;
5301}
5302
5303/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005304 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5305 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5307 */
5308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005309eval_index(
5310 char_u **arg,
5311 typval_T *rettv,
5312 int evaluate,
5313 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314{
5315 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 long len = -1;
5319 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005323 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 if (verbose)
5326 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
5328 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005329#ifdef FEAT_FLOAT
5330 else if (rettv->v_type == VAR_FLOAT)
5331 {
5332 if (verbose)
5333 EMSG(_(e_float_as_string));
5334 return FAIL;
5335 }
5336#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005337 else if (rettv->v_type == VAR_SPECIAL)
5338 {
5339 return FAIL;
5340 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005342 init_tv(&var1);
5343 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 /*
5347 * dict.name
5348 */
5349 key = *arg + 1;
5350 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5351 ;
5352 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 }
5356 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 /*
5359 * something[idx]
5360 *
5361 * Get the (first) variable from inside the [].
5362 */
5363 *arg = skipwhite(*arg + 1);
5364 if (**arg == ':')
5365 empty1 = TRUE;
5366 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5367 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005368 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5369 {
5370 /* not a number or string */
5371 clear_tv(&var1);
5372 return FAIL;
5373 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374
5375 /*
5376 * Get the second variable from inside the [:].
5377 */
5378 if (**arg == ':')
5379 {
5380 range = TRUE;
5381 *arg = skipwhite(*arg + 1);
5382 if (**arg == ']')
5383 empty2 = TRUE;
5384 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 if (!empty1)
5387 clear_tv(&var1);
5388 return FAIL;
5389 }
5390 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5391 {
5392 /* not a number or string */
5393 if (!empty1)
5394 clear_tv(&var1);
5395 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 return FAIL;
5397 }
5398 }
5399
5400 /* Check for the ']'. */
5401 if (**arg != ']')
5402 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005403 if (verbose)
5404 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 clear_tv(&var1);
5406 if (range)
5407 clear_tv(&var2);
5408 return FAIL;
5409 }
5410 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 }
5412
5413 if (evaluate)
5414 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 n1 = 0;
5416 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 n1 = get_tv_number(&var1);
5419 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 }
5421 if (range)
5422 {
5423 if (empty2)
5424 n2 = -1;
5425 else
5426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005427 n2 = get_tv_number(&var2);
5428 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 }
5430 }
5431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 {
5434 case VAR_NUMBER:
5435 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 len = (long)STRLEN(s);
5438 if (range)
5439 {
5440 /* The resulting variable is a substring. If the indexes
5441 * are out of range the result is empty. */
5442 if (n1 < 0)
5443 {
5444 n1 = len + n1;
5445 if (n1 < 0)
5446 n1 = 0;
5447 }
5448 if (n2 < 0)
5449 n2 = len + n2;
5450 else if (n2 >= len)
5451 n2 = len;
5452 if (n1 >= len || n2 < 0 || n1 > n2)
5453 s = NULL;
5454 else
5455 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5456 }
5457 else
5458 {
5459 /* The resulting variable is a string of a single
5460 * character. If the index is too big or negative the
5461 * result is empty. */
5462 if (n1 >= len || n1 < 0)
5463 s = NULL;
5464 else
5465 s = vim_strnsave(s + n1, 1);
5466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 clear_tv(rettv);
5468 rettv->v_type = VAR_STRING;
5469 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 break;
5471
5472 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 if (n1 < 0)
5475 n1 = len + n1;
5476 if (!empty1 && (n1 < 0 || n1 >= len))
5477 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005478 /* For a range we allow invalid values and return an empty
5479 * list. A list index out of range is an error. */
5480 if (!range)
5481 {
5482 if (verbose)
5483 EMSGN(_(e_listidx), n1);
5484 return FAIL;
5485 }
5486 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488 if (range)
5489 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 list_T *l;
5491 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492
5493 if (n2 < 0)
5494 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005495 else if (n2 >= len)
5496 n2 = len - 1;
5497 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005498 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 l = list_alloc();
5500 if (l == NULL)
5501 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 n1 <= n2; ++n1)
5504 {
5505 if (list_append_tv(l, &item->li_tv) == FAIL)
5506 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005507 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 return FAIL;
5509 }
5510 item = item->li_next;
5511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 clear_tv(rettv);
5513 rettv->v_type = VAR_LIST;
5514 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005515 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 }
5517 else
5518 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005519 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 clear_tv(rettv);
5521 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 }
5523 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524
5525 case VAR_DICT:
5526 if (range)
5527 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005528 if (verbose)
5529 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005530 if (len == -1)
5531 clear_tv(&var1);
5532 return FAIL;
5533 }
5534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536
5537 if (len == -1)
5538 {
5539 key = get_tv_string(&var1);
5540 if (*key == NUL)
5541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005542 if (verbose)
5543 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 clear_tv(&var1);
5545 return FAIL;
5546 }
5547 }
5548
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005551 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005552 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 if (len == -1)
5554 clear_tv(&var1);
5555 if (item == NULL)
5556 return FAIL;
5557
5558 copy_tv(&item->di_tv, &var1);
5559 clear_tv(rettv);
5560 *rettv = var1;
5561 }
5562 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 }
5564 }
5565
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 return OK;
5567}
5568
5569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 * Get an option value.
5571 * "arg" points to the '&' or '+' before the option name.
5572 * "arg" is advanced to character after the option name.
5573 * Return OK or FAIL.
5574 */
5575 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005576get_option_tv(
5577 char_u **arg,
5578 typval_T *rettv, /* when NULL, only check if option exists */
5579 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 char_u *option_end;
5582 long numval;
5583 char_u *stringval;
5584 int opt_type;
5585 int c;
5586 int working = (**arg == '+'); /* has("+option") */
5587 int ret = OK;
5588 int opt_flags;
5589
5590 /*
5591 * Isolate the option name and find its value.
5592 */
5593 option_end = find_option_end(arg, &opt_flags);
5594 if (option_end == NULL)
5595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 EMSG2(_("E112: Option name missing: %s"), *arg);
5598 return FAIL;
5599 }
5600
5601 if (!evaluate)
5602 {
5603 *arg = option_end;
5604 return OK;
5605 }
5606
5607 c = *option_end;
5608 *option_end = NUL;
5609 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005610 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 if (opt_type == -3) /* invalid name */
5613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 EMSG2(_("E113: Unknown option: %s"), *arg);
5616 ret = FAIL;
5617 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 if (opt_type == -2) /* hidden string option */
5621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 rettv->v_type = VAR_STRING;
5623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625 else if (opt_type == -1) /* hidden number option */
5626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627 rettv->v_type = VAR_NUMBER;
5628 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 else if (opt_type == 1) /* number option */
5631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005632 rettv->v_type = VAR_NUMBER;
5633 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635 else /* string option */
5636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005637 rettv->v_type = VAR_STRING;
5638 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
5640 }
5641 else if (working && (opt_type == -2 || opt_type == -1))
5642 ret = FAIL;
5643
5644 *option_end = c; /* put back for error messages */
5645 *arg = option_end;
5646
5647 return ret;
5648}
5649
5650/*
5651 * Allocate a variable for a string constant.
5652 * Return OK or FAIL.
5653 */
5654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005655get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 char_u *p;
5658 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 int extra = 0;
5660
5661 /*
5662 * Find the end of the string, skipping backslashed characters.
5663 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005664 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
5666 if (*p == '\\' && p[1] != NUL)
5667 {
5668 ++p;
5669 /* A "\<x>" form occupies at least 4 characters, and produces up
5670 * to 6 characters: reserve space for 2 extra */
5671 if (*p == '<')
5672 extra += 2;
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675
5676 if (*p != '"')
5677 {
5678 EMSG2(_("E114: Missing quote: %s"), *arg);
5679 return FAIL;
5680 }
5681
5682 /* If only parsing, set *arg and return here */
5683 if (!evaluate)
5684 {
5685 *arg = p + 1;
5686 return OK;
5687 }
5688
5689 /*
5690 * Copy the string into allocated memory, handling backslashed
5691 * characters.
5692 */
5693 name = alloc((unsigned)(p - *arg + extra));
5694 if (name == NULL)
5695 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 rettv->v_type = VAR_STRING;
5697 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
5701 if (*p == '\\')
5702 {
5703 switch (*++p)
5704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 case 'b': *name++ = BS; ++p; break;
5706 case 'e': *name++ = ESC; ++p; break;
5707 case 'f': *name++ = FF; ++p; break;
5708 case 'n': *name++ = NL; ++p; break;
5709 case 'r': *name++ = CAR; ++p; break;
5710 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 case 'X': /* hex: "\x1", "\x12" */
5713 case 'x':
5714 case 'u': /* Unicode: "\u0023" */
5715 case 'U':
5716 if (vim_isxdigit(p[1]))
5717 {
5718 int n, nr;
5719 int c = toupper(*p);
5720
5721 if (c == 'X')
5722 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005723 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005725 else
5726 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 nr = 0;
5728 while (--n >= 0 && vim_isxdigit(p[1]))
5729 {
5730 ++p;
5731 nr = (nr << 4) + hex2nr(*p);
5732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#ifdef FEAT_MBYTE
5735 /* For "\u" store the number according to
5736 * 'encoding'. */
5737 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 else
5740#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 break;
5744
5745 /* octal: "\1", "\12", "\123" */
5746 case '0':
5747 case '1':
5748 case '2':
5749 case '3':
5750 case '4':
5751 case '5':
5752 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 case '7': *name = *p++ - '0';
5754 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 *name = (*name << 3) + *p++ - '0';
5757 if (*p >= '0' && *p <= '7')
5758 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 break;
5762
5763 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (extra != 0)
5766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 break;
5769 }
5770 /* FALLTHROUGH */
5771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 break;
5774 }
5775 }
5776 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 *arg = p + 1;
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 return OK;
5784}
5785
5786/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 * Return OK or FAIL.
5789 */
5790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005791get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 int reduce = 0;
5796
5797 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 */
5800 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 break;
5806 ++reduce;
5807 ++p;
5808 }
5809 }
5810
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 return FAIL;
5815 }
5816
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 if (!evaluate)
5819 {
5820 *arg = p + 1;
5821 return OK;
5822 }
5823
5824 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005826 */
5827 str = alloc((unsigned)((p - *arg) - reduce));
5828 if (str == NULL)
5829 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 rettv->v_type = VAR_STRING;
5831 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 break;
5839 ++p;
5840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 *arg = p + 1;
5845
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 return OK;
5847}
5848
5849/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 * Allocate a variable for a List and fill it from "*arg".
5851 * Return OK or FAIL.
5852 */
5853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005854get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855{
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 list_T *l = NULL;
5857 typval_T tv;
5858 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859
5860 if (evaluate)
5861 {
5862 l = list_alloc();
5863 if (l == NULL)
5864 return FAIL;
5865 }
5866
5867 *arg = skipwhite(*arg + 1);
5868 while (**arg != ']' && **arg != NUL)
5869 {
5870 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5871 goto failret;
5872 if (evaluate)
5873 {
5874 item = listitem_alloc();
5875 if (item != NULL)
5876 {
5877 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005878 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 list_append(l, item);
5880 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005881 else
5882 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 }
5884
5885 if (**arg == ']')
5886 break;
5887 if (**arg != ',')
5888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 goto failret;
5891 }
5892 *arg = skipwhite(*arg + 1);
5893 }
5894
5895 if (**arg != ']')
5896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898failret:
5899 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005900 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 return FAIL;
5902 }
5903
5904 *arg = skipwhite(*arg + 1);
5905 if (evaluate)
5906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907 rettv->v_type = VAR_LIST;
5908 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 ++l->lv_refcount;
5910 }
5911
5912 return OK;
5913}
5914
5915/*
5916 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005917 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005919 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005922 list_T *l;
5923
5924 l = (list_T *)alloc_clear(sizeof(list_T));
5925 if (l != NULL)
5926 {
5927 /* Prepend the list to the list of lists for garbage collection. */
5928 if (first_list != NULL)
5929 first_list->lv_used_prev = l;
5930 l->lv_used_prev = NULL;
5931 l->lv_used_next = first_list;
5932 first_list = l;
5933 }
5934 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935}
5936
5937/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005938 * Allocate an empty list for a return value.
5939 * Returns OK or FAIL.
5940 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005941 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005943{
5944 list_T *l = list_alloc();
5945
5946 if (l == NULL)
5947 return FAIL;
5948
5949 rettv->vval.v_list = l;
5950 rettv->v_type = VAR_LIST;
5951 ++l->lv_refcount;
5952 return OK;
5953}
5954
5955/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 * Unreference a list: decrement the reference count and free it when it
5957 * becomes zero.
5958 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005960list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005962 if (l != NULL && --l->lv_refcount <= 0)
5963 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964}
5965
5966/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005967 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 * Ignores the reference count.
5969 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971list_free(
5972 list_T *l,
5973 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974{
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005977 /* Remove the list from the list of lists for garbage collection. */
5978 if (l->lv_used_prev == NULL)
5979 first_list = l->lv_used_next;
5980 else
5981 l->lv_used_prev->lv_used_next = l->lv_used_next;
5982 if (l->lv_used_next != NULL)
5983 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5984
Bram Moolenaard9fba312005-06-26 22:34:35 +00005985 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987 /* Remove the item before deleting it. */
5988 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005989 if (recurse || (item->li_tv.v_type != VAR_LIST
5990 && item->li_tv.v_type != VAR_DICT))
5991 clear_tv(&item->li_tv);
5992 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 }
5994 vim_free(l);
5995}
5996
5997/*
5998 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01005999 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006001 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002listitem_alloc()
6003{
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006008 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006013 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 vim_free(item);
6015}
6016
6017/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006018 * Remove a list item from a List and free it. Also clears the value.
6019 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006020 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006021listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006022{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006023 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006024 listitem_free(item);
6025}
6026
6027/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Get the number of items in a list.
6029 */
6030 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 if (l == NULL)
6034 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006035 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036}
6037
6038/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006039 * Return TRUE when two lists have exactly the same values.
6040 */
6041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042list_equal(
6043 list_T *l1,
6044 list_T *l2,
6045 int ic, /* ignore case for strings */
6046 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047{
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006049
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006050 if (l1 == NULL || l2 == NULL)
6051 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006052 if (l1 == l2)
6053 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 if (list_len(l1) != list_len(l2))
6055 return FALSE;
6056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057 for (item1 = l1->lv_first, item2 = l2->lv_first;
6058 item1 != NULL && item2 != NULL;
6059 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061 return FALSE;
6062 return item1 == NULL && item2 == NULL;
6063}
6064
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006065/*
6066 * Return the dictitem that an entry in a hashtable points to.
6067 */
6068 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006070{
6071 return HI2DI(hi);
6072}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006073
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006074/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 * Return TRUE when two dictionaries have exactly the same key/values.
6076 */
6077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078dict_equal(
6079 dict_T *d1,
6080 dict_T *d2,
6081 int ic, /* ignore case for strings */
6082 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006083{
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 hashitem_T *hi;
6085 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006086 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006087
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006088 if (d1 == NULL || d2 == NULL)
6089 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006090 if (d1 == d2)
6091 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006092 if (dict_len(d1) != dict_len(d2))
6093 return FALSE;
6094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006095 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006098 if (!HASHITEM_EMPTY(hi))
6099 {
6100 item2 = dict_find(d2, hi->hi_key, -1);
6101 if (item2 == NULL)
6102 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104 return FALSE;
6105 --todo;
6106 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006107 }
6108 return TRUE;
6109}
6110
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111static int tv_equal_recurse_limit;
6112
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006113/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006116 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 */
6118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119tv_equal(
6120 typval_T *tv1,
6121 typval_T *tv2,
6122 int ic, /* ignore case */
6123 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006124{
6125 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006126 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006127 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006128 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006130 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006131 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006132
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006133 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 * recursiveness to a limit. We guess they are equal then.
6135 * A fixed limit has the problem of still taking an awful long time.
6136 * Reduce the limit every time running into it. That should work fine for
6137 * deeply linked structures that are not recursively linked and catch
6138 * recursiveness quickly. */
6139 if (!recursive)
6140 tv_equal_recurse_limit = 1000;
6141 if (recursive_cnt >= tv_equal_recurse_limit)
6142 {
6143 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006144 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006146
6147 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006149 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 ++recursive_cnt;
6151 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6152 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006153 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006154
6155 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006156 ++recursive_cnt;
6157 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6158 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006159 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006160
6161 case VAR_FUNC:
6162 return (tv1->vval.v_string != NULL
6163 && tv2->vval.v_string != NULL
6164 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6165
6166 case VAR_NUMBER:
6167 return tv1->vval.v_number == tv2->vval.v_number;
6168
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006169#ifdef FEAT_FLOAT
6170 case VAR_FLOAT:
6171 return tv1->vval.v_float == tv2->vval.v_float;
6172#endif
6173
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006174 case VAR_STRING:
6175 s1 = get_tv_string_buf(tv1, buf1);
6176 s2 = get_tv_string_buf(tv2, buf2);
6177 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006178
6179 case VAR_SPECIAL:
6180 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006182
6183 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 return TRUE;
6185}
6186
6187/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 * Locate item with index "n" in list "l" and return it.
6189 * A negative index is counted from the end; -1 is the last item.
6190 * Returns NULL when "n" is out of range.
6191 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006192 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006193list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194{
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 long idx;
6197
6198 if (l == NULL)
6199 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200
6201 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006203 n = l->lv_len + n;
6204
6205 /* Check for index out of range. */
6206 if (n < 0 || n >= l->lv_len)
6207 return NULL;
6208
6209 /* When there is a cached index may start search from there. */
6210 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006211 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006212 if (n < l->lv_idx / 2)
6213 {
6214 /* closest to the start of the list */
6215 item = l->lv_first;
6216 idx = 0;
6217 }
6218 else if (n > (l->lv_idx + l->lv_len) / 2)
6219 {
6220 /* closest to the end of the list */
6221 item = l->lv_last;
6222 idx = l->lv_len - 1;
6223 }
6224 else
6225 {
6226 /* closest to the cached index */
6227 item = l->lv_idx_item;
6228 idx = l->lv_idx;
6229 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006230 }
6231 else
6232 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006233 if (n < l->lv_len / 2)
6234 {
6235 /* closest to the start of the list */
6236 item = l->lv_first;
6237 idx = 0;
6238 }
6239 else
6240 {
6241 /* closest to the end of the list */
6242 item = l->lv_last;
6243 idx = l->lv_len - 1;
6244 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006246
6247 while (n > idx)
6248 {
6249 /* search forward */
6250 item = item->li_next;
6251 ++idx;
6252 }
6253 while (n < idx)
6254 {
6255 /* search backward */
6256 item = item->li_prev;
6257 --idx;
6258 }
6259
6260 /* cache the used index */
6261 l->lv_idx = idx;
6262 l->lv_idx_item = item;
6263
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 return item;
6265}
6266
6267/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006268 * Get list item "l[idx]" as a number.
6269 */
6270 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006271list_find_nr(
6272 list_T *l,
6273 long idx,
6274 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006275{
6276 listitem_T *li;
6277
6278 li = list_find(l, idx);
6279 if (li == NULL)
6280 {
6281 if (errorp != NULL)
6282 *errorp = TRUE;
6283 return -1L;
6284 }
6285 return get_tv_number_chk(&li->li_tv, errorp);
6286}
6287
6288/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006289 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6290 */
6291 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006292list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006293{
6294 listitem_T *li;
6295
6296 li = list_find(l, idx - 1);
6297 if (li == NULL)
6298 {
6299 EMSGN(_(e_listidx), idx);
6300 return NULL;
6301 }
6302 return get_tv_string(&li->li_tv);
6303}
6304
6305/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006306 * Locate "item" list "l" and return its index.
6307 * Returns -1 when "item" is not in the list.
6308 */
6309 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006310list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006311{
6312 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006314
6315 if (l == NULL)
6316 return -1;
6317 idx = 0;
6318 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6319 ++idx;
6320 if (li == NULL)
6321 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006322 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006323}
6324
6325/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 * Append item "item" to the end of list "l".
6327 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006329list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330{
6331 if (l->lv_last == NULL)
6332 {
6333 /* empty list */
6334 l->lv_first = item;
6335 l->lv_last = item;
6336 item->li_prev = NULL;
6337 }
6338 else
6339 {
6340 l->lv_last->li_next = item;
6341 item->li_prev = l->lv_last;
6342 l->lv_last = item;
6343 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006344 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 item->li_next = NULL;
6346}
6347
6348/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006350 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006353list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006355 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356
Bram Moolenaar05159a02005-02-26 23:04:13 +00006357 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006359 copy_tv(tv, &li->li_tv);
6360 list_append(l, li);
6361 return OK;
6362}
6363
6364/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006365 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006366 * Return FAIL when out of memory.
6367 */
6368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006369list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006370{
6371 listitem_T *li = listitem_alloc();
6372
6373 if (li == NULL)
6374 return FAIL;
6375 li->li_tv.v_type = VAR_DICT;
6376 li->li_tv.v_lock = 0;
6377 li->li_tv.vval.v_dict = dict;
6378 list_append(list, li);
6379 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380 return OK;
6381}
6382
6383/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006384 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006385 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006386 * Returns FAIL when out of memory.
6387 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006390{
6391 listitem_T *li = listitem_alloc();
6392
6393 if (li == NULL)
6394 return FAIL;
6395 list_append(l, li);
6396 li->li_tv.v_type = VAR_STRING;
6397 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006398 if (str == NULL)
6399 li->li_tv.vval.v_string = NULL;
6400 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006401 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006402 return FAIL;
6403 return OK;
6404}
6405
6406/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006407 * Append "n" to list "l".
6408 * Returns FAIL when out of memory.
6409 */
6410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006412{
6413 listitem_T *li;
6414
6415 li = listitem_alloc();
6416 if (li == NULL)
6417 return FAIL;
6418 li->li_tv.v_type = VAR_NUMBER;
6419 li->li_tv.v_lock = 0;
6420 li->li_tv.vval.v_number = n;
6421 list_append(l, li);
6422 return OK;
6423}
6424
6425/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 * If "item" is NULL append at the end.
6428 * Return FAIL when out of memory.
6429 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006431list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
6435 if (ni == NULL)
6436 return FAIL;
6437 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006438 list_insert(l, ni, item);
6439 return OK;
6440}
6441
6442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006443list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006444{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445 if (item == NULL)
6446 /* Append new item at end of list. */
6447 list_append(l, ni);
6448 else
6449 {
6450 /* Insert new item before existing item. */
6451 ni->li_prev = item->li_prev;
6452 ni->li_next = item;
6453 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 ++l->lv_idx;
6457 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006461 l->lv_idx_item = NULL;
6462 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466}
6467
6468/*
6469 * Extend "l1" with "l2".
6470 * If "bef" is NULL append at the end, otherwise insert before this item.
6471 * Returns FAIL when out of memory.
6472 */
6473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006474list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475{
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006477 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006479 /* We also quit the loop when we have inserted the original item count of
6480 * the list, avoid a hang when we extend a list with itself. */
6481 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6483 return FAIL;
6484 return OK;
6485}
6486
6487/*
6488 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6489 * Return FAIL when out of memory.
6490 */
6491 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493{
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006496 if (l1 == NULL || l2 == NULL)
6497 return FAIL;
6498
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501 if (l == NULL)
6502 return FAIL;
6503 tv->v_type = VAR_LIST;
6504 tv->vval.v_list = l;
6505
6506 /* append all items from the second list */
6507 return list_extend(l, l2, NULL);
6508}
6509
6510/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006513 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 * Returns NULL when out of memory.
6515 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518{
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 list_T *copy;
6520 listitem_T *item;
6521 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
6523 if (orig == NULL)
6524 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525
6526 copy = list_alloc();
6527 if (copy != NULL)
6528 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (copyID != 0)
6530 {
6531 /* Do this before adding the items, because one of the items may
6532 * refer back to this list. */
6533 orig->lv_copyID = copyID;
6534 orig->lv_copylist = copy;
6535 }
6536 for (item = orig->lv_first; item != NULL && !got_int;
6537 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 {
6539 ni = listitem_alloc();
6540 if (ni == NULL)
6541 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006542 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 {
6544 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6545 {
6546 vim_free(ni);
6547 break;
6548 }
6549 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006551 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 list_append(copy, ni);
6553 }
6554 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 if (item != NULL)
6556 {
6557 list_unref(copy);
6558 copy = NULL;
6559 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 }
6561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 return copy;
6563}
6564
6565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006567 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006568 * This used to be called list_remove, but that conflicts with a Sun header
6569 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006572vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573{
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 /* notify watchers */
6577 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006579 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580 list_fix_watch(l, ip);
6581 if (ip == item2)
6582 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584
6585 if (item2->li_next == NULL)
6586 l->lv_last = item->li_prev;
6587 else
6588 item2->li_next->li_prev = item->li_prev;
6589 if (item->li_prev == NULL)
6590 l->lv_first = item2->li_next;
6591 else
6592 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006593 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594}
6595
6596/*
6597 * Return an allocated string with the string representation of a list.
6598 * May return NULL.
6599 */
6600 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602{
6603 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604
6605 if (tv->vval.v_list == NULL)
6606 return NULL;
6607 ga_init2(&ga, (int)sizeof(char), 80);
6608 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006609 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 {
6611 vim_free(ga.ga_data);
6612 return NULL;
6613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 ga_append(&ga, ']');
6615 ga_append(&ga, NUL);
6616 return (char_u *)ga.ga_data;
6617}
6618
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006619typedef struct join_S {
6620 char_u *s;
6621 char_u *tofree;
6622} join_T;
6623
6624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006625list_join_inner(
6626 garray_T *gap, /* to store the result in */
6627 list_T *l,
6628 char_u *sep,
6629 int echo_style,
6630 int copyID,
6631 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006632{
6633 int i;
6634 join_T *p;
6635 int len;
6636 int sumlen = 0;
6637 int first = TRUE;
6638 char_u *tofree;
6639 char_u numbuf[NUMBUFLEN];
6640 listitem_T *item;
6641 char_u *s;
6642
6643 /* Stringify each item in the list. */
6644 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6645 {
6646 if (echo_style)
6647 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6648 else
6649 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6650 if (s == NULL)
6651 return FAIL;
6652
6653 len = (int)STRLEN(s);
6654 sumlen += len;
6655
Bram Moolenaarcde88542015-08-11 19:14:00 +02006656 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006657 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6658 if (tofree != NULL || s != numbuf)
6659 {
6660 p->s = s;
6661 p->tofree = tofree;
6662 }
6663 else
6664 {
6665 p->s = vim_strnsave(s, len);
6666 p->tofree = p->s;
6667 }
6668
6669 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006670 if (did_echo_string_emsg) /* recursion error, bail out */
6671 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006672 }
6673
6674 /* Allocate result buffer with its total size, avoid re-allocation and
6675 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6676 if (join_gap->ga_len >= 2)
6677 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6678 if (ga_grow(gap, sumlen + 2) == FAIL)
6679 return FAIL;
6680
6681 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6682 {
6683 if (first)
6684 first = FALSE;
6685 else
6686 ga_concat(gap, sep);
6687 p = ((join_T *)join_gap->ga_data) + i;
6688
6689 if (p->s != NULL)
6690 ga_concat(gap, p->s);
6691 line_breakcheck();
6692 }
6693
6694 return OK;
6695}
6696
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006699 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006700 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703list_join(
6704 garray_T *gap,
6705 list_T *l,
6706 char_u *sep,
6707 int echo_style,
6708 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710 garray_T join_ga;
6711 int retval;
6712 join_T *p;
6713 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714
Bram Moolenaard39a7512015-04-16 22:51:22 +02006715 if (l->lv_len < 1)
6716 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6718 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6719
6720 /* Dispose each item in join_ga. */
6721 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723 p = (join_T *)join_ga.ga_data;
6724 for (i = 0; i < join_ga.ga_len; ++i)
6725 {
6726 vim_free(p->tofree);
6727 ++p;
6728 }
6729 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006731
6732 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733}
6734
6735/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006736 * Return the next (unique) copy ID.
6737 * Used for serializing nested structures.
6738 */
6739 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006740get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006741{
6742 current_copyID += COPYID_INC;
6743 return current_copyID;
6744}
6745
6746/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 * Garbage collection for lists and dictionaries.
6748 *
6749 * We use reference counts to be able to free most items right away when they
6750 * are no longer used. But for composite items it's possible that it becomes
6751 * unused while the reference count is > 0: When there is a recursive
6752 * reference. Example:
6753 * :let l = [1, 2, 3]
6754 * :let d = {9: l}
6755 * :let l[1] = d
6756 *
6757 * Since this is quite unusual we handle this with garbage collection: every
6758 * once in a while find out which lists and dicts are not referenced from any
6759 * variable.
6760 *
6761 * Here is a good reference text about garbage collection (refers to Python
6762 * but it applies to all reference-counting mechanisms):
6763 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765
6766/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 * Do garbage collection for lists and dicts.
6768 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006772{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006773 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006774 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 buf_T *buf;
6776 win_T *wp;
6777 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006778 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006779 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006780 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006781#ifdef FEAT_WINDOWS
6782 tabpage_T *tp;
6783#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006785 /* Only do this once. */
6786 want_garbage_collect = FALSE;
6787 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006788 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006789
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006790 /* We advance by two because we add one for items referenced through
6791 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006792 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006793
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 /*
6795 * 1. Go through all accessible variables and mark all lists and dicts
6796 * with copyID.
6797 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798
6799 /* Don't free variables in the previous_funccal list unless they are only
6800 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006801 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006802 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6803 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006804 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6805 NULL);
6806 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6807 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006808 }
6809
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 /* script-local variables */
6811 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006812 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813
6814 /* buffer-local variables */
6815 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006816 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6817 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006818
6819 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006820 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006821 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6822 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006823#ifdef FEAT_AUTOCMD
6824 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006825 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6826 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006827#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006829#ifdef FEAT_WINDOWS
6830 /* tabpage-local variables */
6831 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006832 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6833 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006834#endif
6835
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006837 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838
6839 /* function-local variables */
6840 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6841 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006842 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6843 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844 }
6845
Bram Moolenaard812df62008-11-09 12:46:09 +00006846 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006847 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006848
Bram Moolenaar1dced572012-04-05 16:54:08 +02006849#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006850 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006851#endif
6852
Bram Moolenaardb913952012-06-29 12:54:53 +02006853#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006854 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006855#endif
6856
6857#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006859#endif
6860
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006863 /*
6864 * 2. Free lists and dictionaries that are not referenced.
6865 */
6866 did_free = free_unref_items(copyID);
6867
6868 /*
6869 * 3. Check if any funccal can be freed now.
6870 */
6871 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006873 if (can_free_funccal(*pfc, copyID))
6874 {
6875 fc = *pfc;
6876 *pfc = fc->caller;
6877 free_funccal(fc, TRUE);
6878 did_free = TRUE;
6879 did_free_funccal = TRUE;
6880 }
6881 else
6882 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 if (did_free_funccal)
6885 /* When a funccal was freed some more items might be garbage
6886 * collected, so run again. */
6887 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 else if (p_verbose > 0)
6890 {
6891 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6892 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893
6894 return did_free;
6895}
6896
6897/*
6898 * Free lists and dictionaries that are no longer referenced.
6899 */
6900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006901free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006903 dict_T *dd, *dd_next;
6904 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905 int did_free = FALSE;
6906
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006908 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 */
6910 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006911 {
6912 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006913 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006915 /* Free the Dictionary and ordinary items it contains, but don't
6916 * recurse into Lists and Dictionaries, they will be in the list
6917 * of dicts or list of lists. */
6918 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006921 dd = dd_next;
6922 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923
6924 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006925 * Go through the list of lists and free items without the copyID.
6926 * But don't free a list that has a watcher (used in a for loop), these
6927 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 */
6929 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006930 {
6931 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006932 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6933 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006934 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006935 /* Free the List and ordinary items it contains, but don't recurse
6936 * into Lists and Dictionaries, they will be in the list of dicts
6937 * or list of lists. */
6938 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006941 ll = ll_next;
6942 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943 return did_free;
6944}
6945
6946/*
6947 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 * "list_stack" is used to add lists to be marked. Can be NULL.
6949 *
6950 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006953set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954{
6955 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 hashtab_T *cur_ht;
6959 ht_stack_T *ht_stack = NULL;
6960 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 cur_ht = ht;
6963 for (;;)
6964 {
6965 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 /* Mark each item in the hashtab. If the item contains a hashtab
6968 * it is added to ht_stack, if it contains a list it is added to
6969 * list_stack. */
6970 todo = (int)cur_ht->ht_used;
6971 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6972 if (!HASHITEM_EMPTY(hi))
6973 {
6974 --todo;
6975 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6976 &ht_stack, list_stack);
6977 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979
6980 if (ht_stack == NULL)
6981 break;
6982
6983 /* take an item from the stack */
6984 cur_ht = ht_stack->ht;
6985 tempitem = ht_stack;
6986 ht_stack = ht_stack->prev;
6987 free(tempitem);
6988 }
6989
6990 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991}
6992
6993/*
6994 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006995 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6996 *
6997 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007000set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007002 listitem_T *li;
7003 int abort = FALSE;
7004 list_T *cur_l;
7005 list_stack_T *list_stack = NULL;
7006 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007008 cur_l = l;
7009 for (;;)
7010 {
7011 if (!abort)
7012 /* Mark each item in the list. If the item contains a hashtab
7013 * it is added to ht_stack, if it contains a list it is added to
7014 * list_stack. */
7015 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7016 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7017 ht_stack, &list_stack);
7018 if (list_stack == NULL)
7019 break;
7020
7021 /* take an item from the stack */
7022 cur_l = list_stack->list;
7023 tempitem = list_stack;
7024 list_stack = list_stack->prev;
7025 free(tempitem);
7026 }
7027
7028 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029}
7030
7031/*
7032 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 * "list_stack" is used to add lists to be marked. Can be NULL.
7034 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7035 *
7036 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007039set_ref_in_item(
7040 typval_T *tv,
7041 int copyID,
7042 ht_stack_T **ht_stack,
7043 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044{
7045 dict_T *dd;
7046 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007048
7049 switch (tv->v_type)
7050 {
7051 case VAR_DICT:
7052 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007053 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007054 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 /* Didn't see this dict yet. */
7056 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 if (ht_stack == NULL)
7058 {
7059 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7060 }
7061 else
7062 {
7063 ht_stack_T *newitem = (ht_stack_T*)malloc(
7064 sizeof(ht_stack_T));
7065 if (newitem == NULL)
7066 abort = TRUE;
7067 else
7068 {
7069 newitem->ht = &dd->dv_hashtab;
7070 newitem->prev = *ht_stack;
7071 *ht_stack = newitem;
7072 }
7073 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007074 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007076
7077 case VAR_LIST:
7078 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007079 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007080 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 /* Didn't see this list yet. */
7082 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 if (list_stack == NULL)
7084 {
7085 abort = set_ref_in_list(ll, copyID, ht_stack);
7086 }
7087 else
7088 {
7089 list_stack_T *newitem = (list_stack_T*)malloc(
7090 sizeof(list_stack_T));
7091 if (newitem == NULL)
7092 abort = TRUE;
7093 else
7094 {
7095 newitem->list = ll;
7096 newitem->prev = *list_stack;
7097 *list_stack = newitem;
7098 }
7099 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007100 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007102 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007104}
7105
7106/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107 * Allocate an empty header for a dictionary.
7108 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007109 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111{
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007116 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007117 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007118 if (first_dict != NULL)
7119 first_dict->dv_used_prev = d;
7120 d->dv_used_next = first_dict;
7121 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007122 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007126 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007127 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007128 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007129 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131}
7132
7133/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007134 * Allocate an empty dict for a return value.
7135 * Returns OK or FAIL.
7136 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007137 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007138rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007139{
7140 dict_T *d = dict_alloc();
7141
7142 if (d == NULL)
7143 return FAIL;
7144
7145 rettv->vval.v_dict = d;
7146 rettv->v_type = VAR_DICT;
7147 ++d->dv_refcount;
7148 return OK;
7149}
7150
7151
7152/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153 * Unreference a Dictionary: decrement the reference count and free it when it
7154 * becomes zero.
7155 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007159 if (d != NULL && --d->dv_refcount <= 0)
7160 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007161}
7162
7163/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007164 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 * Ignores the reference count.
7166 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007168dict_free(
7169 dict_T *d,
7170 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 /* Remove the dict from the list of dicts for garbage collection. */
7177 if (d->dv_used_prev == NULL)
7178 first_dict = d->dv_used_next;
7179 else
7180 d->dv_used_prev->dv_used_next = d->dv_used_next;
7181 if (d->dv_used_next != NULL)
7182 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7183
7184 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007185 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007186 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007189 if (!HASHITEM_EMPTY(hi))
7190 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007191 /* Remove the item before deleting it, just in case there is
7192 * something recursive causing trouble. */
7193 di = HI2DI(hi);
7194 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007195 if (recurse || (di->di_tv.v_type != VAR_LIST
7196 && di->di_tv.v_type != VAR_DICT))
7197 clear_tv(&di->di_tv);
7198 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 --todo;
7200 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 vim_free(d);
7204}
7205
7206/*
7207 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 * The "key" is copied to the new item.
7209 * Note that the value of the item "di_tv" still needs to be initialized!
7210 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007212 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214{
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007217 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007221 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007222 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224}
7225
7226/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 * Make a copy of a Dictionary item.
7228 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007229 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231{
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007234 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7235 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 if (di != NULL)
7237 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007239 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240 copy_tv(&org->di_tv, &di->di_tv);
7241 }
7242 return di;
7243}
7244
7245/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 * Remove item "item" from Dictionary "dict" and free it.
7247 */
7248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007249dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007250{
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007254 if (HASHITEM_EMPTY(hi))
7255 EMSG2(_(e_intern2), "dictitem_remove()");
7256 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 dictitem_free(item);
7259}
7260
7261/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 * Free a dict item. Also clears the value.
7263 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007265dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007268 if (item->di_flags & DI_FLAGS_ALLOC)
7269 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270}
7271
7272/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7274 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007275 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 * Returns NULL when out of memory.
7277 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007279dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280{
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dict_T *copy;
7282 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285
7286 if (orig == NULL)
7287 return NULL;
7288
7289 copy = dict_alloc();
7290 if (copy != NULL)
7291 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007292 if (copyID != 0)
7293 {
7294 orig->dv_copyID = copyID;
7295 orig->dv_copydict = copy;
7296 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007298 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 --todo;
7303
7304 di = dictitem_alloc(hi->hi_key);
7305 if (di == NULL)
7306 break;
7307 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007308 {
7309 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7310 copyID) == FAIL)
7311 {
7312 vim_free(di);
7313 break;
7314 }
7315 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 else
7317 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7318 if (dict_add(copy, di) == FAIL)
7319 {
7320 dictitem_free(di);
7321 break;
7322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007327 if (todo > 0)
7328 {
7329 dict_unref(copy);
7330 copy = NULL;
7331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 }
7333
7334 return copy;
7335}
7336
7337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007339 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345}
7346
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007348 * Add a number or string entry to dictionary "d".
7349 * When "str" is NULL use number "nr", otherwise use "str".
7350 * Returns FAIL when out of memory and when key already exists.
7351 */
7352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353dict_add_nr_str(
7354 dict_T *d,
7355 char *key,
7356 long nr,
7357 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007358{
7359 dictitem_T *item;
7360
7361 item = dictitem_alloc((char_u *)key);
7362 if (item == NULL)
7363 return FAIL;
7364 item->di_tv.v_lock = 0;
7365 if (str == NULL)
7366 {
7367 item->di_tv.v_type = VAR_NUMBER;
7368 item->di_tv.vval.v_number = nr;
7369 }
7370 else
7371 {
7372 item->di_tv.v_type = VAR_STRING;
7373 item->di_tv.vval.v_string = vim_strsave(str);
7374 }
7375 if (dict_add(d, item) == FAIL)
7376 {
7377 dictitem_free(item);
7378 return FAIL;
7379 }
7380 return OK;
7381}
7382
7383/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007384 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007385 * Returns FAIL when out of memory and when key already exists.
7386 */
7387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007388dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007389{
7390 dictitem_T *item;
7391
7392 item = dictitem_alloc((char_u *)key);
7393 if (item == NULL)
7394 return FAIL;
7395 item->di_tv.v_lock = 0;
7396 item->di_tv.v_type = VAR_LIST;
7397 item->di_tv.vval.v_list = list;
7398 if (dict_add(d, item) == FAIL)
7399 {
7400 dictitem_free(item);
7401 return FAIL;
7402 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007403 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007404 return OK;
7405}
7406
7407/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 * Get the number of items in a Dictionary.
7409 */
7410 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 if (d == NULL)
7414 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007415 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416}
7417
7418/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 * Find item "key[len]" in Dictionary "d".
7420 * If "len" is negative use strlen(key).
7421 * Returns NULL when not found.
7422 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007423 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426#define AKEYLEN 200
7427 char_u buf[AKEYLEN];
7428 char_u *akey;
7429 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007432 if (len < 0)
7433 akey = key;
7434 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436 tofree = akey = vim_strnsave(key, len);
7437 if (akey == NULL)
7438 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007439 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007440 else
7441 {
7442 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007443 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 akey = buf;
7445 }
7446
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 vim_free(tofree);
7449 if (HASHITEM_EMPTY(hi))
7450 return NULL;
7451 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452}
7453
7454/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007455 * Get a string item from a dictionary.
7456 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007457 * Returns NULL if the entry doesn't exist or out of memory.
7458 */
7459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007460get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007461{
7462 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007463 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007464
7465 di = dict_find(d, key, -1);
7466 if (di == NULL)
7467 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007468 s = get_tv_string(&di->di_tv);
7469 if (save && s != NULL)
7470 s = vim_strsave(s);
7471 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007472}
7473
7474/*
7475 * Get a number item from a dictionary.
7476 * Returns 0 if the entry doesn't exist or out of memory.
7477 */
7478 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007479get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007480{
7481 dictitem_T *di;
7482
7483 di = dict_find(d, key, -1);
7484 if (di == NULL)
7485 return 0;
7486 return get_tv_number(&di->di_tv);
7487}
7488
7489/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 * Return an allocated string with the string representation of a Dictionary.
7491 * May return NULL.
7492 */
7493 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007494dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495{
7496 garray_T ga;
7497 int first = TRUE;
7498 char_u *tofree;
7499 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 return NULL;
7507 ga_init2(&ga, (int)sizeof(char), 80);
7508 ga_append(&ga, '{');
7509
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007510 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007511 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 --todo;
7516
7517 if (first)
7518 first = FALSE;
7519 else
7520 ga_concat(&ga, (char_u *)", ");
7521
7522 tofree = string_quote(hi->hi_key, FALSE);
7523 if (tofree != NULL)
7524 {
7525 ga_concat(&ga, tofree);
7526 vim_free(tofree);
7527 }
7528 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007529 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 if (s != NULL)
7531 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007533 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007534 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007535 line_breakcheck();
7536
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007539 if (todo > 0)
7540 {
7541 vim_free(ga.ga_data);
7542 return NULL;
7543 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544
7545 ga_append(&ga, '}');
7546 ga_append(&ga, NUL);
7547 return (char_u *)ga.ga_data;
7548}
7549
7550/*
7551 * Allocate a variable for a Dictionary and fill it from "*arg".
7552 * Return OK or FAIL. Returns NOTDONE for {expr}.
7553 */
7554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556{
Bram Moolenaar33570922005-01-25 22:26:29 +00007557 dict_T *d = NULL;
7558 typval_T tvkey;
7559 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007560 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564
7565 /*
7566 * First check if it's not a curly-braces thing: {expr}.
7567 * Must do this without evaluating, otherwise a function may be called
7568 * twice. Unfortunately this means we need to call eval1() twice for the
7569 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 if (*start != '}')
7573 {
7574 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7575 return FAIL;
7576 if (*start == '}')
7577 return NOTDONE;
7578 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579
7580 if (evaluate)
7581 {
7582 d = dict_alloc();
7583 if (d == NULL)
7584 return FAIL;
7585 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 tvkey.v_type = VAR_UNKNOWN;
7587 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588
7589 *arg = skipwhite(*arg + 1);
7590 while (**arg != '}' && **arg != NUL)
7591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593 goto failret;
7594 if (**arg != ':')
7595 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007596 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 goto failret;
7599 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007600 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007602 key = get_tv_string_buf_chk(&tvkey, buf);
7603 if (key == NULL || *key == NUL)
7604 {
7605 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7606 if (key != NULL)
7607 EMSG(_(e_emptykey));
7608 clear_tv(&tvkey);
7609 goto failret;
7610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612
7613 *arg = skipwhite(*arg + 1);
7614 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7615 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007616 if (evaluate)
7617 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 goto failret;
7619 }
7620 if (evaluate)
7621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007622 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 if (item != NULL)
7624 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007625 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 clear_tv(&tv);
7628 goto failret;
7629 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 item = dictitem_alloc(key);
7631 clear_tv(&tvkey);
7632 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007635 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 if (dict_add(d, item) == FAIL)
7637 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 }
7639 }
7640
7641 if (**arg == '}')
7642 break;
7643 if (**arg != ',')
7644 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007645 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 goto failret;
7647 }
7648 *arg = skipwhite(*arg + 1);
7649 }
7650
7651 if (**arg != '}')
7652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007653 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654failret:
7655 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007656 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 return FAIL;
7658 }
7659
7660 *arg = skipwhite(*arg + 1);
7661 if (evaluate)
7662 {
7663 rettv->v_type = VAR_DICT;
7664 rettv->vval.v_dict = d;
7665 ++d->dv_refcount;
7666 }
7667
7668 return OK;
7669}
7670
Bram Moolenaar17a13432016-01-24 14:22:10 +01007671 static char *
7672get_var_special_name(int nr)
7673{
7674 switch (nr)
7675 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007676 case VVAL_FALSE: return "v:false";
7677 case VVAL_TRUE: return "v:true";
7678 case VVAL_NONE: return "v:none";
7679 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007680 }
7681 EMSG2(_(e_intern2), "get_var_special_name()");
7682 return "42";
7683}
7684
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686 * Return a string with the string representation of a variable.
7687 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007688 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007689 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007690 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007691 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692 */
7693 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007694echo_string(
7695 typval_T *tv,
7696 char_u **tofree,
7697 char_u *numbuf,
7698 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007699{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700 static int recurse = 0;
7701 char_u *r = NULL;
7702
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007704 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007705 if (!did_echo_string_emsg)
7706 {
7707 /* Only give this message once for a recursive call to avoid
7708 * flooding the user with errors. And stop iterating over lists
7709 * and dicts. */
7710 did_echo_string_emsg = TRUE;
7711 EMSG(_("E724: variable nested too deep for displaying"));
7712 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007714 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 }
7716 ++recurse;
7717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718 switch (tv->v_type)
7719 {
7720 case VAR_FUNC:
7721 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 r = tv->vval.v_string;
7723 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007724
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007726 if (tv->vval.v_list == NULL)
7727 {
7728 *tofree = NULL;
7729 r = NULL;
7730 }
7731 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7732 {
7733 *tofree = NULL;
7734 r = (char_u *)"[...]";
7735 }
7736 else
7737 {
7738 tv->vval.v_list->lv_copyID = copyID;
7739 *tofree = list2string(tv, copyID);
7740 r = *tofree;
7741 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007743
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007745 if (tv->vval.v_dict == NULL)
7746 {
7747 *tofree = NULL;
7748 r = NULL;
7749 }
7750 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7751 {
7752 *tofree = NULL;
7753 r = (char_u *)"{...}";
7754 }
7755 else
7756 {
7757 tv->vval.v_dict->dv_copyID = copyID;
7758 *tofree = dict2string(tv, copyID);
7759 r = *tofree;
7760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763 case VAR_STRING:
7764 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765 *tofree = NULL;
7766 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007768
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007769#ifdef FEAT_FLOAT
7770 case VAR_FLOAT:
7771 *tofree = NULL;
7772 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7773 r = numbuf;
7774 break;
7775#endif
7776
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007777 case VAR_SPECIAL:
7778 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007779 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007780 break;
7781
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007782 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007783 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007785 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786
Bram Moolenaar8502c702014-06-17 12:51:16 +02007787 if (--recurse == 0)
7788 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007790}
7791
7792/*
7793 * Return a string with the string representation of a variable.
7794 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7795 * "numbuf" is used for a number.
7796 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007797 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007798 */
7799 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007800tv2string(
7801 typval_T *tv,
7802 char_u **tofree,
7803 char_u *numbuf,
7804 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007805{
7806 switch (tv->v_type)
7807 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007808 case VAR_FUNC:
7809 *tofree = string_quote(tv->vval.v_string, TRUE);
7810 return *tofree;
7811 case VAR_STRING:
7812 *tofree = string_quote(tv->vval.v_string, FALSE);
7813 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007814#ifdef FEAT_FLOAT
7815 case VAR_FLOAT:
7816 *tofree = NULL;
7817 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7818 return numbuf;
7819#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007821 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007822 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007823 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007825 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007828 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829}
7830
7831/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 * Return string "str" in ' quotes, doubling ' characters.
7833 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007834 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007835 */
7836 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838{
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007840 char_u *p, *r, *s;
7841
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 len = (function ? 13 : 3);
7843 if (str != NULL)
7844 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007845 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007846 for (p = str; *p != NUL; mb_ptr_adv(p))
7847 if (*p == '\'')
7848 ++len;
7849 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007850 s = r = alloc(len);
7851 if (r != NULL)
7852 {
7853 if (function)
7854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007855 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007856 r += 10;
7857 }
7858 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007859 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 if (str != NULL)
7861 for (p = str; *p != NUL; )
7862 {
7863 if (*p == '\'')
7864 *r++ = '\'';
7865 MB_COPY_CHAR(p, r);
7866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007867 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007868 if (function)
7869 *r++ = ')';
7870 *r++ = NUL;
7871 }
7872 return s;
7873}
7874
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007875#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876/*
7877 * Convert the string "text" to a floating point number.
7878 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7879 * this always uses a decimal point.
7880 * Returns the length of the text that was consumed.
7881 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007883string2float(
7884 char_u *text,
7885 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886{
7887 char *s = (char *)text;
7888 float_T f;
7889
7890 f = strtod(s, &s);
7891 *value = f;
7892 return (int)((char_u *)s - text);
7893}
7894#endif
7895
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 * Get the value of an environment variable.
7898 * "arg" is pointing to the '$'. It is advanced to after the name.
7899 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007900 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 */
7902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007903get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 char_u *string = NULL;
7906 int len;
7907 int cc;
7908 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007909 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
7911 ++*arg;
7912 name = *arg;
7913 len = get_env_len(arg);
7914 if (evaluate)
7915 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007916 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007917 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007918
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007919 cc = name[len];
7920 name[len] = NUL;
7921 /* first try vim_getenv(), fast for normal environment vars */
7922 string = vim_getenv(name, &mustfree);
7923 if (string != NULL && *string != NUL)
7924 {
7925 if (!mustfree)
7926 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007928 else
7929 {
7930 if (mustfree)
7931 vim_free(string);
7932
7933 /* next try expanding things like $VIM and ${HOME} */
7934 string = expand_env_save(name - 1);
7935 if (string != NULL && *string == '$')
7936 {
7937 vim_free(string);
7938 string = NULL;
7939 }
7940 }
7941 name[len] = cc;
7942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 rettv->v_type = VAR_STRING;
7944 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 }
7946
7947 return OK;
7948}
7949
7950/*
7951 * Array with names and number of arguments of all internal functions
7952 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7953 */
7954static struct fst
7955{
7956 char *f_name; /* function name */
7957 char f_min_argc; /* minimal number of arguments */
7958 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007959 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007960 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961} functions[] =
7962{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007963#ifdef FEAT_FLOAT
7964 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007965 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007967 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007968 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007969 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"append", 2, 2, f_append},
7971 {"argc", 0, 0, f_argc},
7972 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007973 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007974 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007975#ifdef FEAT_FLOAT
7976 {"asin", 1, 1, f_asin}, /* WJMc */
7977#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007978 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007979 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007980 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007981 {"assert_false", 1, 2, f_assert_false},
7982 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#ifdef FEAT_FLOAT
7984 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007985 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007988 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"bufexists", 1, 1, f_bufexists},
7990 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7991 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7992 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7993 {"buflisted", 1, 1, f_buflisted},
7994 {"bufloaded", 1, 1, f_bufloaded},
7995 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007996 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {"bufwinnr", 1, 1, f_bufwinnr},
7998 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007999 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008000 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008001 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008002#ifdef FEAT_FLOAT
8003 {"ceil", 1, 1, f_ceil},
8004#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008005 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008006 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008008 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008010#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008011 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008012 {"complete_add", 1, 1, f_complete_add},
8013 {"complete_check", 0, 0, f_complete_check},
8014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"confirm", 1, 4, f_confirm},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008016#ifdef FEAT_CHANNEL
8017 {"connect", 2, 3, f_connect},
8018#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020#ifdef FEAT_FLOAT
8021 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008022 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008024 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008026 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008027 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008028 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008030 {"diff_filler", 1, 1, f_diff_filler},
8031 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008032#ifdef FEAT_CHANNEL
8033 {"disconnect", 1, 1, f_disconnect},
8034#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008035 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"eventhandler", 0, 0, f_eventhandler},
8039 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008040 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008042#ifdef FEAT_FLOAT
8043 {"exp", 1, 1, f_exp},
8044#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008045 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008046 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008047 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8049 {"filereadable", 1, 1, f_filereadable},
8050 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008051 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008052 {"finddir", 1, 3, f_finddir},
8053 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008054#ifdef FEAT_FLOAT
8055 {"float2nr", 1, 1, f_float2nr},
8056 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008057 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008059 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"fnamemodify", 2, 2, f_fnamemodify},
8061 {"foldclosed", 1, 1, f_foldclosed},
8062 {"foldclosedend", 1, 1, f_foldclosedend},
8063 {"foldlevel", 1, 1, f_foldlevel},
8064 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008065 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008067 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008068 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008069 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008070 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008071 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"getchar", 0, 1, f_getchar},
8073 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008074 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"getcmdline", 0, 0, f_getcmdline},
8076 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008077 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008078 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008079 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008080 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008081 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008082 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"getfsize", 1, 1, f_getfsize},
8084 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008085 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008086 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008087 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008088 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008089 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008090 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008091 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008092 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008094 {"gettabvar", 2, 3, f_gettabvar},
8095 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"getwinposx", 0, 0, f_getwinposx},
8097 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008098 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008099 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008100 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008101 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008104 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008105 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8107 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8108 {"histadd", 2, 2, f_histadd},
8109 {"histdel", 1, 2, f_histdel},
8110 {"histget", 1, 2, f_histget},
8111 {"histnr", 1, 1, f_histnr},
8112 {"hlID", 1, 1, f_hlID},
8113 {"hlexists", 1, 1, f_hlexists},
8114 {"hostname", 0, 0, f_hostname},
8115 {"iconv", 3, 3, f_iconv},
8116 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008117 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008118 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008120 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"inputrestore", 0, 0, f_inputrestore},
8122 {"inputsave", 0, 0, f_inputsave},
8123 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008125 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008127 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008128 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008129 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008130 {"jsondecode", 1, 1, f_jsondecode},
8131 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008132 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"libcall", 3, 3, f_libcall},
8136 {"libcallnr", 3, 3, f_libcallnr},
8137 {"line", 1, 1, f_line},
8138 {"line2byte", 1, 1, f_line2byte},
8139 {"lispindent", 1, 1, f_lispindent},
8140 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008142 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008143 {"log10", 1, 1, f_log10},
8144#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008145#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008146 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008147#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008148 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008149 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008150 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008151 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008152 {"matchadd", 2, 5, f_matchadd},
8153 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008154 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008155 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008156 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008157 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008158 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008159 {"max", 1, 1, f_max},
8160 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008161#ifdef vim_mkdir
8162 {"mkdir", 1, 3, f_mkdir},
8163#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008165#ifdef FEAT_MZSCHEME
8166 {"mzeval", 1, 1, f_mzeval},
8167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008169 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008170 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008171 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008172#ifdef FEAT_PERL
8173 {"perleval", 1, 1, f_perleval},
8174#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008175#ifdef FEAT_FLOAT
8176 {"pow", 2, 2, f_pow},
8177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008179 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008180 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008181#ifdef FEAT_PYTHON3
8182 {"py3eval", 1, 1, f_py3eval},
8183#endif
8184#ifdef FEAT_PYTHON
8185 {"pyeval", 1, 1, f_pyeval},
8186#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008187 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008188 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008189 {"reltime", 0, 2, f_reltime},
8190 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"remote_expr", 2, 3, f_remote_expr},
8192 {"remote_foreground", 1, 1, f_remote_foreground},
8193 {"remote_peek", 1, 2, f_remote_peek},
8194 {"remote_read", 1, 1, f_remote_read},
8195 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008196 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008198 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008200 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201#ifdef FEAT_FLOAT
8202 {"round", 1, 1, f_round},
8203#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008204 {"screenattr", 2, 2, f_screenattr},
8205 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008206 {"screencol", 0, 0, f_screencol},
8207 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008208 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008209 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008210 {"searchpair", 3, 7, f_searchpair},
8211 {"searchpairpos", 3, 7, f_searchpairpos},
8212 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008213#ifdef FEAT_CHANNEL
8214 {"sendexpr", 2, 3, f_sendexpr},
8215 {"sendraw", 2, 3, f_sendraw},
8216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"server2client", 2, 2, f_server2client},
8218 {"serverlist", 0, 0, f_serverlist},
8219 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008220 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"setcmdpos", 1, 1, f_setcmdpos},
8222 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008223 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008224 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008225 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008226 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008228 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008229 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008231#ifdef FEAT_CRYPT
8232 {"sha256", 1, 1, f_sha256},
8233#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008234 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008235 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#ifdef FEAT_FLOAT
8238 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008241 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008242 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008243 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008244 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008245 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008246#ifdef FEAT_FLOAT
8247 {"sqrt", 1, 1, f_sqrt},
8248 {"str2float", 1, 1, f_str2float},
8249#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008250 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008251 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008252 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#ifdef HAVE_STRFTIME
8254 {"strftime", 1, 2, f_strftime},
8255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"strlen", 1, 1, f_strlen},
8259 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008260 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008262 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008263 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"substitute", 4, 4, f_substitute},
8265 {"synID", 3, 3, f_synID},
8266 {"synIDattr", 2, 3, f_synIDattr},
8267 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008268 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008269 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008270 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008271 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008272 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008273 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008274 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008275 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008276 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008277#ifdef FEAT_FLOAT
8278 {"tan", 1, 1, f_tan},
8279 {"tanh", 1, 1, f_tanh},
8280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008282 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"tolower", 1, 1, f_tolower},
8284 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008285 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008286#ifdef FEAT_FLOAT
8287 {"trunc", 1, 1, f_trunc},
8288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008290 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008291 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008292 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008293 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"virtcol", 1, 1, f_virtcol},
8295 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008296 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"winbufnr", 1, 1, f_winbufnr},
8298 {"wincol", 0, 0, f_wincol},
8299 {"winheight", 1, 1, f_winheight},
8300 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008301 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008303 {"winrestview", 1, 1, f_winrestview},
8304 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008306 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008307 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008308 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309};
8310
8311#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8312
8313/*
8314 * Function given to ExpandGeneric() to obtain the list of internal
8315 * or user defined function names.
8316 */
8317 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008318get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320 static int intidx = -1;
8321 char_u *name;
8322
8323 if (idx == 0)
8324 intidx = -1;
8325 if (intidx < 0)
8326 {
8327 name = get_user_func_name(xp, idx);
8328 if (name != NULL)
8329 return name;
8330 }
8331 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8332 {
8333 STRCPY(IObuff, functions[intidx].f_name);
8334 STRCAT(IObuff, "(");
8335 if (functions[intidx].f_max_argc == 0)
8336 STRCAT(IObuff, ")");
8337 return IObuff;
8338 }
8339
8340 return NULL;
8341}
8342
8343/*
8344 * Function given to ExpandGeneric() to obtain the list of internal or
8345 * user defined variable or function names.
8346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008348get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 static int intidx = -1;
8351 char_u *name;
8352
8353 if (idx == 0)
8354 intidx = -1;
8355 if (intidx < 0)
8356 {
8357 name = get_function_name(xp, idx);
8358 if (name != NULL)
8359 return name;
8360 }
8361 return get_user_var_name(xp, ++intidx);
8362}
8363
8364#endif /* FEAT_CMDL_COMPL */
8365
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008366#if defined(EBCDIC) || defined(PROTO)
8367/*
8368 * Compare struct fst by function name.
8369 */
8370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008371compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008372{
8373 struct fst *p1 = (struct fst *)s1;
8374 struct fst *p2 = (struct fst *)s2;
8375
8376 return STRCMP(p1->f_name, p2->f_name);
8377}
8378
8379/*
8380 * Sort the function table by function name.
8381 * The sorting of the table above is ASCII dependant.
8382 * On machines using EBCDIC we have to sort it.
8383 */
8384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008385sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008386{
8387 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8388
8389 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8390}
8391#endif
8392
8393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394/*
8395 * Find internal function in table above.
8396 * Return index, or -1 if not found
8397 */
8398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008399find_internal_func(
8400 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401{
8402 int first = 0;
8403 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8404 int cmp;
8405 int x;
8406
8407 /*
8408 * Find the function name in the table. Binary search.
8409 */
8410 while (first <= last)
8411 {
8412 x = first + ((unsigned)(last - first) >> 1);
8413 cmp = STRCMP(name, functions[x].f_name);
8414 if (cmp < 0)
8415 last = x - 1;
8416 else if (cmp > 0)
8417 first = x + 1;
8418 else
8419 return x;
8420 }
8421 return -1;
8422}
8423
8424/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008425 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8426 * name it contains, otherwise return "name".
8427 */
8428 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008429deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008430{
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432 int cc;
8433
8434 cc = name[*lenp];
8435 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008436 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008438 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008439 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008441 {
8442 *lenp = 0;
8443 return (char_u *)""; /* just in case */
8444 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008445 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008446 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008447 }
8448
8449 return name;
8450}
8451
8452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 * Allocate a variable for the result of a function.
8454 * Return OK or FAIL.
8455 */
8456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008457get_func_tv(
8458 char_u *name, /* name of the function */
8459 int len, /* length of "name" */
8460 typval_T *rettv,
8461 char_u **arg, /* argument, pointing to the '(' */
8462 linenr_T firstline, /* first line of range */
8463 linenr_T lastline, /* last line of range */
8464 int *doesrange, /* return: function handled range */
8465 int evaluate,
8466 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468 char_u *argp;
8469 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008470 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 int argcount = 0; /* number of arguments found */
8472
8473 /*
8474 * Get the arguments.
8475 */
8476 argp = *arg;
8477 while (argcount < MAX_FUNC_ARGS)
8478 {
8479 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8480 if (*argp == ')' || *argp == ',' || *argp == NUL)
8481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8483 {
8484 ret = FAIL;
8485 break;
8486 }
8487 ++argcount;
8488 if (*argp != ',')
8489 break;
8490 }
8491 if (*argp == ')')
8492 ++argp;
8493 else
8494 ret = FAIL;
8495
8496 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 {
8501 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008502 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008504 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
8507 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
8510 *arg = skipwhite(argp);
8511 return ret;
8512}
8513
8514
8515/*
8516 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008517 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008518 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521call_func(
8522 char_u *funcname, /* name of the function */
8523 int len, /* length of "name" */
8524 typval_T *rettv, /* return value goes here */
8525 int argcount, /* number of "argvars" */
8526 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008527 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008528 linenr_T firstline, /* first line of range */
8529 linenr_T lastline, /* last line of range */
8530 int *doesrange, /* return: function handled range */
8531 int evaluate,
8532 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535#define ERROR_UNKNOWN 0
8536#define ERROR_TOOMANY 1
8537#define ERROR_TOOFEW 2
8538#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539#define ERROR_DICT 4
8540#define ERROR_NONE 5
8541#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 int error = ERROR_NONE;
8543 int i;
8544 int llen;
8545 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#define FLEN_FIXED 40
8547 char_u fname_buf[FLEN_FIXED + 1];
8548 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008549 char_u *name;
8550
8551 /* Make a copy of the name, if it comes from a funcref variable it could
8552 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008553 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008554 if (name == NULL)
8555 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
8557 /*
8558 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8559 * Change <SNR>123_name() to K_SNR 123_name().
8560 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 llen = eval_fname_script(name);
8563 if (llen > 0)
8564 {
8565 fname_buf[0] = K_SPECIAL;
8566 fname_buf[1] = KS_EXTRA;
8567 fname_buf[2] = (int)KE_SNR;
8568 i = 3;
8569 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8570 {
8571 if (current_SID <= 0)
8572 error = ERROR_SCRIPT;
8573 else
8574 {
8575 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8576 i = (int)STRLEN(fname_buf);
8577 }
8578 }
8579 if (i + STRLEN(name + llen) < FLEN_FIXED)
8580 {
8581 STRCPY(fname_buf + i, name + llen);
8582 fname = fname_buf;
8583 }
8584 else
8585 {
8586 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8587 if (fname == NULL)
8588 error = ERROR_OTHER;
8589 else
8590 {
8591 mch_memmove(fname, fname_buf, (size_t)i);
8592 STRCPY(fname + i, name + llen);
8593 }
8594 }
8595 }
8596 else
8597 fname = name;
8598
8599 *doesrange = FALSE;
8600
8601
8602 /* execute the function if no errors detected and executing */
8603 if (evaluate && error == ERROR_NONE)
8604 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008605 char_u *rfname = fname;
8606
8607 /* Ignore "g:" before a function name. */
8608 if (fname[0] == 'g' && fname[1] == ':')
8609 rfname = fname + 2;
8610
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008611 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8612 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 error = ERROR_UNKNOWN;
8614
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008615 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {
8617 /*
8618 * User defined function.
8619 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008620 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008623 /* Trigger FuncUndefined event, may load the function. */
8624 if (fp == NULL
8625 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008626 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008627 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008629 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008630 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 }
8632#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008633 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008634 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008635 {
8636 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008637 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008638 }
8639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 if (fp != NULL)
8641 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008642 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008644 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008646 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008648 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008649 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 else
8651 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008652 int did_save_redo = FALSE;
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 /*
8655 * Call the user function.
8656 * Save and restore search patterns, script variables and
8657 * redo buffer.
8658 */
8659 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008660#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008661 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008662#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008663 {
8664 saveRedobuff();
8665 did_save_redo = TRUE;
8666 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008667 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008669 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008670 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8671 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8672 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008673 /* Function was unreferenced while being used, free it
8674 * now. */
8675 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008676 if (did_save_redo)
8677 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 restore_search_patterns();
8679 error = ERROR_NONE;
8680 }
8681 }
8682 }
8683 else
8684 {
8685 /*
8686 * Find the function name in the table, call its implementation.
8687 */
8688 i = find_internal_func(fname);
8689 if (i >= 0)
8690 {
8691 if (argcount < functions[i].f_min_argc)
8692 error = ERROR_TOOFEW;
8693 else if (argcount > functions[i].f_max_argc)
8694 error = ERROR_TOOMANY;
8695 else
8696 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008697 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 error = ERROR_NONE;
8700 }
8701 }
8702 }
8703 /*
8704 * The function call (or "FuncUndefined" autocommand sequence) might
8705 * have been aborted by an error, an interrupt, or an explicitly thrown
8706 * exception that has not been caught so far. This situation can be
8707 * tested for by calling aborting(). For an error in an internal
8708 * function or for the "E132" error in call_user_func(), however, the
8709 * throw point at which the "force_abort" flag (temporarily reset by
8710 * emsg()) is normally updated has not been reached yet. We need to
8711 * update that flag first to make aborting() reliable.
8712 */
8713 update_force_abort();
8714 }
8715 if (error == ERROR_NONE)
8716 ret = OK;
8717
8718 /*
8719 * Report an error unless the argument evaluation or function call has been
8720 * cancelled due to an aborting error, an interrupt, or an exception.
8721 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008722 if (!aborting())
8723 {
8724 switch (error)
8725 {
8726 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008727 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008728 break;
8729 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008730 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 break;
8732 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008733 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008734 name);
8735 break;
8736 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008737 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008738 name);
8739 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008740 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008741 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008742 name);
8743 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008744 }
8745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 if (fname != name && fname != fname_buf)
8748 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008749 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
8751 return ret;
8752}
8753
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008754/*
8755 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008756 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008757 */
8758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008759emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008760{
8761 char_u *p;
8762
8763 if (*name == K_SPECIAL)
8764 p = concat_str((char_u *)"<SNR>", name + 3);
8765 else
8766 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008767 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008768 if (p != name)
8769 vim_free(p);
8770}
8771
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008772/*
8773 * Return TRUE for a non-zero Number and a non-empty String.
8774 */
8775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008776non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008777{
8778 return ((argvars[0].v_type == VAR_NUMBER
8779 && argvars[0].vval.v_number != 0)
8780 || (argvars[0].v_type == VAR_STRING
8781 && argvars[0].vval.v_string != NULL
8782 && *argvars[0].vval.v_string != NUL));
8783}
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785/*********************************************
8786 * Implementation of the built-in functions
8787 */
8788
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008789#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008790static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008791
8792/*
8793 * Get the float value of "argvars[0]" into "f".
8794 * Returns FAIL when the argument is not a Number or Float.
8795 */
8796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008797get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008798{
8799 if (argvars[0].v_type == VAR_FLOAT)
8800 {
8801 *f = argvars[0].vval.v_float;
8802 return OK;
8803 }
8804 if (argvars[0].v_type == VAR_NUMBER)
8805 {
8806 *f = (float_T)argvars[0].vval.v_number;
8807 return OK;
8808 }
8809 EMSG(_("E808: Number or Float required"));
8810 return FAIL;
8811}
8812
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008813/*
8814 * "abs(expr)" function
8815 */
8816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008817f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008818{
8819 if (argvars[0].v_type == VAR_FLOAT)
8820 {
8821 rettv->v_type = VAR_FLOAT;
8822 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8823 }
8824 else
8825 {
8826 varnumber_T n;
8827 int error = FALSE;
8828
8829 n = get_tv_number_chk(&argvars[0], &error);
8830 if (error)
8831 rettv->vval.v_number = -1;
8832 else if (n > 0)
8833 rettv->vval.v_number = n;
8834 else
8835 rettv->vval.v_number = -n;
8836 }
8837}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008838
8839/*
8840 * "acos()" function
8841 */
8842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008843f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008844{
8845 float_T f;
8846
8847 rettv->v_type = VAR_FLOAT;
8848 if (get_float_arg(argvars, &f) == OK)
8849 rettv->vval.v_float = acos(f);
8850 else
8851 rettv->vval.v_float = 0.0;
8852}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008853#endif
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008856 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 */
8858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008859f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008864 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008866 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008867 && !tv_check_lock(l->lv_lock,
8868 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008869 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 }
8872 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008873 EMSG(_(e_listreq));
8874}
8875
8876/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008877 * "alloc_fail(id, countdown, repeat)" function
8878 */
8879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008880f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008881{
8882 if (argvars[0].v_type != VAR_NUMBER
8883 || argvars[0].vval.v_number <= 0
8884 || argvars[1].v_type != VAR_NUMBER
8885 || argvars[1].vval.v_number < 0
8886 || argvars[2].v_type != VAR_NUMBER)
8887 EMSG(_(e_invarg));
8888 else
8889 {
8890 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008891 if (alloc_fail_id >= aid_last)
8892 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008893 alloc_fail_countdown = argvars[1].vval.v_number;
8894 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008895 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008896 }
8897}
8898
8899/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008900 * "and(expr, expr)" function
8901 */
8902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008903f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008904{
8905 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8906 & get_tv_number_chk(&argvars[1], NULL);
8907}
8908
8909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008910 * "append(lnum, string/list)" function
8911 */
8912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008913f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008914{
8915 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 list_T *l = NULL;
8918 listitem_T *li = NULL;
8919 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008920 long added = 0;
8921
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008922 /* When coming here from Insert mode, sync undo, so that this can be
8923 * undone separately from what was previously inserted. */
8924 if (u_sync_once == 2)
8925 {
8926 u_sync_once = 1; /* notify that u_sync() was called */
8927 u_sync(TRUE);
8928 }
8929
Bram Moolenaar0d660222005-01-07 21:51:51 +00008930 lnum = get_tv_lnum(argvars);
8931 if (lnum >= 0
8932 && lnum <= curbuf->b_ml.ml_line_count
8933 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008934 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008935 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008936 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008937 l = argvars[1].vval.v_list;
8938 if (l == NULL)
8939 return;
8940 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008941 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008942 for (;;)
8943 {
8944 if (l == NULL)
8945 tv = &argvars[1]; /* append a string */
8946 else if (li == NULL)
8947 break; /* end of list */
8948 else
8949 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008950 line = get_tv_string_chk(tv);
8951 if (line == NULL) /* type error */
8952 {
8953 rettv->vval.v_number = 1; /* Failed */
8954 break;
8955 }
8956 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008957 ++added;
8958 if (l == NULL)
8959 break;
8960 li = li->li_next;
8961 }
8962
8963 appended_lines_mark(lnum, added);
8964 if (curwin->w_cursor.lnum > lnum)
8965 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 else
8968 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969}
8970
8971/*
8972 * "argc()" function
8973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
8980/*
8981 * "argidx()" function
8982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008984f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008990 * "arglistid()" function
8991 */
8992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008993f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008994{
8995 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008996
8997 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01008998 wp = find_tabwin(&argvars[0], &argvars[1]);
8999 if (wp != NULL)
9000 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009001}
9002
9003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 * "argv(nr)" function
9005 */
9006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009007f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 int idx;
9010
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009011 if (argvars[0].v_type != VAR_UNKNOWN)
9012 {
9013 idx = get_tv_number_chk(&argvars[0], NULL);
9014 if (idx >= 0 && idx < ARGCOUNT)
9015 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9016 else
9017 rettv->vval.v_string = NULL;
9018 rettv->v_type = VAR_STRING;
9019 }
9020 else if (rettv_list_alloc(rettv) == OK)
9021 for (idx = 0; idx < ARGCOUNT; ++idx)
9022 list_append_string(rettv->vval.v_list,
9023 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024}
9025
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009026static void prepare_assert_error(garray_T*gap);
9027static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9028static void assert_error(garray_T *gap);
9029static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009030
9031/*
9032 * Prepare "gap" for an assert error and add the sourcing position.
9033 */
9034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009035prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009036{
9037 char buf[NUMBUFLEN];
9038
9039 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009040 if (sourcing_name != NULL)
9041 {
9042 ga_concat(gap, sourcing_name);
9043 if (sourcing_lnum > 0)
9044 ga_concat(gap, (char_u *)" ");
9045 }
9046 if (sourcing_lnum > 0)
9047 {
9048 sprintf(buf, "line %ld", (long)sourcing_lnum);
9049 ga_concat(gap, (char_u *)buf);
9050 }
9051 if (sourcing_name != NULL || sourcing_lnum > 0)
9052 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009053}
9054
9055/*
9056 * Fill "gap" with information about an assert error.
9057 */
9058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009059fill_assert_error(
9060 garray_T *gap,
9061 typval_T *opt_msg_tv,
9062 char_u *exp_str,
9063 typval_T *exp_tv,
9064 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009065{
9066 char_u numbuf[NUMBUFLEN];
9067 char_u *tofree;
9068
9069 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9070 {
9071 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9072 vim_free(tofree);
9073 }
9074 else
9075 {
9076 ga_concat(gap, (char_u *)"Expected ");
9077 if (exp_str == NULL)
9078 {
9079 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9080 vim_free(tofree);
9081 }
9082 else
9083 ga_concat(gap, exp_str);
9084 ga_concat(gap, (char_u *)" but got ");
9085 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9086 vim_free(tofree);
9087 }
9088}
Bram Moolenaar43345542015-11-29 17:35:35 +01009089
9090/*
9091 * Add an assert error to v:errors.
9092 */
9093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009094assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009095{
9096 struct vimvar *vp = &vimvars[VV_ERRORS];
9097
9098 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9099 /* Make sure v:errors is a list. */
9100 set_vim_var_list(VV_ERRORS, list_alloc());
9101 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9102}
9103
Bram Moolenaar43345542015-11-29 17:35:35 +01009104/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009105 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009106 */
9107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009108f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009109{
9110 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009111
9112 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9113 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009114 prepare_assert_error(&ga);
9115 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9116 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009117 ga_clear(&ga);
9118 }
9119}
9120
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009121/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009122 * "assert_exception(string[, msg])" function
9123 */
9124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009125f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009126{
9127 garray_T ga;
9128 char *error;
9129
9130 error = (char *)get_tv_string_chk(&argvars[0]);
9131 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9132 {
9133 prepare_assert_error(&ga);
9134 ga_concat(&ga, (char_u *)"v:exception is not set");
9135 assert_error(&ga);
9136 ga_clear(&ga);
9137 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009138 else if (error != NULL
9139 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009140 {
9141 prepare_assert_error(&ga);
9142 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9143 &vimvars[VV_EXCEPTION].vv_tv);
9144 assert_error(&ga);
9145 ga_clear(&ga);
9146 }
9147}
9148
9149/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009150 * "assert_fails(cmd [, error])" function
9151 */
9152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009153f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009154{
9155 char_u *cmd = get_tv_string_chk(&argvars[0]);
9156 garray_T ga;
9157
9158 called_emsg = FALSE;
9159 suppress_errthrow = TRUE;
9160 emsg_silent = TRUE;
9161 do_cmdline_cmd(cmd);
9162 if (!called_emsg)
9163 {
9164 prepare_assert_error(&ga);
9165 ga_concat(&ga, (char_u *)"command did not fail: ");
9166 ga_concat(&ga, cmd);
9167 assert_error(&ga);
9168 ga_clear(&ga);
9169 }
9170 else if (argvars[1].v_type != VAR_UNKNOWN)
9171 {
9172 char_u buf[NUMBUFLEN];
9173 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9174
9175 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9176 {
9177 prepare_assert_error(&ga);
9178 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9179 &vimvars[VV_ERRMSG].vv_tv);
9180 assert_error(&ga);
9181 ga_clear(&ga);
9182 }
9183 }
9184
9185 called_emsg = FALSE;
9186 suppress_errthrow = FALSE;
9187 emsg_silent = FALSE;
9188 emsg_on_display = FALSE;
9189 set_vim_var_string(VV_ERRMSG, NULL, 0);
9190}
9191
9192/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009193 * Common for assert_true() and assert_false().
9194 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009197{
9198 int error = FALSE;
9199 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009200
9201 if (argvars[0].v_type != VAR_NUMBER
9202 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9203 || error)
9204 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009205 prepare_assert_error(&ga);
9206 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009207 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009208 NULL, &argvars[0]);
9209 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009210 ga_clear(&ga);
9211 }
9212}
9213
9214/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009215 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009216 */
9217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009218f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009219{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009220 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009221}
9222
9223/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009224 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009225 */
9226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009227f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009228{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009229 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009230}
9231
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009232#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009233/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009234 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009235 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009237f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009238{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009239 float_T f;
9240
9241 rettv->v_type = VAR_FLOAT;
9242 if (get_float_arg(argvars, &f) == OK)
9243 rettv->vval.v_float = asin(f);
9244 else
9245 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009246}
9247
9248/*
9249 * "atan()" function
9250 */
9251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009252f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009253{
9254 float_T f;
9255
9256 rettv->v_type = VAR_FLOAT;
9257 if (get_float_arg(argvars, &f) == OK)
9258 rettv->vval.v_float = atan(f);
9259 else
9260 rettv->vval.v_float = 0.0;
9261}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009262
9263/*
9264 * "atan2()" function
9265 */
9266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009267f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009268{
9269 float_T fx, fy;
9270
9271 rettv->v_type = VAR_FLOAT;
9272 if (get_float_arg(argvars, &fx) == OK
9273 && get_float_arg(&argvars[1], &fy) == OK)
9274 rettv->vval.v_float = atan2(fx, fy);
9275 else
9276 rettv->vval.v_float = 0.0;
9277}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009278#endif
9279
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280/*
9281 * "browse(save, title, initdir, default)" function
9282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009284f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286#ifdef FEAT_BROWSE
9287 int save;
9288 char_u *title;
9289 char_u *initdir;
9290 char_u *defname;
9291 char_u buf[NUMBUFLEN];
9292 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 save = get_tv_number_chk(&argvars[0], &error);
9296 title = get_tv_string_chk(&argvars[1]);
9297 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9298 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 if (error || title == NULL || initdir == NULL || defname == NULL)
9301 rettv->vval.v_string = NULL;
9302 else
9303 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009304 do_browse(save ? BROWSE_SAVE : 0,
9305 title, defname, NULL, initdir, NULL, curbuf);
9306#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009308#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009310}
9311
9312/*
9313 * "browsedir(title, initdir)" function
9314 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009316f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009317{
9318#ifdef FEAT_BROWSE
9319 char_u *title;
9320 char_u *initdir;
9321 char_u buf[NUMBUFLEN];
9322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 title = get_tv_string_chk(&argvars[0]);
9324 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 if (title == NULL || initdir == NULL)
9327 rettv->vval.v_string = NULL;
9328 else
9329 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009330 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335}
9336
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009337static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009338
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339/*
9340 * Find a buffer by number or exact name.
9341 */
9342 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009343find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
9345 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009347 if (avar->v_type == VAR_NUMBER)
9348 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009349 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009351 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009352 if (buf == NULL)
9353 {
9354 /* No full path name match, try a match with a URL or a "nofile"
9355 * buffer, these don't use the full path. */
9356 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9357 if (buf->b_fname != NULL
9358 && (path_with_url(buf->b_fname)
9359#ifdef FEAT_QUICKFIX
9360 || bt_nofile(buf)
9361#endif
9362 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009363 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009364 break;
9365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 }
9367 return buf;
9368}
9369
9370/*
9371 * "bufexists(expr)" function
9372 */
9373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009374f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377}
9378
9379/*
9380 * "buflisted(expr)" function
9381 */
9382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009383f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 buf_T *buf;
9386
9387 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
9392 * "bufloaded(expr)" function
9393 */
9394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397 buf_T *buf;
9398
9399 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009403static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405/*
9406 * Get buffer by number or pattern.
9407 */
9408 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009409get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 int save_magic;
9413 char_u *save_cpo;
9414 buf_T *buf;
9415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 if (tv->v_type == VAR_NUMBER)
9417 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009418 if (tv->v_type != VAR_STRING)
9419 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 if (name == NULL || *name == NUL)
9421 return curbuf;
9422 if (name[0] == '$' && name[1] == NUL)
9423 return lastbuf;
9424
9425 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9426 save_magic = p_magic;
9427 p_magic = TRUE;
9428 save_cpo = p_cpo;
9429 p_cpo = (char_u *)"";
9430
9431 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009432 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433
9434 p_magic = save_magic;
9435 p_cpo = save_cpo;
9436
9437 /* If not found, try expanding the name, like done for bufexists(). */
9438 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440
9441 return buf;
9442}
9443
9444/*
9445 * "bufname(expr)" function
9446 */
9447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009448f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 buf_T *buf;
9451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009454 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 --emsg_off;
9461}
9462
9463/*
9464 * "bufnr(expr)" function
9465 */
9466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009470 int error = FALSE;
9471 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009475 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009476 --emsg_off;
9477
9478 /* If the buffer isn't found and the second argument is not zero create a
9479 * new buffer. */
9480 if (buf == NULL
9481 && argvars[1].v_type != VAR_UNKNOWN
9482 && get_tv_number_chk(&argvars[1], &error) != 0
9483 && !error
9484 && (name = get_tv_string_chk(&argvars[0])) != NULL
9485 && !error)
9486 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9487
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492}
9493
9494/*
9495 * "bufwinnr(nr)" function
9496 */
9497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009498f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
9500#ifdef FEAT_WINDOWS
9501 win_T *wp;
9502 int winnr = 0;
9503#endif
9504 buf_T *buf;
9505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009508 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509#ifdef FEAT_WINDOWS
9510 for (wp = firstwin; wp; wp = wp->w_next)
9511 {
9512 ++winnr;
9513 if (wp->w_buffer == buf)
9514 break;
9515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519#endif
9520 --emsg_off;
9521}
9522
9523/*
9524 * "byte2line(byte)" function
9525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009527f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531#else
9532 long boff = 0;
9533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 (linenr_T)0, &boff);
9540#endif
9541}
9542
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009545{
9546#ifdef FEAT_MBYTE
9547 char_u *t;
9548#endif
9549 char_u *str;
9550 long idx;
9551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 str = get_tv_string_chk(&argvars[0]);
9553 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009556 return;
9557
9558#ifdef FEAT_MBYTE
9559 t = str;
9560 for ( ; idx > 0; idx--)
9561 {
9562 if (*t == NUL) /* EOL reached */
9563 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009564 if (enc_utf8 && comp)
9565 t += utf_ptr2len(t);
9566 else
9567 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009568 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009569 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009570#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009571 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009573#endif
9574}
9575
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009576/*
9577 * "byteidx()" function
9578 */
9579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009581{
9582 byteidx(argvars, rettv, FALSE);
9583}
9584
9585/*
9586 * "byteidxcomp()" function
9587 */
9588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009589f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009590{
9591 byteidx(argvars, rettv, TRUE);
9592}
9593
Bram Moolenaardb913952012-06-29 12:54:53 +02009594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009595func_call(
9596 char_u *name,
9597 typval_T *args,
9598 dict_T *selfdict,
9599 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009600{
9601 listitem_T *item;
9602 typval_T argv[MAX_FUNC_ARGS + 1];
9603 int argc = 0;
9604 int dummy;
9605 int r = 0;
9606
9607 for (item = args->vval.v_list->lv_first; item != NULL;
9608 item = item->li_next)
9609 {
9610 if (argc == MAX_FUNC_ARGS)
9611 {
9612 EMSG(_("E699: Too many arguments"));
9613 break;
9614 }
9615 /* Make a copy of each argument. This is needed to be able to set
9616 * v_lock to VAR_FIXED in the copy without changing the original list.
9617 */
9618 copy_tv(&item->li_tv, &argv[argc++]);
9619 }
9620
9621 if (item == NULL)
9622 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9624 &dummy, TRUE, selfdict);
9625
9626 /* Free the arguments. */
9627 while (argc > 0)
9628 clear_tv(&argv[--argc]);
9629
9630 return r;
9631}
9632
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009633/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009634 * "call(func, arglist)" function
9635 */
9636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009637f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009638{
9639 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009641
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642 if (argvars[1].v_type != VAR_LIST)
9643 {
9644 EMSG(_(e_listreq));
9645 return;
9646 }
9647 if (argvars[1].vval.v_list == NULL)
9648 return;
9649
9650 if (argvars[0].v_type == VAR_FUNC)
9651 func = argvars[0].vval.v_string;
9652 else
9653 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009654 if (*func == NUL)
9655 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009656
Bram Moolenaare9a41262005-01-15 22:18:47 +00009657 if (argvars[2].v_type != VAR_UNKNOWN)
9658 {
9659 if (argvars[2].v_type != VAR_DICT)
9660 {
9661 EMSG(_(e_dictreq));
9662 return;
9663 }
9664 selfdict = argvars[2].vval.v_dict;
9665 }
9666
Bram Moolenaardb913952012-06-29 12:54:53 +02009667 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009668}
9669
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009670#ifdef FEAT_FLOAT
9671/*
9672 * "ceil({float})" function
9673 */
9674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009675f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009676{
9677 float_T f;
9678
9679 rettv->v_type = VAR_FLOAT;
9680 if (get_float_arg(argvars, &f) == OK)
9681 rettv->vval.v_float = ceil(f);
9682 else
9683 rettv->vval.v_float = 0.0;
9684}
9685#endif
9686
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009687/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009688 * "changenr()" function
9689 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009691f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009692{
9693 rettv->vval.v_number = curbuf->b_u_seq_cur;
9694}
9695
9696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 * "char2nr(string)" function
9698 */
9699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009700f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702#ifdef FEAT_MBYTE
9703 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009704 {
9705 int utf8 = 0;
9706
9707 if (argvars[1].v_type != VAR_UNKNOWN)
9708 utf8 = get_tv_number_chk(&argvars[1], NULL);
9709
9710 if (utf8)
9711 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9712 else
9713 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
9716#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720/*
9721 * "cindent(lnum)" function
9722 */
9723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009724f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726#ifdef FEAT_CINDENT
9727 pos_T pos;
9728 linenr_T lnum;
9729
9730 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9733 {
9734 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 curwin->w_cursor = pos;
9737 }
9738 else
9739#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741}
9742
9743/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009744 * "clearmatches()" function
9745 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009747f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009748{
9749#ifdef FEAT_SEARCH_EXTRA
9750 clear_matches(curwin);
9751#endif
9752}
9753
9754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 * "col(string)" function
9756 */
9757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009758f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760 colnr_T col = 0;
9761 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009762 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009764 fp = var2fpos(&argvars[0], FALSE, &fnum);
9765 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
9767 if (fp->col == MAXCOL)
9768 {
9769 /* '> can be MAXCOL, get the length of the line then */
9770 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009771 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 else
9773 col = MAXCOL;
9774 }
9775 else
9776 {
9777 col = fp->col + 1;
9778#ifdef FEAT_VIRTUALEDIT
9779 /* col(".") when the cursor is on the NUL at the end of the line
9780 * because of "coladd" can be seen as an extra column. */
9781 if (virtual_active() && fp == &curwin->w_cursor)
9782 {
9783 char_u *p = ml_get_cursor();
9784
9785 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9786 curwin->w_virtcol - curwin->w_cursor.coladd))
9787 {
9788# ifdef FEAT_MBYTE
9789 int l;
9790
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009791 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 col += l;
9793# else
9794 if (*p != NUL && p[1] == NUL)
9795 ++col;
9796# endif
9797 }
9798 }
9799#endif
9800 }
9801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
Bram Moolenaar572cb562005-08-05 21:35:02 +00009805#if defined(FEAT_INS_EXPAND)
9806/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009807 * "complete()" function
9808 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009810f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +00009811{
9812 int startcol;
9813
9814 if ((State & INSERT) == 0)
9815 {
9816 EMSG(_("E785: complete() can only be used in Insert mode"));
9817 return;
9818 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009819
9820 /* Check for undo allowed here, because if something was already inserted
9821 * the line was already saved for undo and this check isn't done. */
9822 if (!undo_allowed())
9823 return;
9824
Bram Moolenaarade00832006-03-10 21:46:58 +00009825 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9826 {
9827 EMSG(_(e_invarg));
9828 return;
9829 }
9830
9831 startcol = get_tv_number_chk(&argvars[0], NULL);
9832 if (startcol <= 0)
9833 return;
9834
9835 set_completion(startcol - 1, argvars[1].vval.v_list);
9836}
9837
9838/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009839 * "complete_add()" function
9840 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009842f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009843{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009844 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009845}
9846
9847/*
9848 * "complete_check()" function
9849 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009852{
9853 int saved = RedrawingDisabled;
9854
9855 RedrawingDisabled = 0;
9856 ins_compl_check_keys(0);
9857 rettv->vval.v_number = compl_interrupted;
9858 RedrawingDisabled = saved;
9859}
9860#endif
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862/*
9863 * "confirm(message, buttons[, default [, type]])" function
9864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009866f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
9868#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9869 char_u *message;
9870 char_u *buttons = NULL;
9871 char_u buf[NUMBUFLEN];
9872 char_u buf2[NUMBUFLEN];
9873 int def = 1;
9874 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009875 char_u *typestr;
9876 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 message = get_tv_string_chk(&argvars[0]);
9879 if (message == NULL)
9880 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009881 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009883 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9884 if (buttons == NULL)
9885 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009886 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009889 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009891 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9892 if (typestr == NULL)
9893 error = TRUE;
9894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009896 switch (TOUPPER_ASC(*typestr))
9897 {
9898 case 'E': type = VIM_ERROR; break;
9899 case 'Q': type = VIM_QUESTION; break;
9900 case 'I': type = VIM_INFO; break;
9901 case 'W': type = VIM_WARNING; break;
9902 case 'G': type = VIM_GENERIC; break;
9903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 }
9905 }
9906 }
9907 }
9908
9909 if (buttons == NULL || *buttons == NUL)
9910 buttons = (char_u *)_("&Ok");
9911
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009912 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009913 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009914 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915#endif
9916}
9917
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009918/*
9919 * "copy()" function
9920 */
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009923{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009924 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009927#ifdef FEAT_FLOAT
9928/*
9929 * "cos()" function
9930 */
9931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009932f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009933{
9934 float_T f;
9935
9936 rettv->v_type = VAR_FLOAT;
9937 if (get_float_arg(argvars, &f) == OK)
9938 rettv->vval.v_float = cos(f);
9939 else
9940 rettv->vval.v_float = 0.0;
9941}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009942
9943/*
9944 * "cosh()" function
9945 */
9946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009948{
9949 float_T f;
9950
9951 rettv->v_type = VAR_FLOAT;
9952 if (get_float_arg(argvars, &f) == OK)
9953 rettv->vval.v_float = cosh(f);
9954 else
9955 rettv->vval.v_float = 0.0;
9956}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009957#endif
9958
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009960 * "count()" function
9961 */
9962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009963f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009964{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009965 long n = 0;
9966 int ic = FALSE;
9967
Bram Moolenaare9a41262005-01-15 22:18:47 +00009968 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009969 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009970 listitem_T *li;
9971 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009972 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009973
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 if ((l = argvars[0].vval.v_list) != NULL)
9975 {
9976 li = l->lv_first;
9977 if (argvars[2].v_type != VAR_UNKNOWN)
9978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009979 int error = FALSE;
9980
9981 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009982 if (argvars[3].v_type != VAR_UNKNOWN)
9983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 idx = get_tv_number_chk(&argvars[3], &error);
9985 if (!error)
9986 {
9987 li = list_find(l, idx);
9988 if (li == NULL)
9989 EMSGN(_(e_listidx), idx);
9990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 if (error)
9993 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009994 }
9995
9996 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009997 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009998 ++n;
9999 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010000 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 else if (argvars[0].v_type == VAR_DICT)
10002 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 int todo;
10004 dict_T *d;
10005 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010006
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010007 if ((d = argvars[0].vval.v_dict) != NULL)
10008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009 int error = FALSE;
10010
Bram Moolenaare9a41262005-01-15 22:18:47 +000010011 if (argvars[2].v_type != VAR_UNKNOWN)
10012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010014 if (argvars[3].v_type != VAR_UNKNOWN)
10015 EMSG(_(e_invarg));
10016 }
10017
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010018 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010019 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010020 {
10021 if (!HASHITEM_EMPTY(hi))
10022 {
10023 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010024 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010025 ++n;
10026 }
10027 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010028 }
10029 }
10030 else
10031 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010032 rettv->vval.v_number = n;
10033}
10034
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010035#ifdef FEAT_CHANNEL
10036/*
10037 * Get a callback from "arg". It can be a Funcref or a function name.
10038 * When "arg" is zero return an empty string.
10039 * Return NULL for an invalid argument.
10040 */
10041 static char_u *
10042get_callback(typval_T *arg)
10043{
10044 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
10045 return arg->vval.v_string;
10046 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
10047 return (char_u *)"";
10048 EMSG(_("E999: Invalid callback argument"));
10049 return NULL;
10050}
10051
10052/*
10053 * "connect()" function
10054 */
10055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010056f_connect(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010057{
10058 char_u *address;
10059 char_u *mode;
10060 char_u *callback = NULL;
10061 char_u buf1[NUMBUFLEN];
10062 char_u *p;
10063 int port;
10064 int json_mode = FALSE;
10065
10066 address = get_tv_string(&argvars[0]);
10067 mode = get_tv_string_buf(&argvars[1], buf1);
10068 if (argvars[2].v_type != VAR_UNKNOWN)
10069 {
10070 callback = get_callback(&argvars[2]);
10071 if (callback == NULL)
10072 return;
10073 }
10074
10075 /* parse address */
10076 p = vim_strchr(address, ':');
10077 if (p == NULL)
10078 {
10079 EMSG2(_(e_invarg2), address);
10080 return;
10081 }
10082 *p++ = NUL;
10083 port = atoi((char *)p);
10084 if (*address == NUL || port <= 0)
10085 {
10086 p[-1] = ':';
10087 EMSG2(_(e_invarg2), address);
10088 return;
10089 }
10090
10091 /* parse mode */
10092 if (STRCMP(mode, "json") == 0)
10093 json_mode = TRUE;
10094 else if (STRCMP(mode, "raw") != 0)
10095 {
10096 EMSG2(_(e_invarg2), mode);
10097 return;
10098 }
10099
10100 rettv->vval.v_number = channel_open((char *)address, port, NULL);
10101 if (rettv->vval.v_number >= 0)
10102 {
10103 channel_set_json_mode(rettv->vval.v_number, json_mode);
10104 if (callback != NULL && *callback != NUL)
10105 channel_set_callback(rettv->vval.v_number, callback);
10106 }
10107}
10108#endif
10109
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10112 *
10113 * Checks the existence of a cscope connection.
10114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010116f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118#ifdef FEAT_CSCOPE
10119 int num = 0;
10120 char_u *dbpath = NULL;
10121 char_u *prepend = NULL;
10122 char_u buf[NUMBUFLEN];
10123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010124 if (argvars[0].v_type != VAR_UNKNOWN
10125 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 num = (int)get_tv_number(&argvars[0]);
10128 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010129 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 }
10132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134#endif
10135}
10136
10137/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010138 * "cursor(lnum, col)" function, or
10139 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010141 * Moves the cursor to the specified line and column.
10142 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010145f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
10147 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010148#ifdef FEAT_VIRTUALEDIT
10149 long coladd = 0;
10150#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010151 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010153 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010154 if (argvars[1].v_type == VAR_UNKNOWN)
10155 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010156 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010157 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010158
Bram Moolenaar493c1782014-05-28 14:34:46 +020010159 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010160 {
10161 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010162 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010163 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010164 line = pos.lnum;
10165 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010166#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010167 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010168#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010169 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010170 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010171 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010172 set_curswant = FALSE;
10173 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010174 }
10175 else
10176 {
10177 line = get_tv_lnum(argvars);
10178 col = get_tv_number_chk(&argvars[1], NULL);
10179#ifdef FEAT_VIRTUALEDIT
10180 if (argvars[2].v_type != VAR_UNKNOWN)
10181 coladd = get_tv_number_chk(&argvars[2], NULL);
10182#endif
10183 }
10184 if (line < 0 || col < 0
10185#ifdef FEAT_VIRTUALEDIT
10186 || coladd < 0
10187#endif
10188 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 if (line > 0)
10191 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 if (col > 0)
10193 curwin->w_cursor.col = col - 1;
10194#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010195 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196#endif
10197
10198 /* Make sure the cursor is in a valid position. */
10199 check_cursor();
10200#ifdef FEAT_MBYTE
10201 /* Correct cursor for multi-byte character. */
10202 if (has_mbyte)
10203 mb_adjust_cursor();
10204#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010205
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010206 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010207 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208}
10209
10210/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010211 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 */
10213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010214f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010216 int noref = 0;
10217
10218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010220 if (noref < 0 || noref > 1)
10221 EMSG(_(e_invarg));
10222 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010223 {
10224 current_copyID += COPYID_INC;
10225 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227}
10228
10229/*
10230 * "delete()" function
10231 */
10232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010233f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010235 char_u nbuf[NUMBUFLEN];
10236 char_u *name;
10237 char_u *flags;
10238
10239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010241 return;
10242
10243 name = get_tv_string(&argvars[0]);
10244 if (name == NULL || *name == NUL)
10245 {
10246 EMSG(_(e_invarg));
10247 return;
10248 }
10249
10250 if (argvars[1].v_type != VAR_UNKNOWN)
10251 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010253 flags = (char_u *)"";
10254
10255 if (*flags == NUL)
10256 /* delete a file */
10257 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10258 else if (STRCMP(flags, "d") == 0)
10259 /* delete an empty directory */
10260 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10261 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010262 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010263 rettv->vval.v_number = delete_recursive(name);
10264 else
10265 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266}
10267
10268/*
10269 * "did_filetype()" function
10270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010272f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273{
10274#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276#endif
10277}
10278
10279/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010280 * "diff_filler()" function
10281 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010283f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010284{
10285#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010287#endif
10288}
10289
10290/*
10291 * "diff_hlID()" function
10292 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010294f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010295{
10296#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010297 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010298 static linenr_T prev_lnum = 0;
10299 static int changedtick = 0;
10300 static int fnum = 0;
10301 static int change_start = 0;
10302 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010303 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010304 int filler_lines;
10305 int col;
10306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 if (lnum < 0) /* ignore type error in {lnum} arg */
10308 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010309 if (lnum != prev_lnum
10310 || changedtick != curbuf->b_changedtick
10311 || fnum != curbuf->b_fnum)
10312 {
10313 /* New line, buffer, change: need to get the values. */
10314 filler_lines = diff_check(curwin, lnum);
10315 if (filler_lines < 0)
10316 {
10317 if (filler_lines == -1)
10318 {
10319 change_start = MAXCOL;
10320 change_end = -1;
10321 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10322 hlID = HLF_ADD; /* added line */
10323 else
10324 hlID = HLF_CHD; /* changed line */
10325 }
10326 else
10327 hlID = HLF_ADD; /* added line */
10328 }
10329 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010330 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010331 prev_lnum = lnum;
10332 changedtick = curbuf->b_changedtick;
10333 fnum = curbuf->b_fnum;
10334 }
10335
10336 if (hlID == HLF_CHD || hlID == HLF_TXD)
10337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010338 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010339 if (col >= change_start && col <= change_end)
10340 hlID = HLF_TXD; /* changed text */
10341 else
10342 hlID = HLF_CHD; /* changed line */
10343 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010344 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010345#endif
10346}
10347
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010348#ifdef FEAT_CHANNEL
10349/*
10350 * Get the channel index from the handle argument.
10351 * Returns -1 if the handle is invalid or the channel is closed.
10352 */
10353 static int
10354get_channel_arg(typval_T *tv)
10355{
10356 int ch_idx;
10357
10358 if (tv->v_type != VAR_NUMBER)
10359 {
10360 EMSG2(_(e_invarg2), get_tv_string(tv));
10361 return -1;
10362 }
10363 ch_idx = tv->vval.v_number;
10364
10365 if (!channel_is_open(ch_idx))
10366 {
10367 EMSGN(_("E999: not an open channel"), ch_idx);
10368 return -1;
10369 }
10370 return ch_idx;
10371}
10372
10373/*
10374 * "disconnect()" function
10375 */
10376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010377f_disconnect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010378{
10379 int ch_idx = get_channel_arg(&argvars[0]);
10380
10381 if (ch_idx >= 0)
10382 channel_close(ch_idx);
10383}
10384#endif
10385
Bram Moolenaar47136d72004-10-12 20:02:24 +000010386/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010387 * "empty({expr})" function
10388 */
10389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010390f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010391{
10392 int n;
10393
10394 switch (argvars[0].v_type)
10395 {
10396 case VAR_STRING:
10397 case VAR_FUNC:
10398 n = argvars[0].vval.v_string == NULL
10399 || *argvars[0].vval.v_string == NUL;
10400 break;
10401 case VAR_NUMBER:
10402 n = argvars[0].vval.v_number == 0;
10403 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010404#ifdef FEAT_FLOAT
10405 case VAR_FLOAT:
10406 n = argvars[0].vval.v_float == 0.0;
10407 break;
10408#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010409 case VAR_LIST:
10410 n = argvars[0].vval.v_list == NULL
10411 || argvars[0].vval.v_list->lv_first == NULL;
10412 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010413 case VAR_DICT:
10414 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010416 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010417 case VAR_SPECIAL:
10418 n = argvars[0].vval.v_number != VVAL_TRUE;
10419 break;
10420
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010421 default:
10422 EMSG2(_(e_intern2), "f_empty()");
10423 n = 0;
10424 }
10425
10426 rettv->vval.v_number = n;
10427}
10428
10429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 * "escape({string}, {chars})" function
10431 */
10432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010433f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434{
10435 char_u buf[NUMBUFLEN];
10436
Bram Moolenaar758711c2005-02-02 23:11:38 +000010437 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10438 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440}
10441
10442/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010443 * "eval()" function
10444 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010446f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010447{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010448 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 s = get_tv_string_chk(&argvars[0]);
10451 if (s != NULL)
10452 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010453
Bram Moolenaar615b9972015-01-14 17:15:05 +010010454 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10456 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010457 if (p != NULL && !aborting())
10458 EMSG2(_(e_invexpr2), p);
10459 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010461 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010463 else if (*s != NUL)
10464 EMSG(_(e_trailing));
10465}
10466
10467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 * "eventhandler()" function
10469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010471f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474}
10475
10476/*
10477 * "executable()" function
10478 */
10479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010480f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010482 char_u *name = get_tv_string(&argvars[0]);
10483
10484 /* Check in $PATH and also check directly if there is a directory name. */
10485 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10486 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010487}
10488
10489/*
10490 * "exepath()" function
10491 */
10492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010493f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010494{
10495 char_u *p = NULL;
10496
Bram Moolenaarb5971142015-03-21 17:32:19 +010010497 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010498 rettv->v_type = VAR_STRING;
10499 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500}
10501
10502/*
10503 * "exists()" function
10504 */
10505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010506f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507{
10508 char_u *p;
10509 char_u *name;
10510 int n = FALSE;
10511 int len = 0;
10512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 if (*p == '$') /* environment variable */
10515 {
10516 /* first try "normal" environment variables (fast) */
10517 if (mch_getenv(p + 1) != NULL)
10518 n = TRUE;
10519 else
10520 {
10521 /* try expanding things like $VIM and ${HOME} */
10522 p = expand_env_save(p);
10523 if (p != NULL && *p != '$')
10524 n = TRUE;
10525 vim_free(p);
10526 }
10527 }
10528 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010531 if (*skipwhite(p) != NUL)
10532 n = FALSE; /* trailing garbage */
10533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 else if (*p == '*') /* internal or user defined function */
10535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010536 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 }
10538 else if (*p == ':')
10539 {
10540 n = cmd_exists(p + 1);
10541 }
10542 else if (*p == '#')
10543 {
10544#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010545 if (p[1] == '#')
10546 n = autocmd_supported(p + 2);
10547 else
10548 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549#endif
10550 }
10551 else /* internal variable */
10552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010553 char_u *tofree;
10554 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010556 /* get_name_len() takes care of expanding curly braces */
10557 name = p;
10558 len = get_name_len(&p, &tofree, TRUE, FALSE);
10559 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010561 if (tofree != NULL)
10562 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010563 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010564 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010566 /* handle d.key, l[idx], f(expr) */
10567 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10568 if (n)
10569 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010572 if (*p != NUL)
10573 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010575 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 }
10577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579}
10580
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010581#ifdef FEAT_FLOAT
10582/*
10583 * "exp()" function
10584 */
10585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010586f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010587{
10588 float_T f;
10589
10590 rettv->v_type = VAR_FLOAT;
10591 if (get_float_arg(argvars, &f) == OK)
10592 rettv->vval.v_float = exp(f);
10593 else
10594 rettv->vval.v_float = 0.0;
10595}
10596#endif
10597
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598/*
10599 * "expand()" function
10600 */
10601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010602f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 char_u *s;
10605 int len;
10606 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010607 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010610 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010613 if (argvars[1].v_type != VAR_UNKNOWN
10614 && argvars[2].v_type != VAR_UNKNOWN
10615 && get_tv_number_chk(&argvars[2], &error)
10616 && !error)
10617 {
10618 rettv->v_type = VAR_LIST;
10619 rettv->vval.v_list = NULL;
10620 }
10621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 if (*s == '%' || *s == '#' || *s == '<')
10624 {
10625 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010626 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010628 if (rettv->v_type == VAR_LIST)
10629 {
10630 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10631 list_append_string(rettv->vval.v_list, result, -1);
10632 else
10633 vim_free(result);
10634 }
10635 else
10636 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 }
10638 else
10639 {
10640 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010641 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 if (argvars[1].v_type != VAR_UNKNOWN
10643 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010644 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 if (!error)
10646 {
10647 ExpandInit(&xpc);
10648 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010649 if (p_wic)
10650 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010651 if (rettv->v_type == VAR_STRING)
10652 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10653 options, WILD_ALL);
10654 else if (rettv_list_alloc(rettv) != FAIL)
10655 {
10656 int i;
10657
10658 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10659 for (i = 0; i < xpc.xp_numfiles; i++)
10660 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10661 ExpandCleanup(&xpc);
10662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663 }
10664 else
10665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 }
10667}
10668
10669/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010670 * Go over all entries in "d2" and add them to "d1".
10671 * When "action" is "error" then a duplicate key is an error.
10672 * When "action" is "force" then a duplicate key is overwritten.
10673 * Otherwise duplicate keys are ignored ("action" is "keep").
10674 */
10675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010676dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010677{
10678 dictitem_T *di1;
10679 hashitem_T *hi2;
10680 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010681 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010682
10683 todo = (int)d2->dv_hashtab.ht_used;
10684 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10685 {
10686 if (!HASHITEM_EMPTY(hi2))
10687 {
10688 --todo;
10689 di1 = dict_find(d1, hi2->hi_key, -1);
10690 if (d1->dv_scope != 0)
10691 {
10692 /* Disallow replacing a builtin function in l: and g:.
10693 * Check the key to be valid when adding to any
10694 * scope. */
10695 if (d1->dv_scope == VAR_DEF_SCOPE
10696 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10697 && var_check_func_name(hi2->hi_key,
10698 di1 == NULL))
10699 break;
10700 if (!valid_varname(hi2->hi_key))
10701 break;
10702 }
10703 if (di1 == NULL)
10704 {
10705 di1 = dictitem_copy(HI2DI(hi2));
10706 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10707 dictitem_free(di1);
10708 }
10709 else if (*action == 'e')
10710 {
10711 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10712 break;
10713 }
10714 else if (*action == 'f' && HI2DI(hi2) != di1)
10715 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010716 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10717 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010718 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010719 clear_tv(&di1->di_tv);
10720 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10721 }
10722 }
10723 }
10724}
10725
10726/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010727 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010728 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010729 */
10730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010731f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010732{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010733 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010734
Bram Moolenaare9a41262005-01-15 22:18:47 +000010735 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010736 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 list_T *l1, *l2;
10738 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010739 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010741
Bram Moolenaare9a41262005-01-15 22:18:47 +000010742 l1 = argvars[0].vval.v_list;
10743 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010744 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010745 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 {
10747 if (argvars[2].v_type != VAR_UNKNOWN)
10748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 before = get_tv_number_chk(&argvars[2], &error);
10750 if (error)
10751 return; /* type error; errmsg already given */
10752
Bram Moolenaar758711c2005-02-02 23:11:38 +000010753 if (before == l1->lv_len)
10754 item = NULL;
10755 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010757 item = list_find(l1, before);
10758 if (item == NULL)
10759 {
10760 EMSGN(_(e_listidx), before);
10761 return;
10762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010763 }
10764 }
10765 else
10766 item = NULL;
10767 list_extend(l1, l2, item);
10768
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 copy_tv(&argvars[0], rettv);
10770 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10773 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010774 dict_T *d1, *d2;
10775 char_u *action;
10776 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777
10778 d1 = argvars[0].vval.v_dict;
10779 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010780 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010781 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 {
10783 /* Check the third argument. */
10784 if (argvars[2].v_type != VAR_UNKNOWN)
10785 {
10786 static char *(av[]) = {"keep", "force", "error"};
10787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 action = get_tv_string_chk(&argvars[2]);
10789 if (action == NULL)
10790 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010791 for (i = 0; i < 3; ++i)
10792 if (STRCMP(action, av[i]) == 0)
10793 break;
10794 if (i == 3)
10795 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010796 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 return;
10798 }
10799 }
10800 else
10801 action = (char_u *)"force";
10802
Bram Moolenaara9922d62013-05-30 13:01:18 +020010803 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010804
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 copy_tv(&argvars[0], rettv);
10806 }
10807 }
10808 else
10809 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010810}
10811
10812/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010813 * "feedkeys()" function
10814 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010816f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010817{
10818 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010819 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010820 char_u *keys, *flags;
10821 char_u nbuf[NUMBUFLEN];
10822 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010823 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010824 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010825
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010826 /* This is not allowed in the sandbox. If the commands would still be
10827 * executed in the sandbox it would be OK, but it probably happens later,
10828 * when "sandbox" is no longer set. */
10829 if (check_secure())
10830 return;
10831
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010832 keys = get_tv_string(&argvars[0]);
10833 if (*keys != NUL)
10834 {
10835 if (argvars[1].v_type != VAR_UNKNOWN)
10836 {
10837 flags = get_tv_string_buf(&argvars[1], nbuf);
10838 for ( ; *flags != NUL; ++flags)
10839 {
10840 switch (*flags)
10841 {
10842 case 'n': remap = FALSE; break;
10843 case 'm': remap = TRUE; break;
10844 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010845 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010846 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010847 }
10848 }
10849 }
10850
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010851 /* Need to escape K_SPECIAL and CSI before putting the string in the
10852 * typeahead buffer. */
10853 keys_esc = vim_strsave_escape_csi(keys);
10854 if (keys_esc != NULL)
10855 {
10856 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010857 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010858 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010859 if (vgetc_busy)
10860 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010861 if (execute)
10862 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010863 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010864 }
10865}
10866
10867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 * "filereadable()" function
10869 */
10870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010871f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010873 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 char_u *p;
10875 int n;
10876
Bram Moolenaarc236c162008-07-13 17:41:49 +000010877#ifndef O_NONBLOCK
10878# define O_NONBLOCK 0
10879#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010881 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10882 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 {
10884 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010885 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 }
10887 else
10888 n = FALSE;
10889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
10893/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010894 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 * rights to write into.
10896 */
10897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010898f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010900 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901}
10902
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010903static void findfilendir(typval_T *argvars, typval_T *rettv, int find_what);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010904
10905 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010906findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010907 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010908 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010909 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010910{
10911#ifdef FEAT_SEARCHPATH
10912 char_u *fname;
10913 char_u *fresult = NULL;
10914 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10915 char_u *p;
10916 char_u pathbuf[NUMBUFLEN];
10917 int count = 1;
10918 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010919 int error = FALSE;
10920#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010921
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010922 rettv->vval.v_string = NULL;
10923 rettv->v_type = VAR_STRING;
10924
10925#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010927
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010928 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10931 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010932 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 else
10934 {
10935 if (*p != NUL)
10936 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010939 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010941 }
10942
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010943 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10944 error = TRUE;
10945
10946 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 do
10949 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010950 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010951 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 fresult = find_file_in_path_option(first ? fname : NULL,
10953 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010954 0, first, path,
10955 find_what,
10956 curbuf->b_ffname,
10957 find_what == FINDFILE_DIR
10958 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010960
10961 if (fresult != NULL && rettv->v_type == VAR_LIST)
10962 list_append_string(rettv->vval.v_list, fresult, -1);
10963
10964 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010966
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010967 if (rettv->v_type == VAR_STRING)
10968 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010969#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010970}
10971
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010972static void filter_map(typval_T *argvars, typval_T *rettv, int map);
10973static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010974
10975/*
10976 * Implementation of map() and filter().
10977 */
10978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010979filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010980{
10981 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 listitem_T *li, *nli;
10984 list_T *l = NULL;
10985 dictitem_T *di;
10986 hashtab_T *ht;
10987 hashitem_T *hi;
10988 dict_T *d = NULL;
10989 typval_T save_val;
10990 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010991 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010992 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010993 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010994 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010995 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010996 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010997 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010998
Bram Moolenaare9a41262005-01-15 22:18:47 +000010999 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011000 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011001 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011002 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 return;
11004 }
11005 else if (argvars[0].v_type == VAR_DICT)
11006 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011007 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011008 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 return;
11010 }
11011 else
11012 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011013 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 return;
11015 }
11016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 expr = get_tv_string_buf_chk(&argvars[1], buf);
11018 /* On type errors, the preceding call has already displayed an error
11019 * message. Avoid a misleading error message for an empty string that
11020 * was not passed as argument. */
11021 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 prepare_vimvar(VV_VAL, &save_val);
11024 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011025
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011026 /* We reset "did_emsg" to be able to detect whether an error
11027 * occurred during evaluation of the expression. */
11028 save_did_emsg = did_emsg;
11029 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011030
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011031 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 vimvars[VV_KEY].vv_type = VAR_STRING;
11035
11036 ht = &d->dv_hashtab;
11037 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011038 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011039 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 if (!HASHITEM_EMPTY(hi))
11042 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011043 int r;
11044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 --todo;
11046 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011047 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011048 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11049 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 break;
11051 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011052 r = filter_map_one(&di->di_tv, expr, map, &rem);
11053 clear_tv(&vimvars[VV_KEY].vv_tv);
11054 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 break;
11056 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011057 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011058 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11059 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011060 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 }
11064 }
11065 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 }
11067 else
11068 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011069 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 for (li = l->lv_first; li != NULL; li = nli)
11072 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011073 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011074 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011076 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011077 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011078 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011079 break;
11080 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011082 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011083 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011084 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011085
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011086 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011088
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011089 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091
11092 copy_tv(&argvars[0], rettv);
11093}
11094
11095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011096filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011097{
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011100 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103 s = expr;
11104 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011105 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106 if (*s != NUL) /* check for trailing chars after expr */
11107 {
11108 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011109 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011110 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 }
11112 if (map)
11113 {
11114 /* map(): replace the list item value */
11115 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011116 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 *tv = rettv;
11118 }
11119 else
11120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011121 int error = FALSE;
11122
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 /* On type error, nothing has been removed; return FAIL to stop the
11127 * loop. The error message was given by get_tv_number_chk(). */
11128 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011129 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011131 retval = OK;
11132theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011134 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011135}
11136
11137/*
11138 * "filter()" function
11139 */
11140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011141f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011142{
11143 filter_map(argvars, rettv, FALSE);
11144}
11145
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147 * "finddir({fname}[, {path}[, {count}]])" function
11148 */
11149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011150f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011152 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153}
11154
11155/*
11156 * "findfile({fname}[, {path}[, {count}]])" function
11157 */
11158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011159f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011160{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011161 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162}
11163
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011164#ifdef FEAT_FLOAT
11165/*
11166 * "float2nr({float})" function
11167 */
11168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011169f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011170{
11171 float_T f;
11172
11173 if (get_float_arg(argvars, &f) == OK)
11174 {
11175 if (f < -0x7fffffff)
11176 rettv->vval.v_number = -0x7fffffff;
11177 else if (f > 0x7fffffff)
11178 rettv->vval.v_number = 0x7fffffff;
11179 else
11180 rettv->vval.v_number = (varnumber_T)f;
11181 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011182}
11183
11184/*
11185 * "floor({float})" function
11186 */
11187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011188f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011189{
11190 float_T f;
11191
11192 rettv->v_type = VAR_FLOAT;
11193 if (get_float_arg(argvars, &f) == OK)
11194 rettv->vval.v_float = floor(f);
11195 else
11196 rettv->vval.v_float = 0.0;
11197}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011198
11199/*
11200 * "fmod()" function
11201 */
11202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011204{
11205 float_T fx, fy;
11206
11207 rettv->v_type = VAR_FLOAT;
11208 if (get_float_arg(argvars, &fx) == OK
11209 && get_float_arg(&argvars[1], &fy) == OK)
11210 rettv->vval.v_float = fmod(fx, fy);
11211 else
11212 rettv->vval.v_float = 0.0;
11213}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011214#endif
11215
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011217 * "fnameescape({string})" function
11218 */
11219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011220f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011221{
11222 rettv->vval.v_string = vim_strsave_fnameescape(
11223 get_tv_string(&argvars[0]), FALSE);
11224 rettv->v_type = VAR_STRING;
11225}
11226
11227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 * "fnamemodify({fname}, {mods})" function
11229 */
11230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011231f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232{
11233 char_u *fname;
11234 char_u *mods;
11235 int usedlen = 0;
11236 int len;
11237 char_u *fbuf = NULL;
11238 char_u buf[NUMBUFLEN];
11239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 fname = get_tv_string_chk(&argvars[0]);
11241 mods = get_tv_string_buf_chk(&argvars[1], buf);
11242 if (fname == NULL || mods == NULL)
11243 fname = NULL;
11244 else
11245 {
11246 len = (int)STRLEN(fname);
11247 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 vim_free(fbuf);
11256}
11257
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011258static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259
11260/*
11261 * "foldclosed()" function
11262 */
11263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011264foldclosed_both(
11265 typval_T *argvars UNUSED,
11266 typval_T *rettv,
11267 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268{
11269#ifdef FEAT_FOLDING
11270 linenr_T lnum;
11271 linenr_T first, last;
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11275 {
11276 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11277 {
11278 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 return;
11283 }
11284 }
11285#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287}
11288
11289/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290 * "foldclosed()" function
11291 */
11292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011293f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011294{
11295 foldclosed_both(argvars, rettv, FALSE);
11296}
11297
11298/*
11299 * "foldclosedend()" function
11300 */
11301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011303{
11304 foldclosed_both(argvars, rettv, TRUE);
11305}
11306
11307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 * "foldlevel()" function
11309 */
11310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011311f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312{
11313#ifdef FEAT_FOLDING
11314 linenr_T lnum;
11315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320}
11321
11322/*
11323 * "foldtext()" function
11324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011326f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328#ifdef FEAT_FOLDING
11329 linenr_T lnum;
11330 char_u *s;
11331 char_u *r;
11332 int len;
11333 char *txt;
11334#endif
11335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->v_type = VAR_STRING;
11337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011339 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11340 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11341 <= curbuf->b_ml.ml_line_count
11342 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 {
11344 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011345 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11346 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 {
11348 if (!linewhite(lnum))
11349 break;
11350 ++lnum;
11351 }
11352
11353 /* Find interesting text in this line. */
11354 s = skipwhite(ml_get(lnum));
11355 /* skip C comment-start */
11356 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011359 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011360 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011361 {
11362 s = skipwhite(ml_get(lnum + 1));
11363 if (*s == '*')
11364 s = skipwhite(s + 1);
11365 }
11366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 txt = _("+-%s%3ld lines: ");
11368 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011369 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 + 20 /* for %3ld */
11371 + STRLEN(s))); /* concatenated */
11372 if (r != NULL)
11373 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011374 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11375 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11376 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 len = (int)STRLEN(r);
11378 STRCAT(r, s);
11379 /* remove 'foldmarker' and 'commentstring' */
11380 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383 }
11384#endif
11385}
11386
11387/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011388 * "foldtextresult(lnum)" function
11389 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011391f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011392{
11393#ifdef FEAT_FOLDING
11394 linenr_T lnum;
11395 char_u *text;
11396 char_u buf[51];
11397 foldinfo_T foldinfo;
11398 int fold_count;
11399#endif
11400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->v_type = VAR_STRING;
11402 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011403#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 /* treat illegal types and illegal string values for {lnum} the same */
11406 if (lnum < 0)
11407 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011408 fold_count = foldedCount(curwin, lnum, &foldinfo);
11409 if (fold_count > 0)
11410 {
11411 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11412 &foldinfo, buf);
11413 if (text == buf)
11414 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011416 }
11417#endif
11418}
11419
11420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 * "foreground()" function
11422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011424f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426#ifdef FEAT_GUI
11427 if (gui.in_use)
11428 gui_mch_set_foreground();
11429#else
11430# ifdef WIN32
11431 win32_set_foreground();
11432# endif
11433#endif
11434}
11435
11436/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011437 * "function()" function
11438 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011440f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441{
11442 char_u *s;
11443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011445 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011446 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011447 /* Don't check an autoload name for existence here. */
11448 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011449 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011450 else
11451 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011452 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011453 {
11454 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011455 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011456
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011457 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11458 * also be called from another script. Using trans_function_name()
11459 * would also work, but some plugins depend on the name being
11460 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011461 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011462 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011463 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011464 if (rettv->vval.v_string != NULL)
11465 {
11466 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011467 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011468 }
11469 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011470 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011471 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011473 }
11474}
11475
11476/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011477 * "garbagecollect()" function
11478 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011481{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011482 /* This is postponed until we are back at the toplevel, because we may be
11483 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11484 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011485
11486 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11487 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011488}
11489
11490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011491 * "get()" function
11492 */
11493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011494f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495{
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 listitem_T *li;
11497 list_T *l;
11498 dictitem_T *di;
11499 dict_T *d;
11500 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 int error = FALSE;
11507
11508 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11509 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011512 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 else if (argvars[0].v_type == VAR_DICT)
11514 {
11515 if ((d = argvars[0].vval.v_dict) != NULL)
11516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011517 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 if (di != NULL)
11519 tv = &di->di_tv;
11520 }
11521 }
11522 else
11523 EMSG2(_(e_listdictarg), "get()");
11524
11525 if (tv == NULL)
11526 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 copy_tv(&argvars[2], rettv);
11529 }
11530 else
11531 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532}
11533
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011534static 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 +000011535
11536/*
11537 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011538 * Return a range (from start to end) of lines in rettv from the specified
11539 * buffer.
11540 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011541 */
11542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011543get_buffer_lines(
11544 buf_T *buf,
11545 linenr_T start,
11546 linenr_T end,
11547 int retlist,
11548 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011549{
11550 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011551
Bram Moolenaar959a1432013-12-14 12:17:38 +010011552 rettv->v_type = VAR_STRING;
11553 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011554 if (retlist && rettv_list_alloc(rettv) == FAIL)
11555 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011556
11557 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11558 return;
11559
11560 if (!retlist)
11561 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11563 p = ml_get_buf(buf, start, FALSE);
11564 else
11565 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011566 rettv->vval.v_string = vim_strsave(p);
11567 }
11568 else
11569 {
11570 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 return;
11572
11573 if (start < 1)
11574 start = 1;
11575 if (end > buf->b_ml.ml_line_count)
11576 end = buf->b_ml.ml_line_count;
11577 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011578 if (list_append_string(rettv->vval.v_list,
11579 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011581 }
11582}
11583
11584/*
11585 * "getbufline()" function
11586 */
11587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011588f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011589{
11590 linenr_T lnum;
11591 linenr_T end;
11592 buf_T *buf;
11593
11594 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11595 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011596 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 --emsg_off;
11598
Bram Moolenaar661b1822005-07-28 22:36:45 +000011599 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011600 if (argvars[2].v_type == VAR_UNKNOWN)
11601 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011602 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011603 end = get_tv_lnum_buf(&argvars[2], buf);
11604
Bram Moolenaar342337a2005-07-21 21:11:17 +000011605 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011606}
11607
Bram Moolenaar0d660222005-01-07 21:51:51 +000011608/*
11609 * "getbufvar()" function
11610 */
11611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011612f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613{
11614 buf_T *buf;
11615 buf_T *save_curbuf;
11616 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011618 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11621 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011622 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011623 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011625 rettv->v_type = VAR_STRING;
11626 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011627
11628 if (buf != NULL && varname != NULL)
11629 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011630 /* set curbuf to be our buf, temporarily */
11631 save_curbuf = curbuf;
11632 curbuf = buf;
11633
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011635 {
11636 if (get_option_tv(&varname, rettv, TRUE) == OK)
11637 done = TRUE;
11638 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011639 else if (STRCMP(varname, "changedtick") == 0)
11640 {
11641 rettv->v_type = VAR_NUMBER;
11642 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011643 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 else
11646 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011647 /* Look up the variable. */
11648 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11649 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11650 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011652 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011654 done = TRUE;
11655 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011657
11658 /* restore previous notion of curbuf */
11659 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660 }
11661
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011662 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11663 /* use the default value */
11664 copy_tv(&argvars[2], rettv);
11665
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666 --emsg_off;
11667}
11668
11669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 * "getchar()" function
11671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
11675 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011678 /* Position the cursor. Needed after a message that ends in a space. */
11679 windgoto(msg_row, msg_col);
11680
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 ++no_mapping;
11682 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011683 for (;;)
11684 {
11685 if (argvars[0].v_type == VAR_UNKNOWN)
11686 /* getchar(): blocking wait. */
11687 n = safe_vgetc();
11688 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11689 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011690 n = vpeekc_any();
11691 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011692 /* illegal argument or getchar(0) and no char avail: return zero */
11693 n = 0;
11694 else
11695 /* getchar(0) and char avail: return char */
11696 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011697
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011698 if (n == K_IGNORE)
11699 continue;
11700 break;
11701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 --no_mapping;
11703 --allow_keys;
11704
Bram Moolenaar219b8702006-11-01 14:32:36 +000011705 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11706 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11707 vimvars[VV_MOUSE_COL].vv_nr = 0;
11708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 if (IS_SPECIAL(n) || mod_mask != 0)
11711 {
11712 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11713 int i = 0;
11714
11715 /* Turn a special key into three bytes, plus modifier. */
11716 if (mod_mask != 0)
11717 {
11718 temp[i++] = K_SPECIAL;
11719 temp[i++] = KS_MODIFIER;
11720 temp[i++] = mod_mask;
11721 }
11722 if (IS_SPECIAL(n))
11723 {
11724 temp[i++] = K_SPECIAL;
11725 temp[i++] = K_SECOND(n);
11726 temp[i++] = K_THIRD(n);
11727 }
11728#ifdef FEAT_MBYTE
11729 else if (has_mbyte)
11730 i += (*mb_char2bytes)(n, temp + i);
11731#endif
11732 else
11733 temp[i++] = n;
11734 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 rettv->v_type = VAR_STRING;
11736 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011737
11738#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011739 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011740 {
11741 int row = mouse_row;
11742 int col = mouse_col;
11743 win_T *win;
11744 linenr_T lnum;
11745# ifdef FEAT_WINDOWS
11746 win_T *wp;
11747# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011748 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011749
11750 if (row >= 0 && col >= 0)
11751 {
11752 /* Find the window at the mouse coordinates and compute the
11753 * text position. */
11754 win = mouse_find_win(&row, &col);
11755 (void)mouse_comp_pos(win, &row, &col, &lnum);
11756# ifdef FEAT_WINDOWS
11757 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011758 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011759# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011760 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011761 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11762 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11763 }
11764 }
11765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 }
11767}
11768
11769/*
11770 * "getcharmod()" function
11771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011773f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776}
11777
11778/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011779 * "getcharsearch()" function
11780 */
11781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011782f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011783{
11784 if (rettv_dict_alloc(rettv) != FAIL)
11785 {
11786 dict_T *dict = rettv->vval.v_dict;
11787
11788 dict_add_nr_str(dict, "char", 0L, last_csearch());
11789 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11790 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11791 }
11792}
11793
11794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 * "getcmdline()" function
11796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011798f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 rettv->v_type = VAR_STRING;
11801 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802}
11803
11804/*
11805 * "getcmdpos()" function
11806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011808f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011814 * "getcmdtype()" function
11815 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011817f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011818{
11819 rettv->v_type = VAR_STRING;
11820 rettv->vval.v_string = alloc(2);
11821 if (rettv->vval.v_string != NULL)
11822 {
11823 rettv->vval.v_string[0] = get_cmdline_type();
11824 rettv->vval.v_string[1] = NUL;
11825 }
11826}
11827
11828/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011829 * "getcmdwintype()" function
11830 */
11831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011832f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011833{
11834 rettv->v_type = VAR_STRING;
11835 rettv->vval.v_string = NULL;
11836#ifdef FEAT_CMDWIN
11837 rettv->vval.v_string = alloc(2);
11838 if (rettv->vval.v_string != NULL)
11839 {
11840 rettv->vval.v_string[0] = cmdwin_type;
11841 rettv->vval.v_string[1] = NUL;
11842 }
11843#endif
11844}
11845
11846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 * "getcwd()" function
11848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011850f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011852 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011853 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011856 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011857
11858 wp = find_tabwin(&argvars[0], &argvars[1]);
11859 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011861 if (wp->w_localdir != NULL)
11862 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11863 else if(globaldir != NULL)
11864 rettv->vval.v_string = vim_strsave(globaldir);
11865 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011866 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011867 cwd = alloc(MAXPATHL);
11868 if (cwd != NULL)
11869 {
11870 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11871 rettv->vval.v_string = vim_strsave(cwd);
11872 vim_free(cwd);
11873 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011874 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011875#ifdef BACKSLASH_IN_FILENAME
11876 if (rettv->vval.v_string != NULL)
11877 slash_adjust(rettv->vval.v_string);
11878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 }
11880}
11881
11882/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011883 * "getfontname()" function
11884 */
11885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011886f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 rettv->v_type = VAR_STRING;
11889 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011890#ifdef FEAT_GUI
11891 if (gui.in_use)
11892 {
11893 GuiFont font;
11894 char_u *name = NULL;
11895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011896 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011897 {
11898 /* Get the "Normal" font. Either the name saved by
11899 * hl_set_font_name() or from the font ID. */
11900 font = gui.norm_font;
11901 name = hl_get_font_name();
11902 }
11903 else
11904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011906 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11907 return;
11908 font = gui_mch_get_font(name, FALSE);
11909 if (font == NOFONT)
11910 return; /* Invalid font name, return empty string. */
11911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011913 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011914 gui_mch_free_font(font);
11915 }
11916#endif
11917}
11918
11919/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011920 * "getfperm({fname})" function
11921 */
11922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011923f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011924{
11925 char_u *fname;
11926 struct stat st;
11927 char_u *perm = NULL;
11928 char_u flags[] = "rwx";
11929 int i;
11930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011934 if (mch_stat((char *)fname, &st) >= 0)
11935 {
11936 perm = vim_strsave((char_u *)"---------");
11937 if (perm != NULL)
11938 {
11939 for (i = 0; i < 9; i++)
11940 {
11941 if (st.st_mode & (1 << (8 - i)))
11942 perm[i] = flags[i % 3];
11943 }
11944 }
11945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011947}
11948
11949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 * "getfsize({fname})" function
11951 */
11952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011953f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
11955 char_u *fname;
11956 struct stat st;
11957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
11962 if (mch_stat((char *)fname, &st) >= 0)
11963 {
11964 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011969
11970 /* non-perfect check for overflow */
11971 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11972 rettv->vval.v_number = -2;
11973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 }
11975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977}
11978
11979/*
11980 * "getftime({fname})" function
11981 */
11982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011983f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
11985 char_u *fname;
11986 struct stat st;
11987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989
11990 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994}
11995
11996/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011997 * "getftype({fname})" function
11998 */
11999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012001{
12002 char_u *fname;
12003 struct stat st;
12004 char_u *type = NULL;
12005 char *t;
12006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012010 if (mch_lstat((char *)fname, &st) >= 0)
12011 {
12012#ifdef S_ISREG
12013 if (S_ISREG(st.st_mode))
12014 t = "file";
12015 else if (S_ISDIR(st.st_mode))
12016 t = "dir";
12017# ifdef S_ISLNK
12018 else if (S_ISLNK(st.st_mode))
12019 t = "link";
12020# endif
12021# ifdef S_ISBLK
12022 else if (S_ISBLK(st.st_mode))
12023 t = "bdev";
12024# endif
12025# ifdef S_ISCHR
12026 else if (S_ISCHR(st.st_mode))
12027 t = "cdev";
12028# endif
12029# ifdef S_ISFIFO
12030 else if (S_ISFIFO(st.st_mode))
12031 t = "fifo";
12032# endif
12033# ifdef S_ISSOCK
12034 else if (S_ISSOCK(st.st_mode))
12035 t = "fifo";
12036# endif
12037 else
12038 t = "other";
12039#else
12040# ifdef S_IFMT
12041 switch (st.st_mode & S_IFMT)
12042 {
12043 case S_IFREG: t = "file"; break;
12044 case S_IFDIR: t = "dir"; break;
12045# ifdef S_IFLNK
12046 case S_IFLNK: t = "link"; break;
12047# endif
12048# ifdef S_IFBLK
12049 case S_IFBLK: t = "bdev"; break;
12050# endif
12051# ifdef S_IFCHR
12052 case S_IFCHR: t = "cdev"; break;
12053# endif
12054# ifdef S_IFIFO
12055 case S_IFIFO: t = "fifo"; break;
12056# endif
12057# ifdef S_IFSOCK
12058 case S_IFSOCK: t = "socket"; break;
12059# endif
12060 default: t = "other";
12061 }
12062# else
12063 if (mch_isdir(fname))
12064 t = "dir";
12065 else
12066 t = "file";
12067# endif
12068#endif
12069 type = vim_strsave((char_u *)t);
12070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012071 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012072}
12073
12074/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012075 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012076 */
12077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012078f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012079{
12080 linenr_T lnum;
12081 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012082 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012083
12084 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012085 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012086 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012088 retlist = FALSE;
12089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012091 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012093 retlist = TRUE;
12094 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012095
Bram Moolenaar342337a2005-07-21 21:11:17 +000012096 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097}
12098
12099/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012100 * "getmatches()" function
12101 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012103f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012104{
12105#ifdef FEAT_SEARCH_EXTRA
12106 dict_T *dict;
12107 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012108 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012109
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012110 if (rettv_list_alloc(rettv) == OK)
12111 {
12112 while (cur != NULL)
12113 {
12114 dict = dict_alloc();
12115 if (dict == NULL)
12116 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012117 if (cur->match.regprog == NULL)
12118 {
12119 /* match added with matchaddpos() */
12120 for (i = 0; i < MAXPOSMATCH; ++i)
12121 {
12122 llpos_T *llpos;
12123 char buf[6];
12124 list_T *l;
12125
12126 llpos = &cur->pos.pos[i];
12127 if (llpos->lnum == 0)
12128 break;
12129 l = list_alloc();
12130 if (l == NULL)
12131 break;
12132 list_append_number(l, (varnumber_T)llpos->lnum);
12133 if (llpos->col > 0)
12134 {
12135 list_append_number(l, (varnumber_T)llpos->col);
12136 list_append_number(l, (varnumber_T)llpos->len);
12137 }
12138 sprintf(buf, "pos%d", i + 1);
12139 dict_add_list(dict, buf, l);
12140 }
12141 }
12142 else
12143 {
12144 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12145 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012146 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012147 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12148 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012149# ifdef FEAT_CONCEAL
12150 if (cur->conceal_char)
12151 {
12152 char_u buf[MB_MAXBYTES + 1];
12153
12154 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12155 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12156 }
12157# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012158 list_append_dict(rettv->vval.v_list, dict);
12159 cur = cur->next;
12160 }
12161 }
12162#endif
12163}
12164
12165/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012166 * "getpid()" function
12167 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012169f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012170{
12171 rettv->vval.v_number = mch_get_pid();
12172}
12173
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012174static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012175
12176/*
12177 * "getcurpos()" function
12178 */
12179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012180f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012181{
12182 getpos_both(argvars, rettv, TRUE);
12183}
12184
Bram Moolenaar18081e32008-02-20 19:11:07 +000012185/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012186 * "getpos(string)" function
12187 */
12188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012190{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012191 getpos_both(argvars, rettv, FALSE);
12192}
12193
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195getpos_both(
12196 typval_T *argvars,
12197 typval_T *rettv,
12198 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012199{
Bram Moolenaara5525202006-03-02 22:52:09 +000012200 pos_T *fp;
12201 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012202 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012203
12204 if (rettv_list_alloc(rettv) == OK)
12205 {
12206 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012207 if (getcurpos)
12208 fp = &curwin->w_cursor;
12209 else
12210 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012211 if (fnum != -1)
12212 list_append_number(l, (varnumber_T)fnum);
12213 else
12214 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012215 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12216 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012217 list_append_number(l, (fp != NULL)
12218 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012219 : (varnumber_T)0);
12220 list_append_number(l,
12221#ifdef FEAT_VIRTUALEDIT
12222 (fp != NULL) ? (varnumber_T)fp->coladd :
12223#endif
12224 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012225 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012226 list_append_number(l, curwin->w_curswant == MAXCOL ?
12227 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012228 }
12229 else
12230 rettv->vval.v_number = FALSE;
12231}
12232
12233/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012234 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012235 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012237f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012238{
12239#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012240 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012241#endif
12242
Bram Moolenaar2641f772005-03-25 21:58:17 +000012243#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012244 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012245 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012246 wp = NULL;
12247 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12248 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012249 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012250 if (wp == NULL)
12251 return;
12252 }
12253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012254 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012255 }
12256#endif
12257}
12258
12259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 * "getreg()" function
12261 */
12262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012263f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264{
12265 char_u *strregname;
12266 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012267 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012268 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012271 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 strregname = get_tv_string_chk(&argvars[0]);
12274 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012275 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012277 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012278 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12279 return_list = get_tv_number_chk(&argvars[2], &error);
12280 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012283 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012284
12285 if (error)
12286 return;
12287
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 regname = (strregname == NULL ? '"' : *strregname);
12289 if (regname == 0)
12290 regname = '"';
12291
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012292 if (return_list)
12293 {
12294 rettv->v_type = VAR_LIST;
12295 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12296 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012297 if (rettv->vval.v_list != NULL)
12298 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012299 }
12300 else
12301 {
12302 rettv->v_type = VAR_STRING;
12303 rettv->vval.v_string = get_reg_contents(regname,
12304 arg2 ? GREG_EXPR_SRC : 0);
12305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306}
12307
12308/*
12309 * "getregtype()" function
12310 */
12311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012312f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
12314 char_u *strregname;
12315 int regname;
12316 char_u buf[NUMBUFLEN + 2];
12317 long reglen = 0;
12318
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012319 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 {
12321 strregname = get_tv_string_chk(&argvars[0]);
12322 if (strregname == NULL) /* type error; errmsg already given */
12323 {
12324 rettv->v_type = VAR_STRING;
12325 rettv->vval.v_string = NULL;
12326 return;
12327 }
12328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 else
12330 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012331 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332
12333 regname = (strregname == NULL ? '"' : *strregname);
12334 if (regname == 0)
12335 regname = '"';
12336
12337 buf[0] = NUL;
12338 buf[1] = NUL;
12339 switch (get_reg_type(regname, &reglen))
12340 {
12341 case MLINE: buf[0] = 'V'; break;
12342 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 case MBLOCK:
12344 buf[0] = Ctrl_V;
12345 sprintf((char *)buf + 1, "%ld", reglen + 1);
12346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->v_type = VAR_STRING;
12349 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350}
12351
12352/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012353 * "gettabvar()" function
12354 */
12355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012357{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012358 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012359 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012360 dictitem_T *v;
12361 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012362 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012363
12364 rettv->v_type = VAR_STRING;
12365 rettv->vval.v_string = NULL;
12366
12367 varname = get_tv_string_chk(&argvars[1]);
12368 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12369 if (tp != NULL && varname != NULL)
12370 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012371 /* Set tp to be our tabpage, temporarily. Also set the window to the
12372 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012373 if (switch_win(&oldcurwin, &oldtabpage,
12374 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012375 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012376 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012377 /* look up the variable */
12378 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12379 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12380 if (v != NULL)
12381 {
12382 copy_tv(&v->di_tv, rettv);
12383 done = TRUE;
12384 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012385 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012386
12387 /* restore previous notion of curwin */
12388 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012389 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012390
12391 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012392 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012393}
12394
12395/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012396 * "gettabwinvar()" function
12397 */
12398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012399f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012400{
12401 getwinvar(argvars, rettv, 1);
12402}
12403
12404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 * "getwinposx()" function
12406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012408f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411#ifdef FEAT_GUI
12412 if (gui.in_use)
12413 {
12414 int x, y;
12415
12416 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 }
12419#endif
12420}
12421
12422/*
12423 * "getwinposy()" function
12424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429#ifdef FEAT_GUI
12430 if (gui.in_use)
12431 {
12432 int x, y;
12433
12434 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 }
12437#endif
12438}
12439
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012440/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012441 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012442 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012443 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012444find_win_by_nr(
12445 typval_T *vp,
12446 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012447{
12448#ifdef FEAT_WINDOWS
12449 win_T *wp;
12450#endif
12451 int nr;
12452
12453 nr = get_tv_number_chk(vp, NULL);
12454
12455#ifdef FEAT_WINDOWS
12456 if (nr < 0)
12457 return NULL;
12458 if (nr == 0)
12459 return curwin;
12460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012461 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12462 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012463 if (--nr <= 0)
12464 break;
12465 return wp;
12466#else
12467 if (nr == 0 || nr == 1)
12468 return curwin;
12469 return NULL;
12470#endif
12471}
12472
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012474 * Find window specified by "wvp" in tabpage "tvp".
12475 */
12476 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012477find_tabwin(
12478 typval_T *wvp, /* VAR_UNKNOWN for current window */
12479 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012480{
12481 win_T *wp = NULL;
12482 tabpage_T *tp = NULL;
12483 long n;
12484
12485 if (wvp->v_type != VAR_UNKNOWN)
12486 {
12487 if (tvp->v_type != VAR_UNKNOWN)
12488 {
12489 n = get_tv_number(tvp);
12490 if (n >= 0)
12491 tp = find_tabpage(n);
12492 }
12493 else
12494 tp = curtab;
12495
12496 if (tp != NULL)
12497 wp = find_win_by_nr(wvp, tp);
12498 }
12499 else
12500 wp = curwin;
12501
12502 return wp;
12503}
12504
12505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 * "getwinvar()" function
12507 */
12508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012509f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012511 getwinvar(argvars, rettv, 0);
12512}
12513
12514/*
12515 * getwinvar() and gettabwinvar()
12516 */
12517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012518getwinvar(
12519 typval_T *argvars,
12520 typval_T *rettv,
12521 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012522{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012523 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012526 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012527 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012528#ifdef FEAT_WINDOWS
12529 win_T *oldcurwin;
12530 tabpage_T *oldtabpage;
12531 int need_switch_win;
12532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012534#ifdef FEAT_WINDOWS
12535 if (off == 1)
12536 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12537 else
12538 tp = curtab;
12539#endif
12540 win = find_win_by_nr(&argvars[off], tp);
12541 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012544 rettv->v_type = VAR_STRING;
12545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546
12547 if (win != NULL && varname != NULL)
12548 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012549#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012550 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012551 * otherwise the window is not valid. Only do this when needed,
12552 * autocommands get blocked. */
12553 need_switch_win = !(tp == curtab && win == curwin);
12554 if (!need_switch_win
12555 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12556#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012557 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012558 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012559 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012560 if (get_option_tv(&varname, rettv, 1) == OK)
12561 done = TRUE;
12562 }
12563 else
12564 {
12565 /* Look up the variable. */
12566 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12567 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12568 varname, FALSE);
12569 if (v != NULL)
12570 {
12571 copy_tv(&v->di_tv, rettv);
12572 done = TRUE;
12573 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012576
Bram Moolenaarba117c22015-09-29 16:53:22 +020012577#ifdef FEAT_WINDOWS
12578 if (need_switch_win)
12579 /* restore previous notion of curwin */
12580 restore_win(oldcurwin, oldtabpage, TRUE);
12581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582 }
12583
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012584 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12585 /* use the default return value */
12586 copy_tv(&argvars[off + 2], rettv);
12587
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 --emsg_off;
12589}
12590
12591/*
12592 * "glob()" function
12593 */
12594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012595f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012597 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012599 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012601 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012602 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012604 if (argvars[1].v_type != VAR_UNKNOWN)
12605 {
12606 if (get_tv_number_chk(&argvars[1], &error))
12607 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012608 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012609 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012610 if (get_tv_number_chk(&argvars[2], &error))
12611 {
12612 rettv->v_type = VAR_LIST;
12613 rettv->vval.v_list = NULL;
12614 }
12615 if (argvars[3].v_type != VAR_UNKNOWN
12616 && get_tv_number_chk(&argvars[3], &error))
12617 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012618 }
12619 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012620 if (!error)
12621 {
12622 ExpandInit(&xpc);
12623 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012624 if (p_wic)
12625 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012626 if (rettv->v_type == VAR_STRING)
12627 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012628 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012629 else if (rettv_list_alloc(rettv) != FAIL)
12630 {
12631 int i;
12632
12633 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12634 NULL, options, WILD_ALL_KEEP);
12635 for (i = 0; i < xpc.xp_numfiles; i++)
12636 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12637
12638 ExpandCleanup(&xpc);
12639 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012640 }
12641 else
12642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643}
12644
12645/*
12646 * "globpath()" function
12647 */
12648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012649f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012651 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012654 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012655 garray_T ga;
12656 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012658 /* When the optional second argument is non-zero, don't remove matches
12659 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012661 if (argvars[2].v_type != VAR_UNKNOWN)
12662 {
12663 if (get_tv_number_chk(&argvars[2], &error))
12664 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012665 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012666 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012667 if (get_tv_number_chk(&argvars[3], &error))
12668 {
12669 rettv->v_type = VAR_LIST;
12670 rettv->vval.v_list = NULL;
12671 }
12672 if (argvars[4].v_type != VAR_UNKNOWN
12673 && get_tv_number_chk(&argvars[4], &error))
12674 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012675 }
12676 }
12677 if (file != NULL && !error)
12678 {
12679 ga_init2(&ga, (int)sizeof(char_u *), 10);
12680 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12681 if (rettv->v_type == VAR_STRING)
12682 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12683 else if (rettv_list_alloc(rettv) != FAIL)
12684 for (i = 0; i < ga.ga_len; ++i)
12685 list_append_string(rettv->vval.v_list,
12686 ((char_u **)(ga.ga_data))[i], -1);
12687 ga_clear_strings(&ga);
12688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691}
12692
12693/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012694 * "glob2regpat()" function
12695 */
12696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012697f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012698{
12699 char_u *pat = get_tv_string_chk(&argvars[0]);
12700
12701 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012702 rettv->vval.v_string = (pat == NULL)
12703 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012704}
12705
12706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 * "has()" function
12708 */
12709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012710f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711{
12712 int i;
12713 char_u *name;
12714 int n = FALSE;
12715 static char *(has_list[]) =
12716 {
12717#ifdef AMIGA
12718 "amiga",
12719# ifdef FEAT_ARP
12720 "arp",
12721# endif
12722#endif
12723#ifdef __BEOS__
12724 "beos",
12725#endif
12726#ifdef MSDOS
12727# ifdef DJGPP
12728 "dos32",
12729# else
12730 "dos16",
12731# endif
12732#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012733#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 "mac",
12735#endif
12736#if defined(MACOS_X_UNIX)
12737 "macunix",
12738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739#ifdef __QNX__
12740 "qnx",
12741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742#ifdef UNIX
12743 "unix",
12744#endif
12745#ifdef VMS
12746 "vms",
12747#endif
12748#ifdef WIN16
12749 "win16",
12750#endif
12751#ifdef WIN32
12752 "win32",
12753#endif
12754#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12755 "win32unix",
12756#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012757#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 "win64",
12759#endif
12760#ifdef EBCDIC
12761 "ebcdic",
12762#endif
12763#ifndef CASE_INSENSITIVE_FILENAME
12764 "fname_case",
12765#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012766#ifdef HAVE_ACL
12767 "acl",
12768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#ifdef FEAT_ARABIC
12770 "arabic",
12771#endif
12772#ifdef FEAT_AUTOCMD
12773 "autocmd",
12774#endif
12775#ifdef FEAT_BEVAL
12776 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012777# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12778 "balloon_multiline",
12779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780#endif
12781#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12782 "builtin_terms",
12783# ifdef ALL_BUILTIN_TCAPS
12784 "all_builtin_terms",
12785# endif
12786#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012787#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12788 || defined(FEAT_GUI_W32) \
12789 || defined(FEAT_GUI_MOTIF))
12790 "browsefilter",
12791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792#ifdef FEAT_BYTEOFF
12793 "byte_offset",
12794#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012795#ifdef FEAT_CHANNEL
12796 "channel",
12797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798#ifdef FEAT_CINDENT
12799 "cindent",
12800#endif
12801#ifdef FEAT_CLIENTSERVER
12802 "clientserver",
12803#endif
12804#ifdef FEAT_CLIPBOARD
12805 "clipboard",
12806#endif
12807#ifdef FEAT_CMDL_COMPL
12808 "cmdline_compl",
12809#endif
12810#ifdef FEAT_CMDHIST
12811 "cmdline_hist",
12812#endif
12813#ifdef FEAT_COMMENTS
12814 "comments",
12815#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012816#ifdef FEAT_CONCEAL
12817 "conceal",
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819#ifdef FEAT_CRYPT
12820 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012821 "crypt-blowfish",
12822 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823#endif
12824#ifdef FEAT_CSCOPE
12825 "cscope",
12826#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012827#ifdef FEAT_CURSORBIND
12828 "cursorbind",
12829#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012830#ifdef CURSOR_SHAPE
12831 "cursorshape",
12832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833#ifdef DEBUG
12834 "debug",
12835#endif
12836#ifdef FEAT_CON_DIALOG
12837 "dialog_con",
12838#endif
12839#ifdef FEAT_GUI_DIALOG
12840 "dialog_gui",
12841#endif
12842#ifdef FEAT_DIFF
12843 "diff",
12844#endif
12845#ifdef FEAT_DIGRAPHS
12846 "digraphs",
12847#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012848#ifdef FEAT_DIRECTX
12849 "directx",
12850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#ifdef FEAT_DND
12852 "dnd",
12853#endif
12854#ifdef FEAT_EMACS_TAGS
12855 "emacs_tags",
12856#endif
12857 "eval", /* always present, of course! */
12858#ifdef FEAT_EX_EXTRA
12859 "ex_extra",
12860#endif
12861#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 {
13687# ifdef FEAT_EX_EXTRA
13688 int save_ex_normal_busy = ex_normal_busy;
13689 ex_normal_busy = 0;
13690# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013692 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13693 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013694# ifdef FEAT_EX_EXTRA
13695 ex_normal_busy = save_ex_normal_busy;
13696# endif
13697 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013698 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013699 && argvars[1].v_type != VAR_UNKNOWN
13700 && argvars[2].v_type != VAR_UNKNOWN)
13701 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13702 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013703
13704 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 /* since the user typed this, no need to wait for return */
13707 need_wait_return = FALSE;
13708 msg_didout = FALSE;
13709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 cmd_silent = cmd_silent_save;
13711}
13712
13713/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013714 * "input()" function
13715 * Also handles inputsecret() when inputsecret is set.
13716 */
13717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013718f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013719{
13720 get_user_input(argvars, rettv, FALSE);
13721}
13722
13723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 * "inputdialog()" function
13725 */
13726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013727f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728{
13729#if defined(FEAT_GUI_TEXTDIALOG)
13730 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13731 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13732 {
13733 char_u *message;
13734 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013735 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013737 message = get_tv_string_chk(&argvars[0]);
13738 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013739 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013740 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 else
13742 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013743 if (message != NULL && defstr != NULL
13744 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013745 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 else
13748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 if (message != NULL && defstr != NULL
13750 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013751 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->vval.v_string = vim_strsave(
13753 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 }
13759 else
13760#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013761 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762}
13763
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013764/*
13765 * "inputlist()" function
13766 */
13767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013768f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013769{
13770 listitem_T *li;
13771 int selected;
13772 int mouse_used;
13773
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013774#ifdef NO_CONSOLE_INPUT
13775 /* While starting up, there is no place to enter text. */
13776 if (no_console_input())
13777 return;
13778#endif
13779 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13780 {
13781 EMSG2(_(e_listarg), "inputlist()");
13782 return;
13783 }
13784
13785 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013786 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013787 lines_left = Rows; /* avoid more prompt */
13788 msg_scroll = TRUE;
13789 msg_clr_eos();
13790
13791 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13792 {
13793 msg_puts(get_tv_string(&li->li_tv));
13794 msg_putchar('\n');
13795 }
13796
13797 /* Ask for choice. */
13798 selected = prompt_for_number(&mouse_used);
13799 if (mouse_used)
13800 selected -= lines_left;
13801
13802 rettv->vval.v_number = selected;
13803}
13804
13805
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13807
13808/*
13809 * "inputrestore()" function
13810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013812f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813{
13814 if (ga_userinput.ga_len > 0)
13815 {
13816 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13818 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013819 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 }
13821 else if (p_verbose > 1)
13822 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013823 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 }
13826}
13827
13828/*
13829 * "inputsave()" function
13830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013832f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013834 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 if (ga_grow(&ga_userinput, 1) == OK)
13836 {
13837 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13838 + ga_userinput.ga_len);
13839 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013840 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 }
13842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013843 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844}
13845
13846/*
13847 * "inputsecret()" function
13848 */
13849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013850f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851{
13852 ++cmdline_star;
13853 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 --cmdline_star;
13856 --inputsecret_flag;
13857}
13858
13859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860 * "insert()" function
13861 */
13862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013863f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013864{
13865 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013866 listitem_T *item;
13867 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013868 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013869
13870 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013872 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013873 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013874 {
13875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876 before = get_tv_number_chk(&argvars[2], &error);
13877 if (error)
13878 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013879
Bram Moolenaar758711c2005-02-02 23:11:38 +000013880 if (before == l->lv_len)
13881 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013882 else
13883 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013884 item = list_find(l, before);
13885 if (item == NULL)
13886 {
13887 EMSGN(_(e_listidx), before);
13888 l = NULL;
13889 }
13890 }
13891 if (l != NULL)
13892 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013893 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013894 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013895 }
13896 }
13897}
13898
13899/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013900 * "invert(expr)" function
13901 */
13902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013903f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013904{
13905 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13906}
13907
13908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 * "isdirectory()" function
13910 */
13911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013912f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915}
13916
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013918 * "islocked()" function
13919 */
13920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013921f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013922{
13923 lval_T lv;
13924 char_u *end;
13925 dictitem_T *di;
13926
13927 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013928 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13929 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013930 if (end != NULL && lv.ll_name != NULL)
13931 {
13932 if (*end != NUL)
13933 EMSG(_(e_trailing));
13934 else
13935 {
13936 if (lv.ll_tv == NULL)
13937 {
13938 if (check_changedtick(lv.ll_name))
13939 rettv->vval.v_number = 1; /* always locked */
13940 else
13941 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013942 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013943 if (di != NULL)
13944 {
13945 /* Consider a variable locked when:
13946 * 1. the variable itself is locked
13947 * 2. the value of the variable is locked.
13948 * 3. the List or Dict value is locked.
13949 */
13950 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13951 || tv_islocked(&di->di_tv));
13952 }
13953 }
13954 }
13955 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013956 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013957 else if (lv.ll_newkey != NULL)
13958 EMSG2(_(e_dictkey), lv.ll_newkey);
13959 else if (lv.ll_list != NULL)
13960 /* List item. */
13961 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13962 else
13963 /* Dictionary item. */
13964 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13965 }
13966 }
13967
13968 clear_lval(&lv);
13969}
13970
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013971static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013972
13973/*
13974 * Turn a dict into a list:
13975 * "what" == 0: list of keys
13976 * "what" == 1: list of values
13977 * "what" == 2: list of items
13978 */
13979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013980dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013981{
Bram Moolenaar33570922005-01-25 22:26:29 +000013982 list_T *l2;
13983 dictitem_T *di;
13984 hashitem_T *hi;
13985 listitem_T *li;
13986 listitem_T *li2;
13987 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013988 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013989
Bram Moolenaar8c711452005-01-14 21:53:12 +000013990 if (argvars[0].v_type != VAR_DICT)
13991 {
13992 EMSG(_(e_dictreq));
13993 return;
13994 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013995 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013996 return;
13997
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013998 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013999 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014000
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014001 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014002 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014004 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014005 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014006 --todo;
14007 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014008
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014009 li = listitem_alloc();
14010 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014011 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014012 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014013
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014014 if (what == 0)
14015 {
14016 /* keys() */
14017 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014018 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014019 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14020 }
14021 else if (what == 1)
14022 {
14023 /* values() */
14024 copy_tv(&di->di_tv, &li->li_tv);
14025 }
14026 else
14027 {
14028 /* items() */
14029 l2 = list_alloc();
14030 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014031 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014032 li->li_tv.vval.v_list = l2;
14033 if (l2 == NULL)
14034 break;
14035 ++l2->lv_refcount;
14036
14037 li2 = listitem_alloc();
14038 if (li2 == NULL)
14039 break;
14040 list_append(l2, li2);
14041 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014042 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014043 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14044
14045 li2 = listitem_alloc();
14046 if (li2 == NULL)
14047 break;
14048 list_append(l2, li2);
14049 copy_tv(&di->di_tv, &li2->li_tv);
14050 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014051 }
14052 }
14053}
14054
14055/*
14056 * "items(dict)" function
14057 */
14058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014059f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014060{
14061 dict_list(argvars, rettv, 2);
14062}
14063
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014065 * "join()" function
14066 */
14067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014068f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014069{
14070 garray_T ga;
14071 char_u *sep;
14072
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014073 if (argvars[0].v_type != VAR_LIST)
14074 {
14075 EMSG(_(e_listreq));
14076 return;
14077 }
14078 if (argvars[0].vval.v_list == NULL)
14079 return;
14080 if (argvars[1].v_type == VAR_UNKNOWN)
14081 sep = (char_u *)" ";
14082 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014083 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014084
14085 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086
14087 if (sep != NULL)
14088 {
14089 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014090 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 ga_append(&ga, NUL);
14092 rettv->vval.v_string = (char_u *)ga.ga_data;
14093 }
14094 else
14095 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014096}
14097
14098/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014099 * "jsondecode()" function
14100 */
14101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014102f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014103{
14104 js_read_T reader;
14105
14106 reader.js_buf = get_tv_string(&argvars[0]);
14107 reader.js_eof = TRUE;
14108 reader.js_used = 0;
14109 json_decode(&reader, rettv);
14110}
14111
14112/*
14113 * "jsonencode()" function
14114 */
14115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014116f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014117{
14118 rettv->v_type = VAR_STRING;
14119 rettv->vval.v_string = json_encode(&argvars[0]);
14120}
14121
14122/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123 * "keys()" function
14124 */
14125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014126f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127{
14128 dict_list(argvars, rettv, 0);
14129}
14130
14131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 * "last_buffer_nr()" function.
14133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014135f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136{
14137 int n = 0;
14138 buf_T *buf;
14139
14140 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14141 if (n < buf->b_fnum)
14142 n = buf->b_fnum;
14143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014145}
14146
14147/*
14148 * "len()" function
14149 */
14150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014151f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014152{
14153 switch (argvars[0].v_type)
14154 {
14155 case VAR_STRING:
14156 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_number = (varnumber_T)STRLEN(
14158 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014159 break;
14160 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014162 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014163 case VAR_DICT:
14164 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14165 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014166 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014167 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014168 break;
14169 }
14170}
14171
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014172static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014173
14174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014175libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014176{
14177#ifdef FEAT_LIBCALL
14178 char_u *string_in;
14179 char_u **string_result;
14180 int nr_result;
14181#endif
14182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014184 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014186
14187 if (check_restricted() || check_secure())
14188 return;
14189
14190#ifdef FEAT_LIBCALL
14191 /* The first two args must be strings, otherwise its meaningless */
14192 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014194 string_in = NULL;
14195 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014196 string_in = argvars[2].vval.v_string;
14197 if (type == VAR_NUMBER)
14198 string_result = NULL;
14199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014201 if (mch_libcall(argvars[0].vval.v_string,
14202 argvars[1].vval.v_string,
14203 string_in,
14204 argvars[2].vval.v_number,
14205 string_result,
14206 &nr_result) == OK
14207 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014209 }
14210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211}
14212
14213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014214 * "libcall()" function
14215 */
14216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014217f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218{
14219 libcall_common(argvars, rettv, VAR_STRING);
14220}
14221
14222/*
14223 * "libcallnr()" function
14224 */
14225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014226f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227{
14228 libcall_common(argvars, rettv, VAR_NUMBER);
14229}
14230
14231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 * "line(string)" function
14233 */
14234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014235f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236{
14237 linenr_T lnum = 0;
14238 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014239 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014241 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 if (fp != NULL)
14243 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245}
14246
14247/*
14248 * "line2byte(lnum)" function
14249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014251f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252{
14253#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255#else
14256 linenr_T lnum;
14257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14263 if (rettv->vval.v_number >= 0)
14264 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265#endif
14266}
14267
14268/*
14269 * "lispindent(lnum)" function
14270 */
14271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014272f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
14274#ifdef FEAT_LISP
14275 pos_T pos;
14276 linenr_T lnum;
14277
14278 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14281 {
14282 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 curwin->w_cursor = pos;
14285 }
14286 else
14287#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289}
14290
14291/*
14292 * "localtime()" function
14293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014295f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298}
14299
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014300static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301
14302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014303get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304{
14305 char_u *keys;
14306 char_u *which;
14307 char_u buf[NUMBUFLEN];
14308 char_u *keys_buf = NULL;
14309 char_u *rhs;
14310 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014311 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014312 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014313 mapblock_T *mp;
14314 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315
14316 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->v_type = VAR_STRING;
14318 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014320 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 if (*keys == NUL)
14322 return;
14323
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014324 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014327 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014328 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014329 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014330 if (argvars[3].v_type != VAR_UNKNOWN)
14331 get_dict = get_tv_number(&argvars[3]);
14332 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 else
14335 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 if (which == NULL)
14337 return;
14338
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 mode = get_map_mode(&which, 0);
14340
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014341 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014342 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014344
14345 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014347 /* Return a string. */
14348 if (rhs != NULL)
14349 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350
Bram Moolenaarbd743252010-10-20 21:23:33 +020014351 }
14352 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14353 {
14354 /* Return a dictionary. */
14355 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14356 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14357 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358
Bram Moolenaarbd743252010-10-20 21:23:33 +020014359 dict_add_nr_str(dict, "lhs", 0L, lhs);
14360 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14361 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14362 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14363 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14364 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14365 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014366 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014367 dict_add_nr_str(dict, "mode", 0L, mapmode);
14368
14369 vim_free(lhs);
14370 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 }
14372}
14373
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014374#ifdef FEAT_FLOAT
14375/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014376 * "log()" function
14377 */
14378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014379f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014380{
14381 float_T f;
14382
14383 rettv->v_type = VAR_FLOAT;
14384 if (get_float_arg(argvars, &f) == OK)
14385 rettv->vval.v_float = log(f);
14386 else
14387 rettv->vval.v_float = 0.0;
14388}
14389
14390/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014391 * "log10()" function
14392 */
14393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014394f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014395{
14396 float_T f;
14397
14398 rettv->v_type = VAR_FLOAT;
14399 if (get_float_arg(argvars, &f) == OK)
14400 rettv->vval.v_float = log10(f);
14401 else
14402 rettv->vval.v_float = 0.0;
14403}
14404#endif
14405
Bram Moolenaar1dced572012-04-05 16:54:08 +020014406#ifdef FEAT_LUA
14407/*
14408 * "luaeval()" function
14409 */
14410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014411f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014412{
14413 char_u *str;
14414 char_u buf[NUMBUFLEN];
14415
14416 str = get_tv_string_buf(&argvars[0], buf);
14417 do_luaeval(str, argvars + 1, rettv);
14418}
14419#endif
14420
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014422 * "map()" function
14423 */
14424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014425f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014426{
14427 filter_map(argvars, rettv, TRUE);
14428}
14429
14430/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 */
14433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437}
14438
14439/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 */
14442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014443f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446}
14447
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014448static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449
14450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014451find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014453 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014454 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014455 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 char_u *pat;
14457 regmatch_T regmatch;
14458 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014459 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 char_u *save_cpo;
14461 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014462 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014463 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014464 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014465 list_T *l = NULL;
14466 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014467 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014468 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469
14470 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14471 save_cpo = p_cpo;
14472 p_cpo = (char_u *)"";
14473
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014474 rettv->vval.v_number = -1;
14475 if (type == 3)
14476 {
14477 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014478 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014479 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014480 }
14481 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->v_type = VAR_STRING;
14484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014487 if (argvars[0].v_type == VAR_LIST)
14488 {
14489 if ((l = argvars[0].vval.v_list) == NULL)
14490 goto theend;
14491 li = l->lv_first;
14492 }
14493 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014494 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014495 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014496 len = (long)STRLEN(str);
14497 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014499 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14500 if (pat == NULL)
14501 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014503 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 int error = FALSE;
14506
14507 start = get_tv_number_chk(&argvars[2], &error);
14508 if (error)
14509 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014510 if (l != NULL)
14511 {
14512 li = list_find(l, start);
14513 if (li == NULL)
14514 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014515 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516 }
14517 else
14518 {
14519 if (start < 0)
14520 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014521 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014522 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014523 /* When "count" argument is there ignore matches before "start",
14524 * otherwise skip part of the string. Differs when pattern is "^"
14525 * or "\<". */
14526 if (argvars[3].v_type != VAR_UNKNOWN)
14527 startcol = start;
14528 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014529 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014530 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014531 len -= start;
14532 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014533 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014534
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014535 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014536 nth = get_tv_number_chk(&argvars[3], &error);
14537 if (error)
14538 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 }
14540
14541 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14542 if (regmatch.regprog != NULL)
14543 {
14544 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014545
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014546 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014547 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014548 if (l != NULL)
14549 {
14550 if (li == NULL)
14551 {
14552 match = FALSE;
14553 break;
14554 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014555 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014556 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014557 if (str == NULL)
14558 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014559 }
14560
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014561 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014563 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014564 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014565 if (l == NULL && !match)
14566 break;
14567
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014568 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014569 if (l != NULL)
14570 {
14571 li = li->li_next;
14572 ++idx;
14573 }
14574 else
14575 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014576#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014577 startcol = (colnr_T)(regmatch.startp[0]
14578 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014579#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014580 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014581#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014582 if (startcol > (colnr_T)len
14583 || str + startcol <= regmatch.startp[0])
14584 {
14585 match = FALSE;
14586 break;
14587 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014588 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014589 }
14590
14591 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014594 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014595 int i;
14596
14597 /* return list with matched string and submatches */
14598 for (i = 0; i < NSUBEXP; ++i)
14599 {
14600 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014601 {
14602 if (list_append_string(rettv->vval.v_list,
14603 (char_u *)"", 0) == FAIL)
14604 break;
14605 }
14606 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014607 regmatch.startp[i],
14608 (int)(regmatch.endp[i] - regmatch.startp[i]))
14609 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014610 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014611 }
14612 }
14613 else if (type == 2)
14614 {
14615 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014616 if (l != NULL)
14617 copy_tv(&li->li_tv, rettv);
14618 else
14619 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014621 }
14622 else if (l != NULL)
14623 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 else
14625 {
14626 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 (varnumber_T)(regmatch.startp[0] - str);
14629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014632 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 }
14634 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014635 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 }
14637
14638theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014639 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 p_cpo = save_cpo;
14641}
14642
14643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 * "match()" function
14645 */
14646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014647f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648{
14649 find_some_match(argvars, rettv, 1);
14650}
14651
14652/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014653 * "matchadd()" function
14654 */
14655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014656f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014657{
14658#ifdef FEAT_SEARCH_EXTRA
14659 char_u buf[NUMBUFLEN];
14660 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14661 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14662 int prio = 10; /* default priority */
14663 int id = -1;
14664 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014665 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014666
14667 rettv->vval.v_number = -1;
14668
14669 if (grp == NULL || pat == NULL)
14670 return;
14671 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014672 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014673 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014674 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014675 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014676 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014677 if (argvars[4].v_type != VAR_UNKNOWN)
14678 {
14679 if (argvars[4].v_type != VAR_DICT)
14680 {
14681 EMSG(_(e_dictreq));
14682 return;
14683 }
14684 if (dict_find(argvars[4].vval.v_dict,
14685 (char_u *)"conceal", -1) != NULL)
14686 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14687 (char_u *)"conceal", FALSE);
14688 }
14689 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014690 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014691 if (error == TRUE)
14692 return;
14693 if (id >= 1 && id <= 3)
14694 {
14695 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14696 return;
14697 }
14698
Bram Moolenaar6561d522015-07-21 15:48:27 +020014699 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14700 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014701#endif
14702}
14703
14704/*
14705 * "matchaddpos()" function
14706 */
14707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014708f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014709{
14710#ifdef FEAT_SEARCH_EXTRA
14711 char_u buf[NUMBUFLEN];
14712 char_u *group;
14713 int prio = 10;
14714 int id = -1;
14715 int error = FALSE;
14716 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014717 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014718
14719 rettv->vval.v_number = -1;
14720
14721 group = get_tv_string_buf_chk(&argvars[0], buf);
14722 if (group == NULL)
14723 return;
14724
14725 if (argvars[1].v_type != VAR_LIST)
14726 {
14727 EMSG2(_(e_listarg), "matchaddpos()");
14728 return;
14729 }
14730 l = argvars[1].vval.v_list;
14731 if (l == NULL)
14732 return;
14733
14734 if (argvars[2].v_type != VAR_UNKNOWN)
14735 {
14736 prio = get_tv_number_chk(&argvars[2], &error);
14737 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014738 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014739 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014740 if (argvars[4].v_type != VAR_UNKNOWN)
14741 {
14742 if (argvars[4].v_type != VAR_DICT)
14743 {
14744 EMSG(_(e_dictreq));
14745 return;
14746 }
14747 if (dict_find(argvars[4].vval.v_dict,
14748 (char_u *)"conceal", -1) != NULL)
14749 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14750 (char_u *)"conceal", FALSE);
14751 }
14752 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014753 }
14754 if (error == TRUE)
14755 return;
14756
14757 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14758 if (id == 1 || id == 2)
14759 {
14760 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14761 return;
14762 }
14763
Bram Moolenaar6561d522015-07-21 15:48:27 +020014764 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14765 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014766#endif
14767}
14768
14769/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014770 * "matcharg()" function
14771 */
14772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014773f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014774{
14775 if (rettv_list_alloc(rettv) == OK)
14776 {
14777#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014778 int id = get_tv_number(&argvars[0]);
14779 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014780
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014781 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014782 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014783 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14784 {
14785 list_append_string(rettv->vval.v_list,
14786 syn_id2name(m->hlg_id), -1);
14787 list_append_string(rettv->vval.v_list, m->pattern, -1);
14788 }
14789 else
14790 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014791 list_append_string(rettv->vval.v_list, NULL, -1);
14792 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014793 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014794 }
14795#endif
14796 }
14797}
14798
14799/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014800 * "matchdelete()" function
14801 */
14802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014803f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014804{
14805#ifdef FEAT_SEARCH_EXTRA
14806 rettv->vval.v_number = match_delete(curwin,
14807 (int)get_tv_number(&argvars[0]), TRUE);
14808#endif
14809}
14810
14811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812 * "matchend()" function
14813 */
14814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014815f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816{
14817 find_some_match(argvars, rettv, 0);
14818}
14819
14820/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821 * "matchlist()" function
14822 */
14823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014824f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825{
14826 find_some_match(argvars, rettv, 3);
14827}
14828
14829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014830 * "matchstr()" function
14831 */
14832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014833f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014834{
14835 find_some_match(argvars, rettv, 2);
14836}
14837
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014838static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014839
14840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014842{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014843 long n = 0;
14844 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014846
14847 if (argvars[0].v_type == VAR_LIST)
14848 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 list_T *l;
14850 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014851
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014852 l = argvars[0].vval.v_list;
14853 if (l != NULL)
14854 {
14855 li = l->lv_first;
14856 if (li != NULL)
14857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014859 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014860 {
14861 li = li->li_next;
14862 if (li == NULL)
14863 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014864 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014865 if (domax ? i > n : i < n)
14866 n = i;
14867 }
14868 }
14869 }
14870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014871 else if (argvars[0].v_type == VAR_DICT)
14872 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014874 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014876 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014877
14878 d = argvars[0].vval.v_dict;
14879 if (d != NULL)
14880 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014881 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014883 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014884 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014885 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014886 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014888 if (first)
14889 {
14890 n = i;
14891 first = FALSE;
14892 }
14893 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014894 n = i;
14895 }
14896 }
14897 }
14898 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014899 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014900 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014901 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014902}
14903
14904/*
14905 * "max()" function
14906 */
14907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014908f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014909{
14910 max_min(argvars, rettv, TRUE);
14911}
14912
14913/*
14914 * "min()" function
14915 */
14916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014917f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014918{
14919 max_min(argvars, rettv, FALSE);
14920}
14921
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014922static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014923
14924/*
14925 * Create the directory in which "dir" is located, and higher levels when
14926 * needed.
14927 */
14928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010014929mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014930{
14931 char_u *p;
14932 char_u *updir;
14933 int r = FAIL;
14934
14935 /* Get end of directory name in "dir".
14936 * We're done when it's "/" or "c:/". */
14937 p = gettail_sep(dir);
14938 if (p <= get_past_head(dir))
14939 return OK;
14940
14941 /* If the directory exists we're done. Otherwise: create it.*/
14942 updir = vim_strnsave(dir, (int)(p - dir));
14943 if (updir == NULL)
14944 return FAIL;
14945 if (mch_isdir(updir))
14946 r = OK;
14947 else if (mkdir_recurse(updir, prot) == OK)
14948 r = vim_mkdir_emsg(updir, prot);
14949 vim_free(updir);
14950 return r;
14951}
14952
14953#ifdef vim_mkdir
14954/*
14955 * "mkdir()" function
14956 */
14957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014958f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014959{
14960 char_u *dir;
14961 char_u buf[NUMBUFLEN];
14962 int prot = 0755;
14963
14964 rettv->vval.v_number = FAIL;
14965 if (check_restricted() || check_secure())
14966 return;
14967
14968 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014969 if (*dir == NUL)
14970 rettv->vval.v_number = FAIL;
14971 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014972 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014973 if (*gettail(dir) == NUL)
14974 /* remove trailing slashes */
14975 *gettail_sep(dir) = NUL;
14976
14977 if (argvars[1].v_type != VAR_UNKNOWN)
14978 {
14979 if (argvars[2].v_type != VAR_UNKNOWN)
14980 prot = get_tv_number_chk(&argvars[2], NULL);
14981 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14982 mkdir_recurse(dir, prot);
14983 }
14984 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014985 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014986}
14987#endif
14988
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 * "mode()" function
14991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014993f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014995 char_u buf[3];
14996
14997 buf[1] = NUL;
14998 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 if (VIsual_active)
15001 {
15002 if (VIsual_select)
15003 buf[0] = VIsual_mode + 's' - 'v';
15004 else
15005 buf[0] = VIsual_mode;
15006 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015007 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015008 || State == CONFIRM)
15009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015011 if (State == ASKMORE)
15012 buf[1] = 'm';
15013 else if (State == CONFIRM)
15014 buf[1] = '?';
15015 }
15016 else if (State == EXTERNCMD)
15017 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 else if (State & INSERT)
15019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015020#ifdef FEAT_VREPLACE
15021 if (State & VREPLACE_FLAG)
15022 {
15023 buf[0] = 'R';
15024 buf[1] = 'v';
15025 }
15026 else
15027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 if (State & REPLACE_FLAG)
15029 buf[0] = 'R';
15030 else
15031 buf[0] = 'i';
15032 }
15033 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015036 if (exmode_active)
15037 buf[1] = 'v';
15038 }
15039 else if (exmode_active)
15040 {
15041 buf[0] = 'c';
15042 buf[1] = 'e';
15043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015047 if (finish_op)
15048 buf[1] = 'o';
15049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015051 /* Clear out the minor mode when the argument is not a non-zero number or
15052 * non-empty string. */
15053 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015054 buf[1] = NUL;
15055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 rettv->vval.v_string = vim_strsave(buf);
15057 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058}
15059
Bram Moolenaar429fa852013-04-15 12:27:36 +020015060#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015061/*
15062 * "mzeval()" function
15063 */
15064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015065f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015066{
15067 char_u *str;
15068 char_u buf[NUMBUFLEN];
15069
15070 str = get_tv_string_buf(&argvars[0], buf);
15071 do_mzeval(str, rettv);
15072}
Bram Moolenaar75676462013-01-30 14:55:42 +010015073
15074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015075mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015076{
15077 typval_T argvars[3];
15078
15079 argvars[0].v_type = VAR_STRING;
15080 argvars[0].vval.v_string = name;
15081 copy_tv(args, &argvars[1]);
15082 argvars[2].v_type = VAR_UNKNOWN;
15083 f_call(argvars, rettv);
15084 clear_tv(&argvars[1]);
15085}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015086#endif
15087
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 * "nextnonblank()" function
15090 */
15091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015092f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093{
15094 linenr_T lnum;
15095
15096 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 {
15100 lnum = 0;
15101 break;
15102 }
15103 if (*skipwhite(ml_get(lnum)) != NUL)
15104 break;
15105 }
15106 rettv->vval.v_number = lnum;
15107}
15108
15109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 * "nr2char()" function
15111 */
15112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015113f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114{
15115 char_u buf[NUMBUFLEN];
15116
15117#ifdef FEAT_MBYTE
15118 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015119 {
15120 int utf8 = 0;
15121
15122 if (argvars[1].v_type != VAR_UNKNOWN)
15123 utf8 = get_tv_number_chk(&argvars[1], NULL);
15124 if (utf8)
15125 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15126 else
15127 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 else
15130#endif
15131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 buf[1] = NUL;
15134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 rettv->v_type = VAR_STRING;
15136 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015137}
15138
15139/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015140 * "or(expr, expr)" function
15141 */
15142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015143f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015144{
15145 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15146 | get_tv_number_chk(&argvars[1], NULL);
15147}
15148
15149/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015150 * "pathshorten()" function
15151 */
15152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015153f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015154{
15155 char_u *p;
15156
15157 rettv->v_type = VAR_STRING;
15158 p = get_tv_string_chk(&argvars[0]);
15159 if (p == NULL)
15160 rettv->vval.v_string = NULL;
15161 else
15162 {
15163 p = vim_strsave(p);
15164 rettv->vval.v_string = p;
15165 if (p != NULL)
15166 shorten_dir(p);
15167 }
15168}
15169
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015170#ifdef FEAT_PERL
15171/*
15172 * "perleval()" function
15173 */
15174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015175f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015176{
15177 char_u *str;
15178 char_u buf[NUMBUFLEN];
15179
15180 str = get_tv_string_buf(&argvars[0], buf);
15181 do_perleval(str, rettv);
15182}
15183#endif
15184
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015185#ifdef FEAT_FLOAT
15186/*
15187 * "pow()" function
15188 */
15189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015190f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015191{
15192 float_T fx, fy;
15193
15194 rettv->v_type = VAR_FLOAT;
15195 if (get_float_arg(argvars, &fx) == OK
15196 && get_float_arg(&argvars[1], &fy) == OK)
15197 rettv->vval.v_float = pow(fx, fy);
15198 else
15199 rettv->vval.v_float = 0.0;
15200}
15201#endif
15202
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015203/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204 * "prevnonblank()" function
15205 */
15206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015207f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208{
15209 linenr_T lnum;
15210
15211 lnum = get_tv_lnum(argvars);
15212 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15213 lnum = 0;
15214 else
15215 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15216 --lnum;
15217 rettv->vval.v_number = lnum;
15218}
15219
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015220#ifdef HAVE_STDARG_H
15221/* This dummy va_list is here because:
15222 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15223 * - locally in the function results in a "used before set" warning
15224 * - using va_start() to initialize it gives "function with fixed args" error */
15225static va_list ap;
15226#endif
15227
Bram Moolenaar8c711452005-01-14 21:53:12 +000015228/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015229 * "printf()" function
15230 */
15231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015232f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015233{
15234 rettv->v_type = VAR_STRING;
15235 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015236#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015237 {
15238 char_u buf[NUMBUFLEN];
15239 int len;
15240 char_u *s;
15241 int saved_did_emsg = did_emsg;
15242 char *fmt;
15243
15244 /* Get the required length, allocate the buffer and do it for real. */
15245 did_emsg = FALSE;
15246 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015247 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015248 if (!did_emsg)
15249 {
15250 s = alloc(len + 1);
15251 if (s != NULL)
15252 {
15253 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015254 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015255 }
15256 }
15257 did_emsg |= saved_did_emsg;
15258 }
15259#endif
15260}
15261
15262/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015263 * "pumvisible()" function
15264 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015266f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015267{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015268#ifdef FEAT_INS_EXPAND
15269 if (pum_visible())
15270 rettv->vval.v_number = 1;
15271#endif
15272}
15273
Bram Moolenaardb913952012-06-29 12:54:53 +020015274#ifdef FEAT_PYTHON3
15275/*
15276 * "py3eval()" function
15277 */
15278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015279f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015280{
15281 char_u *str;
15282 char_u buf[NUMBUFLEN];
15283
15284 str = get_tv_string_buf(&argvars[0], buf);
15285 do_py3eval(str, rettv);
15286}
15287#endif
15288
15289#ifdef FEAT_PYTHON
15290/*
15291 * "pyeval()" function
15292 */
15293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015294f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015295{
15296 char_u *str;
15297 char_u buf[NUMBUFLEN];
15298
15299 str = get_tv_string_buf(&argvars[0], buf);
15300 do_pyeval(str, rettv);
15301}
15302#endif
15303
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015304/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015305 * "range()" function
15306 */
15307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015308f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015309{
15310 long start;
15311 long end;
15312 long stride = 1;
15313 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015317 if (argvars[1].v_type == VAR_UNKNOWN)
15318 {
15319 end = start - 1;
15320 start = 0;
15321 }
15322 else
15323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015325 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015326 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015327 }
15328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015329 if (error)
15330 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015331 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015332 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015333 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015334 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015335 else
15336 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015337 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015338 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015339 if (list_append_number(rettv->vval.v_list,
15340 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015341 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015342 }
15343}
15344
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015345/*
15346 * "readfile()" function
15347 */
15348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015349f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350{
15351 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015352 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015353 char_u *fname;
15354 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015355 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15356 int io_size = sizeof(buf);
15357 int readlen; /* size of last fread() */
15358 char_u *prev = NULL; /* previously read bytes, if any */
15359 long prevlen = 0; /* length of data in prev */
15360 long prevsize = 0; /* size of prev buffer */
15361 long maxline = MAXLNUM;
15362 long cnt = 0;
15363 char_u *p; /* position in buf */
15364 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015366 if (argvars[1].v_type != VAR_UNKNOWN)
15367 {
15368 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15369 binary = TRUE;
15370 if (argvars[2].v_type != VAR_UNKNOWN)
15371 maxline = get_tv_number(&argvars[2]);
15372 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015374 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015375 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015376
15377 /* Always open the file in binary mode, library functions have a mind of
15378 * their own about CR-LF conversion. */
15379 fname = get_tv_string(&argvars[0]);
15380 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15381 {
15382 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15383 return;
15384 }
15385
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015386 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015387 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015388 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015389
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015390 /* This for loop processes what was read, but is also entered at end
15391 * of file so that either:
15392 * - an incomplete line gets written
15393 * - a "binary" file gets an empty line at the end if it ends in a
15394 * newline. */
15395 for (p = buf, start = buf;
15396 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15397 ++p)
15398 {
15399 if (*p == '\n' || readlen <= 0)
15400 {
15401 listitem_T *li;
15402 char_u *s = NULL;
15403 long_u len = p - start;
15404
15405 /* Finished a line. Remove CRs before NL. */
15406 if (readlen > 0 && !binary)
15407 {
15408 while (len > 0 && start[len - 1] == '\r')
15409 --len;
15410 /* removal may cross back to the "prev" string */
15411 if (len == 0)
15412 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15413 --prevlen;
15414 }
15415 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015416 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015417 else
15418 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015419 /* Change "prev" buffer to be the right size. This way
15420 * the bytes are only copied once, and very long lines are
15421 * allocated only once. */
15422 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015423 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015424 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015426 prev = NULL; /* the list will own the string */
15427 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015428 }
15429 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015430 if (s == NULL)
15431 {
15432 do_outofmem_msg((long_u) prevlen + len + 1);
15433 failed = TRUE;
15434 break;
15435 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015436
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015437 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015438 {
15439 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015440 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015441 break;
15442 }
15443 li->li_tv.v_type = VAR_STRING;
15444 li->li_tv.v_lock = 0;
15445 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015446 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015447
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015448 start = p + 1; /* step over newline */
15449 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015450 break;
15451 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015452 else if (*p == NUL)
15453 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015454#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015455 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15456 * when finding the BF and check the previous two bytes. */
15457 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015458 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015459 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15460 * + 1, these may be in the "prev" string. */
15461 char_u back1 = p >= buf + 1 ? p[-1]
15462 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15463 char_u back2 = p >= buf + 2 ? p[-2]
15464 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15465 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015466
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015467 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015468 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015469 char_u *dest = p - 2;
15470
15471 /* Usually a BOM is at the beginning of a file, and so at
15472 * the beginning of a line; then we can just step over it.
15473 */
15474 if (start == dest)
15475 start = p + 1;
15476 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015477 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015478 /* have to shuffle buf to close gap */
15479 int adjust_prevlen = 0;
15480
15481 if (dest < buf)
15482 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015483 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015484 dest = buf;
15485 }
15486 if (readlen > p - buf + 1)
15487 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15488 readlen -= 3 - adjust_prevlen;
15489 prevlen -= adjust_prevlen;
15490 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015491 }
15492 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015493 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015494#endif
15495 } /* for */
15496
15497 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15498 break;
15499 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015500 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015501 /* There's part of a line in buf, store it in "prev". */
15502 if (p - start + prevlen >= prevsize)
15503 {
15504 /* need bigger "prev" buffer */
15505 char_u *newprev;
15506
15507 /* A common use case is ordinary text files and "prev" gets a
15508 * fragment of a line, so the first allocation is made
15509 * small, to avoid repeatedly 'allocing' large and
15510 * 'reallocing' small. */
15511 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015512 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015513 else
15514 {
15515 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015516 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015517 prevsize = grow50pc > growmin ? grow50pc : growmin;
15518 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015519 newprev = prev == NULL ? alloc(prevsize)
15520 : vim_realloc(prev, prevsize);
15521 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015522 {
15523 do_outofmem_msg((long_u)prevsize);
15524 failed = TRUE;
15525 break;
15526 }
15527 prev = newprev;
15528 }
15529 /* Add the line part to end of "prev". */
15530 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015531 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015532 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015533 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015534
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015535 /*
15536 * For a negative line count use only the lines at the end of the file,
15537 * free the rest.
15538 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015539 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015540 while (cnt > -maxline)
15541 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015542 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015543 --cnt;
15544 }
15545
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015546 if (failed)
15547 {
15548 list_free(rettv->vval.v_list, TRUE);
15549 /* readfile doc says an empty list is returned on error */
15550 rettv->vval.v_list = list_alloc();
15551 }
15552
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015553 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015554 fclose(fd);
15555}
15556
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015557#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015558static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015559
15560/*
15561 * Convert a List to proftime_T.
15562 * Return FAIL when there is something wrong.
15563 */
15564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015565list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015566{
15567 long n1, n2;
15568 int error = FALSE;
15569
15570 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15571 || arg->vval.v_list->lv_len != 2)
15572 return FAIL;
15573 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15574 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15575# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015576 tm->HighPart = n1;
15577 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015578# else
15579 tm->tv_sec = n1;
15580 tm->tv_usec = n2;
15581# endif
15582 return error ? FAIL : OK;
15583}
15584#endif /* FEAT_RELTIME */
15585
15586/*
15587 * "reltime()" function
15588 */
15589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015590f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015591{
15592#ifdef FEAT_RELTIME
15593 proftime_T res;
15594 proftime_T start;
15595
15596 if (argvars[0].v_type == VAR_UNKNOWN)
15597 {
15598 /* No arguments: get current time. */
15599 profile_start(&res);
15600 }
15601 else if (argvars[1].v_type == VAR_UNKNOWN)
15602 {
15603 if (list2proftime(&argvars[0], &res) == FAIL)
15604 return;
15605 profile_end(&res);
15606 }
15607 else
15608 {
15609 /* Two arguments: compute the difference. */
15610 if (list2proftime(&argvars[0], &start) == FAIL
15611 || list2proftime(&argvars[1], &res) == FAIL)
15612 return;
15613 profile_sub(&res, &start);
15614 }
15615
15616 if (rettv_list_alloc(rettv) == OK)
15617 {
15618 long n1, n2;
15619
15620# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015621 n1 = res.HighPart;
15622 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015623# else
15624 n1 = res.tv_sec;
15625 n2 = res.tv_usec;
15626# endif
15627 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15628 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15629 }
15630#endif
15631}
15632
15633/*
15634 * "reltimestr()" function
15635 */
15636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015637f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015638{
15639#ifdef FEAT_RELTIME
15640 proftime_T tm;
15641#endif
15642
15643 rettv->v_type = VAR_STRING;
15644 rettv->vval.v_string = NULL;
15645#ifdef FEAT_RELTIME
15646 if (list2proftime(&argvars[0], &tm) == OK)
15647 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15648#endif
15649}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015650
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015652static void make_connection(void);
15653static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654
15655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015656make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657{
15658 if (X_DISPLAY == NULL
15659# ifdef FEAT_GUI
15660 && !gui.in_use
15661# endif
15662 )
15663 {
15664 x_force_connect = TRUE;
15665 setup_term_clip();
15666 x_force_connect = FALSE;
15667 }
15668}
15669
15670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015671check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015672{
15673 make_connection();
15674 if (X_DISPLAY == NULL)
15675 {
15676 EMSG(_("E240: No connection to Vim server"));
15677 return FAIL;
15678 }
15679 return OK;
15680}
15681#endif
15682
15683#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015684static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685
15686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015687remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688{
15689 char_u *server_name;
15690 char_u *keys;
15691 char_u *r = NULL;
15692 char_u buf[NUMBUFLEN];
15693# ifdef WIN32
15694 HWND w;
15695# else
15696 Window w;
15697# endif
15698
15699 if (check_restricted() || check_secure())
15700 return;
15701
15702# ifdef FEAT_X11
15703 if (check_connection() == FAIL)
15704 return;
15705# endif
15706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 server_name = get_tv_string_chk(&argvars[0]);
15708 if (server_name == NULL)
15709 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015710 keys = get_tv_string_buf(&argvars[1], buf);
15711# ifdef WIN32
15712 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15713# else
15714 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15715 < 0)
15716# endif
15717 {
15718 if (r != NULL)
15719 EMSG(r); /* sending worked but evaluation failed */
15720 else
15721 EMSG2(_("E241: Unable to send to %s"), server_name);
15722 return;
15723 }
15724
15725 rettv->vval.v_string = r;
15726
15727 if (argvars[2].v_type != VAR_UNKNOWN)
15728 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015729 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015730 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015731 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015733 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 v.di_tv.v_type = VAR_STRING;
15735 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015736 idvar = get_tv_string_chk(&argvars[2]);
15737 if (idvar != NULL)
15738 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015739 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015740 }
15741}
15742#endif
15743
15744/*
15745 * "remote_expr()" function
15746 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015748f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749{
15750 rettv->v_type = VAR_STRING;
15751 rettv->vval.v_string = NULL;
15752#ifdef FEAT_CLIENTSERVER
15753 remote_common(argvars, rettv, TRUE);
15754#endif
15755}
15756
15757/*
15758 * "remote_foreground()" function
15759 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015761f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763#ifdef FEAT_CLIENTSERVER
15764# ifdef WIN32
15765 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015766 {
15767 char_u *server_name = get_tv_string_chk(&argvars[0]);
15768
15769 if (server_name != NULL)
15770 serverForeground(server_name);
15771 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772# else
15773 /* Send a foreground() expression to the server. */
15774 argvars[1].v_type = VAR_STRING;
15775 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15776 argvars[2].v_type = VAR_UNKNOWN;
15777 remote_common(argvars, rettv, TRUE);
15778 vim_free(argvars[1].vval.v_string);
15779# endif
15780#endif
15781}
15782
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015784f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785{
15786#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015787 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788 char_u *s = NULL;
15789# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015790 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015792 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793
15794 if (check_restricted() || check_secure())
15795 {
15796 rettv->vval.v_number = -1;
15797 return;
15798 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015799 serverid = get_tv_string_chk(&argvars[0]);
15800 if (serverid == NULL)
15801 {
15802 rettv->vval.v_number = -1;
15803 return; /* type error; errmsg already given */
15804 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015806 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807 if (n == 0)
15808 rettv->vval.v_number = -1;
15809 else
15810 {
15811 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15812 rettv->vval.v_number = (s != NULL);
15813 }
15814# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815 if (check_connection() == FAIL)
15816 return;
15817
15818 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015820# endif
15821
15822 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015824 char_u *retvar;
15825
Bram Moolenaar33570922005-01-25 22:26:29 +000015826 v.di_tv.v_type = VAR_STRING;
15827 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015828 retvar = get_tv_string_chk(&argvars[1]);
15829 if (retvar != NULL)
15830 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015831 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832 }
15833#else
15834 rettv->vval.v_number = -1;
15835#endif
15836}
15837
Bram Moolenaar0d660222005-01-07 21:51:51 +000015838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015839f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015840{
15841 char_u *r = NULL;
15842
15843#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015844 char_u *serverid = get_tv_string_chk(&argvars[0]);
15845
15846 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847 {
15848# ifdef WIN32
15849 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015850 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015851
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015852 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015853 if (n != 0)
15854 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15855 if (r == NULL)
15856# else
15857 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859# endif
15860 EMSG(_("E277: Unable to read a server reply"));
15861 }
15862#endif
15863 rettv->v_type = VAR_STRING;
15864 rettv->vval.v_string = r;
15865}
15866
15867/*
15868 * "remote_send()" function
15869 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015871f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015872{
15873 rettv->v_type = VAR_STRING;
15874 rettv->vval.v_string = NULL;
15875#ifdef FEAT_CLIENTSERVER
15876 remote_common(argvars, rettv, FALSE);
15877#endif
15878}
15879
15880/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015881 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015882 */
15883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015884f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015885{
Bram Moolenaar33570922005-01-25 22:26:29 +000015886 list_T *l;
15887 listitem_T *item, *item2;
15888 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015889 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015890 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015891 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015892 dict_T *d;
15893 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015894 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015895
Bram Moolenaar8c711452005-01-14 21:53:12 +000015896 if (argvars[0].v_type == VAR_DICT)
15897 {
15898 if (argvars[2].v_type != VAR_UNKNOWN)
15899 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015900 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015901 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015903 key = get_tv_string_chk(&argvars[1]);
15904 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015906 di = dict_find(d, key, -1);
15907 if (di == NULL)
15908 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015909 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15910 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015911 {
15912 *rettv = di->di_tv;
15913 init_tv(&di->di_tv);
15914 dictitem_remove(d, di);
15915 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015917 }
15918 }
15919 else if (argvars[0].v_type != VAR_LIST)
15920 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015921 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015922 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 int error = FALSE;
15925
15926 idx = get_tv_number_chk(&argvars[1], &error);
15927 if (error)
15928 ; /* type error: do nothing, errmsg already given */
15929 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 EMSGN(_(e_listidx), idx);
15931 else
15932 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015933 if (argvars[2].v_type == VAR_UNKNOWN)
15934 {
15935 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015936 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015937 *rettv = item->li_tv;
15938 vim_free(item);
15939 }
15940 else
15941 {
15942 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015943 end = get_tv_number_chk(&argvars[2], &error);
15944 if (error)
15945 ; /* type error: do nothing */
15946 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015947 EMSGN(_(e_listidx), end);
15948 else
15949 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015950 int cnt = 0;
15951
15952 for (li = item; li != NULL; li = li->li_next)
15953 {
15954 ++cnt;
15955 if (li == item2)
15956 break;
15957 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015958 if (li == NULL) /* didn't find "item2" after "item" */
15959 EMSG(_(e_invrange));
15960 else
15961 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015962 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015964 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015965 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015966 l->lv_first = item;
15967 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015968 item->li_prev = NULL;
15969 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015970 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015971 }
15972 }
15973 }
15974 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015975 }
15976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977}
15978
15979/*
15980 * "rename({from}, {to})" function
15981 */
15982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015983f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984{
15985 char_u buf[NUMBUFLEN];
15986
15987 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015988 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015990 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15991 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992}
15993
15994/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015995 * "repeat()" function
15996 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015998f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015999{
16000 char_u *p;
16001 int n;
16002 int slen;
16003 int len;
16004 char_u *r;
16005 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016006
16007 n = get_tv_number(&argvars[1]);
16008 if (argvars[0].v_type == VAR_LIST)
16009 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016010 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016011 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016012 if (list_extend(rettv->vval.v_list,
16013 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016014 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016015 }
16016 else
16017 {
16018 p = get_tv_string(&argvars[0]);
16019 rettv->v_type = VAR_STRING;
16020 rettv->vval.v_string = NULL;
16021
16022 slen = (int)STRLEN(p);
16023 len = slen * n;
16024 if (len <= 0)
16025 return;
16026
16027 r = alloc(len + 1);
16028 if (r != NULL)
16029 {
16030 for (i = 0; i < n; i++)
16031 mch_memmove(r + i * slen, p, (size_t)slen);
16032 r[len] = NUL;
16033 }
16034
16035 rettv->vval.v_string = r;
16036 }
16037}
16038
16039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 * "resolve()" function
16041 */
16042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016043f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044{
16045 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016046#ifdef HAVE_READLINK
16047 char_u *buf = NULL;
16048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016050 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051#ifdef FEAT_SHORTCUT
16052 {
16053 char_u *v = NULL;
16054
16055 v = mch_resolve_shortcut(p);
16056 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016057 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016059 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 }
16061#else
16062# ifdef HAVE_READLINK
16063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 char_u *cpy;
16065 int len;
16066 char_u *remain = NULL;
16067 char_u *q;
16068 int is_relative_to_current = FALSE;
16069 int has_trailing_pathsep = FALSE;
16070 int limit = 100;
16071
16072 p = vim_strsave(p);
16073
16074 if (p[0] == '.' && (vim_ispathsep(p[1])
16075 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16076 is_relative_to_current = TRUE;
16077
16078 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016079 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016082 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084
16085 q = getnextcomp(p);
16086 if (*q != NUL)
16087 {
16088 /* Separate the first path component in "p", and keep the
16089 * remainder (beginning with the path separator). */
16090 remain = vim_strsave(q - 1);
16091 q[-1] = NUL;
16092 }
16093
Bram Moolenaard9462e32011-04-11 21:35:11 +020016094 buf = alloc(MAXPATHL + 1);
16095 if (buf == NULL)
16096 goto fail;
16097
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 for (;;)
16099 {
16100 for (;;)
16101 {
16102 len = readlink((char *)p, (char *)buf, MAXPATHL);
16103 if (len <= 0)
16104 break;
16105 buf[len] = NUL;
16106
16107 if (limit-- == 0)
16108 {
16109 vim_free(p);
16110 vim_free(remain);
16111 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 goto fail;
16114 }
16115
16116 /* Ensure that the result will have a trailing path separator
16117 * if the argument has one. */
16118 if (remain == NULL && has_trailing_pathsep)
16119 add_pathsep(buf);
16120
16121 /* Separate the first path component in the link value and
16122 * concatenate the remainders. */
16123 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16124 if (*q != NUL)
16125 {
16126 if (remain == NULL)
16127 remain = vim_strsave(q - 1);
16128 else
16129 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016130 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 if (cpy != NULL)
16132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 vim_free(remain);
16134 remain = cpy;
16135 }
16136 }
16137 q[-1] = NUL;
16138 }
16139
16140 q = gettail(p);
16141 if (q > p && *q == NUL)
16142 {
16143 /* Ignore trailing path separator. */
16144 q[-1] = NUL;
16145 q = gettail(p);
16146 }
16147 if (q > p && !mch_isFullName(buf))
16148 {
16149 /* symlink is relative to directory of argument */
16150 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16151 if (cpy != NULL)
16152 {
16153 STRCPY(cpy, p);
16154 STRCPY(gettail(cpy), buf);
16155 vim_free(p);
16156 p = cpy;
16157 }
16158 }
16159 else
16160 {
16161 vim_free(p);
16162 p = vim_strsave(buf);
16163 }
16164 }
16165
16166 if (remain == NULL)
16167 break;
16168
16169 /* Append the first path component of "remain" to "p". */
16170 q = getnextcomp(remain + 1);
16171 len = q - remain - (*q != NUL);
16172 cpy = vim_strnsave(p, STRLEN(p) + len);
16173 if (cpy != NULL)
16174 {
16175 STRNCAT(cpy, remain, len);
16176 vim_free(p);
16177 p = cpy;
16178 }
16179 /* Shorten "remain". */
16180 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016181 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 else
16183 {
16184 vim_free(remain);
16185 remain = NULL;
16186 }
16187 }
16188
16189 /* If the result is a relative path name, make it explicitly relative to
16190 * the current directory if and only if the argument had this form. */
16191 if (!vim_ispathsep(*p))
16192 {
16193 if (is_relative_to_current
16194 && *p != NUL
16195 && !(p[0] == '.'
16196 && (p[1] == NUL
16197 || vim_ispathsep(p[1])
16198 || (p[1] == '.'
16199 && (p[2] == NUL
16200 || vim_ispathsep(p[2]))))))
16201 {
16202 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016203 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 if (cpy != NULL)
16205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 vim_free(p);
16207 p = cpy;
16208 }
16209 }
16210 else if (!is_relative_to_current)
16211 {
16212 /* Strip leading "./". */
16213 q = p;
16214 while (q[0] == '.' && vim_ispathsep(q[1]))
16215 q += 2;
16216 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016217 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 }
16219 }
16220
16221 /* Ensure that the result will have no trailing path separator
16222 * if the argument had none. But keep "/" or "//". */
16223 if (!has_trailing_pathsep)
16224 {
16225 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016226 if (after_pathsep(p, q))
16227 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 }
16229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016230 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 }
16232# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016233 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234# endif
16235#endif
16236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016237 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238
16239#ifdef HAVE_READLINK
16240fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016241 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244}
16245
16246/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 */
16249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016250f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251{
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 list_T *l;
16253 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 if (argvars[0].v_type != VAR_LIST)
16256 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016257 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016258 && !tv_check_lock(l->lv_lock,
16259 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 {
16261 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016262 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016263 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264 while (li != NULL)
16265 {
16266 ni = li->li_prev;
16267 list_append(l, li);
16268 li = ni;
16269 }
16270 rettv->vval.v_list = l;
16271 rettv->v_type = VAR_LIST;
16272 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016273 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275}
16276
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016277#define SP_NOMOVE 0x01 /* don't move cursor */
16278#define SP_REPEAT 0x02 /* repeat to find outer pair */
16279#define SP_RETCOUNT 0x04 /* return matchcount */
16280#define SP_SETPCMARK 0x08 /* set previous context mark */
16281#define SP_START 0x10 /* accept match at start position */
16282#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16283#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016284#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016285
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016286static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287
16288/*
16289 * Get flags for a search function.
16290 * Possibly sets "p_ws".
16291 * Returns BACKWARD, FORWARD or zero (for an error).
16292 */
16293 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016294get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295{
16296 int dir = FORWARD;
16297 char_u *flags;
16298 char_u nbuf[NUMBUFLEN];
16299 int mask;
16300
16301 if (varp->v_type != VAR_UNKNOWN)
16302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016303 flags = get_tv_string_buf_chk(varp, nbuf);
16304 if (flags == NULL)
16305 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306 while (*flags != NUL)
16307 {
16308 switch (*flags)
16309 {
16310 case 'b': dir = BACKWARD; break;
16311 case 'w': p_ws = TRUE; break;
16312 case 'W': p_ws = FALSE; break;
16313 default: mask = 0;
16314 if (flagsp != NULL)
16315 switch (*flags)
16316 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016317 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016318 case 'e': mask = SP_END; break;
16319 case 'm': mask = SP_RETCOUNT; break;
16320 case 'n': mask = SP_NOMOVE; break;
16321 case 'p': mask = SP_SUBPAT; break;
16322 case 'r': mask = SP_REPEAT; break;
16323 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016324 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325 }
16326 if (mask == 0)
16327 {
16328 EMSG2(_(e_invarg2), flags);
16329 dir = 0;
16330 }
16331 else
16332 *flagsp |= mask;
16333 }
16334 if (dir == 0)
16335 break;
16336 ++flags;
16337 }
16338 }
16339 return dir;
16340}
16341
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016343 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016346search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016348 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349 char_u *pat;
16350 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016351 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 int save_p_ws = p_ws;
16353 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016354 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016355 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016356 proftime_T tm;
16357#ifdef FEAT_RELTIME
16358 long time_limit = 0;
16359#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016360 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016361 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016363 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016364 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016365 if (dir == 0)
16366 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016367 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016368 if (flags & SP_START)
16369 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016370 if (flags & SP_END)
16371 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016372 if (flags & SP_COLUMN)
16373 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016374
Bram Moolenaar76929292008-01-06 19:07:36 +000016375 /* Optional arguments: line number to stop searching and timeout. */
16376 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016377 {
16378 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16379 if (lnum_stop < 0)
16380 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016381#ifdef FEAT_RELTIME
16382 if (argvars[3].v_type != VAR_UNKNOWN)
16383 {
16384 time_limit = get_tv_number_chk(&argvars[3], NULL);
16385 if (time_limit < 0)
16386 goto theend;
16387 }
16388#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016389 }
16390
Bram Moolenaar76929292008-01-06 19:07:36 +000016391#ifdef FEAT_RELTIME
16392 /* Set the time limit, if there is one. */
16393 profile_setlimit(time_limit, &tm);
16394#endif
16395
Bram Moolenaar231334e2005-07-25 20:46:57 +000016396 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016397 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016398 * Check to make sure only those flags are set.
16399 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16400 * flags cannot be set. Check for that condition also.
16401 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016402 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016403 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016405 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016406 goto theend;
16407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016409 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016410 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016411 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016412 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016414 if (flags & SP_SUBPAT)
16415 retval = subpatnum;
16416 else
16417 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016418 if (flags & SP_SETPCMARK)
16419 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016421 if (match_pos != NULL)
16422 {
16423 /* Store the match cursor position */
16424 match_pos->lnum = pos.lnum;
16425 match_pos->col = pos.col + 1;
16426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 /* "/$" will put the cursor after the end of the line, may need to
16428 * correct that here */
16429 check_cursor();
16430 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016431
16432 /* If 'n' flag is used: restore cursor position. */
16433 if (flags & SP_NOMOVE)
16434 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016435 else
16436 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016437theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016439
16440 return retval;
16441}
16442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016443#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016444
16445/*
16446 * round() is not in C90, use ceil() or floor() instead.
16447 */
16448 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016449vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016450{
16451 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16452}
16453
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016454/*
16455 * "round({float})" function
16456 */
16457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016458f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016459{
16460 float_T f;
16461
16462 rettv->v_type = VAR_FLOAT;
16463 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016464 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016465 else
16466 rettv->vval.v_float = 0.0;
16467}
16468#endif
16469
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016470/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016471 * "screenattr()" function
16472 */
16473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016474f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016475{
16476 int row;
16477 int col;
16478 int c;
16479
16480 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16481 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16482 if (row < 0 || row >= screen_Rows
16483 || col < 0 || col >= screen_Columns)
16484 c = -1;
16485 else
16486 c = ScreenAttrs[LineOffset[row] + col];
16487 rettv->vval.v_number = c;
16488}
16489
16490/*
16491 * "screenchar()" function
16492 */
16493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016494f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016495{
16496 int row;
16497 int col;
16498 int off;
16499 int c;
16500
16501 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16502 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16503 if (row < 0 || row >= screen_Rows
16504 || col < 0 || col >= screen_Columns)
16505 c = -1;
16506 else
16507 {
16508 off = LineOffset[row] + col;
16509#ifdef FEAT_MBYTE
16510 if (enc_utf8 && ScreenLinesUC[off] != 0)
16511 c = ScreenLinesUC[off];
16512 else
16513#endif
16514 c = ScreenLines[off];
16515 }
16516 rettv->vval.v_number = c;
16517}
16518
16519/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016520 * "screencol()" function
16521 *
16522 * First column is 1 to be consistent with virtcol().
16523 */
16524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016525f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016526{
16527 rettv->vval.v_number = screen_screencol() + 1;
16528}
16529
16530/*
16531 * "screenrow()" function
16532 */
16533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016534f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016535{
16536 rettv->vval.v_number = screen_screenrow() + 1;
16537}
16538
16539/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016540 * "search()" function
16541 */
16542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016543f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016544{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016545 int flags = 0;
16546
16547 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548}
16549
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016551 * "searchdecl()" function
16552 */
16553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016554f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016555{
16556 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016557 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016558 int error = FALSE;
16559 char_u *name;
16560
16561 rettv->vval.v_number = 1; /* default: FAIL */
16562
16563 name = get_tv_string_chk(&argvars[0]);
16564 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016565 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016566 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016567 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16568 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16569 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016570 if (!error && name != NULL)
16571 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016572 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016573}
16574
16575/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016576 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016579searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580{
16581 char_u *spat, *mpat, *epat;
16582 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 int dir;
16585 int flags = 0;
16586 char_u nbuf1[NUMBUFLEN];
16587 char_u nbuf2[NUMBUFLEN];
16588 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016589 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016590 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016591 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016594 spat = get_tv_string_chk(&argvars[0]);
16595 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16596 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16597 if (spat == NULL || mpat == NULL || epat == NULL)
16598 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 /* Handle the optional fourth argument: flags */
16601 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016602 if (dir == 0)
16603 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016604
16605 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016606 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16607 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016608 if ((flags & (SP_END | SP_SUBPAT)) != 0
16609 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016610 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016611 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016612 goto theend;
16613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016615 /* Using 'r' implies 'W', otherwise it doesn't work. */
16616 if (flags & SP_REPEAT)
16617 p_ws = FALSE;
16618
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016619 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016620 if (argvars[3].v_type == VAR_UNKNOWN
16621 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 skip = (char_u *)"";
16623 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016625 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016626 if (argvars[5].v_type != VAR_UNKNOWN)
16627 {
16628 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16629 if (lnum_stop < 0)
16630 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016631#ifdef FEAT_RELTIME
16632 if (argvars[6].v_type != VAR_UNKNOWN)
16633 {
16634 time_limit = get_tv_number_chk(&argvars[6], NULL);
16635 if (time_limit < 0)
16636 goto theend;
16637 }
16638#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016639 }
16640 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016641 if (skip == NULL)
16642 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016644 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016645 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016646
16647theend:
16648 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016649
16650 return retval;
16651}
16652
16653/*
16654 * "searchpair()" function
16655 */
16656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016657f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658{
16659 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16660}
16661
16662/*
16663 * "searchpairpos()" function
16664 */
16665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016666f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016667{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016668 pos_T match_pos;
16669 int lnum = 0;
16670 int col = 0;
16671
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016672 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016673 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016674
16675 if (searchpair_cmn(argvars, &match_pos) > 0)
16676 {
16677 lnum = match_pos.lnum;
16678 col = match_pos.col;
16679 }
16680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016681 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16682 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016683}
16684
16685/*
16686 * Search for a start/middle/end thing.
16687 * Used by searchpair(), see its documentation for the details.
16688 * Returns 0 or -1 for no match,
16689 */
16690 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016691do_searchpair(
16692 char_u *spat, /* start pattern */
16693 char_u *mpat, /* middle pattern */
16694 char_u *epat, /* end pattern */
16695 int dir, /* BACKWARD or FORWARD */
16696 char_u *skip, /* skip expression */
16697 int flags, /* SP_SETPCMARK and other SP_ values */
16698 pos_T *match_pos,
16699 linenr_T lnum_stop, /* stop at this line if not zero */
16700 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016701{
16702 char_u *save_cpo;
16703 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16704 long retval = 0;
16705 pos_T pos;
16706 pos_T firstpos;
16707 pos_T foundpos;
16708 pos_T save_cursor;
16709 pos_T save_pos;
16710 int n;
16711 int r;
16712 int nest = 1;
16713 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016714 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016715 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016716
16717 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16718 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016719 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016720
Bram Moolenaar76929292008-01-06 19:07:36 +000016721#ifdef FEAT_RELTIME
16722 /* Set the time limit, if there is one. */
16723 profile_setlimit(time_limit, &tm);
16724#endif
16725
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016726 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16727 * start/middle/end (pat3, for the top pair). */
16728 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16729 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16730 if (pat2 == NULL || pat3 == NULL)
16731 goto theend;
16732 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16733 if (*mpat == NUL)
16734 STRCPY(pat3, pat2);
16735 else
16736 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16737 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016738 if (flags & SP_START)
16739 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016740
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 save_cursor = curwin->w_cursor;
16742 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016743 clearpos(&firstpos);
16744 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 pat = pat3;
16746 for (;;)
16747 {
16748 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016749 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16751 /* didn't find it or found the first match again: FAIL */
16752 break;
16753
16754 if (firstpos.lnum == 0)
16755 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016756 if (equalpos(pos, foundpos))
16757 {
16758 /* Found the same position again. Can happen with a pattern that
16759 * has "\zs" at the end and searching backwards. Advance one
16760 * character and try again. */
16761 if (dir == BACKWARD)
16762 decl(&pos);
16763 else
16764 incl(&pos);
16765 }
16766 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016768 /* clear the start flag to avoid getting stuck here */
16769 options &= ~SEARCH_START;
16770
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 /* If the skip pattern matches, ignore this match. */
16772 if (*skip != NUL)
16773 {
16774 save_pos = curwin->w_cursor;
16775 curwin->w_cursor = pos;
16776 r = eval_to_bool(skip, &err, NULL, FALSE);
16777 curwin->w_cursor = save_pos;
16778 if (err)
16779 {
16780 /* Evaluating {skip} caused an error, break here. */
16781 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016782 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 break;
16784 }
16785 if (r)
16786 continue;
16787 }
16788
16789 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16790 {
16791 /* Found end when searching backwards or start when searching
16792 * forward: nested pair. */
16793 ++nest;
16794 pat = pat2; /* nested, don't search for middle */
16795 }
16796 else
16797 {
16798 /* Found end when searching forward or start when searching
16799 * backward: end of (nested) pair; or found middle in outer pair. */
16800 if (--nest == 1)
16801 pat = pat3; /* outer level, search for middle */
16802 }
16803
16804 if (nest == 0)
16805 {
16806 /* Found the match: return matchcount or line number. */
16807 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016808 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016810 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016811 if (flags & SP_SETPCMARK)
16812 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 curwin->w_cursor = pos;
16814 if (!(flags & SP_REPEAT))
16815 break;
16816 nest = 1; /* search for next unmatched */
16817 }
16818 }
16819
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016820 if (match_pos != NULL)
16821 {
16822 /* Store the match cursor position */
16823 match_pos->lnum = curwin->w_cursor.lnum;
16824 match_pos->col = curwin->w_cursor.col + 1;
16825 }
16826
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016828 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 curwin->w_cursor = save_cursor;
16830
16831theend:
16832 vim_free(pat2);
16833 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016834 if (p_cpo == empty_option)
16835 p_cpo = save_cpo;
16836 else
16837 /* Darn, evaluating the {skip} expression changed the value. */
16838 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016839
16840 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841}
16842
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016843/*
16844 * "searchpos()" function
16845 */
16846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016847f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016848{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016849 pos_T match_pos;
16850 int lnum = 0;
16851 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016852 int n;
16853 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016854
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016855 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016856 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016857
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016858 n = search_cmn(argvars, &match_pos, &flags);
16859 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016860 {
16861 lnum = match_pos.lnum;
16862 col = match_pos.col;
16863 }
16864
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016865 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16866 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016867 if (flags & SP_SUBPAT)
16868 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016869}
16870
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016871#ifdef FEAT_CHANNEL
16872/*
16873 * common for "sendexpr()" and "sendraw()"
16874 * Returns the channel index if the caller should read the response.
16875 * Otherwise returns -1.
16876 */
16877 static int
16878send_common(typval_T *argvars, char_u *text, char *fun)
16879{
16880 int ch_idx;
16881 char_u *callback = NULL;
16882
16883 ch_idx = get_channel_arg(&argvars[0]);
16884 if (ch_idx < 0)
16885 return -1;
16886
16887 if (argvars[2].v_type != VAR_UNKNOWN)
16888 {
16889 callback = get_callback(&argvars[2]);
16890 if (callback == NULL)
16891 return -1;
16892 }
16893 /* Set the callback or clear it. An empty callback means no callback and
16894 * not reading the response. */
16895 channel_set_req_callback(ch_idx,
16896 callback != NULL && *callback == NUL ? NULL : callback);
16897 if (callback == NULL)
16898 channel_will_block(ch_idx);
16899
16900 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
16901 return ch_idx;
16902 return -1;
16903}
16904
16905/*
16906 * "sendexpr()" function
16907 */
16908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016909f_sendexpr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016910{
16911 char_u *text;
16912 char_u *resp;
16913 typval_T nrtv;
16914 typval_T listtv;
16915 int ch_idx;
16916
16917 /* return an empty string by default */
16918 rettv->v_type = VAR_STRING;
16919 rettv->vval.v_string = NULL;
16920
16921 nrtv.v_type = VAR_NUMBER;
16922 nrtv.vval.v_number = channel_get_id();
16923 if (rettv_list_alloc(&listtv) == FAIL)
16924 return;
16925 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
16926 || list_append_tv(listtv.vval.v_list, &argvars[1]) == FAIL)
16927 {
16928 list_unref(listtv.vval.v_list);
16929 return;
16930 }
16931
16932 text = json_encode(&listtv);
16933 list_unref(listtv.vval.v_list);
16934
16935 ch_idx = send_common(argvars, text, "sendexpr");
16936 if (ch_idx >= 0)
16937 {
16938 /* TODO: read until the whole JSON message is received */
16939 /* TODO: only use the message with the right message ID */
16940 resp = channel_read_block(ch_idx);
16941 if (resp != NULL)
16942 {
16943 channel_decode_json(resp, rettv);
16944 vim_free(resp);
16945 }
16946 }
16947}
16948
16949/*
16950 * "sendraw()" function
16951 */
16952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016953f_sendraw(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016954{
16955 char_u buf[NUMBUFLEN];
16956 char_u *text;
16957 int ch_idx;
16958
16959 /* return an empty string by default */
16960 rettv->v_type = VAR_STRING;
16961 rettv->vval.v_string = NULL;
16962
16963 text = get_tv_string_buf(&argvars[1], buf);
16964 ch_idx = send_common(argvars, text, "sendraw");
16965 if (ch_idx >= 0)
16966 rettv->vval.v_string = channel_read_block(ch_idx);
16967}
16968#endif
16969
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016970
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016972f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974#ifdef FEAT_CLIENTSERVER
16975 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016976 char_u *server = get_tv_string_chk(&argvars[0]);
16977 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016980 if (server == NULL || reply == NULL)
16981 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 if (check_restricted() || check_secure())
16983 return;
16984# ifdef FEAT_X11
16985 if (check_connection() == FAIL)
16986 return;
16987# endif
16988
16989 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 EMSG(_("E258: Unable to send to client"));
16992 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994 rettv->vval.v_number = 0;
16995#else
16996 rettv->vval.v_number = -1;
16997#endif
16998}
16999
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017001f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002{
17003 char_u *r = NULL;
17004
17005#ifdef FEAT_CLIENTSERVER
17006# ifdef WIN32
17007 r = serverGetVimNames();
17008# else
17009 make_connection();
17010 if (X_DISPLAY != NULL)
17011 r = serverGetVimNames(X_DISPLAY);
17012# endif
17013#endif
17014 rettv->v_type = VAR_STRING;
17015 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016}
17017
17018/*
17019 * "setbufvar()" function
17020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017022f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023{
17024 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017027 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 char_u nbuf[NUMBUFLEN];
17029
17030 if (check_restricted() || check_secure())
17031 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017032 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17033 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017034 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 varp = &argvars[2];
17036
17037 if (buf != NULL && varname != NULL && varp != NULL)
17038 {
17039 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041
17042 if (*varname == '&')
17043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017044 long numval;
17045 char_u *strval;
17046 int error = FALSE;
17047
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017049 numval = get_tv_number_chk(varp, &error);
17050 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 if (!error && strval != NULL)
17052 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 }
17054 else
17055 {
17056 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17057 if (bufvarname != NULL)
17058 {
17059 STRCPY(bufvarname, "b:");
17060 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017061 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 vim_free(bufvarname);
17063 }
17064 }
17065
17066 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069}
17070
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017072f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017073{
17074 dict_T *d;
17075 dictitem_T *di;
17076 char_u *csearch;
17077
17078 if (argvars[0].v_type != VAR_DICT)
17079 {
17080 EMSG(_(e_dictreq));
17081 return;
17082 }
17083
17084 if ((d = argvars[0].vval.v_dict) != NULL)
17085 {
17086 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17087 if (csearch != NULL)
17088 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017089#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017090 if (enc_utf8)
17091 {
17092 int pcc[MAX_MCO];
17093 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017094
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017095 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17096 }
17097 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017098#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017099 set_last_csearch(PTR2CHAR(csearch),
17100 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017101 }
17102
17103 di = dict_find(d, (char_u *)"forward", -1);
17104 if (di != NULL)
17105 set_csearch_direction(get_tv_number(&di->di_tv)
17106 ? FORWARD : BACKWARD);
17107
17108 di = dict_find(d, (char_u *)"until", -1);
17109 if (di != NULL)
17110 set_csearch_until(!!get_tv_number(&di->di_tv));
17111 }
17112}
17113
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114/*
17115 * "setcmdpos()" function
17116 */
17117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017118f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 int pos = (int)get_tv_number(&argvars[0]) - 1;
17121
17122 if (pos >= 0)
17123 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124}
17125
17126/*
17127 * "setline()" function
17128 */
17129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017130f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131{
17132 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017133 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017134 list_T *l = NULL;
17135 listitem_T *li = NULL;
17136 long added = 0;
17137 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017139 lnum = get_tv_lnum(&argvars[0]);
17140 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017142 l = argvars[1].vval.v_list;
17143 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017145 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017146 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017147
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017148 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017149 for (;;)
17150 {
17151 if (l != NULL)
17152 {
17153 /* list argument, get next string */
17154 if (li == NULL)
17155 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017157 li = li->li_next;
17158 }
17159
17160 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017161 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017162 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017163
17164 /* When coming here from Insert mode, sync undo, so that this can be
17165 * undone separately from what was previously inserted. */
17166 if (u_sync_once == 2)
17167 {
17168 u_sync_once = 1; /* notify that u_sync() was called */
17169 u_sync(TRUE);
17170 }
17171
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017172 if (lnum <= curbuf->b_ml.ml_line_count)
17173 {
17174 /* existing line, replace it */
17175 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17176 {
17177 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017178 if (lnum == curwin->w_cursor.lnum)
17179 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017180 rettv->vval.v_number = 0; /* OK */
17181 }
17182 }
17183 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17184 {
17185 /* lnum is one past the last line, append the line */
17186 ++added;
17187 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17188 rettv->vval.v_number = 0; /* OK */
17189 }
17190
17191 if (l == NULL) /* only one string argument */
17192 break;
17193 ++lnum;
17194 }
17195
17196 if (added > 0)
17197 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198}
17199
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017200static 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 +000017201
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017203 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017204 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017206set_qf_ll_list(
17207 win_T *wp UNUSED,
17208 typval_T *list_arg UNUSED,
17209 typval_T *action_arg UNUSED,
17210 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017211{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017212#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017213 char_u *act;
17214 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017215#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017216
Bram Moolenaar2641f772005-03-25 21:58:17 +000017217 rettv->vval.v_number = -1;
17218
17219#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017220 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017221 EMSG(_(e_listreq));
17222 else
17223 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017224 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017225
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017226 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017227 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017228 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017229 if (act == NULL)
17230 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017231 if (*act == 'a' || *act == 'r')
17232 action = *act;
17233 }
17234
Bram Moolenaar81484f42012-12-05 15:16:47 +010017235 if (l != NULL && set_errorlist(wp, l, action,
17236 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017237 rettv->vval.v_number = 0;
17238 }
17239#endif
17240}
17241
17242/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017243 * "setloclist()" function
17244 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017246f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017247{
17248 win_T *win;
17249
17250 rettv->vval.v_number = -1;
17251
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017252 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017253 if (win != NULL)
17254 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17255}
17256
17257/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017258 * "setmatches()" function
17259 */
17260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017261f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017262{
17263#ifdef FEAT_SEARCH_EXTRA
17264 list_T *l;
17265 listitem_T *li;
17266 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017267 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017268
17269 rettv->vval.v_number = -1;
17270 if (argvars[0].v_type != VAR_LIST)
17271 {
17272 EMSG(_(e_listreq));
17273 return;
17274 }
17275 if ((l = argvars[0].vval.v_list) != NULL)
17276 {
17277
17278 /* To some extent make sure that we are dealing with a list from
17279 * "getmatches()". */
17280 li = l->lv_first;
17281 while (li != NULL)
17282 {
17283 if (li->li_tv.v_type != VAR_DICT
17284 || (d = li->li_tv.vval.v_dict) == NULL)
17285 {
17286 EMSG(_(e_invarg));
17287 return;
17288 }
17289 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017290 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17291 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017292 && dict_find(d, (char_u *)"priority", -1) != NULL
17293 && dict_find(d, (char_u *)"id", -1) != NULL))
17294 {
17295 EMSG(_(e_invarg));
17296 return;
17297 }
17298 li = li->li_next;
17299 }
17300
17301 clear_matches(curwin);
17302 li = l->lv_first;
17303 while (li != NULL)
17304 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017305 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017306 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017307 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017308 char_u *group;
17309 int priority;
17310 int id;
17311 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017312
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017313 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017314 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17315 {
17316 if (s == NULL)
17317 {
17318 s = list_alloc();
17319 if (s == NULL)
17320 return;
17321 }
17322
17323 /* match from matchaddpos() */
17324 for (i = 1; i < 9; i++)
17325 {
17326 sprintf((char *)buf, (char *)"pos%d", i);
17327 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17328 {
17329 if (di->di_tv.v_type != VAR_LIST)
17330 return;
17331
17332 list_append_tv(s, &di->di_tv);
17333 s->lv_refcount++;
17334 }
17335 else
17336 break;
17337 }
17338 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017339
17340 group = get_dict_string(d, (char_u *)"group", FALSE);
17341 priority = (int)get_dict_number(d, (char_u *)"priority");
17342 id = (int)get_dict_number(d, (char_u *)"id");
17343 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17344 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17345 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017346 if (i == 0)
17347 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017348 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017349 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017350 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017351 }
17352 else
17353 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017354 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017355 list_unref(s);
17356 s = NULL;
17357 }
17358
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017359 li = li->li_next;
17360 }
17361 rettv->vval.v_number = 0;
17362 }
17363#endif
17364}
17365
17366/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017367 * "setpos()" function
17368 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017370f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017371{
17372 pos_T pos;
17373 int fnum;
17374 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017375 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017376
Bram Moolenaar08250432008-02-13 11:42:46 +000017377 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017378 name = get_tv_string_chk(argvars);
17379 if (name != NULL)
17380 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017381 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017382 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017383 if (--pos.col < 0)
17384 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017385 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017386 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017387 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017388 if (fnum == curbuf->b_fnum)
17389 {
17390 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017391 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017392 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017393 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017394 curwin->w_set_curswant = FALSE;
17395 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017396 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017397 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017398 }
17399 else
17400 EMSG(_(e_invarg));
17401 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017402 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17403 {
17404 /* set mark */
17405 if (setmark_pos(name[1], &pos, fnum) == OK)
17406 rettv->vval.v_number = 0;
17407 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017408 else
17409 EMSG(_(e_invarg));
17410 }
17411 }
17412}
17413
17414/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017415 * "setqflist()" function
17416 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017418f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017419{
17420 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17421}
17422
17423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 * "setreg()" function
17425 */
17426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017427f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428{
17429 int regname;
17430 char_u *strregname;
17431 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017432 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 int append;
17434 char_u yank_type;
17435 long block_len;
17436
17437 block_len = -1;
17438 yank_type = MAUTO;
17439 append = FALSE;
17440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017442 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017444 if (strregname == NULL)
17445 return; /* type error; errmsg already given */
17446 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 if (regname == 0 || regname == '@')
17448 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017452 stropt = get_tv_string_chk(&argvars[2]);
17453 if (stropt == NULL)
17454 return; /* type error */
17455 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 switch (*stropt)
17457 {
17458 case 'a': case 'A': /* append */
17459 append = TRUE;
17460 break;
17461 case 'v': case 'c': /* character-wise selection */
17462 yank_type = MCHAR;
17463 break;
17464 case 'V': case 'l': /* line-wise selection */
17465 yank_type = MLINE;
17466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 case 'b': case Ctrl_V: /* block-wise selection */
17468 yank_type = MBLOCK;
17469 if (VIM_ISDIGIT(stropt[1]))
17470 {
17471 ++stropt;
17472 block_len = getdigits(&stropt) - 1;
17473 --stropt;
17474 }
17475 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 }
17477 }
17478
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017479 if (argvars[1].v_type == VAR_LIST)
17480 {
17481 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017482 char_u **allocval;
17483 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017484 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017485 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017486 int len = argvars[1].vval.v_list->lv_len;
17487 listitem_T *li;
17488
Bram Moolenaar7d647822014-04-05 21:28:56 +020017489 /* First half: use for pointers to result lines; second half: use for
17490 * pointers to allocated copies. */
17491 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017492 if (lstval == NULL)
17493 return;
17494 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017495 allocval = lstval + len + 2;
17496 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017497
17498 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17499 li = li->li_next)
17500 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017501 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017502 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017503 goto free_lstval;
17504 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017505 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017506 /* Need to make a copy, next get_tv_string_buf_chk() will
17507 * overwrite the string. */
17508 strval = vim_strsave(buf);
17509 if (strval == NULL)
17510 goto free_lstval;
17511 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017512 }
17513 *curval++ = strval;
17514 }
17515 *curval++ = NULL;
17516
17517 write_reg_contents_lst(regname, lstval, -1,
17518 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017519free_lstval:
17520 while (curallocval > allocval)
17521 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017522 vim_free(lstval);
17523 }
17524 else
17525 {
17526 strval = get_tv_string_chk(&argvars[1]);
17527 if (strval == NULL)
17528 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017529 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017531 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017532 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533}
17534
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017535/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017536 * "settabvar()" function
17537 */
17538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017539f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017540{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017541#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017542 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017543 tabpage_T *tp;
17544#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017545 char_u *varname, *tabvarname;
17546 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017547
17548 rettv->vval.v_number = 0;
17549
17550 if (check_restricted() || check_secure())
17551 return;
17552
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017553#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017554 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017555#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017556 varname = get_tv_string_chk(&argvars[1]);
17557 varp = &argvars[2];
17558
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017559 if (varname != NULL && varp != NULL
17560#ifdef FEAT_WINDOWS
17561 && tp != NULL
17562#endif
17563 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017564 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017565#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017566 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017567 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017568#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017569
17570 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17571 if (tabvarname != NULL)
17572 {
17573 STRCPY(tabvarname, "t:");
17574 STRCPY(tabvarname + 2, varname);
17575 set_var(tabvarname, varp, TRUE);
17576 vim_free(tabvarname);
17577 }
17578
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017579#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017580 /* Restore current tabpage */
17581 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017582 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017583#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017584 }
17585}
17586
17587/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017588 * "settabwinvar()" function
17589 */
17590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017591f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017592{
17593 setwinvar(argvars, rettv, 1);
17594}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595
17596/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017597 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017600f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017602 setwinvar(argvars, rettv, 0);
17603}
17604
17605/*
17606 * "setwinvar()" and "settabwinvar()" functions
17607 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017608
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017610setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017611{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 win_T *win;
17613#ifdef FEAT_WINDOWS
17614 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017615 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017616 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617#endif
17618 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017619 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017621 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622
17623 if (check_restricted() || check_secure())
17624 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017625
17626#ifdef FEAT_WINDOWS
17627 if (off == 1)
17628 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17629 else
17630 tp = curtab;
17631#endif
17632 win = find_win_by_nr(&argvars[off], tp);
17633 varname = get_tv_string_chk(&argvars[off + 1]);
17634 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635
17636 if (win != NULL && varname != NULL && varp != NULL)
17637 {
17638#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017639 need_switch_win = !(tp == curtab && win == curwin);
17640 if (!need_switch_win
17641 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017644 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017646 long numval;
17647 char_u *strval;
17648 int error = FALSE;
17649
17650 ++varname;
17651 numval = get_tv_number_chk(varp, &error);
17652 strval = get_tv_string_buf_chk(varp, nbuf);
17653 if (!error && strval != NULL)
17654 set_option_value(varname, numval, strval, OPT_LOCAL);
17655 }
17656 else
17657 {
17658 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17659 if (winvarname != NULL)
17660 {
17661 STRCPY(winvarname, "w:");
17662 STRCPY(winvarname + 2, varname);
17663 set_var(winvarname, varp, TRUE);
17664 vim_free(winvarname);
17665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 }
17667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017669 if (need_switch_win)
17670 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671#endif
17672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673}
17674
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017675#ifdef FEAT_CRYPT
17676/*
17677 * "sha256({string})" function
17678 */
17679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017680f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017681{
17682 char_u *p;
17683
17684 p = get_tv_string(&argvars[0]);
17685 rettv->vval.v_string = vim_strsave(
17686 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17687 rettv->v_type = VAR_STRING;
17688}
17689#endif /* FEAT_CRYPT */
17690
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017692 * "shellescape({string})" function
17693 */
17694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017695f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017696{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017697 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017698 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017699 rettv->v_type = VAR_STRING;
17700}
17701
17702/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017703 * shiftwidth() function
17704 */
17705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017706f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017707{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017708 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017709}
17710
17711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 */
17714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017715f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017717 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718
Bram Moolenaar0d660222005-01-07 21:51:51 +000017719 p = get_tv_string(&argvars[0]);
17720 rettv->vval.v_string = vim_strsave(p);
17721 simplify_filename(rettv->vval.v_string); /* simplify in place */
17722 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723}
17724
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017725#ifdef FEAT_FLOAT
17726/*
17727 * "sin()" function
17728 */
17729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017730f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017731{
17732 float_T f;
17733
17734 rettv->v_type = VAR_FLOAT;
17735 if (get_float_arg(argvars, &f) == OK)
17736 rettv->vval.v_float = sin(f);
17737 else
17738 rettv->vval.v_float = 0.0;
17739}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017740
17741/*
17742 * "sinh()" function
17743 */
17744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017745f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017746{
17747 float_T f;
17748
17749 rettv->v_type = VAR_FLOAT;
17750 if (get_float_arg(argvars, &f) == OK)
17751 rettv->vval.v_float = sinh(f);
17752 else
17753 rettv->vval.v_float = 0.0;
17754}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017755#endif
17756
Bram Moolenaar0d660222005-01-07 21:51:51 +000017757static int
17758#ifdef __BORLANDC__
17759 _RTLENTRYF
17760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017761 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017762static int
17763#ifdef __BORLANDC__
17764 _RTLENTRYF
17765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017766 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017767
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017768/* struct used in the array that's given to qsort() */
17769typedef struct
17770{
17771 listitem_T *item;
17772 int idx;
17773} sortItem_T;
17774
Bram Moolenaar0d660222005-01-07 21:51:51 +000017775static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017776static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017777static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017778#ifdef FEAT_FLOAT
17779static int item_compare_float;
17780#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017781static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017782static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017783static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017784static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017785static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017786#define ITEM_COMPARE_FAIL 999
17787
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017789 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017791 static int
17792#ifdef __BORLANDC__
17793_RTLENTRYF
17794#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017795item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017797 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017798 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017799 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017800 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017801 int res;
17802 char_u numbuf1[NUMBUFLEN];
17803 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017805 si1 = (sortItem_T *)s1;
17806 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017807 tv1 = &si1->item->li_tv;
17808 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017809
17810 if (item_compare_numbers)
17811 {
17812 long v1 = get_tv_number(tv1);
17813 long v2 = get_tv_number(tv2);
17814
17815 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17816 }
17817
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017818#ifdef FEAT_FLOAT
17819 if (item_compare_float)
17820 {
17821 float_T v1 = get_tv_float(tv1);
17822 float_T v2 = get_tv_float(tv2);
17823
17824 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17825 }
17826#endif
17827
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017828 /* tv2string() puts quotes around a string and allocates memory. Don't do
17829 * that for string variables. Use a single quote when comparing with a
17830 * non-string to do what the docs promise. */
17831 if (tv1->v_type == VAR_STRING)
17832 {
17833 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17834 p1 = (char_u *)"'";
17835 else
17836 p1 = tv1->vval.v_string;
17837 }
17838 else
17839 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17840 if (tv2->v_type == VAR_STRING)
17841 {
17842 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17843 p2 = (char_u *)"'";
17844 else
17845 p2 = tv2->vval.v_string;
17846 }
17847 else
17848 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017849 if (p1 == NULL)
17850 p1 = (char_u *)"";
17851 if (p2 == NULL)
17852 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017853 if (!item_compare_numeric)
17854 {
17855 if (item_compare_ic)
17856 res = STRICMP(p1, p2);
17857 else
17858 res = STRCMP(p1, p2);
17859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017861 {
17862 double n1, n2;
17863 n1 = strtod((char *)p1, (char **)&p1);
17864 n2 = strtod((char *)p2, (char **)&p2);
17865 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17866 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017867
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017868 /* When the result would be zero, compare the item indexes. Makes the
17869 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017870 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017871 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017872
Bram Moolenaar0d660222005-01-07 21:51:51 +000017873 vim_free(tofree1);
17874 vim_free(tofree2);
17875 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876}
17877
17878 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879#ifdef __BORLANDC__
17880_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017882item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017883{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017884 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017885 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017886 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017887 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017888 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017890 /* shortcut after failure in previous call; compare all items equal */
17891 if (item_compare_func_err)
17892 return 0;
17893
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017894 si1 = (sortItem_T *)s1;
17895 si2 = (sortItem_T *)s2;
17896
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017897 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017898 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017899 copy_tv(&si1->item->li_tv, &argv[0]);
17900 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017901
17902 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017903 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017904 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17905 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906 clear_tv(&argv[0]);
17907 clear_tv(&argv[1]);
17908
17909 if (res == FAIL)
17910 res = ITEM_COMPARE_FAIL;
17911 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017912 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17913 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017914 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017915 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017916
17917 /* When the result would be zero, compare the pointers themselves. Makes
17918 * the sort stable. */
17919 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017920 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017921
Bram Moolenaar0d660222005-01-07 21:51:51 +000017922 return res;
17923}
17924
17925/*
17926 * "sort({list})" function
17927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017929do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930{
Bram Moolenaar33570922005-01-25 22:26:29 +000017931 list_T *l;
17932 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017933 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017934 long len;
17935 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936
Bram Moolenaar0d660222005-01-07 21:51:51 +000017937 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017938 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 else
17940 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017941 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017942 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017943 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17944 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017945 return;
17946 rettv->vval.v_list = l;
17947 rettv->v_type = VAR_LIST;
17948 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949
Bram Moolenaar0d660222005-01-07 21:51:51 +000017950 len = list_len(l);
17951 if (len <= 1)
17952 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953
Bram Moolenaar0d660222005-01-07 21:51:51 +000017954 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017955 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017956 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017957#ifdef FEAT_FLOAT
17958 item_compare_float = FALSE;
17959#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017961 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017962 if (argvars[1].v_type != VAR_UNKNOWN)
17963 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017964 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017965 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017966 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017967 else
17968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017969 int error = FALSE;
17970
17971 i = get_tv_number_chk(&argvars[1], &error);
17972 if (error)
17973 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017974 if (i == 1)
17975 item_compare_ic = TRUE;
17976 else
17977 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017978 if (item_compare_func != NULL)
17979 {
17980 if (STRCMP(item_compare_func, "n") == 0)
17981 {
17982 item_compare_func = NULL;
17983 item_compare_numeric = TRUE;
17984 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017985 else if (STRCMP(item_compare_func, "N") == 0)
17986 {
17987 item_compare_func = NULL;
17988 item_compare_numbers = TRUE;
17989 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017990#ifdef FEAT_FLOAT
17991 else if (STRCMP(item_compare_func, "f") == 0)
17992 {
17993 item_compare_func = NULL;
17994 item_compare_float = TRUE;
17995 }
17996#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017997 else if (STRCMP(item_compare_func, "i") == 0)
17998 {
17999 item_compare_func = NULL;
18000 item_compare_ic = TRUE;
18001 }
18002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018003 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018004
18005 if (argvars[2].v_type != VAR_UNKNOWN)
18006 {
18007 /* optional third argument: {dict} */
18008 if (argvars[2].v_type != VAR_DICT)
18009 {
18010 EMSG(_(e_dictreq));
18011 return;
18012 }
18013 item_compare_selfdict = argvars[2].vval.v_dict;
18014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018018 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018019 if (ptrs == NULL)
18020 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021
Bram Moolenaar327aa022014-03-25 18:24:23 +010018022 i = 0;
18023 if (sort)
18024 {
18025 /* sort(): ptrs will be the list to sort */
18026 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018027 {
18028 ptrs[i].item = li;
18029 ptrs[i].idx = i;
18030 ++i;
18031 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018032
18033 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018034 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018035 /* test the compare function */
18036 if (item_compare_func != NULL
18037 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018038 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018039 EMSG(_("E702: Sort compare function failed"));
18040 else
18041 {
18042 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018043 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018044 item_compare_func == NULL ? item_compare : item_compare2);
18045
18046 if (!item_compare_func_err)
18047 {
18048 /* Clear the List and append the items in sorted order. */
18049 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18050 l->lv_len = 0;
18051 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018052 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018053 }
18054 }
18055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018057 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018058 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018059
18060 /* f_uniq(): ptrs will be a stack of items to remove */
18061 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018062 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018063 item_compare_func_ptr = item_compare_func
18064 ? item_compare2 : item_compare;
18065
18066 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18067 li = li->li_next)
18068 {
18069 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18070 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018071 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018072 if (item_compare_func_err)
18073 {
18074 EMSG(_("E882: Uniq compare function failed"));
18075 break;
18076 }
18077 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018079 if (!item_compare_func_err)
18080 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018081 while (--i >= 0)
18082 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018083 li = ptrs[i].item->li_next;
18084 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018085 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018086 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018087 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018088 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018089 list_fix_watch(l, li);
18090 listitem_free(li);
18091 l->lv_len--;
18092 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018093 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094 }
18095
18096 vim_free(ptrs);
18097 }
18098}
18099
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018100/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018101 * "sort({list})" function
18102 */
18103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018104f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018105{
18106 do_sort_uniq(argvars, rettv, TRUE);
18107}
18108
18109/*
18110 * "uniq({list})" function
18111 */
18112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018113f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018114{
18115 do_sort_uniq(argvars, rettv, FALSE);
18116}
18117
18118/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018119 * "soundfold({word})" function
18120 */
18121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018122f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018123{
18124 char_u *s;
18125
18126 rettv->v_type = VAR_STRING;
18127 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018128#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018129 rettv->vval.v_string = eval_soundfold(s);
18130#else
18131 rettv->vval.v_string = vim_strsave(s);
18132#endif
18133}
18134
18135/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018136 * "spellbadword()" function
18137 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018139f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018140{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018141 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018142 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018143 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018144
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018145 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018146 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018147
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018148#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018149 if (argvars[0].v_type == VAR_UNKNOWN)
18150 {
18151 /* Find the start and length of the badly spelled word. */
18152 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18153 if (len != 0)
18154 word = ml_get_cursor();
18155 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018156 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018157 {
18158 char_u *str = get_tv_string_chk(&argvars[0]);
18159 int capcol = -1;
18160
18161 if (str != NULL)
18162 {
18163 /* Check the argument for spelling. */
18164 while (*str != NUL)
18165 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018166 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018167 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018168 {
18169 word = str;
18170 break;
18171 }
18172 str += len;
18173 }
18174 }
18175 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018176#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018177
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018178 list_append_string(rettv->vval.v_list, word, len);
18179 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018180 attr == HLF_SPB ? "bad" :
18181 attr == HLF_SPR ? "rare" :
18182 attr == HLF_SPL ? "local" :
18183 attr == HLF_SPC ? "caps" :
18184 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018185}
18186
18187/*
18188 * "spellsuggest()" function
18189 */
18190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018191f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018192{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018193#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018194 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018195 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018196 int maxcount;
18197 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018198 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018199 listitem_T *li;
18200 int need_capital = FALSE;
18201#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018202
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018203 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018204 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018205
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018206#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018207 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018208 {
18209 str = get_tv_string(&argvars[0]);
18210 if (argvars[1].v_type != VAR_UNKNOWN)
18211 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018212 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018213 if (maxcount <= 0)
18214 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018215 if (argvars[2].v_type != VAR_UNKNOWN)
18216 {
18217 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18218 if (typeerr)
18219 return;
18220 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018221 }
18222 else
18223 maxcount = 25;
18224
Bram Moolenaar4770d092006-01-12 23:22:24 +000018225 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018226
18227 for (i = 0; i < ga.ga_len; ++i)
18228 {
18229 str = ((char_u **)ga.ga_data)[i];
18230
18231 li = listitem_alloc();
18232 if (li == NULL)
18233 vim_free(str);
18234 else
18235 {
18236 li->li_tv.v_type = VAR_STRING;
18237 li->li_tv.v_lock = 0;
18238 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018239 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018240 }
18241 }
18242 ga_clear(&ga);
18243 }
18244#endif
18245}
18246
Bram Moolenaar0d660222005-01-07 21:51:51 +000018247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018248f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018249{
18250 char_u *str;
18251 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018252 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253 regmatch_T regmatch;
18254 char_u patbuf[NUMBUFLEN];
18255 char_u *save_cpo;
18256 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018257 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018258 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018259 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018260
18261 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18262 save_cpo = p_cpo;
18263 p_cpo = (char_u *)"";
18264
18265 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018266 if (argvars[1].v_type != VAR_UNKNOWN)
18267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018268 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18269 if (pat == NULL)
18270 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018271 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018273 }
18274 if (pat == NULL || *pat == NUL)
18275 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018277 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018279 if (typeerr)
18280 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18283 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018286 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018288 if (*str == NUL)
18289 match = FALSE; /* empty item at the end */
18290 else
18291 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018292 if (match)
18293 end = regmatch.startp[0];
18294 else
18295 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018296 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18297 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018298 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018299 if (list_append_string(rettv->vval.v_list, str,
18300 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018301 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018302 }
18303 if (!match)
18304 break;
18305 /* Advance to just after the match. */
18306 if (regmatch.endp[0] > str)
18307 col = 0;
18308 else
18309 {
18310 /* Don't get stuck at the same match. */
18311#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018312 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018313#else
18314 col = 1;
18315#endif
18316 }
18317 str = regmatch.endp[0];
18318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319
Bram Moolenaar473de612013-06-08 18:19:48 +020018320 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324}
18325
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018326#ifdef FEAT_FLOAT
18327/*
18328 * "sqrt()" function
18329 */
18330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018331f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018332{
18333 float_T f;
18334
18335 rettv->v_type = VAR_FLOAT;
18336 if (get_float_arg(argvars, &f) == OK)
18337 rettv->vval.v_float = sqrt(f);
18338 else
18339 rettv->vval.v_float = 0.0;
18340}
18341
18342/*
18343 * "str2float()" function
18344 */
18345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018346f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018347{
18348 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18349
18350 if (*p == '+')
18351 p = skipwhite(p + 1);
18352 (void)string2float(p, &rettv->vval.v_float);
18353 rettv->v_type = VAR_FLOAT;
18354}
18355#endif
18356
Bram Moolenaar2c932302006-03-18 21:42:09 +000018357/*
18358 * "str2nr()" function
18359 */
18360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018361f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018362{
18363 int base = 10;
18364 char_u *p;
18365 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018366 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018367
18368 if (argvars[1].v_type != VAR_UNKNOWN)
18369 {
18370 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018371 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018372 {
18373 EMSG(_(e_invarg));
18374 return;
18375 }
18376 }
18377
18378 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018379 if (*p == '+')
18380 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018381 switch (base)
18382 {
18383 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18384 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18385 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18386 default: what = 0;
18387 }
18388 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018389 rettv->vval.v_number = n;
18390}
18391
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392#ifdef HAVE_STRFTIME
18393/*
18394 * "strftime({format}[, {time}])" function
18395 */
18396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018397f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398{
18399 char_u result_buf[256];
18400 struct tm *curtime;
18401 time_t seconds;
18402 char_u *p;
18403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018404 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018406 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018407 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 seconds = time(NULL);
18409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018410 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 curtime = localtime(&seconds);
18412 /* MSVC returns NULL for an invalid value of seconds. */
18413 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018414 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 else
18416 {
18417# ifdef FEAT_MBYTE
18418 vimconv_T conv;
18419 char_u *enc;
18420
18421 conv.vc_type = CONV_NONE;
18422 enc = enc_locale();
18423 convert_setup(&conv, p_enc, enc);
18424 if (conv.vc_type != CONV_NONE)
18425 p = string_convert(&conv, p, NULL);
18426# endif
18427 if (p != NULL)
18428 (void)strftime((char *)result_buf, sizeof(result_buf),
18429 (char *)p, curtime);
18430 else
18431 result_buf[0] = NUL;
18432
18433# ifdef FEAT_MBYTE
18434 if (conv.vc_type != CONV_NONE)
18435 vim_free(p);
18436 convert_setup(&conv, enc, p_enc);
18437 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018438 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 else
18440# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018441 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442
18443# ifdef FEAT_MBYTE
18444 /* Release conversion descriptors */
18445 convert_setup(&conv, NULL, NULL);
18446 vim_free(enc);
18447# endif
18448 }
18449}
18450#endif
18451
18452/*
18453 * "stridx()" function
18454 */
18455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018456f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457{
18458 char_u buf[NUMBUFLEN];
18459 char_u *needle;
18460 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018461 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018463 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018465 needle = get_tv_string_chk(&argvars[1]);
18466 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018467 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018468 if (needle == NULL || haystack == NULL)
18469 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470
Bram Moolenaar33570922005-01-25 22:26:29 +000018471 if (argvars[2].v_type != VAR_UNKNOWN)
18472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018473 int error = FALSE;
18474
18475 start_idx = get_tv_number_chk(&argvars[2], &error);
18476 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018477 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018478 if (start_idx >= 0)
18479 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 }
18481
18482 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18483 if (pos != NULL)
18484 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485}
18486
18487/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018488 * "string()" function
18489 */
18490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018491f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018492{
18493 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018494 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018496 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018497 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018498 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018499 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018500 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501}
18502
18503/*
18504 * "strlen()" function
18505 */
18506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018507f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018509 rettv->vval.v_number = (varnumber_T)(STRLEN(
18510 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511}
18512
18513/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018514 * "strchars()" function
18515 */
18516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018517f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018518{
18519 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018520 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018521#ifdef FEAT_MBYTE
18522 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018523 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018524#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018525
18526 if (argvars[1].v_type != VAR_UNKNOWN)
18527 skipcc = get_tv_number_chk(&argvars[1], NULL);
18528 if (skipcc < 0 || skipcc > 1)
18529 EMSG(_(e_invarg));
18530 else
18531 {
18532#ifdef FEAT_MBYTE
18533 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18534 while (*s != NUL)
18535 {
18536 func_mb_ptr2char_adv(&s);
18537 ++len;
18538 }
18539 rettv->vval.v_number = len;
18540#else
18541 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18542#endif
18543 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018544}
18545
18546/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018547 * "strdisplaywidth()" function
18548 */
18549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018550f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018551{
18552 char_u *s = get_tv_string(&argvars[0]);
18553 int col = 0;
18554
18555 if (argvars[1].v_type != VAR_UNKNOWN)
18556 col = get_tv_number(&argvars[1]);
18557
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018558 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018559}
18560
18561/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018562 * "strwidth()" function
18563 */
18564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018565f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018566{
18567 char_u *s = get_tv_string(&argvars[0]);
18568
18569 rettv->vval.v_number = (varnumber_T)(
18570#ifdef FEAT_MBYTE
18571 mb_string2cells(s, -1)
18572#else
18573 STRLEN(s)
18574#endif
18575 );
18576}
18577
18578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 * "strpart()" function
18580 */
18581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018582f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583{
18584 char_u *p;
18585 int n;
18586 int len;
18587 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018588 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018590 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 slen = (int)STRLEN(p);
18592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018593 n = get_tv_number_chk(&argvars[1], &error);
18594 if (error)
18595 len = 0;
18596 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 else
18599 len = slen - n; /* default len: all bytes that are available. */
18600
18601 /*
18602 * Only return the overlap between the specified part and the actual
18603 * string.
18604 */
18605 if (n < 0)
18606 {
18607 len += n;
18608 n = 0;
18609 }
18610 else if (n > slen)
18611 n = slen;
18612 if (len < 0)
18613 len = 0;
18614 else if (n + len > slen)
18615 len = slen - n;
18616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 rettv->v_type = VAR_STRING;
18618 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619}
18620
18621/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018622 * "strridx()" function
18623 */
18624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018625f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018626{
18627 char_u buf[NUMBUFLEN];
18628 char_u *needle;
18629 char_u *haystack;
18630 char_u *rest;
18631 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018632 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018634 needle = get_tv_string_chk(&argvars[1]);
18635 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018636
18637 rettv->vval.v_number = -1;
18638 if (needle == NULL || haystack == NULL)
18639 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018640
18641 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018642 if (argvars[2].v_type != VAR_UNKNOWN)
18643 {
18644 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018645 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018646 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018647 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018648 }
18649 else
18650 end_idx = haystack_len;
18651
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018653 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018655 lastmatch = haystack + end_idx;
18656 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018657 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018658 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018659 for (rest = haystack; *rest != '\0'; ++rest)
18660 {
18661 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018662 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663 break;
18664 lastmatch = rest;
18665 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018666 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018667
18668 if (lastmatch == NULL)
18669 rettv->vval.v_number = -1;
18670 else
18671 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18672}
18673
18674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 * "strtrans()" function
18676 */
18677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018678f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 rettv->v_type = VAR_STRING;
18681 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682}
18683
18684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018685 * "submatch()" function
18686 */
18687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018688f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018689{
Bram Moolenaar41571762014-04-02 19:00:58 +020018690 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018691 int no;
18692 int retList = 0;
18693
18694 no = (int)get_tv_number_chk(&argvars[0], &error);
18695 if (error)
18696 return;
18697 error = FALSE;
18698 if (argvars[1].v_type != VAR_UNKNOWN)
18699 retList = get_tv_number_chk(&argvars[1], &error);
18700 if (error)
18701 return;
18702
18703 if (retList == 0)
18704 {
18705 rettv->v_type = VAR_STRING;
18706 rettv->vval.v_string = reg_submatch(no);
18707 }
18708 else
18709 {
18710 rettv->v_type = VAR_LIST;
18711 rettv->vval.v_list = reg_submatch_list(no);
18712 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018713}
18714
18715/*
18716 * "substitute()" function
18717 */
18718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018719f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018720{
18721 char_u patbuf[NUMBUFLEN];
18722 char_u subbuf[NUMBUFLEN];
18723 char_u flagsbuf[NUMBUFLEN];
18724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018725 char_u *str = get_tv_string_chk(&argvars[0]);
18726 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18727 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18728 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18729
Bram Moolenaar0d660222005-01-07 21:51:51 +000018730 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018731 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18732 rettv->vval.v_string = NULL;
18733 else
18734 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018735}
18736
18737/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018738 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018741f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742{
18743 int id = 0;
18744#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018745 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 long col;
18747 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018748 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018750 lnum = get_tv_lnum(argvars); /* -1 on type error */
18751 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18752 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018754 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018755 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018756 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757#endif
18758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760}
18761
18762/*
18763 * "synIDattr(id, what [, mode])" function
18764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018766f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767{
18768 char_u *p = NULL;
18769#ifdef FEAT_SYN_HL
18770 int id;
18771 char_u *what;
18772 char_u *mode;
18773 char_u modebuf[NUMBUFLEN];
18774 int modec;
18775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776 id = get_tv_number(&argvars[0]);
18777 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018782 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 modec = 0; /* replace invalid with current */
18784 }
18785 else
18786 {
18787#ifdef FEAT_GUI
18788 if (gui.in_use)
18789 modec = 'g';
18790 else
18791#endif
18792 if (t_colors > 1)
18793 modec = 'c';
18794 else
18795 modec = 't';
18796 }
18797
18798
18799 switch (TOLOWER_ASC(what[0]))
18800 {
18801 case 'b':
18802 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18803 p = highlight_color(id, what, modec);
18804 else /* bold */
18805 p = highlight_has_attr(id, HL_BOLD, modec);
18806 break;
18807
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018808 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 p = highlight_color(id, what, modec);
18810 break;
18811
18812 case 'i':
18813 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18814 p = highlight_has_attr(id, HL_INVERSE, modec);
18815 else /* italic */
18816 p = highlight_has_attr(id, HL_ITALIC, modec);
18817 break;
18818
18819 case 'n': /* name */
18820 p = get_highlight_name(NULL, id - 1);
18821 break;
18822
18823 case 'r': /* reverse */
18824 p = highlight_has_attr(id, HL_INVERSE, modec);
18825 break;
18826
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018827 case 's':
18828 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18829 p = highlight_color(id, what, modec);
18830 else /* standout */
18831 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 break;
18833
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018834 case 'u':
18835 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18836 /* underline */
18837 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18838 else
18839 /* undercurl */
18840 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 break;
18842 }
18843
18844 if (p != NULL)
18845 p = vim_strsave(p);
18846#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018847 rettv->v_type = VAR_STRING;
18848 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849}
18850
18851/*
18852 * "synIDtrans(id)" function
18853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018855f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856{
18857 int id;
18858
18859#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861
18862 if (id > 0)
18863 id = syn_get_final_id(id);
18864 else
18865#endif
18866 id = 0;
18867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018868 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869}
18870
18871/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018872 * "synconcealed(lnum, col)" function
18873 */
18874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018875f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018876{
18877#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18878 long lnum;
18879 long col;
18880 int syntax_flags = 0;
18881 int cchar;
18882 int matchid = 0;
18883 char_u str[NUMBUFLEN];
18884#endif
18885
18886 rettv->v_type = VAR_LIST;
18887 rettv->vval.v_list = NULL;
18888
18889#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18890 lnum = get_tv_lnum(argvars); /* -1 on type error */
18891 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18892
18893 vim_memset(str, NUL, sizeof(str));
18894
18895 if (rettv_list_alloc(rettv) != FAIL)
18896 {
18897 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18898 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18899 && curwin->w_p_cole > 0)
18900 {
18901 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18902 syntax_flags = get_syntax_info(&matchid);
18903
18904 /* get the conceal character */
18905 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18906 {
18907 cchar = syn_get_sub_char();
18908 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18909 cchar = lcs_conceal;
18910 if (cchar != NUL)
18911 {
18912# ifdef FEAT_MBYTE
18913 if (has_mbyte)
18914 (*mb_char2bytes)(cchar, str);
18915 else
18916# endif
18917 str[0] = cchar;
18918 }
18919 }
18920 }
18921
18922 list_append_number(rettv->vval.v_list,
18923 (syntax_flags & HL_CONCEAL) != 0);
18924 /* -1 to auto-determine strlen */
18925 list_append_string(rettv->vval.v_list, str, -1);
18926 list_append_number(rettv->vval.v_list, matchid);
18927 }
18928#endif
18929}
18930
18931/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018932 * "synstack(lnum, col)" function
18933 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018935f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018936{
18937#ifdef FEAT_SYN_HL
18938 long lnum;
18939 long col;
18940 int i;
18941 int id;
18942#endif
18943
18944 rettv->v_type = VAR_LIST;
18945 rettv->vval.v_list = NULL;
18946
18947#ifdef FEAT_SYN_HL
18948 lnum = get_tv_lnum(argvars); /* -1 on type error */
18949 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18950
18951 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018952 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018953 && rettv_list_alloc(rettv) != FAIL)
18954 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018955 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018956 for (i = 0; ; ++i)
18957 {
18958 id = syn_get_stack_item(i);
18959 if (id < 0)
18960 break;
18961 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18962 break;
18963 }
18964 }
18965#endif
18966}
18967
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018969get_cmd_output_as_rettv(
18970 typval_T *argvars,
18971 typval_T *rettv,
18972 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018974 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018976 char_u *infile = NULL;
18977 char_u buf[NUMBUFLEN];
18978 int err = FALSE;
18979 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018980 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018981 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018983 rettv->v_type = VAR_STRING;
18984 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018985 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018986 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018987
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018988 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018989 {
18990 /*
18991 * Write the string to a temp file, to be used for input of the shell
18992 * command.
18993 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018994 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018995 {
18996 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018997 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018998 }
18999
19000 fd = mch_fopen((char *)infile, WRITEBIN);
19001 if (fd == NULL)
19002 {
19003 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019004 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019005 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019006 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019007 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019008 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19009 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019010 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019011 else
19012 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019013 size_t len;
19014
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019015 p = get_tv_string_buf_chk(&argvars[1], buf);
19016 if (p == NULL)
19017 {
19018 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019019 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019020 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019021 len = STRLEN(p);
19022 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019023 err = TRUE;
19024 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019025 if (fclose(fd) != 0)
19026 err = TRUE;
19027 if (err)
19028 {
19029 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019030 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019031 }
19032 }
19033
Bram Moolenaar52a72462014-08-29 15:53:52 +020019034 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19035 * echoes typeahead, that messes up the display. */
19036 if (!msg_silent)
19037 flags += SHELL_COOKED;
19038
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019039 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019041 int len;
19042 listitem_T *li;
19043 char_u *s = NULL;
19044 char_u *start;
19045 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019046 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047
Bram Moolenaar52a72462014-08-29 15:53:52 +020019048 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019049 if (res == NULL)
19050 goto errret;
19051
19052 list = list_alloc();
19053 if (list == NULL)
19054 goto errret;
19055
19056 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019058 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019059 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019060 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019061 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019062
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019063 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019064 if (s == NULL)
19065 goto errret;
19066
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019067 for (p = s; start < end; ++p, ++start)
19068 *p = *start == NUL ? NL : *start;
19069 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019070
19071 li = listitem_alloc();
19072 if (li == NULL)
19073 {
19074 vim_free(s);
19075 goto errret;
19076 }
19077 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019078 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019079 li->li_tv.vval.v_string = s;
19080 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019082
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019083 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019084 rettv->v_type = VAR_LIST;
19085 rettv->vval.v_list = list;
19086 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019088 else
19089 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019090 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019091#ifdef USE_CR
19092 /* translate <CR> into <NL> */
19093 if (res != NULL)
19094 {
19095 char_u *s;
19096
19097 for (s = res; *s; ++s)
19098 {
19099 if (*s == CAR)
19100 *s = NL;
19101 }
19102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103#else
19104# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019105 /* translate <CR><NL> into <NL> */
19106 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019108 char_u *s, *d;
19109
19110 d = res;
19111 for (s = res; *s; ++s)
19112 {
19113 if (s[0] == CAR && s[1] == NL)
19114 ++s;
19115 *d++ = *s;
19116 }
19117 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119# endif
19120#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019121 rettv->vval.v_string = res;
19122 res = NULL;
19123 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019124
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019125errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019126 if (infile != NULL)
19127 {
19128 mch_remove(infile);
19129 vim_free(infile);
19130 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019131 if (res != NULL)
19132 vim_free(res);
19133 if (list != NULL)
19134 list_free(list, TRUE);
19135}
19136
19137/*
19138 * "system()" function
19139 */
19140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019141f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019142{
19143 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19144}
19145
19146/*
19147 * "systemlist()" function
19148 */
19149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019150f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019151{
19152 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153}
19154
19155/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019156 * "tabpagebuflist()" function
19157 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019159f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019160{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019161#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019162 tabpage_T *tp;
19163 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019164
19165 if (argvars[0].v_type == VAR_UNKNOWN)
19166 wp = firstwin;
19167 else
19168 {
19169 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19170 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019171 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019172 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019173 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019174 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019175 for (; wp != NULL; wp = wp->w_next)
19176 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019177 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019178 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019179 }
19180#endif
19181}
19182
19183
19184/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019185 * "tabpagenr()" function
19186 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019188f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019189{
19190 int nr = 1;
19191#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019192 char_u *arg;
19193
19194 if (argvars[0].v_type != VAR_UNKNOWN)
19195 {
19196 arg = get_tv_string_chk(&argvars[0]);
19197 nr = 0;
19198 if (arg != NULL)
19199 {
19200 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019201 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019202 else
19203 EMSG2(_(e_invexpr2), arg);
19204 }
19205 }
19206 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019207 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019208#endif
19209 rettv->vval.v_number = nr;
19210}
19211
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019212
19213#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019214static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019215
19216/*
19217 * Common code for tabpagewinnr() and winnr().
19218 */
19219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019220get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019221{
19222 win_T *twin;
19223 int nr = 1;
19224 win_T *wp;
19225 char_u *arg;
19226
19227 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19228 if (argvar->v_type != VAR_UNKNOWN)
19229 {
19230 arg = get_tv_string_chk(argvar);
19231 if (arg == NULL)
19232 nr = 0; /* type error; errmsg already given */
19233 else if (STRCMP(arg, "$") == 0)
19234 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19235 else if (STRCMP(arg, "#") == 0)
19236 {
19237 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19238 if (twin == NULL)
19239 nr = 0;
19240 }
19241 else
19242 {
19243 EMSG2(_(e_invexpr2), arg);
19244 nr = 0;
19245 }
19246 }
19247
19248 if (nr > 0)
19249 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19250 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019251 {
19252 if (wp == NULL)
19253 {
19254 /* didn't find it in this tabpage */
19255 nr = 0;
19256 break;
19257 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019258 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019259 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019260 return nr;
19261}
19262#endif
19263
19264/*
19265 * "tabpagewinnr()" function
19266 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019268f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019269{
19270 int nr = 1;
19271#ifdef FEAT_WINDOWS
19272 tabpage_T *tp;
19273
19274 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19275 if (tp == NULL)
19276 nr = 0;
19277 else
19278 nr = get_winnr(tp, &argvars[1]);
19279#endif
19280 rettv->vval.v_number = nr;
19281}
19282
19283
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019284/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019285 * "tagfiles()" function
19286 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019288f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019289{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019290 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019291 tagname_T tn;
19292 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019293
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019294 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019295 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019296 fname = alloc(MAXPATHL);
19297 if (fname == NULL)
19298 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019300 for (first = TRUE; ; first = FALSE)
19301 if (get_tagfname(&tn, first, fname) == FAIL
19302 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019303 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019304 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019305 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019306}
19307
19308/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019309 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019310 */
19311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019312f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019313{
19314 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019315
19316 tag_pattern = get_tv_string(&argvars[0]);
19317
19318 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019319 if (*tag_pattern == NUL)
19320 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019321
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019322 if (rettv_list_alloc(rettv) == OK)
19323 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019324}
19325
19326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 * "tempname()" function
19328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019330f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331{
19332 static int x = 'A';
19333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019334 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019335 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336
19337 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19338 * names. Skip 'I' and 'O', they are used for shell redirection. */
19339 do
19340 {
19341 if (x == 'Z')
19342 x = '0';
19343 else if (x == '9')
19344 x = 'A';
19345 else
19346 {
19347#ifdef EBCDIC
19348 if (x == 'I')
19349 x = 'J';
19350 else if (x == 'R')
19351 x = 'S';
19352 else
19353#endif
19354 ++x;
19355 }
19356 } while (x == 'I' || x == 'O');
19357}
19358
19359/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019360 * "test(list)" function: Just checking the walls...
19361 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019363f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019364{
19365 /* Used for unit testing. Change the code below to your liking. */
19366#if 0
19367 listitem_T *li;
19368 list_T *l;
19369 char_u *bad, *good;
19370
19371 if (argvars[0].v_type != VAR_LIST)
19372 return;
19373 l = argvars[0].vval.v_list;
19374 if (l == NULL)
19375 return;
19376 li = l->lv_first;
19377 if (li == NULL)
19378 return;
19379 bad = get_tv_string(&li->li_tv);
19380 li = li->li_next;
19381 if (li == NULL)
19382 return;
19383 good = get_tv_string(&li->li_tv);
19384 rettv->vval.v_number = test_edit_score(bad, good);
19385#endif
19386}
19387
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019388#ifdef FEAT_FLOAT
19389/*
19390 * "tan()" function
19391 */
19392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019393f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019394{
19395 float_T f;
19396
19397 rettv->v_type = VAR_FLOAT;
19398 if (get_float_arg(argvars, &f) == OK)
19399 rettv->vval.v_float = tan(f);
19400 else
19401 rettv->vval.v_float = 0.0;
19402}
19403
19404/*
19405 * "tanh()" function
19406 */
19407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019408f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019409{
19410 float_T f;
19411
19412 rettv->v_type = VAR_FLOAT;
19413 if (get_float_arg(argvars, &f) == OK)
19414 rettv->vval.v_float = tanh(f);
19415 else
19416 rettv->vval.v_float = 0.0;
19417}
19418#endif
19419
Bram Moolenaard52d9742005-08-21 22:20:28 +000019420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 * "tolower(string)" function
19422 */
19423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019424f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425{
19426 char_u *p;
19427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 p = vim_strsave(get_tv_string(&argvars[0]));
19429 rettv->v_type = VAR_STRING;
19430 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431
19432 if (p != NULL)
19433 while (*p != NUL)
19434 {
19435#ifdef FEAT_MBYTE
19436 int l;
19437
19438 if (enc_utf8)
19439 {
19440 int c, lc;
19441
19442 c = utf_ptr2char(p);
19443 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019444 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 /* TODO: reallocate string when byte count changes. */
19446 if (utf_char2len(lc) == l)
19447 utf_char2bytes(lc, p);
19448 p += l;
19449 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019450 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 p += l; /* skip multi-byte character */
19452 else
19453#endif
19454 {
19455 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19456 ++p;
19457 }
19458 }
19459}
19460
19461/*
19462 * "toupper(string)" function
19463 */
19464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019465f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019468 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469}
19470
19471/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019472 * "tr(string, fromstr, tostr)" function
19473 */
19474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019475f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019476{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019477 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019478 char_u *fromstr;
19479 char_u *tostr;
19480 char_u *p;
19481#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019482 int inlen;
19483 int fromlen;
19484 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019485 int idx;
19486 char_u *cpstr;
19487 int cplen;
19488 int first = TRUE;
19489#endif
19490 char_u buf[NUMBUFLEN];
19491 char_u buf2[NUMBUFLEN];
19492 garray_T ga;
19493
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019494 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019495 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19496 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019497
19498 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019499 rettv->v_type = VAR_STRING;
19500 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019501 if (fromstr == NULL || tostr == NULL)
19502 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019503 ga_init2(&ga, (int)sizeof(char), 80);
19504
19505#ifdef FEAT_MBYTE
19506 if (!has_mbyte)
19507#endif
19508 /* not multi-byte: fromstr and tostr must be the same length */
19509 if (STRLEN(fromstr) != STRLEN(tostr))
19510 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019511#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019512error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019513#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019514 EMSG2(_(e_invarg2), fromstr);
19515 ga_clear(&ga);
19516 return;
19517 }
19518
19519 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019520 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019521 {
19522#ifdef FEAT_MBYTE
19523 if (has_mbyte)
19524 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019525 inlen = (*mb_ptr2len)(in_str);
19526 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019527 cplen = inlen;
19528 idx = 0;
19529 for (p = fromstr; *p != NUL; p += fromlen)
19530 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019531 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019532 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019533 {
19534 for (p = tostr; *p != NUL; p += tolen)
19535 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019536 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019537 if (idx-- == 0)
19538 {
19539 cplen = tolen;
19540 cpstr = p;
19541 break;
19542 }
19543 }
19544 if (*p == NUL) /* tostr is shorter than fromstr */
19545 goto error;
19546 break;
19547 }
19548 ++idx;
19549 }
19550
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019551 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019552 {
19553 /* Check that fromstr and tostr have the same number of
19554 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019555 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019556 first = FALSE;
19557 for (p = tostr; *p != NUL; p += tolen)
19558 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019559 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019560 --idx;
19561 }
19562 if (idx != 0)
19563 goto error;
19564 }
19565
Bram Moolenaarcde88542015-08-11 19:14:00 +020019566 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019567 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019568 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019569
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019570 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019571 }
19572 else
19573#endif
19574 {
19575 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019576 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019577 if (p != NULL)
19578 ga_append(&ga, tostr[p - fromstr]);
19579 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019580 ga_append(&ga, *in_str);
19581 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019582 }
19583 }
19584
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019585 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019586 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019587 ga_append(&ga, NUL);
19588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019589 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019590}
19591
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019592#ifdef FEAT_FLOAT
19593/*
19594 * "trunc({float})" function
19595 */
19596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019597f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019598{
19599 float_T f;
19600
19601 rettv->v_type = VAR_FLOAT;
19602 if (get_float_arg(argvars, &f) == OK)
19603 /* trunc() is not in C90, use floor() or ceil() instead. */
19604 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19605 else
19606 rettv->vval.v_float = 0.0;
19607}
19608#endif
19609
Bram Moolenaar8299df92004-07-10 09:47:34 +000019610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 * "type(expr)" function
19612 */
19613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019614f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019616 int n;
19617
19618 switch (argvars[0].v_type)
19619 {
19620 case VAR_NUMBER: n = 0; break;
19621 case VAR_STRING: n = 1; break;
19622 case VAR_FUNC: n = 2; break;
19623 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019624 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019625#ifdef FEAT_FLOAT
19626 case VAR_FLOAT: n = 5; break;
19627#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019628 case VAR_SPECIAL:
19629 if (argvars[0].vval.v_number == VVAL_FALSE
19630 || argvars[0].vval.v_number == VVAL_TRUE)
19631 n = 6;
19632 else
19633 n = 7;
19634 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019635 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19636 }
19637 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638}
19639
19640/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019641 * "undofile(name)" function
19642 */
19643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019644f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019645{
19646 rettv->v_type = VAR_STRING;
19647#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019648 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019649 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019650
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019651 if (*fname == NUL)
19652 {
19653 /* If there is no file name there will be no undo file. */
19654 rettv->vval.v_string = NULL;
19655 }
19656 else
19657 {
19658 char_u *ffname = FullName_save(fname, FALSE);
19659
19660 if (ffname != NULL)
19661 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19662 vim_free(ffname);
19663 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019664 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019665#else
19666 rettv->vval.v_string = NULL;
19667#endif
19668}
19669
19670/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019671 * "undotree()" function
19672 */
19673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019674f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019675{
19676 if (rettv_dict_alloc(rettv) == OK)
19677 {
19678 dict_T *dict = rettv->vval.v_dict;
19679 list_T *list;
19680
Bram Moolenaar730cde92010-06-27 05:18:54 +020019681 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019682 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019683 dict_add_nr_str(dict, "save_last",
19684 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019685 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19686 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019687 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019688
19689 list = list_alloc();
19690 if (list != NULL)
19691 {
19692 u_eval_tree(curbuf->b_u_oldhead, list);
19693 dict_add_list(dict, "entries", list);
19694 }
19695 }
19696}
19697
19698/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019699 * "values(dict)" function
19700 */
19701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019702f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019703{
19704 dict_list(argvars, rettv, 1);
19705}
19706
19707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 * "virtcol(string)" function
19709 */
19710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019711f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712{
19713 colnr_T vcol = 0;
19714 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019715 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019717 fp = var2fpos(&argvars[0], FALSE, &fnum);
19718 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19719 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 {
19721 getvvcol(curwin, fp, NULL, NULL, &vcol);
19722 ++vcol;
19723 }
19724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726}
19727
19728/*
19729 * "visualmode()" function
19730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019732f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 char_u str[2];
19735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 str[0] = curbuf->b_visual_mode_eval;
19738 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740
19741 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019742 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744}
19745
19746/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019747 * "wildmenumode()" function
19748 */
19749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019750f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019751{
19752#ifdef FEAT_WILDMENU
19753 if (wild_menu_showing)
19754 rettv->vval.v_number = 1;
19755#endif
19756}
19757
19758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 * "winbufnr(nr)" function
19760 */
19761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019762f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763{
19764 win_T *wp;
19765
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019766 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771}
19772
19773/*
19774 * "wincol()" function
19775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019777f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778{
19779 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781}
19782
19783/*
19784 * "winheight(nr)" function
19785 */
19786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019787f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788{
19789 win_T *wp;
19790
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019791 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796}
19797
19798/*
19799 * "winline()" function
19800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019802f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803{
19804 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806}
19807
19808/*
19809 * "winnr()" function
19810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019812f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813{
19814 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019815
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019817 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820}
19821
19822/*
19823 * "winrestcmd()" function
19824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019826f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827{
19828#ifdef FEAT_WINDOWS
19829 win_T *wp;
19830 int winnr = 1;
19831 garray_T ga;
19832 char_u buf[50];
19833
19834 ga_init2(&ga, (int)sizeof(char), 70);
19835 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19836 {
19837 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19838 ga_concat(&ga, buf);
19839# ifdef FEAT_VERTSPLIT
19840 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19841 ga_concat(&ga, buf);
19842# endif
19843 ++winnr;
19844 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019845 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019847 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852}
19853
19854/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019855 * "winrestview()" function
19856 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019858f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019859{
19860 dict_T *dict;
19861
19862 if (argvars[0].v_type != VAR_DICT
19863 || (dict = argvars[0].vval.v_dict) == NULL)
19864 EMSG(_(e_invarg));
19865 else
19866 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019867 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19868 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19869 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19870 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019871#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019872 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19873 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019874#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019875 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19876 {
19877 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19878 curwin->w_set_curswant = FALSE;
19879 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019880
Bram Moolenaar82c25852014-05-28 16:47:16 +020019881 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19882 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019883#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019884 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19885 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019886#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019887 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19888 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19889 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19890 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019891
19892 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019893 win_new_height(curwin, curwin->w_height);
19894# ifdef FEAT_VERTSPLIT
19895 win_new_width(curwin, W_WIDTH(curwin));
19896# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019897 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019898
Bram Moolenaarb851a962014-10-31 15:45:52 +010019899 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019900 curwin->w_topline = 1;
19901 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19902 curwin->w_topline = curbuf->b_ml.ml_line_count;
19903#ifdef FEAT_DIFF
19904 check_topfill(curwin, TRUE);
19905#endif
19906 }
19907}
19908
19909/*
19910 * "winsaveview()" function
19911 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019913f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019914{
19915 dict_T *dict;
19916
Bram Moolenaara800b422010-06-27 01:15:55 +020019917 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019918 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019919 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019920
19921 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19922 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19923#ifdef FEAT_VIRTUALEDIT
19924 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19925#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019926 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019927 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19928
19929 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19930#ifdef FEAT_DIFF
19931 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19932#endif
19933 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19934 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19935}
19936
19937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 * "winwidth(nr)" function
19939 */
19940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019941f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942{
19943 win_T *wp;
19944
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019945 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019947 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 else
19949#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019952 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953#endif
19954}
19955
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019957 * "wordcount()" function
19958 */
19959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019960f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019961{
19962 if (rettv_dict_alloc(rettv) == FAIL)
19963 return;
19964 cursor_pos_info(rettv->vval.v_dict);
19965}
19966
19967/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019968 * Write list of strings to file
19969 */
19970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019971write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019972{
19973 listitem_T *li;
19974 int c;
19975 int ret = OK;
19976 char_u *s;
19977
19978 for (li = list->lv_first; li != NULL; li = li->li_next)
19979 {
19980 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19981 {
19982 if (*s == '\n')
19983 c = putc(NUL, fd);
19984 else
19985 c = putc(*s, fd);
19986 if (c == EOF)
19987 {
19988 ret = FAIL;
19989 break;
19990 }
19991 }
19992 if (!binary || li->li_next != NULL)
19993 if (putc('\n', fd) == EOF)
19994 {
19995 ret = FAIL;
19996 break;
19997 }
19998 if (ret == FAIL)
19999 {
20000 EMSG(_(e_write));
20001 break;
20002 }
20003 }
20004 return ret;
20005}
20006
20007/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020008 * "writefile()" function
20009 */
20010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020011f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020012{
20013 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020014 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020015 char_u *fname;
20016 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020017 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020018
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020019 if (check_restricted() || check_secure())
20020 return;
20021
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020022 if (argvars[0].v_type != VAR_LIST)
20023 {
20024 EMSG2(_(e_listarg), "writefile()");
20025 return;
20026 }
20027 if (argvars[0].vval.v_list == NULL)
20028 return;
20029
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020030 if (argvars[2].v_type != VAR_UNKNOWN)
20031 {
20032 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20033 binary = TRUE;
20034 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20035 append = TRUE;
20036 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020037
20038 /* Always open the file in binary mode, library functions have a mind of
20039 * their own about CR-LF conversion. */
20040 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020041 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20042 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020043 {
20044 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20045 ret = -1;
20046 }
20047 else
20048 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020049 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20050 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020051 fclose(fd);
20052 }
20053
20054 rettv->vval.v_number = ret;
20055}
20056
20057/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020058 * "xor(expr, expr)" function
20059 */
20060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020061f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020062{
20063 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20064 ^ get_tv_number_chk(&argvars[1], NULL);
20065}
20066
20067
20068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020070 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 */
20072 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020073var2fpos(
20074 typval_T *varp,
20075 int dollar_lnum, /* TRUE when $ is last line */
20076 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020078 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020080 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081
Bram Moolenaara5525202006-03-02 22:52:09 +000020082 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020083 if (varp->v_type == VAR_LIST)
20084 {
20085 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020086 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020087 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020088 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020089
20090 l = varp->vval.v_list;
20091 if (l == NULL)
20092 return NULL;
20093
20094 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020095 pos.lnum = list_find_nr(l, 0L, &error);
20096 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020097 return NULL; /* invalid line number */
20098
20099 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020100 pos.col = list_find_nr(l, 1L, &error);
20101 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020102 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020103 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020104
20105 /* We accept "$" for the column number: last column. */
20106 li = list_find(l, 1L);
20107 if (li != NULL && li->li_tv.v_type == VAR_STRING
20108 && li->li_tv.vval.v_string != NULL
20109 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20110 pos.col = len + 1;
20111
Bram Moolenaara5525202006-03-02 22:52:09 +000020112 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020113 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020114 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020115 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020116
Bram Moolenaara5525202006-03-02 22:52:09 +000020117#ifdef FEAT_VIRTUALEDIT
20118 /* Get the virtual offset. Defaults to zero. */
20119 pos.coladd = list_find_nr(l, 2L, &error);
20120 if (error)
20121 pos.coladd = 0;
20122#endif
20123
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020124 return &pos;
20125 }
20126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020127 name = get_tv_string_chk(varp);
20128 if (name == NULL)
20129 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020130 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020132 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20133 {
20134 if (VIsual_active)
20135 return &VIsual;
20136 return &curwin->w_cursor;
20137 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020138 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020140 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20142 return NULL;
20143 return pp;
20144 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020145
20146#ifdef FEAT_VIRTUALEDIT
20147 pos.coladd = 0;
20148#endif
20149
Bram Moolenaar477933c2007-07-17 14:32:23 +000020150 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020151 {
20152 pos.col = 0;
20153 if (name[1] == '0') /* "w0": first visible line */
20154 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020155 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020156 pos.lnum = curwin->w_topline;
20157 return &pos;
20158 }
20159 else if (name[1] == '$') /* "w$": last visible line */
20160 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020161 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020162 pos.lnum = curwin->w_botline - 1;
20163 return &pos;
20164 }
20165 }
20166 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020168 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 {
20170 pos.lnum = curbuf->b_ml.ml_line_count;
20171 pos.col = 0;
20172 }
20173 else
20174 {
20175 pos.lnum = curwin->w_cursor.lnum;
20176 pos.col = (colnr_T)STRLEN(ml_get_curline());
20177 }
20178 return &pos;
20179 }
20180 return NULL;
20181}
20182
20183/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020184 * Convert list in "arg" into a position and optional file number.
20185 * When "fnump" is NULL there is no file number, only 3 items.
20186 * Note that the column is passed on as-is, the caller may want to decrement
20187 * it to use 1 for the first column.
20188 * Return FAIL when conversion is not possible, doesn't check the position for
20189 * validity.
20190 */
20191 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020192list2fpos(
20193 typval_T *arg,
20194 pos_T *posp,
20195 int *fnump,
20196 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020197{
20198 list_T *l = arg->vval.v_list;
20199 long i = 0;
20200 long n;
20201
Bram Moolenaar493c1782014-05-28 14:34:46 +020020202 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20203 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020204 if (arg->v_type != VAR_LIST
20205 || l == NULL
20206 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020207 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020208 return FAIL;
20209
20210 if (fnump != NULL)
20211 {
20212 n = list_find_nr(l, i++, NULL); /* fnum */
20213 if (n < 0)
20214 return FAIL;
20215 if (n == 0)
20216 n = curbuf->b_fnum; /* current buffer */
20217 *fnump = n;
20218 }
20219
20220 n = list_find_nr(l, i++, NULL); /* lnum */
20221 if (n < 0)
20222 return FAIL;
20223 posp->lnum = n;
20224
20225 n = list_find_nr(l, i++, NULL); /* col */
20226 if (n < 0)
20227 return FAIL;
20228 posp->col = n;
20229
20230#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020231 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020232 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020233 posp->coladd = 0;
20234 else
20235 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020236#endif
20237
Bram Moolenaar493c1782014-05-28 14:34:46 +020020238 if (curswantp != NULL)
20239 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20240
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020241 return OK;
20242}
20243
20244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 * Get the length of an environment variable name.
20246 * Advance "arg" to the first character after the name.
20247 * Return 0 for error.
20248 */
20249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020250get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251{
20252 char_u *p;
20253 int len;
20254
20255 for (p = *arg; vim_isIDc(*p); ++p)
20256 ;
20257 if (p == *arg) /* no name found */
20258 return 0;
20259
20260 len = (int)(p - *arg);
20261 *arg = p;
20262 return len;
20263}
20264
20265/*
20266 * Get the length of the name of a function or internal variable.
20267 * "arg" is advanced to the first non-white character after the name.
20268 * Return 0 if something is wrong.
20269 */
20270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020271get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272{
20273 char_u *p;
20274 int len;
20275
20276 /* Find the end of the name. */
20277 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020278 {
20279 if (*p == ':')
20280 {
20281 /* "s:" is start of "s:var", but "n:" is not and can be used in
20282 * slice "[n:]". Also "xx:" is not a namespace. */
20283 len = (int)(p - *arg);
20284 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20285 || len > 1)
20286 break;
20287 }
20288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 if (p == *arg) /* no name found */
20290 return 0;
20291
20292 len = (int)(p - *arg);
20293 *arg = skipwhite(p);
20294
20295 return len;
20296}
20297
20298/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020299 * Get the length of the name of a variable or function.
20300 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020302 * Return -1 if curly braces expansion failed.
20303 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 * If the name contains 'magic' {}'s, expand them and return the
20305 * expanded name in an allocated string via 'alias' - caller must free.
20306 */
20307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020308get_name_len(
20309 char_u **arg,
20310 char_u **alias,
20311 int evaluate,
20312 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313{
20314 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 char_u *p;
20316 char_u *expr_start;
20317 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318
20319 *alias = NULL; /* default to no alias */
20320
20321 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20322 && (*arg)[2] == (int)KE_SNR)
20323 {
20324 /* hard coded <SNR>, already translated */
20325 *arg += 3;
20326 return get_id_len(arg) + 3;
20327 }
20328 len = eval_fname_script(*arg);
20329 if (len > 0)
20330 {
20331 /* literal "<SID>", "s:" or "<SNR>" */
20332 *arg += len;
20333 }
20334
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020338 p = find_name_end(*arg, &expr_start, &expr_end,
20339 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 if (expr_start != NULL)
20341 {
20342 char_u *temp_string;
20343
20344 if (!evaluate)
20345 {
20346 len += (int)(p - *arg);
20347 *arg = skipwhite(p);
20348 return len;
20349 }
20350
20351 /*
20352 * Include any <SID> etc in the expanded string:
20353 * Thus the -len here.
20354 */
20355 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20356 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020357 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 *alias = temp_string;
20359 *arg = skipwhite(p);
20360 return (int)STRLEN(temp_string);
20361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362
20363 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020364 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 EMSG2(_(e_invexpr2), *arg);
20366
20367 return len;
20368}
20369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370/*
20371 * Find the end of a variable or function name, taking care of magic braces.
20372 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20373 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020374 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020375 * Return a pointer to just after the name. Equal to "arg" if there is no
20376 * valid name.
20377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020379find_name_end(
20380 char_u *arg,
20381 char_u **expr_start,
20382 char_u **expr_end,
20383 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020385 int mb_nest = 0;
20386 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020388 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392 *expr_start = NULL;
20393 *expr_end = NULL;
20394 }
20395
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020396 /* Quick check for valid starting character. */
20397 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20398 return arg;
20399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020400 for (p = arg; *p != NUL
20401 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020402 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020403 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020404 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020405 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020406 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020407 if (*p == '\'')
20408 {
20409 /* skip over 'string' to avoid counting [ and ] inside it. */
20410 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20411 ;
20412 if (*p == NUL)
20413 break;
20414 }
20415 else if (*p == '"')
20416 {
20417 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20418 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20419 if (*p == '\\' && p[1] != NUL)
20420 ++p;
20421 if (*p == NUL)
20422 break;
20423 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020424 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20425 {
20426 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020427 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020428 len = (int)(p - arg);
20429 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020430 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020431 break;
20432 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020434 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020436 if (*p == '[')
20437 ++br_nest;
20438 else if (*p == ']')
20439 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020442 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020444 if (*p == '{')
20445 {
20446 mb_nest++;
20447 if (expr_start != NULL && *expr_start == NULL)
20448 *expr_start = p;
20449 }
20450 else if (*p == '}')
20451 {
20452 mb_nest--;
20453 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20454 *expr_end = p;
20455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 }
20458
20459 return p;
20460}
20461
20462/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020463 * Expands out the 'magic' {}'s in a variable/function name.
20464 * Note that this can call itself recursively, to deal with
20465 * constructs like foo{bar}{baz}{bam}
20466 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20467 * "in_start" ^
20468 * "expr_start" ^
20469 * "expr_end" ^
20470 * "in_end" ^
20471 *
20472 * Returns a new allocated string, which the caller must free.
20473 * Returns NULL for failure.
20474 */
20475 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020476make_expanded_name(
20477 char_u *in_start,
20478 char_u *expr_start,
20479 char_u *expr_end,
20480 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020481{
20482 char_u c1;
20483 char_u *retval = NULL;
20484 char_u *temp_result;
20485 char_u *nextcmd = NULL;
20486
20487 if (expr_end == NULL || in_end == NULL)
20488 return NULL;
20489 *expr_start = NUL;
20490 *expr_end = NUL;
20491 c1 = *in_end;
20492 *in_end = NUL;
20493
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020494 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020495 if (temp_result != NULL && nextcmd == NULL)
20496 {
20497 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20498 + (in_end - expr_end) + 1));
20499 if (retval != NULL)
20500 {
20501 STRCPY(retval, in_start);
20502 STRCAT(retval, temp_result);
20503 STRCAT(retval, expr_end + 1);
20504 }
20505 }
20506 vim_free(temp_result);
20507
20508 *in_end = c1; /* put char back for error messages */
20509 *expr_start = '{';
20510 *expr_end = '}';
20511
20512 if (retval != NULL)
20513 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020514 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020515 if (expr_start != NULL)
20516 {
20517 /* Further expansion! */
20518 temp_result = make_expanded_name(retval, expr_start,
20519 expr_end, temp_result);
20520 vim_free(retval);
20521 retval = temp_result;
20522 }
20523 }
20524
20525 return retval;
20526}
20527
20528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020530 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 */
20532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020533eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020535 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20536}
20537
20538/*
20539 * Return TRUE if character "c" can be used as the first character in a
20540 * variable or function name (excluding '{' and '}').
20541 */
20542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020543eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020544{
20545 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546}
20547
20548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 * Set number v: variable to "val".
20550 */
20551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020552set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020554 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555}
20556
20557/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020558 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 */
20560 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020561get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020563 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564}
20565
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020566/*
20567 * Get string v: variable value. Uses a static buffer, can only be used once.
20568 */
20569 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020570get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020571{
20572 return get_tv_string(&vimvars[idx].vv_tv);
20573}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020574
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020576 * Get List v: variable value. Caller must take care of reference count when
20577 * needed.
20578 */
20579 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020580get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020581{
20582 return vimvars[idx].vv_list;
20583}
20584
20585/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020586 * Set v:char to character "c".
20587 */
20588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020589set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020590{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020591 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020592
20593#ifdef FEAT_MBYTE
20594 if (has_mbyte)
20595 buf[(*mb_char2bytes)(c, buf)] = NUL;
20596 else
20597#endif
20598 {
20599 buf[0] = c;
20600 buf[1] = NUL;
20601 }
20602 set_vim_var_string(VV_CHAR, buf, -1);
20603}
20604
20605/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020606 * Set v:count to "count" and v:count1 to "count1".
20607 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 */
20609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020610set_vcount(
20611 long count,
20612 long count1,
20613 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020615 if (set_prevcount)
20616 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020617 vimvars[VV_COUNT].vv_nr = count;
20618 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619}
20620
20621/*
20622 * Set string v: variable to a copy of "val".
20623 */
20624 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020625set_vim_var_string(
20626 int idx,
20627 char_u *val,
20628 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020630 /* Need to do this (at least) once, since we can't initialize a union.
20631 * Will always be invoked when "v:progname" is set. */
20632 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20633
Bram Moolenaare9a41262005-01-15 22:18:47 +000020634 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020636 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020638 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020640 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641}
20642
20643/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020644 * Set List v: variable to "val".
20645 */
20646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020647set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020648{
20649 list_unref(vimvars[idx].vv_list);
20650 vimvars[idx].vv_list = val;
20651 if (val != NULL)
20652 ++val->lv_refcount;
20653}
20654
20655/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020656 * Set Dictionary v: variable to "val".
20657 */
20658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020659set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020660{
20661 int todo;
20662 hashitem_T *hi;
20663
20664 dict_unref(vimvars[idx].vv_dict);
20665 vimvars[idx].vv_dict = val;
20666 if (val != NULL)
20667 {
20668 ++val->dv_refcount;
20669
20670 /* Set readonly */
20671 todo = (int)val->dv_hashtab.ht_used;
20672 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20673 {
20674 if (HASHITEM_EMPTY(hi))
20675 continue;
20676 --todo;
20677 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20678 }
20679 }
20680}
20681
20682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 * Set v:register if needed.
20684 */
20685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020686set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687{
20688 char_u regname;
20689
20690 if (c == 0 || c == ' ')
20691 regname = '"';
20692 else
20693 regname = c;
20694 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020695 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 set_vim_var_string(VV_REG, &regname, 1);
20697}
20698
20699/*
20700 * Get or set v:exception. If "oldval" == NULL, return the current value.
20701 * Otherwise, restore the value to "oldval" and return NULL.
20702 * Must always be called in pairs to save and restore v:exception! Does not
20703 * take care of memory allocations.
20704 */
20705 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020706v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707{
20708 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020709 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710
Bram Moolenaare9a41262005-01-15 22:18:47 +000020711 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 return NULL;
20713}
20714
20715/*
20716 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20717 * Otherwise, restore the value to "oldval" and return NULL.
20718 * Must always be called in pairs to save and restore v:throwpoint! Does not
20719 * take care of memory allocations.
20720 */
20721 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020722v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723{
20724 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020725 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726
Bram Moolenaare9a41262005-01-15 22:18:47 +000020727 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 return NULL;
20729}
20730
20731#if defined(FEAT_AUTOCMD) || defined(PROTO)
20732/*
20733 * Set v:cmdarg.
20734 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20735 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20736 * Must always be called in pairs!
20737 */
20738 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020739set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740{
20741 char_u *oldval;
20742 char_u *newval;
20743 unsigned len;
20744
Bram Moolenaare9a41262005-01-15 22:18:47 +000020745 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020746 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020748 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020749 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020750 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 }
20752
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020753 if (eap->force_bin == FORCE_BIN)
20754 len = 6;
20755 else if (eap->force_bin == FORCE_NOBIN)
20756 len = 8;
20757 else
20758 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020759
20760 if (eap->read_edit)
20761 len += 7;
20762
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020763 if (eap->force_ff != 0)
20764 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20765# ifdef FEAT_MBYTE
20766 if (eap->force_enc != 0)
20767 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020768 if (eap->bad_char != 0)
20769 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020770# endif
20771
20772 newval = alloc(len + 1);
20773 if (newval == NULL)
20774 return NULL;
20775
20776 if (eap->force_bin == FORCE_BIN)
20777 sprintf((char *)newval, " ++bin");
20778 else if (eap->force_bin == FORCE_NOBIN)
20779 sprintf((char *)newval, " ++nobin");
20780 else
20781 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020782
20783 if (eap->read_edit)
20784 STRCAT(newval, " ++edit");
20785
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020786 if (eap->force_ff != 0)
20787 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20788 eap->cmd + eap->force_ff);
20789# ifdef FEAT_MBYTE
20790 if (eap->force_enc != 0)
20791 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20792 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020793 if (eap->bad_char == BAD_KEEP)
20794 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20795 else if (eap->bad_char == BAD_DROP)
20796 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20797 else if (eap->bad_char != 0)
20798 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020799# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020800 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020801 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802}
20803#endif
20804
20805/*
20806 * Get the value of internal variable "name".
20807 * Return OK or FAIL.
20808 */
20809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020810get_var_tv(
20811 char_u *name,
20812 int len, /* length of "name" */
20813 typval_T *rettv, /* NULL when only checking existence */
20814 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20815 int verbose, /* may give error message */
20816 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817{
20818 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020819 typval_T *tv = NULL;
20820 typval_T atv;
20821 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823
20824 /* truncate the name, so that we can use strcmp() */
20825 cc = name[len];
20826 name[len] = NUL;
20827
20828 /*
20829 * Check for "b:changedtick".
20830 */
20831 if (STRCMP(name, "b:changedtick") == 0)
20832 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020833 atv.v_type = VAR_NUMBER;
20834 atv.vval.v_number = curbuf->b_changedtick;
20835 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 }
20837
20838 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 * Check for user-defined variables.
20840 */
20841 else
20842 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020843 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020845 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020847 if (dip != NULL)
20848 *dip = v;
20849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 }
20851
Bram Moolenaare9a41262005-01-15 22:18:47 +000020852 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020854 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020855 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 ret = FAIL;
20857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020858 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020859 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860
20861 name[len] = cc;
20862
20863 return ret;
20864}
20865
20866/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020867 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20868 * Also handle function call with Funcref variable: func(expr)
20869 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20870 */
20871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020872handle_subscript(
20873 char_u **arg,
20874 typval_T *rettv,
20875 int evaluate, /* do more than finding the end */
20876 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020877{
20878 int ret = OK;
20879 dict_T *selfdict = NULL;
20880 char_u *s;
20881 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020882 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020883
20884 while (ret == OK
20885 && (**arg == '['
20886 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020887 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020888 && !vim_iswhite(*(*arg - 1)))
20889 {
20890 if (**arg == '(')
20891 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020892 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020893 if (evaluate)
20894 {
20895 functv = *rettv;
20896 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020897
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020898 /* Invoke the function. Recursive! */
20899 s = functv.vval.v_string;
20900 }
20901 else
20902 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020903 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020904 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20905 &len, evaluate, selfdict);
20906
20907 /* Clear the funcref afterwards, so that deleting it while
20908 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020909 if (evaluate)
20910 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020911
20912 /* Stop the expression evaluation when immediately aborting on
20913 * error, or when an interrupt occurred or an exception was thrown
20914 * but not caught. */
20915 if (aborting())
20916 {
20917 if (ret == OK)
20918 clear_tv(rettv);
20919 ret = FAIL;
20920 }
20921 dict_unref(selfdict);
20922 selfdict = NULL;
20923 }
20924 else /* **arg == '[' || **arg == '.' */
20925 {
20926 dict_unref(selfdict);
20927 if (rettv->v_type == VAR_DICT)
20928 {
20929 selfdict = rettv->vval.v_dict;
20930 if (selfdict != NULL)
20931 ++selfdict->dv_refcount;
20932 }
20933 else
20934 selfdict = NULL;
20935 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20936 {
20937 clear_tv(rettv);
20938 ret = FAIL;
20939 }
20940 }
20941 }
20942 dict_unref(selfdict);
20943 return ret;
20944}
20945
20946/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020947 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020948 * value).
20949 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020950 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020951alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020952{
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020954}
20955
20956/*
20957 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 * The string "s" must have been allocated, it is consumed.
20959 * Return NULL for out of memory, the variable otherwise.
20960 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020961 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020962alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963{
Bram Moolenaar33570922005-01-25 22:26:29 +000020964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020966 rettv = alloc_tv();
20967 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020969 rettv->v_type = VAR_STRING;
20970 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 }
20972 else
20973 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020974 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975}
20976
20977/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020978 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020981free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982{
20983 if (varp != NULL)
20984 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020985 switch (varp->v_type)
20986 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020987 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020988 func_unref(varp->vval.v_string);
20989 /*FALLTHROUGH*/
20990 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020991 vim_free(varp->vval.v_string);
20992 break;
20993 case VAR_LIST:
20994 list_unref(varp->vval.v_list);
20995 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020996 case VAR_DICT:
20997 dict_unref(varp->vval.v_dict);
20998 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020999 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021000#ifdef FEAT_FLOAT
21001 case VAR_FLOAT:
21002#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021003 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021004 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021005 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021006 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021007 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021008 break;
21009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 vim_free(varp);
21011 }
21012}
21013
21014/*
21015 * Free the memory for a variable value and set the value to NULL or 0.
21016 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021018clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019{
21020 if (varp != NULL)
21021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021022 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021024 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021025 func_unref(varp->vval.v_string);
21026 /*FALLTHROUGH*/
21027 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021028 vim_free(varp->vval.v_string);
21029 varp->vval.v_string = NULL;
21030 break;
21031 case VAR_LIST:
21032 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021033 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021034 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021035 case VAR_DICT:
21036 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021037 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021038 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021039 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021040 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021041 varp->vval.v_number = 0;
21042 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021043#ifdef FEAT_FLOAT
21044 case VAR_FLOAT:
21045 varp->vval.v_float = 0.0;
21046 break;
21047#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021048 case VAR_UNKNOWN:
21049 break;
21050 default:
21051 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021053 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 }
21055}
21056
21057/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021058 * Set the value of a variable to NULL without freeing items.
21059 */
21060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021061init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021062{
21063 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021064 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021065}
21066
21067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 * Get the number value of a variable.
21069 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021070 * For incompatible types, return 0.
21071 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21072 * caller of incompatible types: it sets *denote to TRUE if "denote"
21073 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 */
21075 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021076get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021078 int error = FALSE;
21079
21080 return get_tv_number_chk(varp, &error); /* return 0L on error */
21081}
21082
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021083 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021084get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021086 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021088 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021090 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021091 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021092#ifdef FEAT_FLOAT
21093 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021094 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021095 break;
21096#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021097 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021098 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021099 break;
21100 case VAR_STRING:
21101 if (varp->vval.v_string != NULL)
21102 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021103 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021104 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021105 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021106 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021107 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021108 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021109 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021111 case VAR_SPECIAL:
21112 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21113 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021114 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021115 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021118 if (denote == NULL) /* useful for values that must be unsigned */
21119 n = -1;
21120 else
21121 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021122 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123}
21124
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021125#ifdef FEAT_FLOAT
21126 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021127get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021128{
21129 switch (varp->v_type)
21130 {
21131 case VAR_NUMBER:
21132 return (float_T)(varp->vval.v_number);
21133#ifdef FEAT_FLOAT
21134 case VAR_FLOAT:
21135 return varp->vval.v_float;
21136 break;
21137#endif
21138 case VAR_FUNC:
21139 EMSG(_("E891: Using a Funcref as a Float"));
21140 break;
21141 case VAR_STRING:
21142 EMSG(_("E892: Using a String as a Float"));
21143 break;
21144 case VAR_LIST:
21145 EMSG(_("E893: Using a List as a Float"));
21146 break;
21147 case VAR_DICT:
21148 EMSG(_("E894: Using a Dictionary as a Float"));
21149 break;
21150 default:
21151 EMSG2(_(e_intern2), "get_tv_float()");
21152 break;
21153 }
21154 return 0;
21155}
21156#endif
21157
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021159 * Get the lnum from the first argument.
21160 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021161 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 */
21163 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021164get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165{
Bram Moolenaar33570922005-01-25 22:26:29 +000021166 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 linenr_T lnum;
21168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021169 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 if (lnum == 0) /* no valid number, try using line() */
21171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021172 rettv.v_type = VAR_NUMBER;
21173 f_line(argvars, &rettv);
21174 lnum = rettv.vval.v_number;
21175 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 }
21177 return lnum;
21178}
21179
21180/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021181 * Get the lnum from the first argument.
21182 * Also accepts "$", then "buf" is used.
21183 * Returns 0 on error.
21184 */
21185 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021186get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021187{
21188 if (argvars[0].v_type == VAR_STRING
21189 && argvars[0].vval.v_string != NULL
21190 && argvars[0].vval.v_string[0] == '$'
21191 && buf != NULL)
21192 return buf->b_ml.ml_line_count;
21193 return get_tv_number_chk(&argvars[0], NULL);
21194}
21195
21196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 * Get the string value of a variable.
21198 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021199 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21200 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 * If the String variable has never been set, return an empty string.
21202 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021203 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21204 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 */
21206 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021207get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208{
21209 static char_u mybuf[NUMBUFLEN];
21210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021211 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212}
21213
21214 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021215get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021217 char_u *res = get_tv_string_buf_chk(varp, buf);
21218
21219 return res != NULL ? res : (char_u *)"";
21220}
21221
Bram Moolenaar7d647822014-04-05 21:28:56 +020021222/*
21223 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21224 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021225 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021226get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021227{
21228 static char_u mybuf[NUMBUFLEN];
21229
21230 return get_tv_string_buf_chk(varp, mybuf);
21231}
21232
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021234get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021235{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021236 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021238 case VAR_NUMBER:
21239 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21240 return buf;
21241 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021242 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021243 break;
21244 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021245 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021246 break;
21247 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021248 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021249 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021250#ifdef FEAT_FLOAT
21251 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021252 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021253 break;
21254#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021255 case VAR_STRING:
21256 if (varp->vval.v_string != NULL)
21257 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021258 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021259 case VAR_SPECIAL:
21260 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21261 return buf;
21262
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021263 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021264 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021267 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268}
21269
21270/*
21271 * Find variable "name" in the list of variables.
21272 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021273 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021274 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021275 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021277 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021278find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282
Bram Moolenaara7043832005-01-21 11:56:39 +000021283 ht = find_var_ht(name, &varname);
21284 if (htp != NULL)
21285 *htp = ht;
21286 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021288 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289}
21290
21291/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021292 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021293 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021295 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021296find_var_in_ht(
21297 hashtab_T *ht,
21298 int htname,
21299 char_u *varname,
21300 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021301{
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 hashitem_T *hi;
21303
21304 if (*varname == NUL)
21305 {
21306 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021307 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021308 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021309 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021310 case 'g': return &globvars_var;
21311 case 'v': return &vimvars_var;
21312 case 'b': return &curbuf->b_bufvar;
21313 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021314#ifdef FEAT_WINDOWS
21315 case 't': return &curtab->tp_winvar;
21316#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021317 case 'l': return current_funccal == NULL
21318 ? NULL : &current_funccal->l_vars_var;
21319 case 'a': return current_funccal == NULL
21320 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 }
21322 return NULL;
21323 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021324
21325 hi = hash_find(ht, varname);
21326 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021327 {
21328 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021329 * worked find the variable again. Don't auto-load a script if it was
21330 * loaded already, otherwise it would be loaded every time when
21331 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021332 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021333 {
21334 /* Note: script_autoload() may make "hi" invalid. It must either
21335 * be obtained again or not used. */
21336 if (!script_autoload(varname, FALSE) || aborting())
21337 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021338 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021339 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021340 if (HASHITEM_EMPTY(hi))
21341 return NULL;
21342 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021343 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021344}
21345
21346/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021347 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021348 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021349 * Set "varname" to the start of name without ':'.
21350 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021351 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021352find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021354 hashitem_T *hi;
21355
Bram Moolenaar73627d02015-08-11 15:46:09 +020021356 if (name[0] == NUL)
21357 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 if (name[1] != ':')
21359 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021360 /* The name must not start with a colon or #. */
21361 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 return NULL;
21363 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021364
21365 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021366 hi = hash_find(&compat_hashtab, name);
21367 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021368 return &compat_hashtab;
21369
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021371 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021372 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 }
21374 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021375 if (*name == 'g') /* global variable */
21376 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021377 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21378 */
21379 if (vim_strchr(name + 2, ':') != NULL
21380 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021381 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021383 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021385 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021386#ifdef FEAT_WINDOWS
21387 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021388 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021389#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021390 if (*name == 'v') /* v: variable */
21391 return &vimvarht;
21392 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021393 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021394 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021395 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 if (*name == 's' /* script variable */
21397 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21398 return &SCRIPT_VARS(current_SID);
21399 return NULL;
21400}
21401
21402/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021403 * Get function call environment based on bactrace debug level
21404 */
21405 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021406get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021407{
21408 int i;
21409 funccall_T *funccal;
21410 funccall_T *temp_funccal;
21411
21412 funccal = current_funccal;
21413 if (debug_backtrace_level > 0)
21414 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021415 for (i = 0; i < debug_backtrace_level; i++)
21416 {
21417 temp_funccal = funccal->caller;
21418 if (temp_funccal)
21419 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021420 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021421 /* backtrace level overflow. reset to max */
21422 debug_backtrace_level = i;
21423 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021424 }
21425 return funccal;
21426}
21427
21428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021430 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 * Returns NULL when it doesn't exist.
21432 */
21433 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021434get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435{
Bram Moolenaar33570922005-01-25 22:26:29 +000021436 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021438 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 if (v == NULL)
21440 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021441 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442}
21443
21444/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021445 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 * sourcing this script and when executing functions defined in the script.
21447 */
21448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021449new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450{
Bram Moolenaara7043832005-01-21 11:56:39 +000021451 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021452 hashtab_T *ht;
21453 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021454
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21456 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021457 /* Re-allocating ga_data means that an ht_array pointing to
21458 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021460 for (i = 1; i <= ga_scripts.ga_len; ++i)
21461 {
21462 ht = &SCRIPT_VARS(i);
21463 if (ht->ht_mask == HT_INIT_SIZE - 1)
21464 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021465 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021467 }
21468
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 while (ga_scripts.ga_len < id)
21470 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021471 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021472 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021473 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 }
21476 }
21477}
21478
21479/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021480 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21481 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 */
21483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021484init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485{
Bram Moolenaar33570922005-01-25 22:26:29 +000021486 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021487 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021488 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021489 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021490 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021491 dict_var->di_tv.vval.v_dict = dict;
21492 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021493 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21495 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496}
21497
21498/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021499 * Unreference a dictionary initialized by init_var_dict().
21500 */
21501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021502unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021503{
21504 /* Now the dict needs to be freed if no one else is using it, go back to
21505 * normal reference counting. */
21506 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21507 dict_unref(dict);
21508}
21509
21510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021512 * Frees all allocated variables and the value they contain.
21513 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 */
21515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021516vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021517{
21518 vars_clear_ext(ht, TRUE);
21519}
21520
21521/*
21522 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21523 */
21524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021525vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526{
Bram Moolenaara7043832005-01-21 11:56:39 +000021527 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021528 hashitem_T *hi;
21529 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530
Bram Moolenaar33570922005-01-25 22:26:29 +000021531 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021532 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021533 for (hi = ht->ht_array; todo > 0; ++hi)
21534 {
21535 if (!HASHITEM_EMPTY(hi))
21536 {
21537 --todo;
21538
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021540 * ht_array might change then. hash_clear() takes care of it
21541 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 v = HI2DI(hi);
21543 if (free_val)
21544 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021545 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021547 }
21548 }
21549 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021550 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551}
21552
Bram Moolenaara7043832005-01-21 11:56:39 +000021553/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021554 * Delete a variable from hashtab "ht" at item "hi".
21555 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021558delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559{
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021561
21562 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 clear_tv(&di->di_tv);
21564 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565}
21566
21567/*
21568 * List the value of one internal variable.
21569 */
21570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021571list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021573 char_u *tofree;
21574 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021575 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021577 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021578 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021579 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021580 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581}
21582
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021584list_one_var_a(
21585 char_u *prefix,
21586 char_u *name,
21587 int type,
21588 char_u *string,
21589 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590{
Bram Moolenaar31859182007-08-14 20:41:13 +000021591 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21592 msg_start();
21593 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 if (name != NULL) /* "a:" vars don't have a name stored */
21595 msg_puts(name);
21596 msg_putchar(' ');
21597 msg_advance(22);
21598 if (type == VAR_NUMBER)
21599 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021600 else if (type == VAR_FUNC)
21601 msg_putchar('*');
21602 else if (type == VAR_LIST)
21603 {
21604 msg_putchar('[');
21605 if (*string == '[')
21606 ++string;
21607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021608 else if (type == VAR_DICT)
21609 {
21610 msg_putchar('{');
21611 if (*string == '{')
21612 ++string;
21613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 else
21615 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021616
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021618
21619 if (type == VAR_FUNC)
21620 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021621 if (*first)
21622 {
21623 msg_clr_eos();
21624 *first = FALSE;
21625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626}
21627
21628/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021629 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 * If the variable already exists, the value is updated.
21631 * Otherwise the variable is created.
21632 */
21633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021634set_var(
21635 char_u *name,
21636 typval_T *tv,
21637 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638{
Bram Moolenaar33570922005-01-25 22:26:29 +000021639 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021641 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021643 ht = find_var_ht(name, &varname);
21644 if (ht == NULL || *varname == NUL)
21645 {
21646 EMSG2(_(e_illvar), name);
21647 return;
21648 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021649 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021650
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021651 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21652 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021653
Bram Moolenaar33570922005-01-25 22:26:29 +000021654 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021657 if (var_check_ro(v->di_flags, name, FALSE)
21658 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 return;
21660 if (v->di_tv.v_type != tv->v_type
21661 && !((v->di_tv.v_type == VAR_STRING
21662 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021663 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021664 || tv->v_type == VAR_NUMBER))
21665#ifdef FEAT_FLOAT
21666 && !((v->di_tv.v_type == VAR_NUMBER
21667 || v->di_tv.v_type == VAR_FLOAT)
21668 && (tv->v_type == VAR_NUMBER
21669 || tv->v_type == VAR_FLOAT))
21670#endif
21671 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021672 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021673 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 return;
21675 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021676
21677 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021678 * Handle setting internal v: variables separately where needed to
21679 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 */
21681 if (ht == &vimvarht)
21682 {
21683 if (v->di_tv.v_type == VAR_STRING)
21684 {
21685 vim_free(v->di_tv.vval.v_string);
21686 if (copy || tv->v_type != VAR_STRING)
21687 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21688 else
21689 {
21690 /* Take over the string to avoid an extra alloc/free. */
21691 v->di_tv.vval.v_string = tv->vval.v_string;
21692 tv->vval.v_string = NULL;
21693 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021694 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021695 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021696 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021697 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021698 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021699 if (STRCMP(varname, "searchforward") == 0)
21700 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021701#ifdef FEAT_SEARCH_EXTRA
21702 else if (STRCMP(varname, "hlsearch") == 0)
21703 {
21704 no_hlsearch = !v->di_tv.vval.v_number;
21705 redraw_all_later(SOME_VALID);
21706 }
21707#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021708 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021709 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021710 else if (v->di_tv.v_type != tv->v_type)
21711 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021712 }
21713
21714 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 }
21716 else /* add a new variable */
21717 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021718 /* Can't add "v:" variable. */
21719 if (ht == &vimvarht)
21720 {
21721 EMSG2(_(e_illvar), name);
21722 return;
21723 }
21724
Bram Moolenaar92124a32005-06-17 22:03:40 +000021725 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021726 if (!valid_varname(varname))
21727 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021728
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021729 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21730 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021731 if (v == NULL)
21732 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021734 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021736 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 return;
21738 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021739 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021741
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021742 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021744 else
21745 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021746 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021747 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021748 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750}
21751
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021752/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021753 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 * Also give an error message.
21755 */
21756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021757var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021758{
21759 if (flags & DI_FLAGS_RO)
21760 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021761 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 return TRUE;
21763 }
21764 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21765 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021766 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 return TRUE;
21768 }
21769 return FALSE;
21770}
21771
21772/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021773 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21774 * Also give an error message.
21775 */
21776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021777var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021778{
21779 if (flags & DI_FLAGS_FIX)
21780 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021781 EMSG2(_("E795: Cannot delete variable %s"),
21782 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021783 return TRUE;
21784 }
21785 return FALSE;
21786}
21787
21788/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021789 * Check if a funcref is assigned to a valid variable name.
21790 * Return TRUE and give an error if not.
21791 */
21792 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021793var_check_func_name(
21794 char_u *name, /* points to start of variable name */
21795 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021796{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021797 /* Allow for w: b: s: and t:. */
21798 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021799 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21800 ? name[2] : name[0]))
21801 {
21802 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21803 name);
21804 return TRUE;
21805 }
21806 /* Don't allow hiding a function. When "v" is not NULL we might be
21807 * assigning another function to the same var, the type is checked
21808 * below. */
21809 if (new_var && function_exists(name))
21810 {
21811 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21812 name);
21813 return TRUE;
21814 }
21815 return FALSE;
21816}
21817
21818/*
21819 * Check if a variable name is valid.
21820 * Return FALSE and give an error if not.
21821 */
21822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021823valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021824{
21825 char_u *p;
21826
21827 for (p = varname; *p != NUL; ++p)
21828 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21829 && *p != AUTOLOAD_CHAR)
21830 {
21831 EMSG2(_(e_illvar), varname);
21832 return FALSE;
21833 }
21834 return TRUE;
21835}
21836
21837/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021838 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021839 * Also give an error message, using "name" or _("name") when use_gettext is
21840 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021841 */
21842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021843tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021844{
21845 if (lock & VAR_LOCKED)
21846 {
21847 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021848 name == NULL ? (char_u *)_("Unknown")
21849 : use_gettext ? (char_u *)_(name)
21850 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021851 return TRUE;
21852 }
21853 if (lock & VAR_FIXED)
21854 {
21855 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021856 name == NULL ? (char_u *)_("Unknown")
21857 : use_gettext ? (char_u *)_(name)
21858 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021859 return TRUE;
21860 }
21861 return FALSE;
21862}
21863
21864/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021866 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021867 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021868 * It is OK for "from" and "to" to point to the same item. This is used to
21869 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021870 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021872copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021874 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021875 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021876 switch (from->v_type)
21877 {
21878 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021879 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021880 to->vval.v_number = from->vval.v_number;
21881 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021882#ifdef FEAT_FLOAT
21883 case VAR_FLOAT:
21884 to->vval.v_float = from->vval.v_float;
21885 break;
21886#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021887 case VAR_STRING:
21888 case VAR_FUNC:
21889 if (from->vval.v_string == NULL)
21890 to->vval.v_string = NULL;
21891 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021892 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021893 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021894 if (from->v_type == VAR_FUNC)
21895 func_ref(to->vval.v_string);
21896 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897 break;
21898 case VAR_LIST:
21899 if (from->vval.v_list == NULL)
21900 to->vval.v_list = NULL;
21901 else
21902 {
21903 to->vval.v_list = from->vval.v_list;
21904 ++to->vval.v_list->lv_refcount;
21905 }
21906 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021907 case VAR_DICT:
21908 if (from->vval.v_dict == NULL)
21909 to->vval.v_dict = NULL;
21910 else
21911 {
21912 to->vval.v_dict = from->vval.v_dict;
21913 ++to->vval.v_dict->dv_refcount;
21914 }
21915 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021916 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021917 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021918 break;
21919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920}
21921
21922/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021923 * Make a copy of an item.
21924 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021925 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21926 * reference to an already copied list/dict can be used.
21927 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021928 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021930item_copy(
21931 typval_T *from,
21932 typval_T *to,
21933 int deep,
21934 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021935{
21936 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021937 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021938
Bram Moolenaar33570922005-01-25 22:26:29 +000021939 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021940 {
21941 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021942 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021943 }
21944 ++recurse;
21945
21946 switch (from->v_type)
21947 {
21948 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021949#ifdef FEAT_FLOAT
21950 case VAR_FLOAT:
21951#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021952 case VAR_STRING:
21953 case VAR_FUNC:
21954 copy_tv(from, to);
21955 break;
21956 case VAR_LIST:
21957 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021958 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021959 if (from->vval.v_list == NULL)
21960 to->vval.v_list = NULL;
21961 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21962 {
21963 /* use the copy made earlier */
21964 to->vval.v_list = from->vval.v_list->lv_copylist;
21965 ++to->vval.v_list->lv_refcount;
21966 }
21967 else
21968 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21969 if (to->vval.v_list == NULL)
21970 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971 break;
21972 case VAR_DICT:
21973 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021974 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021975 if (from->vval.v_dict == NULL)
21976 to->vval.v_dict = NULL;
21977 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21978 {
21979 /* use the copy made earlier */
21980 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21981 ++to->vval.v_dict->dv_refcount;
21982 }
21983 else
21984 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21985 if (to->vval.v_dict == NULL)
21986 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021987 break;
21988 default:
21989 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021990 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021991 }
21992 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021993 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021994}
21995
21996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 * ":echo expr1 ..." print each argument separated with a space, add a
21998 * newline at the end.
21999 * ":echon expr1 ..." print each argument plain.
22000 */
22001 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022002ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003{
22004 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022006 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 char_u *p;
22008 int needclr = TRUE;
22009 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022010 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011
22012 if (eap->skip)
22013 ++emsg_skip;
22014 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22015 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022016 /* If eval1() causes an error message the text from the command may
22017 * still need to be cleared. E.g., "echo 22,44". */
22018 need_clr_eos = needclr;
22019
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022021 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 {
22023 /*
22024 * Report the invalid expression unless the expression evaluation
22025 * has been cancelled due to an aborting error, an interrupt, or an
22026 * exception.
22027 */
22028 if (!aborting())
22029 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022030 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 break;
22032 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022033 need_clr_eos = FALSE;
22034
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 if (!eap->skip)
22036 {
22037 if (atstart)
22038 {
22039 atstart = FALSE;
22040 /* Call msg_start() after eval1(), evaluating the expression
22041 * may cause a message to appear. */
22042 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022043 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022044 /* Mark the saved text as finishing the line, so that what
22045 * follows is displayed on a new line when scrolling back
22046 * at the more prompt. */
22047 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 }
22051 else if (eap->cmdidx == CMD_echo)
22052 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022053 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022054 if (p != NULL)
22055 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022057 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022059 if (*p != TAB && needclr)
22060 {
22061 /* remove any text still there from the command */
22062 msg_clr_eos();
22063 needclr = FALSE;
22064 }
22065 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 }
22067 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022068 {
22069#ifdef FEAT_MBYTE
22070 if (has_mbyte)
22071 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022072 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022073
22074 (void)msg_outtrans_len_attr(p, i, echo_attr);
22075 p += i - 1;
22076 }
22077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022079 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022082 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022084 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 arg = skipwhite(arg);
22086 }
22087 eap->nextcmd = check_nextcmd(arg);
22088
22089 if (eap->skip)
22090 --emsg_skip;
22091 else
22092 {
22093 /* remove text that may still be there from the command */
22094 if (needclr)
22095 msg_clr_eos();
22096 if (eap->cmdidx == CMD_echo)
22097 msg_end();
22098 }
22099}
22100
22101/*
22102 * ":echohl {name}".
22103 */
22104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022105ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106{
22107 int id;
22108
22109 id = syn_name2id(eap->arg);
22110 if (id == 0)
22111 echo_attr = 0;
22112 else
22113 echo_attr = syn_id2attr(id);
22114}
22115
22116/*
22117 * ":execute expr1 ..." execute the result of an expression.
22118 * ":echomsg expr1 ..." Print a message
22119 * ":echoerr expr1 ..." Print an error
22120 * Each gets spaces around each argument and a newline at the end for
22121 * echo commands
22122 */
22123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022124ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125{
22126 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022127 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 int ret = OK;
22129 char_u *p;
22130 garray_T ga;
22131 int len;
22132 int save_did_emsg;
22133
22134 ga_init2(&ga, 1, 80);
22135
22136 if (eap->skip)
22137 ++emsg_skip;
22138 while (*arg != NUL && *arg != '|' && *arg != '\n')
22139 {
22140 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022141 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 {
22143 /*
22144 * Report the invalid expression unless the expression evaluation
22145 * has been cancelled due to an aborting error, an interrupt, or an
22146 * exception.
22147 */
22148 if (!aborting())
22149 EMSG2(_(e_invexpr2), p);
22150 ret = FAIL;
22151 break;
22152 }
22153
22154 if (!eap->skip)
22155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022156 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 len = (int)STRLEN(p);
22158 if (ga_grow(&ga, len + 2) == FAIL)
22159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022160 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 ret = FAIL;
22162 break;
22163 }
22164 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 ga.ga_len += len;
22168 }
22169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022170 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 arg = skipwhite(arg);
22172 }
22173
22174 if (ret != FAIL && ga.ga_data != NULL)
22175 {
22176 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022179 out_flush();
22180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 else if (eap->cmdidx == CMD_echoerr)
22182 {
22183 /* We don't want to abort following commands, restore did_emsg. */
22184 save_did_emsg = did_emsg;
22185 EMSG((char_u *)ga.ga_data);
22186 if (!force_abort)
22187 did_emsg = save_did_emsg;
22188 }
22189 else if (eap->cmdidx == CMD_execute)
22190 do_cmdline((char_u *)ga.ga_data,
22191 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22192 }
22193
22194 ga_clear(&ga);
22195
22196 if (eap->skip)
22197 --emsg_skip;
22198
22199 eap->nextcmd = check_nextcmd(arg);
22200}
22201
22202/*
22203 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22204 * "arg" points to the "&" or '+' when called, to "option" when returning.
22205 * Returns NULL when no option name found. Otherwise pointer to the char
22206 * after the option name.
22207 */
22208 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022209find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210{
22211 char_u *p = *arg;
22212
22213 ++p;
22214 if (*p == 'g' && p[1] == ':')
22215 {
22216 *opt_flags = OPT_GLOBAL;
22217 p += 2;
22218 }
22219 else if (*p == 'l' && p[1] == ':')
22220 {
22221 *opt_flags = OPT_LOCAL;
22222 p += 2;
22223 }
22224 else
22225 *opt_flags = 0;
22226
22227 if (!ASCII_ISALPHA(*p))
22228 return NULL;
22229 *arg = p;
22230
22231 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22232 p += 4; /* termcap option */
22233 else
22234 while (ASCII_ISALPHA(*p))
22235 ++p;
22236 return p;
22237}
22238
22239/*
22240 * ":function"
22241 */
22242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022243ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244{
22245 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022246 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 int j;
22248 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022250 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 char_u *name = NULL;
22252 char_u *p;
22253 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022254 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 garray_T newargs;
22256 garray_T newlines;
22257 int varargs = FALSE;
22258 int mustend = FALSE;
22259 int flags = 0;
22260 ufunc_T *fp;
22261 int indent;
22262 int nesting;
22263 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022264 dictitem_T *v;
22265 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022266 static int func_nr = 0; /* number for nameless function */
22267 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022268 hashtab_T *ht;
22269 int todo;
22270 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022271 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272
22273 /*
22274 * ":function" without argument: list functions.
22275 */
22276 if (ends_excmd(*eap->arg))
22277 {
22278 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022279 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022280 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022281 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022282 {
22283 if (!HASHITEM_EMPTY(hi))
22284 {
22285 --todo;
22286 fp = HI2UF(hi);
22287 if (!isdigit(*fp->uf_name))
22288 list_func_head(fp, FALSE);
22289 }
22290 }
22291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 eap->nextcmd = check_nextcmd(eap->arg);
22293 return;
22294 }
22295
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022296 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022297 * ":function /pat": list functions matching pattern.
22298 */
22299 if (*eap->arg == '/')
22300 {
22301 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22302 if (!eap->skip)
22303 {
22304 regmatch_T regmatch;
22305
22306 c = *p;
22307 *p = NUL;
22308 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22309 *p = c;
22310 if (regmatch.regprog != NULL)
22311 {
22312 regmatch.rm_ic = p_ic;
22313
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022314 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022315 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22316 {
22317 if (!HASHITEM_EMPTY(hi))
22318 {
22319 --todo;
22320 fp = HI2UF(hi);
22321 if (!isdigit(*fp->uf_name)
22322 && vim_regexec(&regmatch, fp->uf_name, 0))
22323 list_func_head(fp, FALSE);
22324 }
22325 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022326 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022327 }
22328 }
22329 if (*p == '/')
22330 ++p;
22331 eap->nextcmd = check_nextcmd(p);
22332 return;
22333 }
22334
22335 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022336 * Get the function name. There are these situations:
22337 * func normal function name
22338 * "name" == func, "fudi.fd_dict" == NULL
22339 * dict.func new dictionary entry
22340 * "name" == NULL, "fudi.fd_dict" set,
22341 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22342 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022343 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022344 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22345 * dict.func existing dict entry that's not a Funcref
22346 * "name" == NULL, "fudi.fd_dict" set,
22347 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022348 * s:func script-local function name
22349 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022352 name = trans_function_name(&p, eap->skip, 0, &fudi);
22353 paren = (vim_strchr(p, '(') != NULL);
22354 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 {
22356 /*
22357 * Return on an invalid expression in braces, unless the expression
22358 * evaluation has been cancelled due to an aborting error, an
22359 * interrupt, or an exception.
22360 */
22361 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022363 if (!eap->skip && fudi.fd_newkey != NULL)
22364 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022365 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 else
22369 eap->skip = TRUE;
22370 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022371
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 /* An error in a function call during evaluation of an expression in magic
22373 * braces should not cause the function not to be defined. */
22374 saved_did_emsg = did_emsg;
22375 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376
22377 /*
22378 * ":function func" with only function name: list function.
22379 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022380 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 {
22382 if (!ends_excmd(*skipwhite(p)))
22383 {
22384 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 }
22387 eap->nextcmd = check_nextcmd(p);
22388 if (eap->nextcmd != NULL)
22389 *p = NUL;
22390 if (!eap->skip && !got_int)
22391 {
22392 fp = find_func(name);
22393 if (fp != NULL)
22394 {
22395 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022396 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022398 if (FUNCLINE(fp, j) == NULL)
22399 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 msg_putchar('\n');
22401 msg_outnum((long)(j + 1));
22402 if (j < 9)
22403 msg_putchar(' ');
22404 if (j < 99)
22405 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022406 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 out_flush(); /* show a line at a time */
22408 ui_breakcheck();
22409 }
22410 if (!got_int)
22411 {
22412 msg_putchar('\n');
22413 msg_puts((char_u *)" endfunction");
22414 }
22415 }
22416 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022417 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 }
22421
22422 /*
22423 * ":function name(arg1, arg2)" Define function.
22424 */
22425 p = skipwhite(p);
22426 if (*p != '(')
22427 {
22428 if (!eap->skip)
22429 {
22430 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022431 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 }
22433 /* attempt to continue by skipping some text */
22434 if (vim_strchr(p, '(') != NULL)
22435 p = vim_strchr(p, '(');
22436 }
22437 p = skipwhite(p + 1);
22438
22439 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22440 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22441
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022442 if (!eap->skip)
22443 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022444 /* Check the name of the function. Unless it's a dictionary function
22445 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022446 if (name != NULL)
22447 arg = name;
22448 else
22449 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022450 if (arg != NULL && (fudi.fd_di == NULL
22451 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022452 {
22453 if (*arg == K_SPECIAL)
22454 j = 3;
22455 else
22456 j = 0;
22457 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22458 : eval_isnamec(arg[j])))
22459 ++j;
22460 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022461 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022462 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022463 /* Disallow using the g: dict. */
22464 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22465 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022466 }
22467
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 /*
22469 * Isolate the arguments: "arg1, arg2, ...)"
22470 */
22471 while (*p != ')')
22472 {
22473 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22474 {
22475 varargs = TRUE;
22476 p += 3;
22477 mustend = TRUE;
22478 }
22479 else
22480 {
22481 arg = p;
22482 while (ASCII_ISALNUM(*p) || *p == '_')
22483 ++p;
22484 if (arg == p || isdigit(*arg)
22485 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22486 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22487 {
22488 if (!eap->skip)
22489 EMSG2(_("E125: Illegal argument: %s"), arg);
22490 break;
22491 }
22492 if (ga_grow(&newargs, 1) == FAIL)
22493 goto erret;
22494 c = *p;
22495 *p = NUL;
22496 arg = vim_strsave(arg);
22497 if (arg == NULL)
22498 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022499
22500 /* Check for duplicate argument name. */
22501 for (i = 0; i < newargs.ga_len; ++i)
22502 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22503 {
22504 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022505 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022506 goto erret;
22507 }
22508
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22510 *p = c;
22511 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 if (*p == ',')
22513 ++p;
22514 else
22515 mustend = TRUE;
22516 }
22517 p = skipwhite(p);
22518 if (mustend && *p != ')')
22519 {
22520 if (!eap->skip)
22521 EMSG2(_(e_invarg2), eap->arg);
22522 break;
22523 }
22524 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022525 if (*p != ')')
22526 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 ++p; /* skip the ')' */
22528
Bram Moolenaare9a41262005-01-15 22:18:47 +000022529 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 for (;;)
22531 {
22532 p = skipwhite(p);
22533 if (STRNCMP(p, "range", 5) == 0)
22534 {
22535 flags |= FC_RANGE;
22536 p += 5;
22537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022538 else if (STRNCMP(p, "dict", 4) == 0)
22539 {
22540 flags |= FC_DICT;
22541 p += 4;
22542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 else if (STRNCMP(p, "abort", 5) == 0)
22544 {
22545 flags |= FC_ABORT;
22546 p += 5;
22547 }
22548 else
22549 break;
22550 }
22551
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022552 /* When there is a line break use what follows for the function body.
22553 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22554 if (*p == '\n')
22555 line_arg = p + 1;
22556 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 EMSG(_(e_trailing));
22558
22559 /*
22560 * Read the body of the function, until ":endfunction" is found.
22561 */
22562 if (KeyTyped)
22563 {
22564 /* Check if the function already exists, don't let the user type the
22565 * whole function before telling him it doesn't work! For a script we
22566 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022567 if (!eap->skip && !eap->forceit)
22568 {
22569 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22570 EMSG(_(e_funcdict));
22571 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022572 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022575 if (!eap->skip && did_emsg)
22576 goto erret;
22577
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 msg_putchar('\n'); /* don't overwrite the function name */
22579 cmdline_row = msg_row;
22580 }
22581
22582 indent = 2;
22583 nesting = 0;
22584 for (;;)
22585 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022586 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022587 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022588 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022589 saved_wait_return = FALSE;
22590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022592 sourcing_lnum_off = sourcing_lnum;
22593
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022594 if (line_arg != NULL)
22595 {
22596 /* Use eap->arg, split up in parts by line breaks. */
22597 theline = line_arg;
22598 p = vim_strchr(theline, '\n');
22599 if (p == NULL)
22600 line_arg += STRLEN(line_arg);
22601 else
22602 {
22603 *p = NUL;
22604 line_arg = p + 1;
22605 }
22606 }
22607 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 theline = getcmdline(':', 0L, indent);
22609 else
22610 theline = eap->getline(':', eap->cookie, indent);
22611 if (KeyTyped)
22612 lines_left = Rows - 1;
22613 if (theline == NULL)
22614 {
22615 EMSG(_("E126: Missing :endfunction"));
22616 goto erret;
22617 }
22618
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022619 /* Detect line continuation: sourcing_lnum increased more than one. */
22620 if (sourcing_lnum > sourcing_lnum_off + 1)
22621 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22622 else
22623 sourcing_lnum_off = 0;
22624
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 if (skip_until != NULL)
22626 {
22627 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22628 * don't check for ":endfunc". */
22629 if (STRCMP(theline, skip_until) == 0)
22630 {
22631 vim_free(skip_until);
22632 skip_until = NULL;
22633 }
22634 }
22635 else
22636 {
22637 /* skip ':' and blanks*/
22638 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22639 ;
22640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022641 /* Check for "endfunction". */
22642 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022644 if (line_arg == NULL)
22645 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 break;
22647 }
22648
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022649 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 * at "end". */
22651 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22652 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022653 else if (STRNCMP(p, "if", 2) == 0
22654 || STRNCMP(p, "wh", 2) == 0
22655 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 || STRNCMP(p, "try", 3) == 0)
22657 indent += 2;
22658
22659 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022660 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022662 if (*p == '!')
22663 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022665 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22666 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022668 ++nesting;
22669 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 }
22671 }
22672
22673 /* Check for ":append" or ":insert". */
22674 p = skip_range(p, NULL);
22675 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22676 || (p[0] == 'i'
22677 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22678 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22679 skip_until = vim_strsave((char_u *)".");
22680
22681 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22682 arg = skipwhite(skiptowhite(p));
22683 if (arg[0] == '<' && arg[1] =='<'
22684 && ((p[0] == 'p' && p[1] == 'y'
22685 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22686 || (p[0] == 'p' && p[1] == 'e'
22687 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22688 || (p[0] == 't' && p[1] == 'c'
22689 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022690 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22691 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22693 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022694 || (p[0] == 'm' && p[1] == 'z'
22695 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 ))
22697 {
22698 /* ":python <<" continues until a dot, like ":append" */
22699 p = skipwhite(arg + 2);
22700 if (*p == NUL)
22701 skip_until = vim_strsave((char_u *)".");
22702 else
22703 skip_until = vim_strsave(p);
22704 }
22705 }
22706
22707 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022708 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022709 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022710 if (line_arg == NULL)
22711 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022713 }
22714
22715 /* Copy the line to newly allocated memory. get_one_sourceline()
22716 * allocates 250 bytes per line, this saves 80% on average. The cost
22717 * is an extra alloc/free. */
22718 p = vim_strsave(theline);
22719 if (p != NULL)
22720 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022721 if (line_arg == NULL)
22722 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022723 theline = p;
22724 }
22725
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022726 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22727
22728 /* Add NULL lines for continuation lines, so that the line count is
22729 * equal to the index in the growarray. */
22730 while (sourcing_lnum_off-- > 0)
22731 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022732
22733 /* Check for end of eap->arg. */
22734 if (line_arg != NULL && *line_arg == NUL)
22735 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 }
22737
22738 /* Don't define the function when skipping commands or when an error was
22739 * detected. */
22740 if (eap->skip || did_emsg)
22741 goto erret;
22742
22743 /*
22744 * If there are no errors, add the function
22745 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022746 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022747 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022748 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022749 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022750 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022751 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022752 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022753 goto erret;
22754 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022755
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022756 fp = find_func(name);
22757 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022759 if (!eap->forceit)
22760 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022761 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762 goto erret;
22763 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022764 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022765 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022766 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022767 name);
22768 goto erret;
22769 }
22770 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022771 ga_clear_strings(&(fp->uf_args));
22772 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773 vim_free(name);
22774 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 }
22777 else
22778 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022779 char numbuf[20];
22780
22781 fp = NULL;
22782 if (fudi.fd_newkey == NULL && !eap->forceit)
22783 {
22784 EMSG(_(e_funcdict));
22785 goto erret;
22786 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022787 if (fudi.fd_di == NULL)
22788 {
22789 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022790 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022791 goto erret;
22792 }
22793 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022794 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022795 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022796
22797 /* Give the function a sequential number. Can only be used with a
22798 * Funcref! */
22799 vim_free(name);
22800 sprintf(numbuf, "%d", ++func_nr);
22801 name = vim_strsave((char_u *)numbuf);
22802 if (name == NULL)
22803 goto erret;
22804 }
22805
22806 if (fp == NULL)
22807 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022808 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022809 {
22810 int slen, plen;
22811 char_u *scriptname;
22812
22813 /* Check that the autoload name matches the script name. */
22814 j = FAIL;
22815 if (sourcing_name != NULL)
22816 {
22817 scriptname = autoload_name(name);
22818 if (scriptname != NULL)
22819 {
22820 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022821 plen = (int)STRLEN(p);
22822 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022823 if (slen > plen && fnamecmp(p,
22824 sourcing_name + slen - plen) == 0)
22825 j = OK;
22826 vim_free(scriptname);
22827 }
22828 }
22829 if (j == FAIL)
22830 {
22831 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22832 goto erret;
22833 }
22834 }
22835
22836 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 if (fp == NULL)
22838 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022839
22840 if (fudi.fd_dict != NULL)
22841 {
22842 if (fudi.fd_di == NULL)
22843 {
22844 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022845 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022846 if (fudi.fd_di == NULL)
22847 {
22848 vim_free(fp);
22849 goto erret;
22850 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022851 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22852 {
22853 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022854 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022855 goto erret;
22856 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022857 }
22858 else
22859 /* overwrite existing dict entry */
22860 clear_tv(&fudi.fd_di->di_tv);
22861 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022862 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022863 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022864 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022865
22866 /* behave like "dict" was used */
22867 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022868 }
22869
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022871 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022872 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22873 {
22874 vim_free(fp);
22875 goto erret;
22876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022878 fp->uf_args = newargs;
22879 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022880#ifdef FEAT_PROFILE
22881 fp->uf_tml_count = NULL;
22882 fp->uf_tml_total = NULL;
22883 fp->uf_tml_self = NULL;
22884 fp->uf_profiling = FALSE;
22885 if (prof_def_func())
22886 func_do_profile(fp);
22887#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022888 fp->uf_varargs = varargs;
22889 fp->uf_flags = flags;
22890 fp->uf_calls = 0;
22891 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022892 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893
22894erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 ga_clear_strings(&newargs);
22896 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022897ret_free:
22898 vim_free(skip_until);
22899 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022902 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903}
22904
22905/*
22906 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022907 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022909 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022910 * TFN_INT: internal function name OK
22911 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022912 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 * Advances "pp" to just after the function name (if no error).
22914 */
22915 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022916trans_function_name(
22917 char_u **pp,
22918 int skip, /* only find the end, don't evaluate */
22919 int flags,
22920 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022922 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 char_u *start;
22924 char_u *end;
22925 int lead;
22926 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022928 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022929
22930 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022931 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022933
22934 /* Check for hard coded <SNR>: already translated function ID (from a user
22935 * command). */
22936 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22937 && (*pp)[2] == (int)KE_SNR)
22938 {
22939 *pp += 3;
22940 len = get_id_len(pp) + 3;
22941 return vim_strnsave(start, len);
22942 }
22943
22944 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22945 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022947 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022949
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022950 /* Note that TFN_ flags use the same values as GLV_ flags. */
22951 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022952 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022953 if (end == start)
22954 {
22955 if (!skip)
22956 EMSG(_("E129: Function name required"));
22957 goto theend;
22958 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022959 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022960 {
22961 /*
22962 * Report an invalid expression in braces, unless the expression
22963 * evaluation has been cancelled due to an aborting error, an
22964 * interrupt, or an exception.
22965 */
22966 if (!aborting())
22967 {
22968 if (end != NULL)
22969 EMSG2(_(e_invarg2), start);
22970 }
22971 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022972 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022973 goto theend;
22974 }
22975
22976 if (lv.ll_tv != NULL)
22977 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022978 if (fdp != NULL)
22979 {
22980 fdp->fd_dict = lv.ll_dict;
22981 fdp->fd_newkey = lv.ll_newkey;
22982 lv.ll_newkey = NULL;
22983 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022984 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022985 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22986 {
22987 name = vim_strsave(lv.ll_tv->vval.v_string);
22988 *pp = end;
22989 }
22990 else
22991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022992 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22993 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022994 EMSG(_(e_funcref));
22995 else
22996 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022997 name = NULL;
22998 }
22999 goto theend;
23000 }
23001
23002 if (lv.ll_name == NULL)
23003 {
23004 /* Error found, but continue after the function name. */
23005 *pp = end;
23006 goto theend;
23007 }
23008
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023009 /* Check if the name is a Funcref. If so, use the value. */
23010 if (lv.ll_exp_name != NULL)
23011 {
23012 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023013 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023014 if (name == lv.ll_exp_name)
23015 name = NULL;
23016 }
23017 else
23018 {
23019 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023020 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023021 if (name == *pp)
23022 name = NULL;
23023 }
23024 if (name != NULL)
23025 {
23026 name = vim_strsave(name);
23027 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023028 if (STRNCMP(name, "<SNR>", 5) == 0)
23029 {
23030 /* Change "<SNR>" to the byte sequence. */
23031 name[0] = K_SPECIAL;
23032 name[1] = KS_EXTRA;
23033 name[2] = (int)KE_SNR;
23034 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23035 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023036 goto theend;
23037 }
23038
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023039 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023040 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023041 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023042 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23043 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23044 {
23045 /* When there was "s:" already or the name expanded to get a
23046 * leading "s:" then remove it. */
23047 lv.ll_name += 2;
23048 len -= 2;
23049 lead = 2;
23050 }
23051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023052 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023053 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023054 /* skip over "s:" and "g:" */
23055 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023056 lv.ll_name += 2;
23057 len = (int)(end - lv.ll_name);
23058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023059
23060 /*
23061 * Copy the function name to allocated memory.
23062 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23063 * Accept <SNR>123_name() outside a script.
23064 */
23065 if (skip)
23066 lead = 0; /* do nothing */
23067 else if (lead > 0)
23068 {
23069 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023070 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23071 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023072 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023073 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023074 if (current_SID <= 0)
23075 {
23076 EMSG(_(e_usingsid));
23077 goto theend;
23078 }
23079 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23080 lead += (int)STRLEN(sid_buf);
23081 }
23082 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023083 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023084 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023085 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023086 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023087 goto theend;
23088 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023089 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023090 {
23091 char_u *cp = vim_strchr(lv.ll_name, ':');
23092
23093 if (cp != NULL && cp < end)
23094 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023095 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023096 goto theend;
23097 }
23098 }
23099
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023100 name = alloc((unsigned)(len + lead + 1));
23101 if (name != NULL)
23102 {
23103 if (lead > 0)
23104 {
23105 name[0] = K_SPECIAL;
23106 name[1] = KS_EXTRA;
23107 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023108 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023109 STRCPY(name + 3, sid_buf);
23110 }
23111 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023112 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023113 }
23114 *pp = end;
23115
23116theend:
23117 clear_lval(&lv);
23118 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119}
23120
23121/*
23122 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23123 * Return 2 if "p" starts with "s:".
23124 * Return 0 otherwise.
23125 */
23126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023127eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023129 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23130 * the standard library function. */
23131 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23132 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133 return 5;
23134 if (p[0] == 's' && p[1] == ':')
23135 return 2;
23136 return 0;
23137}
23138
23139/*
23140 * Return TRUE if "p" starts with "<SID>" or "s:".
23141 * Only works if eval_fname_script() returned non-zero for "p"!
23142 */
23143 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023144eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145{
23146 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23147}
23148
23149/*
23150 * List the head of the function: "name(arg1, arg2)".
23151 */
23152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023153list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154{
23155 int j;
23156
23157 msg_start();
23158 if (indent)
23159 MSG_PUTS(" ");
23160 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023161 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 {
23163 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023164 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 }
23166 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023167 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023169 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 {
23171 if (j)
23172 MSG_PUTS(", ");
23173 msg_puts(FUNCARG(fp, j));
23174 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023175 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 {
23177 if (j)
23178 MSG_PUTS(", ");
23179 MSG_PUTS("...");
23180 }
23181 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023182 if (fp->uf_flags & FC_ABORT)
23183 MSG_PUTS(" abort");
23184 if (fp->uf_flags & FC_RANGE)
23185 MSG_PUTS(" range");
23186 if (fp->uf_flags & FC_DICT)
23187 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023188 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023189 if (p_verbose > 0)
23190 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191}
23192
23193/*
23194 * Find a function by name, return pointer to it in ufuncs.
23195 * Return NULL for unknown function.
23196 */
23197 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023198find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023200 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023202 hi = hash_find(&func_hashtab, name);
23203 if (!HASHITEM_EMPTY(hi))
23204 return HI2UF(hi);
23205 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206}
23207
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023208#if defined(EXITFREE) || defined(PROTO)
23209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023210free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023211{
23212 hashitem_T *hi;
23213
23214 /* Need to start all over every time, because func_free() may change the
23215 * hash table. */
23216 while (func_hashtab.ht_used > 0)
23217 for (hi = func_hashtab.ht_array; ; ++hi)
23218 if (!HASHITEM_EMPTY(hi))
23219 {
23220 func_free(HI2UF(hi));
23221 break;
23222 }
23223}
23224#endif
23225
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023227translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023228{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023229 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023230 return find_internal_func(name) >= 0;
23231 return find_func(name) != NULL;
23232}
23233
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023234/*
23235 * Return TRUE if a function "name" exists.
23236 */
23237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023238function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023239{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023240 char_u *nm = name;
23241 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023242 int n = FALSE;
23243
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023244 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23245 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023246 nm = skipwhite(nm);
23247
23248 /* Only accept "funcname", "funcname ", "funcname (..." and
23249 * "funcname(...", not "funcname!...". */
23250 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023251 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023252 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023253 return n;
23254}
23255
Bram Moolenaara1544c02013-05-30 12:35:52 +020023256 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023257get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023258{
23259 char_u *nm = name;
23260 char_u *p;
23261
23262 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23263
23264 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023265 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023266 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023267
Bram Moolenaara1544c02013-05-30 12:35:52 +020023268 vim_free(p);
23269 return NULL;
23270}
23271
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023272/*
23273 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023274 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23275 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023276 */
23277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023278builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023279{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023280 char_u *p;
23281
23282 if (!ASCII_ISLOWER(name[0]))
23283 return FALSE;
23284 p = vim_strchr(name, AUTOLOAD_CHAR);
23285 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023286}
23287
Bram Moolenaar05159a02005-02-26 23:04:13 +000023288#if defined(FEAT_PROFILE) || defined(PROTO)
23289/*
23290 * Start profiling function "fp".
23291 */
23292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023293func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023294{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023295 int len = fp->uf_lines.ga_len;
23296
23297 if (len == 0)
23298 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023299 fp->uf_tm_count = 0;
23300 profile_zero(&fp->uf_tm_self);
23301 profile_zero(&fp->uf_tm_total);
23302 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023303 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023304 if (fp->uf_tml_total == NULL)
23305 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023306 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023307 if (fp->uf_tml_self == NULL)
23308 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023309 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023310 fp->uf_tml_idx = -1;
23311 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23312 || fp->uf_tml_self == NULL)
23313 return; /* out of memory */
23314
23315 fp->uf_profiling = TRUE;
23316}
23317
23318/*
23319 * Dump the profiling results for all functions in file "fd".
23320 */
23321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023322func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023323{
23324 hashitem_T *hi;
23325 int todo;
23326 ufunc_T *fp;
23327 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023328 ufunc_T **sorttab;
23329 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023330
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023331 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023332 if (todo == 0)
23333 return; /* nothing to dump */
23334
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023335 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023336
Bram Moolenaar05159a02005-02-26 23:04:13 +000023337 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23338 {
23339 if (!HASHITEM_EMPTY(hi))
23340 {
23341 --todo;
23342 fp = HI2UF(hi);
23343 if (fp->uf_profiling)
23344 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023345 if (sorttab != NULL)
23346 sorttab[st_len++] = fp;
23347
Bram Moolenaar05159a02005-02-26 23:04:13 +000023348 if (fp->uf_name[0] == K_SPECIAL)
23349 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23350 else
23351 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23352 if (fp->uf_tm_count == 1)
23353 fprintf(fd, "Called 1 time\n");
23354 else
23355 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23356 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23357 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23358 fprintf(fd, "\n");
23359 fprintf(fd, "count total (s) self (s)\n");
23360
23361 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23362 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023363 if (FUNCLINE(fp, i) == NULL)
23364 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023365 prof_func_line(fd, fp->uf_tml_count[i],
23366 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023367 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23368 }
23369 fprintf(fd, "\n");
23370 }
23371 }
23372 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023373
23374 if (sorttab != NULL && st_len > 0)
23375 {
23376 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23377 prof_total_cmp);
23378 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23379 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23380 prof_self_cmp);
23381 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23382 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023383
23384 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023385}
Bram Moolenaar73830342005-02-28 22:48:19 +000023386
23387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023388prof_sort_list(
23389 FILE *fd,
23390 ufunc_T **sorttab,
23391 int st_len,
23392 char *title,
23393 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023394{
23395 int i;
23396 ufunc_T *fp;
23397
23398 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23399 fprintf(fd, "count total (s) self (s) function\n");
23400 for (i = 0; i < 20 && i < st_len; ++i)
23401 {
23402 fp = sorttab[i];
23403 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23404 prefer_self);
23405 if (fp->uf_name[0] == K_SPECIAL)
23406 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23407 else
23408 fprintf(fd, " %s()\n", fp->uf_name);
23409 }
23410 fprintf(fd, "\n");
23411}
23412
23413/*
23414 * Print the count and times for one function or function line.
23415 */
23416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023417prof_func_line(
23418 FILE *fd,
23419 int count,
23420 proftime_T *total,
23421 proftime_T *self,
23422 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023423{
23424 if (count > 0)
23425 {
23426 fprintf(fd, "%5d ", count);
23427 if (prefer_self && profile_equal(total, self))
23428 fprintf(fd, " ");
23429 else
23430 fprintf(fd, "%s ", profile_msg(total));
23431 if (!prefer_self && profile_equal(total, self))
23432 fprintf(fd, " ");
23433 else
23434 fprintf(fd, "%s ", profile_msg(self));
23435 }
23436 else
23437 fprintf(fd, " ");
23438}
23439
23440/*
23441 * Compare function for total time sorting.
23442 */
23443 static int
23444#ifdef __BORLANDC__
23445_RTLENTRYF
23446#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023447prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023448{
23449 ufunc_T *p1, *p2;
23450
23451 p1 = *(ufunc_T **)s1;
23452 p2 = *(ufunc_T **)s2;
23453 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23454}
23455
23456/*
23457 * Compare function for self time sorting.
23458 */
23459 static int
23460#ifdef __BORLANDC__
23461_RTLENTRYF
23462#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023463prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023464{
23465 ufunc_T *p1, *p2;
23466
23467 p1 = *(ufunc_T **)s1;
23468 p2 = *(ufunc_T **)s2;
23469 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23470}
23471
Bram Moolenaar05159a02005-02-26 23:04:13 +000023472#endif
23473
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023474/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023475 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023476 * Return TRUE if a package was loaded.
23477 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023478 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023479script_autoload(
23480 char_u *name,
23481 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023482{
23483 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023484 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023485 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023486 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023487
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023488 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023489 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023490 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023491 return FALSE;
23492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023493 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023495 /* Find the name in the list of previously loaded package names. Skip
23496 * "autoload/", it's always the same. */
23497 for (i = 0; i < ga_loaded.ga_len; ++i)
23498 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23499 break;
23500 if (!reload && i < ga_loaded.ga_len)
23501 ret = FALSE; /* was loaded already */
23502 else
23503 {
23504 /* Remember the name if it wasn't loaded already. */
23505 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23506 {
23507 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23508 tofree = NULL;
23509 }
23510
23511 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023512 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023513 ret = TRUE;
23514 }
23515
23516 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023517 return ret;
23518}
23519
23520/*
23521 * Return the autoload script name for a function or variable name.
23522 * Returns NULL when out of memory.
23523 */
23524 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023525autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023526{
23527 char_u *p;
23528 char_u *scriptname;
23529
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023530 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023531 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23532 if (scriptname == NULL)
23533 return FALSE;
23534 STRCPY(scriptname, "autoload/");
23535 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023536 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023537 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023538 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023539 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023540 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023541}
23542
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23544
23545/*
23546 * Function given to ExpandGeneric() to obtain the list of user defined
23547 * function names.
23548 */
23549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023550get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023552 static long_u done;
23553 static hashitem_T *hi;
23554 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555
23556 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023558 done = 0;
23559 hi = func_hashtab.ht_array;
23560 }
23561 if (done < func_hashtab.ht_used)
23562 {
23563 if (done++ > 0)
23564 ++hi;
23565 while (HASHITEM_EMPTY(hi))
23566 ++hi;
23567 fp = HI2UF(hi);
23568
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023569 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023570 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023571
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023572 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23573 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574
23575 cat_func_name(IObuff, fp);
23576 if (xp->xp_context != EXPAND_USER_FUNC)
23577 {
23578 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023579 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 STRCAT(IObuff, ")");
23581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 return IObuff;
23583 }
23584 return NULL;
23585}
23586
23587#endif /* FEAT_CMDL_COMPL */
23588
23589/*
23590 * Copy the function name of "fp" to buffer "buf".
23591 * "buf" must be able to hold the function name plus three bytes.
23592 * Takes care of script-local function names.
23593 */
23594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023595cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023597 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598 {
23599 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023600 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 }
23602 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023603 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604}
23605
23606/*
23607 * ":delfunction {name}"
23608 */
23609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023610ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023612 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 char_u *p;
23614 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023615 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616
23617 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023618 name = trans_function_name(&p, eap->skip, 0, &fudi);
23619 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023621 {
23622 if (fudi.fd_dict != NULL && !eap->skip)
23623 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 if (!ends_excmd(*skipwhite(p)))
23627 {
23628 vim_free(name);
23629 EMSG(_(e_trailing));
23630 return;
23631 }
23632 eap->nextcmd = check_nextcmd(p);
23633 if (eap->nextcmd != NULL)
23634 *p = NUL;
23635
23636 if (!eap->skip)
23637 fp = find_func(name);
23638 vim_free(name);
23639
23640 if (!eap->skip)
23641 {
23642 if (fp == NULL)
23643 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023644 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 return;
23646 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023647 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 {
23649 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23650 return;
23651 }
23652
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023653 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023655 /* Delete the dict item that refers to the function, it will
23656 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023657 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023659 else
23660 func_free(fp);
23661 }
23662}
23663
23664/*
23665 * Free a function and remove it from the list of functions.
23666 */
23667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023668func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023669{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023670 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023671
23672 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023673 ga_clear_strings(&(fp->uf_args));
23674 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023675#ifdef FEAT_PROFILE
23676 vim_free(fp->uf_tml_count);
23677 vim_free(fp->uf_tml_total);
23678 vim_free(fp->uf_tml_self);
23679#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023681 /* remove the function from the function hashtable */
23682 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23683 if (HASHITEM_EMPTY(hi))
23684 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023685 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023686 hash_remove(&func_hashtab, hi);
23687
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023688 vim_free(fp);
23689}
23690
23691/*
23692 * Unreference a Function: decrement the reference count and free it when it
23693 * becomes zero. Only for numbered functions.
23694 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023696func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023697{
23698 ufunc_T *fp;
23699
23700 if (name != NULL && isdigit(*name))
23701 {
23702 fp = find_func(name);
23703 if (fp == NULL)
23704 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023705 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023706 {
23707 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023708 * when "uf_calls" becomes zero. */
23709 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710 func_free(fp);
23711 }
23712 }
23713}
23714
23715/*
23716 * Count a reference to a Function.
23717 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023719func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023720{
23721 ufunc_T *fp;
23722
23723 if (name != NULL && isdigit(*name))
23724 {
23725 fp = find_func(name);
23726 if (fp == NULL)
23727 EMSG2(_(e_intern2), "func_ref()");
23728 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023729 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 }
23731}
23732
23733/*
23734 * Call a user function.
23735 */
23736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023737call_user_func(
23738 ufunc_T *fp, /* pointer to function */
23739 int argcount, /* nr of args */
23740 typval_T *argvars, /* arguments */
23741 typval_T *rettv, /* return value */
23742 linenr_T firstline, /* first line of range */
23743 linenr_T lastline, /* last line of range */
23744 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745{
Bram Moolenaar33570922005-01-25 22:26:29 +000023746 char_u *save_sourcing_name;
23747 linenr_T save_sourcing_lnum;
23748 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023749 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023750 int save_did_emsg;
23751 static int depth = 0;
23752 dictitem_T *v;
23753 int fixvar_idx = 0; /* index in fixvar[] */
23754 int i;
23755 int ai;
23756 char_u numbuf[NUMBUFLEN];
23757 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023758 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023759#ifdef FEAT_PROFILE
23760 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023761 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763
23764 /* If depth of calling is getting too high, don't execute the function */
23765 if (depth >= p_mfd)
23766 {
23767 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023768 rettv->v_type = VAR_NUMBER;
23769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 return;
23771 }
23772 ++depth;
23773
23774 line_breakcheck(); /* check for CTRL-C hit */
23775
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023776 fc = (funccall_T *)alloc(sizeof(funccall_T));
23777 fc->caller = current_funccal;
23778 current_funccal = fc;
23779 fc->func = fp;
23780 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023781 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023782 fc->linenr = 0;
23783 fc->returned = FALSE;
23784 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023786 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23787 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788
Bram Moolenaar33570922005-01-25 22:26:29 +000023789 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023790 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023791 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23792 * each argument variable and saves a lot of time.
23793 */
23794 /*
23795 * Init l: variables.
23796 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023797 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023798 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023799 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023800 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23801 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023802 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023803 name = v->di_key;
23804 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023805 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023806 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023807 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023808 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023809 v->di_tv.vval.v_dict = selfdict;
23810 ++selfdict->dv_refcount;
23811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023812
Bram Moolenaar33570922005-01-25 22:26:29 +000023813 /*
23814 * Init a: variables.
23815 * Set a:0 to "argcount".
23816 * Set a:000 to a list with room for the "..." arguments.
23817 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023818 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023819 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023820 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023821 /* Use "name" to avoid a warning from some compiler that checks the
23822 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023823 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023824 name = v->di_key;
23825 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023826 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023827 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023828 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023829 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023830 v->di_tv.vval.v_list = &fc->l_varlist;
23831 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23832 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23833 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023834
23835 /*
23836 * Set a:firstline to "firstline" and a:lastline to "lastline".
23837 * Set a:name to named arguments.
23838 * Set a:N to the "..." arguments.
23839 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023840 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023841 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023842 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023843 (varnumber_T)lastline);
23844 for (i = 0; i < argcount; ++i)
23845 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023846 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023847 if (ai < 0)
23848 /* named argument a:name */
23849 name = FUNCARG(fp, i);
23850 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023852 /* "..." argument a:1, a:2, etc. */
23853 sprintf((char *)numbuf, "%d", ai + 1);
23854 name = numbuf;
23855 }
23856 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23857 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023858 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023859 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23860 }
23861 else
23862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023863 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23864 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023865 if (v == NULL)
23866 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023867 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023868 }
23869 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023870 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023871
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023872 /* Note: the values are copied directly to avoid alloc/free.
23873 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023874 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023875 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023876
23877 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23878 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023879 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23880 fc->l_listitems[ai].li_tv = argvars[i];
23881 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023882 }
23883 }
23884
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 /* Don't redraw while executing the function. */
23886 ++RedrawingDisabled;
23887 save_sourcing_name = sourcing_name;
23888 save_sourcing_lnum = sourcing_lnum;
23889 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023890 /* need space for function name + ("function " + 3) or "[number]" */
23891 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23892 + STRLEN(fp->uf_name) + 20;
23893 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 if (sourcing_name != NULL)
23895 {
23896 if (save_sourcing_name != NULL
23897 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023898 sprintf((char *)sourcing_name, "%s[%d]..",
23899 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 else
23901 STRCPY(sourcing_name, "function ");
23902 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23903
23904 if (p_verbose >= 12)
23905 {
23906 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023907 verbose_enter_scroll();
23908
Bram Moolenaar555b2802005-05-19 21:08:39 +000023909 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 if (p_verbose >= 14)
23911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023913 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023914 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023915 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916
23917 msg_puts((char_u *)"(");
23918 for (i = 0; i < argcount; ++i)
23919 {
23920 if (i > 0)
23921 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023922 if (argvars[i].v_type == VAR_NUMBER)
23923 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 else
23925 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023926 /* Do not want errors such as E724 here. */
23927 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023928 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023929 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023930 if (s != NULL)
23931 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023932 if (vim_strsize(s) > MSG_BUF_CLEN)
23933 {
23934 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23935 s = buf;
23936 }
23937 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023938 vim_free(tofree);
23939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 }
23941 }
23942 msg_puts((char_u *)")");
23943 }
23944 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023945
23946 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 --no_wait_return;
23948 }
23949 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023950#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023951 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023952 {
23953 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23954 func_do_profile(fp);
23955 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023956 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023957 {
23958 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023959 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023960 profile_zero(&fp->uf_tm_children);
23961 }
23962 script_prof_save(&wait_start);
23963 }
23964#endif
23965
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023967 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 save_did_emsg = did_emsg;
23969 did_emsg = FALSE;
23970
23971 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023972 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23974
23975 --RedrawingDisabled;
23976
23977 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023978 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023980 clear_tv(rettv);
23981 rettv->v_type = VAR_NUMBER;
23982 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 }
23984
Bram Moolenaar05159a02005-02-26 23:04:13 +000023985#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023986 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023987 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023988 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023989 profile_end(&call_start);
23990 profile_sub_wait(&wait_start, &call_start);
23991 profile_add(&fp->uf_tm_total, &call_start);
23992 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023993 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023994 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023995 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23996 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023997 }
23998 }
23999#endif
24000
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 /* when being verbose, mention the return value */
24002 if (p_verbose >= 12)
24003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024005 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024008 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024009 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024010 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024011 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024014 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024015 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024016 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024017 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024018
Bram Moolenaar555b2802005-05-19 21:08:39 +000024019 /* The value may be very long. Skip the middle part, so that we
24020 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024021 * truncate it at the end. Don't want errors such as E724 here. */
24022 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024023 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024024 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024025 if (s != NULL)
24026 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024027 if (vim_strsize(s) > MSG_BUF_CLEN)
24028 {
24029 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24030 s = buf;
24031 }
24032 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024033 vim_free(tofree);
24034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 }
24036 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024037
24038 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 --no_wait_return;
24040 }
24041
24042 vim_free(sourcing_name);
24043 sourcing_name = save_sourcing_name;
24044 sourcing_lnum = save_sourcing_lnum;
24045 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024046#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024047 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024048 script_prof_restore(&wait_start);
24049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050
24051 if (p_verbose >= 12 && sourcing_name != NULL)
24052 {
24053 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024054 verbose_enter_scroll();
24055
Bram Moolenaar555b2802005-05-19 21:08:39 +000024056 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024058
24059 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 --no_wait_return;
24061 }
24062
24063 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024064 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024066
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024067 /* If the a:000 list and the l: and a: dicts are not referenced we can
24068 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024069 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24070 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24071 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24072 {
24073 free_funccal(fc, FALSE);
24074 }
24075 else
24076 {
24077 hashitem_T *hi;
24078 listitem_T *li;
24079 int todo;
24080
24081 /* "fc" is still in use. This can happen when returning "a:000" or
24082 * assigning "l:" to a global variable.
24083 * Link "fc" in the list for garbage collection later. */
24084 fc->caller = previous_funccal;
24085 previous_funccal = fc;
24086
24087 /* Make a copy of the a: variables, since we didn't do that above. */
24088 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24089 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24090 {
24091 if (!HASHITEM_EMPTY(hi))
24092 {
24093 --todo;
24094 v = HI2DI(hi);
24095 copy_tv(&v->di_tv, &v->di_tv);
24096 }
24097 }
24098
24099 /* Make a copy of the a:000 items, since we didn't do that above. */
24100 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24101 copy_tv(&li->li_tv, &li->li_tv);
24102 }
24103}
24104
24105/*
24106 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024107 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024108 */
24109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024110can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024111{
24112 return (fc->l_varlist.lv_copyID != copyID
24113 && fc->l_vars.dv_copyID != copyID
24114 && fc->l_avars.dv_copyID != copyID);
24115}
24116
24117/*
24118 * Free "fc" and what it contains.
24119 */
24120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024121free_funccal(
24122 funccall_T *fc,
24123 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024124{
24125 listitem_T *li;
24126
24127 /* The a: variables typevals may not have been allocated, only free the
24128 * allocated variables. */
24129 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24130
24131 /* free all l: variables */
24132 vars_clear(&fc->l_vars.dv_hashtab);
24133
24134 /* Free the a:000 variables if they were allocated. */
24135 if (free_val)
24136 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24137 clear_tv(&li->li_tv);
24138
24139 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140}
24141
24142/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024143 * Add a number variable "name" to dict "dp" with value "nr".
24144 */
24145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024146add_nr_var(
24147 dict_T *dp,
24148 dictitem_T *v,
24149 char *name,
24150 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024151{
24152 STRCPY(v->di_key, name);
24153 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24154 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24155 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024156 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024157 v->di_tv.vval.v_number = nr;
24158}
24159
24160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024161 * ":return [expr]"
24162 */
24163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024164ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165{
24166 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024167 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 int returning = FALSE;
24169
24170 if (current_funccal == NULL)
24171 {
24172 EMSG(_("E133: :return not inside a function"));
24173 return;
24174 }
24175
24176 if (eap->skip)
24177 ++emsg_skip;
24178
24179 eap->nextcmd = NULL;
24180 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024181 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 {
24183 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024184 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024186 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 }
24188 /* It's safer to return also on error. */
24189 else if (!eap->skip)
24190 {
24191 /*
24192 * Return unless the expression evaluation has been cancelled due to an
24193 * aborting error, an interrupt, or an exception.
24194 */
24195 if (!aborting())
24196 returning = do_return(eap, FALSE, TRUE, NULL);
24197 }
24198
24199 /* When skipping or the return gets pending, advance to the next command
24200 * in this line (!returning). Otherwise, ignore the rest of the line.
24201 * Following lines will be ignored by get_func_line(). */
24202 if (returning)
24203 eap->nextcmd = NULL;
24204 else if (eap->nextcmd == NULL) /* no argument */
24205 eap->nextcmd = check_nextcmd(arg);
24206
24207 if (eap->skip)
24208 --emsg_skip;
24209}
24210
24211/*
24212 * Return from a function. Possibly makes the return pending. Also called
24213 * for a pending return at the ":endtry" or after returning from an extra
24214 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024215 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024216 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 * FALSE when the return gets pending.
24218 */
24219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024220do_return(
24221 exarg_T *eap,
24222 int reanimate,
24223 int is_cmd,
24224 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225{
24226 int idx;
24227 struct condstack *cstack = eap->cstack;
24228
24229 if (reanimate)
24230 /* Undo the return. */
24231 current_funccal->returned = FALSE;
24232
24233 /*
24234 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24235 * not in its finally clause (which then is to be executed next) is found.
24236 * In this case, make the ":return" pending for execution at the ":endtry".
24237 * Otherwise, return normally.
24238 */
24239 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24240 if (idx >= 0)
24241 {
24242 cstack->cs_pending[idx] = CSTP_RETURN;
24243
24244 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024245 /* A pending return again gets pending. "rettv" points to an
24246 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024248 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 else
24250 {
24251 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024252 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024254 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024256 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 {
24258 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024259 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024260 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 else
24262 EMSG(_(e_outofmem));
24263 }
24264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024265 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266
24267 if (reanimate)
24268 {
24269 /* The pending return value could be overwritten by a ":return"
24270 * without argument in a finally clause; reset the default
24271 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024272 current_funccal->rettv->v_type = VAR_NUMBER;
24273 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 }
24275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024276 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 }
24278 else
24279 {
24280 current_funccal->returned = TRUE;
24281
24282 /* If the return is carried out now, store the return value. For
24283 * a return immediately after reanimation, the value is already
24284 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024285 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024287 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024288 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024290 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 }
24292 }
24293
24294 return idx < 0;
24295}
24296
24297/*
24298 * Free the variable with a pending return value.
24299 */
24300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024301discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302{
Bram Moolenaar33570922005-01-25 22:26:29 +000024303 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304}
24305
24306/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024307 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 * is an allocated string. Used by report_pending() for verbose messages.
24309 */
24310 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024311get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024313 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024314 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024315 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024317 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024318 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024319 if (s == NULL)
24320 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024321
24322 STRCPY(IObuff, ":return ");
24323 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24324 if (STRLEN(s) + 8 >= IOSIZE)
24325 STRCPY(IObuff + IOSIZE - 4, "...");
24326 vim_free(tofree);
24327 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328}
24329
24330/*
24331 * Get next function line.
24332 * Called by do_cmdline() to get the next line.
24333 * Returns allocated string, or NULL for end of function.
24334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024336get_func_line(
24337 int c UNUSED,
24338 void *cookie,
24339 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340{
Bram Moolenaar33570922005-01-25 22:26:29 +000024341 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024342 ufunc_T *fp = fcp->func;
24343 char_u *retval;
24344 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345
24346 /* If breakpoints have been added/deleted need to check for it. */
24347 if (fcp->dbg_tick != debug_tick)
24348 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 sourcing_lnum);
24351 fcp->dbg_tick = debug_tick;
24352 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024353#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024354 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024355 func_line_end(cookie);
24356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357
Bram Moolenaar05159a02005-02-26 23:04:13 +000024358 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024359 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24360 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 retval = NULL;
24362 else
24363 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024364 /* Skip NULL lines (continuation lines). */
24365 while (fcp->linenr < gap->ga_len
24366 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24367 ++fcp->linenr;
24368 if (fcp->linenr >= gap->ga_len)
24369 retval = NULL;
24370 else
24371 {
24372 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24373 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024374#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024375 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024376 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024377#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 }
24380
24381 /* Did we encounter a breakpoint? */
24382 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24383 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024384 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024386 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 sourcing_lnum);
24388 fcp->dbg_tick = debug_tick;
24389 }
24390
24391 return retval;
24392}
24393
Bram Moolenaar05159a02005-02-26 23:04:13 +000024394#if defined(FEAT_PROFILE) || defined(PROTO)
24395/*
24396 * Called when starting to read a function line.
24397 * "sourcing_lnum" must be correct!
24398 * When skipping lines it may not actually be executed, but we won't find out
24399 * until later and we need to store the time now.
24400 */
24401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024402func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024403{
24404 funccall_T *fcp = (funccall_T *)cookie;
24405 ufunc_T *fp = fcp->func;
24406
24407 if (fp->uf_profiling && sourcing_lnum >= 1
24408 && sourcing_lnum <= fp->uf_lines.ga_len)
24409 {
24410 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024411 /* Skip continuation lines. */
24412 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24413 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024414 fp->uf_tml_execed = FALSE;
24415 profile_start(&fp->uf_tml_start);
24416 profile_zero(&fp->uf_tml_children);
24417 profile_get_wait(&fp->uf_tml_wait);
24418 }
24419}
24420
24421/*
24422 * Called when actually executing a function line.
24423 */
24424 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024425func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024426{
24427 funccall_T *fcp = (funccall_T *)cookie;
24428 ufunc_T *fp = fcp->func;
24429
24430 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24431 fp->uf_tml_execed = TRUE;
24432}
24433
24434/*
24435 * Called when done with a function line.
24436 */
24437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024438func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024439{
24440 funccall_T *fcp = (funccall_T *)cookie;
24441 ufunc_T *fp = fcp->func;
24442
24443 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24444 {
24445 if (fp->uf_tml_execed)
24446 {
24447 ++fp->uf_tml_count[fp->uf_tml_idx];
24448 profile_end(&fp->uf_tml_start);
24449 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024450 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024451 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24452 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024453 }
24454 fp->uf_tml_idx = -1;
24455 }
24456}
24457#endif
24458
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459/*
24460 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024461 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 */
24463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024464func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024465{
Bram Moolenaar33570922005-01-25 22:26:29 +000024466 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024467
24468 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24469 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024470 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471 || fcp->returned);
24472}
24473
24474/*
24475 * return TRUE if cookie indicates a function which "abort"s on errors.
24476 */
24477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024478func_has_abort(
24479 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024481 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482}
24483
24484#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24485typedef enum
24486{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024487 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24488 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24489 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490} var_flavour_T;
24491
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024492static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493
24494 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024495var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496{
24497 char_u *p = varname;
24498
24499 if (ASCII_ISUPPER(*p))
24500 {
24501 while (*(++p))
24502 if (ASCII_ISLOWER(*p))
24503 return VAR_FLAVOUR_SESSION;
24504 return VAR_FLAVOUR_VIMINFO;
24505 }
24506 else
24507 return VAR_FLAVOUR_DEFAULT;
24508}
24509#endif
24510
24511#if defined(FEAT_VIMINFO) || defined(PROTO)
24512/*
24513 * Restore global vars that start with a capital from the viminfo file
24514 */
24515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024516read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517{
24518 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024519 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024520 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024521 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024522
24523 if (!writing && (find_viminfo_parameter('!') != NULL))
24524 {
24525 tab = vim_strchr(virp->vir_line + 1, '\t');
24526 if (tab != NULL)
24527 {
24528 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024529 switch (*tab)
24530 {
24531 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024532#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024533 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024534#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024535 case 'D': type = VAR_DICT; break;
24536 case 'L': type = VAR_LIST; break;
24537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538
24539 tab = vim_strchr(tab, '\t');
24540 if (tab != NULL)
24541 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024542 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024543 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024544 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024546#ifdef FEAT_FLOAT
24547 else if (type == VAR_FLOAT)
24548 (void)string2float(tab + 1, &tv.vval.v_float);
24549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024551 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024552 if (type == VAR_DICT || type == VAR_LIST)
24553 {
24554 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24555
24556 if (etv == NULL)
24557 /* Failed to parse back the dict or list, use it as a
24558 * string. */
24559 tv.v_type = VAR_STRING;
24560 else
24561 {
24562 vim_free(tv.vval.v_string);
24563 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024564 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024565 }
24566 }
24567
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024568 /* when in a function use global variables */
24569 save_funccal = current_funccal;
24570 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024571 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024572 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024573
24574 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024575 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024576 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24577 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 }
24579 }
24580 }
24581
24582 return viminfo_readline(virp);
24583}
24584
24585/*
24586 * Write global vars that start with a capital to the viminfo file
24587 */
24588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024589write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024590{
Bram Moolenaar33570922005-01-25 22:26:29 +000024591 hashitem_T *hi;
24592 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024593 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024594 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024595 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024596 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024597 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598
24599 if (find_viminfo_parameter('!') == NULL)
24600 return;
24601
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024602 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024603
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024604 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024605 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024606 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024607 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024609 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024610 this_var = HI2DI(hi);
24611 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024612 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024613 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024614 {
24615 case VAR_STRING: s = "STR"; break;
24616 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024617#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024618 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024619#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024620 case VAR_DICT: s = "DIC"; break;
24621 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024622 default: continue;
24623 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024624 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024625 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024626 if (p != NULL)
24627 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024628 vim_free(tofree);
24629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 }
24631 }
24632}
24633#endif
24634
24635#if defined(FEAT_SESSION) || defined(PROTO)
24636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024637store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638{
Bram Moolenaar33570922005-01-25 22:26:29 +000024639 hashitem_T *hi;
24640 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024641 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 char_u *p, *t;
24643
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024644 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024645 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024647 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024649 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024650 this_var = HI2DI(hi);
24651 if ((this_var->di_tv.v_type == VAR_NUMBER
24652 || this_var->di_tv.v_type == VAR_STRING)
24653 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024654 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024655 /* Escape special characters with a backslash. Turn a LF and
24656 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024657 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024658 (char_u *)"\\\"\n\r");
24659 if (p == NULL) /* out of memory */
24660 break;
24661 for (t = p; *t != NUL; ++t)
24662 if (*t == '\n')
24663 *t = 'n';
24664 else if (*t == '\r')
24665 *t = 'r';
24666 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024667 this_var->di_key,
24668 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24669 : ' ',
24670 p,
24671 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24672 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024673 || put_eol(fd) == FAIL)
24674 {
24675 vim_free(p);
24676 return FAIL;
24677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678 vim_free(p);
24679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024680#ifdef FEAT_FLOAT
24681 else if (this_var->di_tv.v_type == VAR_FLOAT
24682 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24683 {
24684 float_T f = this_var->di_tv.vval.v_float;
24685 int sign = ' ';
24686
24687 if (f < 0)
24688 {
24689 f = -f;
24690 sign = '-';
24691 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024692 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024693 this_var->di_key, sign, f) < 0)
24694 || put_eol(fd) == FAIL)
24695 return FAIL;
24696 }
24697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698 }
24699 }
24700 return OK;
24701}
24702#endif
24703
Bram Moolenaar661b1822005-07-28 22:36:45 +000024704/*
24705 * Display script name where an item was last set.
24706 * Should only be invoked when 'verbose' is non-zero.
24707 */
24708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024709last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024710{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024711 char_u *p;
24712
Bram Moolenaar661b1822005-07-28 22:36:45 +000024713 if (scriptID != 0)
24714 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024715 p = home_replace_save(NULL, get_scriptname(scriptID));
24716 if (p != NULL)
24717 {
24718 verbose_enter();
24719 MSG_PUTS(_("\n\tLast set from "));
24720 MSG_PUTS(p);
24721 vim_free(p);
24722 verbose_leave();
24723 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024724 }
24725}
24726
Bram Moolenaard812df62008-11-09 12:46:09 +000024727/*
24728 * List v:oldfiles in a nice way.
24729 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024731ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024732{
24733 list_T *l = vimvars[VV_OLDFILES].vv_list;
24734 listitem_T *li;
24735 int nr = 0;
24736
24737 if (l == NULL)
24738 msg((char_u *)_("No old files"));
24739 else
24740 {
24741 msg_start();
24742 msg_scroll = TRUE;
24743 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24744 {
24745 msg_outnum((long)++nr);
24746 MSG_PUTS(": ");
24747 msg_outtrans(get_tv_string(&li->li_tv));
24748 msg_putchar('\n');
24749 out_flush(); /* output one line at a time */
24750 ui_breakcheck();
24751 }
24752 /* Assume "got_int" was set to truncate the listing. */
24753 got_int = FALSE;
24754
24755#ifdef FEAT_BROWSE_CMD
24756 if (cmdmod.browse)
24757 {
24758 quit_more = FALSE;
24759 nr = prompt_for_number(FALSE);
24760 msg_starthere();
24761 if (nr > 0)
24762 {
24763 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24764 (long)nr);
24765
24766 if (p != NULL)
24767 {
24768 p = expand_env_save(p);
24769 eap->arg = p;
24770 eap->cmdidx = CMD_edit;
24771 cmdmod.browse = FALSE;
24772 do_exedit(eap, NULL);
24773 vim_free(p);
24774 }
24775 }
24776 }
24777#endif
24778 }
24779}
24780
Bram Moolenaar53744302015-07-17 17:38:22 +020024781/* reset v:option_new, v:option_old and v:option_type */
24782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024783reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024784{
24785 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24786 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24787 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24788}
24789
24790
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791#endif /* FEAT_EVAL */
24792
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024794#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795
24796#ifdef WIN3264
24797/*
24798 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24799 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024800static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24801static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24802static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803
24804/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024805 * Get the short path (8.3) for the filename in "fnamep".
24806 * Only works for a valid file name.
24807 * When the path gets longer "fnamep" is changed and the allocated buffer
24808 * is put in "bufp".
24809 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24810 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811 */
24812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024813get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024815 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 char_u *newbuf;
24817
24818 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819 l = GetShortPathName(*fnamep, *fnamep, len);
24820 if (l > len - 1)
24821 {
24822 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024823 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824 newbuf = vim_strnsave(*fnamep, l);
24825 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024826 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024827
24828 vim_free(*bufp);
24829 *fnamep = *bufp = newbuf;
24830
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024831 /* Really should always succeed, as the buffer is big enough. */
24832 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833 }
24834
24835 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024836 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837}
24838
24839/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024840 * Get the short path (8.3) for the filename in "fname". The converted
24841 * path is returned in "bufp".
24842 *
24843 * Some of the directories specified in "fname" may not exist. This function
24844 * will shorten the existing directories at the beginning of the path and then
24845 * append the remaining non-existing path.
24846 *
24847 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024848 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024849 * bufp - Pointer to an allocated buffer for the filename.
24850 * fnamelen - Length of the filename pointed to by fname
24851 *
24852 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853 */
24854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024855shortpath_for_invalid_fname(
24856 char_u **fname,
24857 char_u **bufp,
24858 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024860 char_u *short_fname, *save_fname, *pbuf_unused;
24861 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024863 int old_len, len;
24864 int new_len, sfx_len;
24865 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866
24867 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024868 old_len = *fnamelen;
24869 save_fname = vim_strnsave(*fname, old_len);
24870 pbuf_unused = NULL;
24871 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024873 endp = save_fname + old_len - 1; /* Find the end of the copy */
24874 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024876 /*
24877 * Try shortening the supplied path till it succeeds by removing one
24878 * directory at a time from the tail of the path.
24879 */
24880 len = 0;
24881 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024883 /* go back one path-separator */
24884 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24885 --endp;
24886 if (endp <= save_fname)
24887 break; /* processed the complete path */
24888
24889 /*
24890 * Replace the path separator with a NUL and try to shorten the
24891 * resulting path.
24892 */
24893 ch = *endp;
24894 *endp = 0;
24895 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024896 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024897 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24898 {
24899 retval = FAIL;
24900 goto theend;
24901 }
24902 *endp = ch; /* preserve the string */
24903
24904 if (len > 0)
24905 break; /* successfully shortened the path */
24906
24907 /* failed to shorten the path. Skip the path separator */
24908 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 }
24910
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024911 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024913 /*
24914 * Succeeded in shortening the path. Now concatenate the shortened
24915 * path with the remaining path at the tail.
24916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024918 /* Compute the length of the new path. */
24919 sfx_len = (int)(save_endp - endp) + 1;
24920 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024922 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024924 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024926 /* There is not enough space in the currently allocated string,
24927 * copy it to a buffer big enough. */
24928 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024930 {
24931 retval = FAIL;
24932 goto theend;
24933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 }
24935 else
24936 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024937 /* Transfer short_fname to the main buffer (it's big enough),
24938 * unless get_short_pathname() did its work in-place. */
24939 *fname = *bufp = save_fname;
24940 if (short_fname != save_fname)
24941 vim_strncpy(save_fname, short_fname, len);
24942 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024944
24945 /* concat the not-shortened part of the path */
24946 vim_strncpy(*fname + len, endp, sfx_len);
24947 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024949
24950theend:
24951 vim_free(pbuf_unused);
24952 vim_free(save_fname);
24953
24954 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955}
24956
24957/*
24958 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024959 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960 */
24961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024962shortpath_for_partial(
24963 char_u **fnamep,
24964 char_u **bufp,
24965 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966{
24967 int sepcount, len, tflen;
24968 char_u *p;
24969 char_u *pbuf, *tfname;
24970 int hasTilde;
24971
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024972 /* Count up the path separators from the RHS.. so we know which part
24973 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024975 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976 if (vim_ispathsep(*p))
24977 ++sepcount;
24978
24979 /* Need full path first (use expand_env() to remove a "~/") */
24980 hasTilde = (**fnamep == '~');
24981 if (hasTilde)
24982 pbuf = tfname = expand_env_save(*fnamep);
24983 else
24984 pbuf = tfname = FullName_save(*fnamep, FALSE);
24985
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024986 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024988 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24989 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990
24991 if (len == 0)
24992 {
24993 /* Don't have a valid filename, so shorten the rest of the
24994 * path if we can. This CAN give us invalid 8.3 filenames, but
24995 * there's not a lot of point in guessing what it might be.
24996 */
24997 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024998 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24999 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 }
25001
25002 /* Count the paths backward to find the beginning of the desired string. */
25003 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025004 {
25005#ifdef FEAT_MBYTE
25006 if (has_mbyte)
25007 p -= mb_head_off(tfname, p);
25008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009 if (vim_ispathsep(*p))
25010 {
25011 if (sepcount == 0 || (hasTilde && sepcount == 1))
25012 break;
25013 else
25014 sepcount --;
25015 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 if (hasTilde)
25018 {
25019 --p;
25020 if (p >= tfname)
25021 *p = '~';
25022 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025023 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 }
25025 else
25026 ++p;
25027
25028 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25029 vim_free(*bufp);
25030 *fnamelen = (int)STRLEN(p);
25031 *bufp = pbuf;
25032 *fnamep = p;
25033
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025034 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035}
25036#endif /* WIN3264 */
25037
25038/*
25039 * Adjust a filename, according to a string of modifiers.
25040 * *fnamep must be NUL terminated when called. When returning, the length is
25041 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025042 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 * When there is an error, *fnamep is set to NULL.
25044 */
25045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025046modify_fname(
25047 char_u *src, /* string with modifiers */
25048 int *usedlen, /* characters after src that are used */
25049 char_u **fnamep, /* file name so far */
25050 char_u **bufp, /* buffer for allocated file name or NULL */
25051 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052{
25053 int valid = 0;
25054 char_u *tail;
25055 char_u *s, *p, *pbuf;
25056 char_u dirname[MAXPATHL];
25057 int c;
25058 int has_fullname = 0;
25059#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025060 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 int has_shortname = 0;
25062#endif
25063
25064repeat:
25065 /* ":p" - full path/file_name */
25066 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25067 {
25068 has_fullname = 1;
25069
25070 valid |= VALID_PATH;
25071 *usedlen += 2;
25072
25073 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25074 if ((*fnamep)[0] == '~'
25075#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25076 && ((*fnamep)[1] == '/'
25077# ifdef BACKSLASH_IN_FILENAME
25078 || (*fnamep)[1] == '\\'
25079# endif
25080 || (*fnamep)[1] == NUL)
25081
25082#endif
25083 )
25084 {
25085 *fnamep = expand_env_save(*fnamep);
25086 vim_free(*bufp); /* free any allocated file name */
25087 *bufp = *fnamep;
25088 if (*fnamep == NULL)
25089 return -1;
25090 }
25091
25092 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025093 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025094 {
25095 if (vim_ispathsep(*p)
25096 && p[1] == '.'
25097 && (p[2] == NUL
25098 || vim_ispathsep(p[2])
25099 || (p[2] == '.'
25100 && (p[3] == NUL || vim_ispathsep(p[3])))))
25101 break;
25102 }
25103
25104 /* FullName_save() is slow, don't use it when not needed. */
25105 if (*p != NUL || !vim_isAbsName(*fnamep))
25106 {
25107 *fnamep = FullName_save(*fnamep, *p != NUL);
25108 vim_free(*bufp); /* free any allocated file name */
25109 *bufp = *fnamep;
25110 if (*fnamep == NULL)
25111 return -1;
25112 }
25113
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025114#ifdef WIN3264
25115# if _WIN32_WINNT >= 0x0500
25116 if (vim_strchr(*fnamep, '~') != NULL)
25117 {
25118 /* Expand 8.3 filename to full path. Needed to make sure the same
25119 * file does not have two different names.
25120 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25121 p = alloc(_MAX_PATH + 1);
25122 if (p != NULL)
25123 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025124 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025125 {
25126 vim_free(*bufp);
25127 *bufp = *fnamep = p;
25128 }
25129 else
25130 vim_free(p);
25131 }
25132 }
25133# endif
25134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 /* Append a path separator to a directory. */
25136 if (mch_isdir(*fnamep))
25137 {
25138 /* Make room for one or two extra characters. */
25139 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25140 vim_free(*bufp); /* free any allocated file name */
25141 *bufp = *fnamep;
25142 if (*fnamep == NULL)
25143 return -1;
25144 add_pathsep(*fnamep);
25145 }
25146 }
25147
25148 /* ":." - path relative to the current directory */
25149 /* ":~" - path relative to the home directory */
25150 /* ":8" - shortname path - postponed till after */
25151 while (src[*usedlen] == ':'
25152 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25153 {
25154 *usedlen += 2;
25155 if (c == '8')
25156 {
25157#ifdef WIN3264
25158 has_shortname = 1; /* Postpone this. */
25159#endif
25160 continue;
25161 }
25162 pbuf = NULL;
25163 /* Need full path first (use expand_env() to remove a "~/") */
25164 if (!has_fullname)
25165 {
25166 if (c == '.' && **fnamep == '~')
25167 p = pbuf = expand_env_save(*fnamep);
25168 else
25169 p = pbuf = FullName_save(*fnamep, FALSE);
25170 }
25171 else
25172 p = *fnamep;
25173
25174 has_fullname = 0;
25175
25176 if (p != NULL)
25177 {
25178 if (c == '.')
25179 {
25180 mch_dirname(dirname, MAXPATHL);
25181 s = shorten_fname(p, dirname);
25182 if (s != NULL)
25183 {
25184 *fnamep = s;
25185 if (pbuf != NULL)
25186 {
25187 vim_free(*bufp); /* free any allocated file name */
25188 *bufp = pbuf;
25189 pbuf = NULL;
25190 }
25191 }
25192 }
25193 else
25194 {
25195 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25196 /* Only replace it when it starts with '~' */
25197 if (*dirname == '~')
25198 {
25199 s = vim_strsave(dirname);
25200 if (s != NULL)
25201 {
25202 *fnamep = s;
25203 vim_free(*bufp);
25204 *bufp = s;
25205 }
25206 }
25207 }
25208 vim_free(pbuf);
25209 }
25210 }
25211
25212 tail = gettail(*fnamep);
25213 *fnamelen = (int)STRLEN(*fnamep);
25214
25215 /* ":h" - head, remove "/file_name", can be repeated */
25216 /* Don't remove the first "/" or "c:\" */
25217 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25218 {
25219 valid |= VALID_HEAD;
25220 *usedlen += 2;
25221 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025222 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025223 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025224 *fnamelen = (int)(tail - *fnamep);
25225#ifdef VMS
25226 if (*fnamelen > 0)
25227 *fnamelen += 1; /* the path separator is part of the path */
25228#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025229 if (*fnamelen == 0)
25230 {
25231 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25232 p = vim_strsave((char_u *)".");
25233 if (p == NULL)
25234 return -1;
25235 vim_free(*bufp);
25236 *bufp = *fnamep = tail = p;
25237 *fnamelen = 1;
25238 }
25239 else
25240 {
25241 while (tail > s && !after_pathsep(s, tail))
25242 mb_ptr_back(*fnamep, tail);
25243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244 }
25245
25246 /* ":8" - shortname */
25247 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25248 {
25249 *usedlen += 2;
25250#ifdef WIN3264
25251 has_shortname = 1;
25252#endif
25253 }
25254
25255#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025256 /*
25257 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 */
25259 if (has_shortname)
25260 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025261 /* Copy the string if it is shortened by :h and when it wasn't copied
25262 * yet, because we are going to change it in place. Avoids changing
25263 * the buffer name for "%:8". */
25264 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 {
25266 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025267 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 return -1;
25269 vim_free(*bufp);
25270 *bufp = *fnamep = p;
25271 }
25272
25273 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025274 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 if (!has_fullname && !vim_isAbsName(*fnamep))
25276 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025277 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278 return -1;
25279 }
25280 else
25281 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025282 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283
Bram Moolenaardc935552011-08-17 15:23:23 +020025284 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025286 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 return -1;
25288
25289 if (l == 0)
25290 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025291 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025293 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 return -1;
25295 }
25296 *fnamelen = l;
25297 }
25298 }
25299#endif /* WIN3264 */
25300
25301 /* ":t" - tail, just the basename */
25302 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25303 {
25304 *usedlen += 2;
25305 *fnamelen -= (int)(tail - *fnamep);
25306 *fnamep = tail;
25307 }
25308
25309 /* ":e" - extension, can be repeated */
25310 /* ":r" - root, without extension, can be repeated */
25311 while (src[*usedlen] == ':'
25312 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25313 {
25314 /* find a '.' in the tail:
25315 * - for second :e: before the current fname
25316 * - otherwise: The last '.'
25317 */
25318 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25319 s = *fnamep - 2;
25320 else
25321 s = *fnamep + *fnamelen - 1;
25322 for ( ; s > tail; --s)
25323 if (s[0] == '.')
25324 break;
25325 if (src[*usedlen + 1] == 'e') /* :e */
25326 {
25327 if (s > tail)
25328 {
25329 *fnamelen += (int)(*fnamep - (s + 1));
25330 *fnamep = s + 1;
25331#ifdef VMS
25332 /* cut version from the extension */
25333 s = *fnamep + *fnamelen - 1;
25334 for ( ; s > *fnamep; --s)
25335 if (s[0] == ';')
25336 break;
25337 if (s > *fnamep)
25338 *fnamelen = s - *fnamep;
25339#endif
25340 }
25341 else if (*fnamep <= tail)
25342 *fnamelen = 0;
25343 }
25344 else /* :r */
25345 {
25346 if (s > tail) /* remove one extension */
25347 *fnamelen = (int)(s - *fnamep);
25348 }
25349 *usedlen += 2;
25350 }
25351
25352 /* ":s?pat?foo?" - substitute */
25353 /* ":gs?pat?foo?" - global substitute */
25354 if (src[*usedlen] == ':'
25355 && (src[*usedlen + 1] == 's'
25356 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25357 {
25358 char_u *str;
25359 char_u *pat;
25360 char_u *sub;
25361 int sep;
25362 char_u *flags;
25363 int didit = FALSE;
25364
25365 flags = (char_u *)"";
25366 s = src + *usedlen + 2;
25367 if (src[*usedlen + 1] == 'g')
25368 {
25369 flags = (char_u *)"g";
25370 ++s;
25371 }
25372
25373 sep = *s++;
25374 if (sep)
25375 {
25376 /* find end of pattern */
25377 p = vim_strchr(s, sep);
25378 if (p != NULL)
25379 {
25380 pat = vim_strnsave(s, (int)(p - s));
25381 if (pat != NULL)
25382 {
25383 s = p + 1;
25384 /* find end of substitution */
25385 p = vim_strchr(s, sep);
25386 if (p != NULL)
25387 {
25388 sub = vim_strnsave(s, (int)(p - s));
25389 str = vim_strnsave(*fnamep, *fnamelen);
25390 if (sub != NULL && str != NULL)
25391 {
25392 *usedlen = (int)(p + 1 - src);
25393 s = do_string_sub(str, pat, sub, flags);
25394 if (s != NULL)
25395 {
25396 *fnamep = s;
25397 *fnamelen = (int)STRLEN(s);
25398 vim_free(*bufp);
25399 *bufp = s;
25400 didit = TRUE;
25401 }
25402 }
25403 vim_free(sub);
25404 vim_free(str);
25405 }
25406 vim_free(pat);
25407 }
25408 }
25409 /* after using ":s", repeat all the modifiers */
25410 if (didit)
25411 goto repeat;
25412 }
25413 }
25414
Bram Moolenaar26df0922014-02-23 23:39:13 +010025415 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25416 {
25417 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25418 if (p == NULL)
25419 return -1;
25420 vim_free(*bufp);
25421 *bufp = *fnamep = p;
25422 *fnamelen = (int)STRLEN(p);
25423 *usedlen += 2;
25424 }
25425
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 return valid;
25427}
25428
25429/*
25430 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25431 * "flags" can be "g" to do a global substitute.
25432 * Returns an allocated string, NULL for error.
25433 */
25434 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025435do_string_sub(
25436 char_u *str,
25437 char_u *pat,
25438 char_u *sub,
25439 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025440{
25441 int sublen;
25442 regmatch_T regmatch;
25443 int i;
25444 int do_all;
25445 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025446 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447 garray_T ga;
25448 char_u *ret;
25449 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025450 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451
25452 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25453 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025454 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455
25456 ga_init2(&ga, 1, 200);
25457
25458 do_all = (flags[0] == 'g');
25459
25460 regmatch.rm_ic = p_ic;
25461 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25462 if (regmatch.regprog != NULL)
25463 {
25464 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025465 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025466 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25467 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025468 /* Skip empty match except for first match. */
25469 if (regmatch.startp[0] == regmatch.endp[0])
25470 {
25471 if (zero_width == regmatch.startp[0])
25472 {
25473 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025474 i = MB_PTR2LEN(tail);
25475 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25476 (size_t)i);
25477 ga.ga_len += i;
25478 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025479 continue;
25480 }
25481 zero_width = regmatch.startp[0];
25482 }
25483
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 /*
25485 * Get some space for a temporary buffer to do the substitution
25486 * into. It will contain:
25487 * - The text up to where the match is.
25488 * - The substituted text.
25489 * - The text after the match.
25490 */
25491 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025492 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25494 {
25495 ga_clear(&ga);
25496 break;
25497 }
25498
25499 /* copy the text up to where the match is */
25500 i = (int)(regmatch.startp[0] - tail);
25501 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25502 /* add the substituted text */
25503 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25504 + ga.ga_len + i, TRUE, TRUE, FALSE);
25505 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025506 tail = regmatch.endp[0];
25507 if (*tail == NUL)
25508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 if (!do_all)
25510 break;
25511 }
25512
25513 if (ga.ga_data != NULL)
25514 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25515
Bram Moolenaar473de612013-06-08 18:19:48 +020025516 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517 }
25518
25519 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25520 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025521 if (p_cpo == empty_option)
25522 p_cpo = save_cpo;
25523 else
25524 /* Darn, evaluating {sub} expression changed the value. */
25525 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526
25527 return ret;
25528}
25529
25530#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */