blob: d2ebeb971af581a890393c5b92982d637570393f [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! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012858 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859#ifdef FEAT_SEARCH_EXTRA
12860 "extra_search",
12861#endif
12862#ifdef FEAT_FKMAP
12863 "farsi",
12864#endif
12865#ifdef FEAT_SEARCHPATH
12866 "file_in_path",
12867#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012868#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012869 "filterpipe",
12870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871#ifdef FEAT_FIND_ID
12872 "find_in_path",
12873#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012874#ifdef FEAT_FLOAT
12875 "float",
12876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877#ifdef FEAT_FOLDING
12878 "folding",
12879#endif
12880#ifdef FEAT_FOOTER
12881 "footer",
12882#endif
12883#if !defined(USE_SYSTEM) && defined(UNIX)
12884 "fork",
12885#endif
12886#ifdef FEAT_GETTEXT
12887 "gettext",
12888#endif
12889#ifdef FEAT_GUI
12890 "gui",
12891#endif
12892#ifdef FEAT_GUI_ATHENA
12893# ifdef FEAT_GUI_NEXTAW
12894 "gui_neXtaw",
12895# else
12896 "gui_athena",
12897# endif
12898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899#ifdef FEAT_GUI_GTK
12900 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012903#ifdef FEAT_GUI_GNOME
12904 "gui_gnome",
12905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906#ifdef FEAT_GUI_MAC
12907 "gui_mac",
12908#endif
12909#ifdef FEAT_GUI_MOTIF
12910 "gui_motif",
12911#endif
12912#ifdef FEAT_GUI_PHOTON
12913 "gui_photon",
12914#endif
12915#ifdef FEAT_GUI_W16
12916 "gui_win16",
12917#endif
12918#ifdef FEAT_GUI_W32
12919 "gui_win32",
12920#endif
12921#ifdef FEAT_HANGULIN
12922 "hangul_input",
12923#endif
12924#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12925 "iconv",
12926#endif
12927#ifdef FEAT_INS_EXPAND
12928 "insert_expand",
12929#endif
12930#ifdef FEAT_JUMPLIST
12931 "jumplist",
12932#endif
12933#ifdef FEAT_KEYMAP
12934 "keymap",
12935#endif
12936#ifdef FEAT_LANGMAP
12937 "langmap",
12938#endif
12939#ifdef FEAT_LIBCALL
12940 "libcall",
12941#endif
12942#ifdef FEAT_LINEBREAK
12943 "linebreak",
12944#endif
12945#ifdef FEAT_LISP
12946 "lispindent",
12947#endif
12948#ifdef FEAT_LISTCMDS
12949 "listcmds",
12950#endif
12951#ifdef FEAT_LOCALMAP
12952 "localmap",
12953#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012954#ifdef FEAT_LUA
12955# ifndef DYNAMIC_LUA
12956 "lua",
12957# endif
12958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959#ifdef FEAT_MENU
12960 "menu",
12961#endif
12962#ifdef FEAT_SESSION
12963 "mksession",
12964#endif
12965#ifdef FEAT_MODIFY_FNAME
12966 "modify_fname",
12967#endif
12968#ifdef FEAT_MOUSE
12969 "mouse",
12970#endif
12971#ifdef FEAT_MOUSESHAPE
12972 "mouseshape",
12973#endif
12974#if defined(UNIX) || defined(VMS)
12975# ifdef FEAT_MOUSE_DEC
12976 "mouse_dec",
12977# endif
12978# ifdef FEAT_MOUSE_GPM
12979 "mouse_gpm",
12980# endif
12981# ifdef FEAT_MOUSE_JSB
12982 "mouse_jsbterm",
12983# endif
12984# ifdef FEAT_MOUSE_NET
12985 "mouse_netterm",
12986# endif
12987# ifdef FEAT_MOUSE_PTERM
12988 "mouse_pterm",
12989# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012990# ifdef FEAT_MOUSE_SGR
12991 "mouse_sgr",
12992# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012993# ifdef FEAT_SYSMOUSE
12994 "mouse_sysmouse",
12995# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012996# ifdef FEAT_MOUSE_URXVT
12997 "mouse_urxvt",
12998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999# ifdef FEAT_MOUSE_XTERM
13000 "mouse_xterm",
13001# endif
13002#endif
13003#ifdef FEAT_MBYTE
13004 "multi_byte",
13005#endif
13006#ifdef FEAT_MBYTE_IME
13007 "multi_byte_ime",
13008#endif
13009#ifdef FEAT_MULTI_LANG
13010 "multi_lang",
13011#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013012#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013013#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013014 "mzscheme",
13015#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017#ifdef FEAT_OLE
13018 "ole",
13019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020#ifdef FEAT_PATH_EXTRA
13021 "path_extra",
13022#endif
13023#ifdef FEAT_PERL
13024#ifndef DYNAMIC_PERL
13025 "perl",
13026#endif
13027#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013028#ifdef FEAT_PERSISTENT_UNDO
13029 "persistent_undo",
13030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031#ifdef FEAT_PYTHON
13032#ifndef DYNAMIC_PYTHON
13033 "python",
13034#endif
13035#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013036#ifdef FEAT_PYTHON3
13037#ifndef DYNAMIC_PYTHON3
13038 "python3",
13039#endif
13040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041#ifdef FEAT_POSTSCRIPT
13042 "postscript",
13043#endif
13044#ifdef FEAT_PRINTER
13045 "printer",
13046#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013047#ifdef FEAT_PROFILE
13048 "profile",
13049#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013050#ifdef FEAT_RELTIME
13051 "reltime",
13052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053#ifdef FEAT_QUICKFIX
13054 "quickfix",
13055#endif
13056#ifdef FEAT_RIGHTLEFT
13057 "rightleft",
13058#endif
13059#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13060 "ruby",
13061#endif
13062#ifdef FEAT_SCROLLBIND
13063 "scrollbind",
13064#endif
13065#ifdef FEAT_CMDL_INFO
13066 "showcmd",
13067 "cmdline_info",
13068#endif
13069#ifdef FEAT_SIGNS
13070 "signs",
13071#endif
13072#ifdef FEAT_SMARTINDENT
13073 "smartindent",
13074#endif
13075#ifdef FEAT_SNIFF
13076 "sniff",
13077#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013078#ifdef STARTUPTIME
13079 "startuptime",
13080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081#ifdef FEAT_STL_OPT
13082 "statusline",
13083#endif
13084#ifdef FEAT_SUN_WORKSHOP
13085 "sun_workshop",
13086#endif
13087#ifdef FEAT_NETBEANS_INTG
13088 "netbeans_intg",
13089#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013090#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013091 "spell",
13092#endif
13093#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 "syntax",
13095#endif
13096#if defined(USE_SYSTEM) || !defined(UNIX)
13097 "system",
13098#endif
13099#ifdef FEAT_TAG_BINS
13100 "tag_binary",
13101#endif
13102#ifdef FEAT_TAG_OLDSTATIC
13103 "tag_old_static",
13104#endif
13105#ifdef FEAT_TAG_ANYWHITE
13106 "tag_any_white",
13107#endif
13108#ifdef FEAT_TCL
13109# ifndef DYNAMIC_TCL
13110 "tcl",
13111# endif
13112#endif
13113#ifdef TERMINFO
13114 "terminfo",
13115#endif
13116#ifdef FEAT_TERMRESPONSE
13117 "termresponse",
13118#endif
13119#ifdef FEAT_TEXTOBJ
13120 "textobjects",
13121#endif
13122#ifdef HAVE_TGETENT
13123 "tgetent",
13124#endif
13125#ifdef FEAT_TITLE
13126 "title",
13127#endif
13128#ifdef FEAT_TOOLBAR
13129 "toolbar",
13130#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013131#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13132 "unnamedplus",
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef FEAT_USR_CMDS
13135 "user-commands", /* was accidentally included in 5.4 */
13136 "user_commands",
13137#endif
13138#ifdef FEAT_VIMINFO
13139 "viminfo",
13140#endif
13141#ifdef FEAT_VERTSPLIT
13142 "vertsplit",
13143#endif
13144#ifdef FEAT_VIRTUALEDIT
13145 "virtualedit",
13146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148#ifdef FEAT_VISUALEXTRA
13149 "visualextra",
13150#endif
13151#ifdef FEAT_VREPLACE
13152 "vreplace",
13153#endif
13154#ifdef FEAT_WILDIGN
13155 "wildignore",
13156#endif
13157#ifdef FEAT_WILDMENU
13158 "wildmenu",
13159#endif
13160#ifdef FEAT_WINDOWS
13161 "windows",
13162#endif
13163#ifdef FEAT_WAK
13164 "winaltkeys",
13165#endif
13166#ifdef FEAT_WRITEBACKUP
13167 "writebackup",
13168#endif
13169#ifdef FEAT_XIM
13170 "xim",
13171#endif
13172#ifdef FEAT_XFONTSET
13173 "xfontset",
13174#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013175#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013176 "xpm",
13177 "xpm_w32", /* for backward compatibility */
13178#else
13179# if defined(HAVE_XPM)
13180 "xpm",
13181# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183#ifdef USE_XSMP
13184 "xsmp",
13185#endif
13186#ifdef USE_XSMP_INTERACT
13187 "xsmp_interact",
13188#endif
13189#ifdef FEAT_XCLIPBOARD
13190 "xterm_clipboard",
13191#endif
13192#ifdef FEAT_XTERM_SAVE
13193 "xterm_save",
13194#endif
13195#if defined(UNIX) && defined(FEAT_X11)
13196 "X11",
13197#endif
13198 NULL
13199 };
13200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 for (i = 0; has_list[i] != NULL; ++i)
13203 if (STRICMP(name, has_list[i]) == 0)
13204 {
13205 n = TRUE;
13206 break;
13207 }
13208
13209 if (n == FALSE)
13210 {
13211 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013212 {
13213 if (name[5] == '-'
13214 && STRLEN(name) > 11
13215 && vim_isdigit(name[6])
13216 && vim_isdigit(name[8])
13217 && vim_isdigit(name[10]))
13218 {
13219 int major = atoi((char *)name + 6);
13220 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013221
13222 /* Expect "patch-9.9.01234". */
13223 n = (major < VIM_VERSION_MAJOR
13224 || (major == VIM_VERSION_MAJOR
13225 && (minor < VIM_VERSION_MINOR
13226 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013227 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013228 }
13229 else
13230 n = has_patch(atoi((char *)name + 5));
13231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 else if (STRICMP(name, "vim_starting") == 0)
13233 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013234#ifdef FEAT_MBYTE
13235 else if (STRICMP(name, "multi_byte_encoding") == 0)
13236 n = has_mbyte;
13237#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013238#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13239 else if (STRICMP(name, "balloon_multiline") == 0)
13240 n = multiline_balloon_available();
13241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#ifdef DYNAMIC_TCL
13243 else if (STRICMP(name, "tcl") == 0)
13244 n = tcl_enabled(FALSE);
13245#endif
13246#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13247 else if (STRICMP(name, "iconv") == 0)
13248 n = iconv_enabled(FALSE);
13249#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013250#ifdef DYNAMIC_LUA
13251 else if (STRICMP(name, "lua") == 0)
13252 n = lua_enabled(FALSE);
13253#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013254#ifdef DYNAMIC_MZSCHEME
13255 else if (STRICMP(name, "mzscheme") == 0)
13256 n = mzscheme_enabled(FALSE);
13257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258#ifdef DYNAMIC_RUBY
13259 else if (STRICMP(name, "ruby") == 0)
13260 n = ruby_enabled(FALSE);
13261#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013262#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263#ifdef DYNAMIC_PYTHON
13264 else if (STRICMP(name, "python") == 0)
13265 n = python_enabled(FALSE);
13266#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013267#endif
13268#ifdef FEAT_PYTHON3
13269#ifdef DYNAMIC_PYTHON3
13270 else if (STRICMP(name, "python3") == 0)
13271 n = python3_enabled(FALSE);
13272#endif
13273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274#ifdef DYNAMIC_PERL
13275 else if (STRICMP(name, "perl") == 0)
13276 n = perl_enabled(FALSE);
13277#endif
13278#ifdef FEAT_GUI
13279 else if (STRICMP(name, "gui_running") == 0)
13280 n = (gui.in_use || gui.starting);
13281# ifdef FEAT_GUI_W32
13282 else if (STRICMP(name, "gui_win32s") == 0)
13283 n = gui_is_win32s();
13284# endif
13285# ifdef FEAT_BROWSE
13286 else if (STRICMP(name, "browse") == 0)
13287 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13288# endif
13289#endif
13290#ifdef FEAT_SYN_HL
13291 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013292 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293#endif
13294#if defined(WIN3264)
13295 else if (STRICMP(name, "win95") == 0)
13296 n = mch_windows95();
13297#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013298#ifdef FEAT_NETBEANS_INTG
13299 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013300 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 }
13303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305}
13306
13307/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013308 * "has_key()" function
13309 */
13310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013311f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013312{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013313 if (argvars[0].v_type != VAR_DICT)
13314 {
13315 EMSG(_(e_dictreq));
13316 return;
13317 }
13318 if (argvars[0].vval.v_dict == NULL)
13319 return;
13320
13321 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013322 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013323}
13324
13325/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013326 * "haslocaldir()" function
13327 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013329f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013330{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013331 win_T *wp = NULL;
13332
13333 wp = find_tabwin(&argvars[0], &argvars[1]);
13334 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013335}
13336
13337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 * "hasmapto()" function
13339 */
13340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013341f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342{
13343 char_u *name;
13344 char_u *mode;
13345 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013346 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013349 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 mode = (char_u *)"nvo";
13351 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013353 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013354 if (argvars[2].v_type != VAR_UNKNOWN)
13355 abbr = get_tv_number(&argvars[2]);
13356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357
Bram Moolenaar2c932302006-03-18 21:42:09 +000013358 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362}
13363
13364/*
13365 * "histadd()" function
13366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013368f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
13370#ifdef FEAT_CMDHIST
13371 int histype;
13372 char_u *str;
13373 char_u buf[NUMBUFLEN];
13374#endif
13375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 if (check_restricted() || check_secure())
13378 return;
13379#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013380 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13381 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 if (histype >= 0)
13383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 if (*str != NUL)
13386 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013387 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 return;
13391 }
13392 }
13393#endif
13394}
13395
13396/*
13397 * "histdel()" function
13398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013400f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401{
13402#ifdef FEAT_CMDHIST
13403 int n;
13404 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13408 if (str == NULL)
13409 n = 0;
13410 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013413 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 else
13418 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013419 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013420 get_tv_string_buf(&argvars[1], buf));
13421 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422#endif
13423}
13424
13425/*
13426 * "histget()" function
13427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013429f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430{
13431#ifdef FEAT_CMDHIST
13432 int type;
13433 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13437 if (str == NULL)
13438 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 {
13441 type = get_histtype(str);
13442 if (argvars[1].v_type == VAR_UNKNOWN)
13443 idx = get_history_idx(type);
13444 else
13445 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13446 /* -1 on type error */
13447 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453}
13454
13455/*
13456 * "histnr()" function
13457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013459f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460{
13461 int i;
13462
13463#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 char_u *history = get_tv_string_chk(&argvars[0]);
13465
13466 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 if (i >= HIST_CMD && i < HIST_COUNT)
13468 i = get_history_idx(i);
13469 else
13470#endif
13471 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473}
13474
13475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 * "highlightID(name)" function
13477 */
13478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013479f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482}
13483
13484/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013485 * "highlight_exists()" function
13486 */
13487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013488f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013489{
13490 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13491}
13492
13493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 * "hostname()" function
13495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013497f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498{
13499 char_u hostname[256];
13500
13501 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->v_type = VAR_STRING;
13503 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504}
13505
13506/*
13507 * iconv() function
13508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013510f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511{
13512#ifdef FEAT_MBYTE
13513 char_u buf1[NUMBUFLEN];
13514 char_u buf2[NUMBUFLEN];
13515 char_u *from, *to, *str;
13516 vimconv_T vimconv;
13517#endif
13518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->v_type = VAR_STRING;
13520 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521
13522#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523 str = get_tv_string(&argvars[0]);
13524 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13525 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 vimconv.vc_type = CONV_NONE;
13527 convert_setup(&vimconv, from, to);
13528
13529 /* If the encodings are equal, no conversion needed. */
13530 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534
13535 convert_setup(&vimconv, NULL, NULL);
13536 vim_free(from);
13537 vim_free(to);
13538#endif
13539}
13540
13541/*
13542 * "indent()" function
13543 */
13544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013545f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546{
13547 linenr_T lnum;
13548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554}
13555
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013556/*
13557 * "index()" function
13558 */
13559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013560f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013561{
Bram Moolenaar33570922005-01-25 22:26:29 +000013562 list_T *l;
13563 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013564 long idx = 0;
13565 int ic = FALSE;
13566
13567 rettv->vval.v_number = -1;
13568 if (argvars[0].v_type != VAR_LIST)
13569 {
13570 EMSG(_(e_listreq));
13571 return;
13572 }
13573 l = argvars[0].vval.v_list;
13574 if (l != NULL)
13575 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013576 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013577 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 int error = FALSE;
13580
Bram Moolenaar758711c2005-02-02 23:11:38 +000013581 /* Start at specified item. Use the cached index that list_find()
13582 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013584 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013585 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 ic = get_tv_number_chk(&argvars[3], &error);
13587 if (error)
13588 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013589 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013590
Bram Moolenaar758711c2005-02-02 23:11:38 +000013591 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013592 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013593 {
13594 rettv->vval.v_number = idx;
13595 break;
13596 }
13597 }
13598}
13599
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600static int inputsecret_flag = 0;
13601
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013602static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013603
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013605 * This function is used by f_input() and f_inputdialog() functions. The third
13606 * argument to f_input() specifies the type of completion to use at the
13607 * prompt. The third argument to f_inputdialog() specifies the value to return
13608 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 */
13610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013611get_user_input(
13612 typval_T *argvars,
13613 typval_T *rettv,
13614 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 char_u *p = NULL;
13618 int c;
13619 char_u buf[NUMBUFLEN];
13620 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013622 int xp_type = EXPAND_NOTHING;
13623 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013626 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627
13628#ifdef NO_CONSOLE_INPUT
13629 /* While starting up, there is no place to enter text. */
13630 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632#endif
13633
13634 cmd_silent = FALSE; /* Want to see the prompt. */
13635 if (prompt != NULL)
13636 {
13637 /* Only the part of the message after the last NL is considered as
13638 * prompt for the command line */
13639 p = vim_strrchr(prompt, '\n');
13640 if (p == NULL)
13641 p = prompt;
13642 else
13643 {
13644 ++p;
13645 c = *p;
13646 *p = NUL;
13647 msg_start();
13648 msg_clr_eos();
13649 msg_puts_attr(prompt, echo_attr);
13650 msg_didout = FALSE;
13651 msg_starthere();
13652 *p = c;
13653 }
13654 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 if (argvars[1].v_type != VAR_UNKNOWN)
13657 {
13658 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13659 if (defstr != NULL)
13660 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013662 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013663 {
13664 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013665 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013666 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013667
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013668 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013669 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013670
Bram Moolenaar4463f292005-09-25 22:20:24 +000013671 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13672 if (xp_name == NULL)
13673 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013675 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013676
Bram Moolenaar4463f292005-09-25 22:20:24 +000013677 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13678 &xp_arg) == FAIL)
13679 return;
13680 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013681 }
13682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013684 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013685 int save_ex_normal_busy = ex_normal_busy;
13686 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013688 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13689 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013690 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013691 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013692 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013693 && argvars[1].v_type != VAR_UNKNOWN
13694 && argvars[2].v_type != VAR_UNKNOWN)
13695 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13696 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013697
13698 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 /* since the user typed this, no need to wait for return */
13701 need_wait_return = FALSE;
13702 msg_didout = FALSE;
13703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 cmd_silent = cmd_silent_save;
13705}
13706
13707/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013708 * "input()" function
13709 * Also handles inputsecret() when inputsecret is set.
13710 */
13711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013712f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013713{
13714 get_user_input(argvars, rettv, FALSE);
13715}
13716
13717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 * "inputdialog()" function
13719 */
13720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013721f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722{
13723#if defined(FEAT_GUI_TEXTDIALOG)
13724 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13725 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13726 {
13727 char_u *message;
13728 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013731 message = get_tv_string_chk(&argvars[0]);
13732 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013733 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013734 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 else
13736 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013737 if (message != NULL && defstr != NULL
13738 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013739 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013740 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 else
13742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013743 if (message != NULL && defstr != NULL
13744 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013745 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->vval.v_string = vim_strsave(
13747 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 }
13753 else
13754#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013755 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756}
13757
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013758/*
13759 * "inputlist()" function
13760 */
13761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013762f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013763{
13764 listitem_T *li;
13765 int selected;
13766 int mouse_used;
13767
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013768#ifdef NO_CONSOLE_INPUT
13769 /* While starting up, there is no place to enter text. */
13770 if (no_console_input())
13771 return;
13772#endif
13773 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13774 {
13775 EMSG2(_(e_listarg), "inputlist()");
13776 return;
13777 }
13778
13779 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013780 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013781 lines_left = Rows; /* avoid more prompt */
13782 msg_scroll = TRUE;
13783 msg_clr_eos();
13784
13785 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13786 {
13787 msg_puts(get_tv_string(&li->li_tv));
13788 msg_putchar('\n');
13789 }
13790
13791 /* Ask for choice. */
13792 selected = prompt_for_number(&mouse_used);
13793 if (mouse_used)
13794 selected -= lines_left;
13795
13796 rettv->vval.v_number = selected;
13797}
13798
13799
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13801
13802/*
13803 * "inputrestore()" function
13804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013806f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807{
13808 if (ga_userinput.ga_len > 0)
13809 {
13810 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13812 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013813 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 }
13815 else if (p_verbose > 1)
13816 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013817 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 }
13820}
13821
13822/*
13823 * "inputsave()" function
13824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013826f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013828 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 if (ga_grow(&ga_userinput, 1) == OK)
13830 {
13831 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13832 + ga_userinput.ga_len);
13833 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013834 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 }
13836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838}
13839
13840/*
13841 * "inputsecret()" function
13842 */
13843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013844f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845{
13846 ++cmdline_star;
13847 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013848 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 --cmdline_star;
13850 --inputsecret_flag;
13851}
13852
13853/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013854 * "insert()" function
13855 */
13856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013857f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013858{
13859 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013860 listitem_T *item;
13861 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013863
13864 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013866 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013867 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013868 {
13869 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 before = get_tv_number_chk(&argvars[2], &error);
13871 if (error)
13872 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013873
Bram Moolenaar758711c2005-02-02 23:11:38 +000013874 if (before == l->lv_len)
13875 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013876 else
13877 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013878 item = list_find(l, before);
13879 if (item == NULL)
13880 {
13881 EMSGN(_(e_listidx), before);
13882 l = NULL;
13883 }
13884 }
13885 if (l != NULL)
13886 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013887 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013888 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013889 }
13890 }
13891}
13892
13893/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013894 * "invert(expr)" function
13895 */
13896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013897f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013898{
13899 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13900}
13901
13902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 * "isdirectory()" function
13904 */
13905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013906f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909}
13910
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013911/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013912 * "islocked()" function
13913 */
13914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013915f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013916{
13917 lval_T lv;
13918 char_u *end;
13919 dictitem_T *di;
13920
13921 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013922 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13923 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013924 if (end != NULL && lv.ll_name != NULL)
13925 {
13926 if (*end != NUL)
13927 EMSG(_(e_trailing));
13928 else
13929 {
13930 if (lv.ll_tv == NULL)
13931 {
13932 if (check_changedtick(lv.ll_name))
13933 rettv->vval.v_number = 1; /* always locked */
13934 else
13935 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013936 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013937 if (di != NULL)
13938 {
13939 /* Consider a variable locked when:
13940 * 1. the variable itself is locked
13941 * 2. the value of the variable is locked.
13942 * 3. the List or Dict value is locked.
13943 */
13944 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13945 || tv_islocked(&di->di_tv));
13946 }
13947 }
13948 }
13949 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013950 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013951 else if (lv.ll_newkey != NULL)
13952 EMSG2(_(e_dictkey), lv.ll_newkey);
13953 else if (lv.ll_list != NULL)
13954 /* List item. */
13955 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13956 else
13957 /* Dictionary item. */
13958 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13959 }
13960 }
13961
13962 clear_lval(&lv);
13963}
13964
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013965static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013966
13967/*
13968 * Turn a dict into a list:
13969 * "what" == 0: list of keys
13970 * "what" == 1: list of values
13971 * "what" == 2: list of items
13972 */
13973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013974dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013975{
Bram Moolenaar33570922005-01-25 22:26:29 +000013976 list_T *l2;
13977 dictitem_T *di;
13978 hashitem_T *hi;
13979 listitem_T *li;
13980 listitem_T *li2;
13981 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013982 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013983
Bram Moolenaar8c711452005-01-14 21:53:12 +000013984 if (argvars[0].v_type != VAR_DICT)
13985 {
13986 EMSG(_(e_dictreq));
13987 return;
13988 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013989 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013990 return;
13991
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013992 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013993 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013994
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013995 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013996 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013998 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014000 --todo;
14001 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014002
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014003 li = listitem_alloc();
14004 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014005 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014006 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014007
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014008 if (what == 0)
14009 {
14010 /* keys() */
14011 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014012 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014013 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14014 }
14015 else if (what == 1)
14016 {
14017 /* values() */
14018 copy_tv(&di->di_tv, &li->li_tv);
14019 }
14020 else
14021 {
14022 /* items() */
14023 l2 = list_alloc();
14024 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014025 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014026 li->li_tv.vval.v_list = l2;
14027 if (l2 == NULL)
14028 break;
14029 ++l2->lv_refcount;
14030
14031 li2 = listitem_alloc();
14032 if (li2 == NULL)
14033 break;
14034 list_append(l2, li2);
14035 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014036 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014037 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14038
14039 li2 = listitem_alloc();
14040 if (li2 == NULL)
14041 break;
14042 list_append(l2, li2);
14043 copy_tv(&di->di_tv, &li2->li_tv);
14044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014045 }
14046 }
14047}
14048
14049/*
14050 * "items(dict)" function
14051 */
14052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014053f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014054{
14055 dict_list(argvars, rettv, 2);
14056}
14057
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014059 * "join()" function
14060 */
14061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014062f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014063{
14064 garray_T ga;
14065 char_u *sep;
14066
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014067 if (argvars[0].v_type != VAR_LIST)
14068 {
14069 EMSG(_(e_listreq));
14070 return;
14071 }
14072 if (argvars[0].vval.v_list == NULL)
14073 return;
14074 if (argvars[1].v_type == VAR_UNKNOWN)
14075 sep = (char_u *)" ";
14076 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014078
14079 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080
14081 if (sep != NULL)
14082 {
14083 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014084 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 ga_append(&ga, NUL);
14086 rettv->vval.v_string = (char_u *)ga.ga_data;
14087 }
14088 else
14089 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014090}
14091
14092/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014093 * "jsondecode()" function
14094 */
14095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014096f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014097{
14098 js_read_T reader;
14099
14100 reader.js_buf = get_tv_string(&argvars[0]);
14101 reader.js_eof = TRUE;
14102 reader.js_used = 0;
14103 json_decode(&reader, rettv);
14104}
14105
14106/*
14107 * "jsonencode()" function
14108 */
14109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014110f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014111{
14112 rettv->v_type = VAR_STRING;
14113 rettv->vval.v_string = json_encode(&argvars[0]);
14114}
14115
14116/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014117 * "keys()" function
14118 */
14119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014120f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014121{
14122 dict_list(argvars, rettv, 0);
14123}
14124
14125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 * "last_buffer_nr()" function.
14127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014129f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130{
14131 int n = 0;
14132 buf_T *buf;
14133
14134 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14135 if (n < buf->b_fnum)
14136 n = buf->b_fnum;
14137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014139}
14140
14141/*
14142 * "len()" function
14143 */
14144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014145f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014146{
14147 switch (argvars[0].v_type)
14148 {
14149 case VAR_STRING:
14150 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->vval.v_number = (varnumber_T)STRLEN(
14152 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014153 break;
14154 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014156 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014157 case VAR_DICT:
14158 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14159 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014160 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014161 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014162 break;
14163 }
14164}
14165
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014166static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014167
14168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014169libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014170{
14171#ifdef FEAT_LIBCALL
14172 char_u *string_in;
14173 char_u **string_result;
14174 int nr_result;
14175#endif
14176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014177 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014178 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014180
14181 if (check_restricted() || check_secure())
14182 return;
14183
14184#ifdef FEAT_LIBCALL
14185 /* The first two args must be strings, otherwise its meaningless */
14186 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14187 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014188 string_in = NULL;
14189 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014190 string_in = argvars[2].vval.v_string;
14191 if (type == VAR_NUMBER)
14192 string_result = NULL;
14193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014195 if (mch_libcall(argvars[0].vval.v_string,
14196 argvars[1].vval.v_string,
14197 string_in,
14198 argvars[2].vval.v_number,
14199 string_result,
14200 &nr_result) == OK
14201 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014202 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014203 }
14204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205}
14206
14207/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208 * "libcall()" function
14209 */
14210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014211f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014212{
14213 libcall_common(argvars, rettv, VAR_STRING);
14214}
14215
14216/*
14217 * "libcallnr()" function
14218 */
14219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014220f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014221{
14222 libcall_common(argvars, rettv, VAR_NUMBER);
14223}
14224
14225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 * "line(string)" function
14227 */
14228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014229f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230{
14231 linenr_T lnum = 0;
14232 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014233 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014235 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 if (fp != NULL)
14237 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239}
14240
14241/*
14242 * "line2byte(lnum)" function
14243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014245f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246{
14247#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249#else
14250 linenr_T lnum;
14251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14257 if (rettv->vval.v_number >= 0)
14258 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259#endif
14260}
14261
14262/*
14263 * "lispindent(lnum)" function
14264 */
14265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014266f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267{
14268#ifdef FEAT_LISP
14269 pos_T pos;
14270 linenr_T lnum;
14271
14272 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14275 {
14276 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 curwin->w_cursor = pos;
14279 }
14280 else
14281#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283}
14284
14285/*
14286 * "localtime()" function
14287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014289f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292}
14293
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014294static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
14296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014297get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298{
14299 char_u *keys;
14300 char_u *which;
14301 char_u buf[NUMBUFLEN];
14302 char_u *keys_buf = NULL;
14303 char_u *rhs;
14304 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014305 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014306 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014307 mapblock_T *mp;
14308 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309
14310 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->v_type = VAR_STRING;
14312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 if (*keys == NUL)
14316 return;
14317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014318 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014320 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014322 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014323 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014324 if (argvars[3].v_type != VAR_UNKNOWN)
14325 get_dict = get_tv_number(&argvars[3]);
14326 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 else
14329 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 if (which == NULL)
14331 return;
14332
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 mode = get_map_mode(&which, 0);
14334
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014335 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014336 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014338
14339 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014341 /* Return a string. */
14342 if (rhs != NULL)
14343 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344
Bram Moolenaarbd743252010-10-20 21:23:33 +020014345 }
14346 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14347 {
14348 /* Return a dictionary. */
14349 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14350 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14351 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352
Bram Moolenaarbd743252010-10-20 21:23:33 +020014353 dict_add_nr_str(dict, "lhs", 0L, lhs);
14354 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14355 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14356 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14357 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14358 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14359 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014360 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014361 dict_add_nr_str(dict, "mode", 0L, mapmode);
14362
14363 vim_free(lhs);
14364 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 }
14366}
14367
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014368#ifdef FEAT_FLOAT
14369/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014370 * "log()" function
14371 */
14372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014374{
14375 float_T f;
14376
14377 rettv->v_type = VAR_FLOAT;
14378 if (get_float_arg(argvars, &f) == OK)
14379 rettv->vval.v_float = log(f);
14380 else
14381 rettv->vval.v_float = 0.0;
14382}
14383
14384/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014385 * "log10()" function
14386 */
14387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014388f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014389{
14390 float_T f;
14391
14392 rettv->v_type = VAR_FLOAT;
14393 if (get_float_arg(argvars, &f) == OK)
14394 rettv->vval.v_float = log10(f);
14395 else
14396 rettv->vval.v_float = 0.0;
14397}
14398#endif
14399
Bram Moolenaar1dced572012-04-05 16:54:08 +020014400#ifdef FEAT_LUA
14401/*
14402 * "luaeval()" function
14403 */
14404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014405f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014406{
14407 char_u *str;
14408 char_u buf[NUMBUFLEN];
14409
14410 str = get_tv_string_buf(&argvars[0], buf);
14411 do_luaeval(str, argvars + 1, rettv);
14412}
14413#endif
14414
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014416 * "map()" function
14417 */
14418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014419f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014420{
14421 filter_map(argvars, rettv, TRUE);
14422}
14423
14424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 */
14427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014428f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431}
14432
14433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 */
14436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014437f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440}
14441
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014442static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443
14444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014445find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014447 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014448 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014449 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 char_u *pat;
14451 regmatch_T regmatch;
14452 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014453 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 char_u *save_cpo;
14455 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014456 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014457 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014458 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 list_T *l = NULL;
14460 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014461 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014462 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463
14464 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14465 save_cpo = p_cpo;
14466 p_cpo = (char_u *)"";
14467
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014468 rettv->vval.v_number = -1;
14469 if (type == 3)
14470 {
14471 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014473 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014474 }
14475 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->v_type = VAR_STRING;
14478 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014481 if (argvars[0].v_type == VAR_LIST)
14482 {
14483 if ((l = argvars[0].vval.v_list) == NULL)
14484 goto theend;
14485 li = l->lv_first;
14486 }
14487 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014488 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014489 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014490 len = (long)STRLEN(str);
14491 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14494 if (pat == NULL)
14495 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014496
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014497 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014499 int error = FALSE;
14500
14501 start = get_tv_number_chk(&argvars[2], &error);
14502 if (error)
14503 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014504 if (l != NULL)
14505 {
14506 li = list_find(l, start);
14507 if (li == NULL)
14508 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014509 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014510 }
14511 else
14512 {
14513 if (start < 0)
14514 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014515 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014517 /* When "count" argument is there ignore matches before "start",
14518 * otherwise skip part of the string. Differs when pattern is "^"
14519 * or "\<". */
14520 if (argvars[3].v_type != VAR_UNKNOWN)
14521 startcol = start;
14522 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014523 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014524 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014525 len -= start;
14526 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014527 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014528
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014529 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 nth = get_tv_number_chk(&argvars[3], &error);
14531 if (error)
14532 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 }
14534
14535 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14536 if (regmatch.regprog != NULL)
14537 {
14538 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014539
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014540 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014541 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014542 if (l != NULL)
14543 {
14544 if (li == NULL)
14545 {
14546 match = FALSE;
14547 break;
14548 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014550 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014551 if (str == NULL)
14552 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014553 }
14554
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014555 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014556
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014557 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014558 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014559 if (l == NULL && !match)
14560 break;
14561
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014562 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014563 if (l != NULL)
14564 {
14565 li = li->li_next;
14566 ++idx;
14567 }
14568 else
14569 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014570#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014571 startcol = (colnr_T)(regmatch.startp[0]
14572 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014573#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014574 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014575#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014576 if (startcol > (colnr_T)len
14577 || str + startcol <= regmatch.startp[0])
14578 {
14579 match = FALSE;
14580 break;
14581 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014582 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014583 }
14584
14585 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014587 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014588 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014589 int i;
14590
14591 /* return list with matched string and submatches */
14592 for (i = 0; i < NSUBEXP; ++i)
14593 {
14594 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014595 {
14596 if (list_append_string(rettv->vval.v_list,
14597 (char_u *)"", 0) == FAIL)
14598 break;
14599 }
14600 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014601 regmatch.startp[i],
14602 (int)(regmatch.endp[i] - regmatch.startp[i]))
14603 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014604 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014605 }
14606 }
14607 else if (type == 2)
14608 {
14609 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014610 if (l != NULL)
14611 copy_tv(&li->li_tv, rettv);
14612 else
14613 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014615 }
14616 else if (l != NULL)
14617 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 else
14619 {
14620 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 (varnumber_T)(regmatch.startp[0] - str);
14623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014626 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 }
14628 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014629 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 }
14631
14632theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014633 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 p_cpo = save_cpo;
14635}
14636
14637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 * "match()" function
14639 */
14640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014641f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642{
14643 find_some_match(argvars, rettv, 1);
14644}
14645
14646/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014647 * "matchadd()" function
14648 */
14649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014650f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014651{
14652#ifdef FEAT_SEARCH_EXTRA
14653 char_u buf[NUMBUFLEN];
14654 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14655 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14656 int prio = 10; /* default priority */
14657 int id = -1;
14658 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014659 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014660
14661 rettv->vval.v_number = -1;
14662
14663 if (grp == NULL || pat == NULL)
14664 return;
14665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014666 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014667 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014668 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014669 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014670 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014671 if (argvars[4].v_type != VAR_UNKNOWN)
14672 {
14673 if (argvars[4].v_type != VAR_DICT)
14674 {
14675 EMSG(_(e_dictreq));
14676 return;
14677 }
14678 if (dict_find(argvars[4].vval.v_dict,
14679 (char_u *)"conceal", -1) != NULL)
14680 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14681 (char_u *)"conceal", FALSE);
14682 }
14683 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014684 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014685 if (error == TRUE)
14686 return;
14687 if (id >= 1 && id <= 3)
14688 {
14689 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14690 return;
14691 }
14692
Bram Moolenaar6561d522015-07-21 15:48:27 +020014693 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14694 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014695#endif
14696}
14697
14698/*
14699 * "matchaddpos()" function
14700 */
14701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014702f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014703{
14704#ifdef FEAT_SEARCH_EXTRA
14705 char_u buf[NUMBUFLEN];
14706 char_u *group;
14707 int prio = 10;
14708 int id = -1;
14709 int error = FALSE;
14710 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014711 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014712
14713 rettv->vval.v_number = -1;
14714
14715 group = get_tv_string_buf_chk(&argvars[0], buf);
14716 if (group == NULL)
14717 return;
14718
14719 if (argvars[1].v_type != VAR_LIST)
14720 {
14721 EMSG2(_(e_listarg), "matchaddpos()");
14722 return;
14723 }
14724 l = argvars[1].vval.v_list;
14725 if (l == NULL)
14726 return;
14727
14728 if (argvars[2].v_type != VAR_UNKNOWN)
14729 {
14730 prio = get_tv_number_chk(&argvars[2], &error);
14731 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014732 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014733 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014734 if (argvars[4].v_type != VAR_UNKNOWN)
14735 {
14736 if (argvars[4].v_type != VAR_DICT)
14737 {
14738 EMSG(_(e_dictreq));
14739 return;
14740 }
14741 if (dict_find(argvars[4].vval.v_dict,
14742 (char_u *)"conceal", -1) != NULL)
14743 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14744 (char_u *)"conceal", FALSE);
14745 }
14746 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014747 }
14748 if (error == TRUE)
14749 return;
14750
14751 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14752 if (id == 1 || id == 2)
14753 {
14754 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14755 return;
14756 }
14757
Bram Moolenaar6561d522015-07-21 15:48:27 +020014758 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14759 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014760#endif
14761}
14762
14763/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014764 * "matcharg()" function
14765 */
14766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014768{
14769 if (rettv_list_alloc(rettv) == OK)
14770 {
14771#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014772 int id = get_tv_number(&argvars[0]);
14773 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014774
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014775 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014776 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014777 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14778 {
14779 list_append_string(rettv->vval.v_list,
14780 syn_id2name(m->hlg_id), -1);
14781 list_append_string(rettv->vval.v_list, m->pattern, -1);
14782 }
14783 else
14784 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014785 list_append_string(rettv->vval.v_list, NULL, -1);
14786 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014787 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014788 }
14789#endif
14790 }
14791}
14792
14793/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014794 * "matchdelete()" function
14795 */
14796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014797f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014798{
14799#ifdef FEAT_SEARCH_EXTRA
14800 rettv->vval.v_number = match_delete(curwin,
14801 (int)get_tv_number(&argvars[0]), TRUE);
14802#endif
14803}
14804
14805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014806 * "matchend()" function
14807 */
14808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014809f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014810{
14811 find_some_match(argvars, rettv, 0);
14812}
14813
14814/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014815 * "matchlist()" function
14816 */
14817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014818f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014819{
14820 find_some_match(argvars, rettv, 3);
14821}
14822
14823/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014824 * "matchstr()" function
14825 */
14826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014827f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014828{
14829 find_some_match(argvars, rettv, 2);
14830}
14831
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014832static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014833
14834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014835max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014836{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014837 long n = 0;
14838 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014839 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014840
14841 if (argvars[0].v_type == VAR_LIST)
14842 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014843 list_T *l;
14844 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014845
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014846 l = argvars[0].vval.v_list;
14847 if (l != NULL)
14848 {
14849 li = l->lv_first;
14850 if (li != NULL)
14851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014852 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014853 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014854 {
14855 li = li->li_next;
14856 if (li == NULL)
14857 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014859 if (domax ? i > n : i < n)
14860 n = i;
14861 }
14862 }
14863 }
14864 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014865 else if (argvars[0].v_type == VAR_DICT)
14866 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014867 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014868 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014869 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014870 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014871
14872 d = argvars[0].vval.v_dict;
14873 if (d != NULL)
14874 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014875 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014876 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014878 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014879 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014880 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014881 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014882 if (first)
14883 {
14884 n = i;
14885 first = FALSE;
14886 }
14887 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014888 n = i;
14889 }
14890 }
14891 }
14892 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014893 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014894 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014895 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014896}
14897
14898/*
14899 * "max()" function
14900 */
14901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014902f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014903{
14904 max_min(argvars, rettv, TRUE);
14905}
14906
14907/*
14908 * "min()" function
14909 */
14910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014911f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014912{
14913 max_min(argvars, rettv, FALSE);
14914}
14915
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014916static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014917
14918/*
14919 * Create the directory in which "dir" is located, and higher levels when
14920 * needed.
14921 */
14922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010014923mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014924{
14925 char_u *p;
14926 char_u *updir;
14927 int r = FAIL;
14928
14929 /* Get end of directory name in "dir".
14930 * We're done when it's "/" or "c:/". */
14931 p = gettail_sep(dir);
14932 if (p <= get_past_head(dir))
14933 return OK;
14934
14935 /* If the directory exists we're done. Otherwise: create it.*/
14936 updir = vim_strnsave(dir, (int)(p - dir));
14937 if (updir == NULL)
14938 return FAIL;
14939 if (mch_isdir(updir))
14940 r = OK;
14941 else if (mkdir_recurse(updir, prot) == OK)
14942 r = vim_mkdir_emsg(updir, prot);
14943 vim_free(updir);
14944 return r;
14945}
14946
14947#ifdef vim_mkdir
14948/*
14949 * "mkdir()" function
14950 */
14951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014952f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014953{
14954 char_u *dir;
14955 char_u buf[NUMBUFLEN];
14956 int prot = 0755;
14957
14958 rettv->vval.v_number = FAIL;
14959 if (check_restricted() || check_secure())
14960 return;
14961
14962 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014963 if (*dir == NUL)
14964 rettv->vval.v_number = FAIL;
14965 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014966 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014967 if (*gettail(dir) == NUL)
14968 /* remove trailing slashes */
14969 *gettail_sep(dir) = NUL;
14970
14971 if (argvars[1].v_type != VAR_UNKNOWN)
14972 {
14973 if (argvars[2].v_type != VAR_UNKNOWN)
14974 prot = get_tv_number_chk(&argvars[2], NULL);
14975 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14976 mkdir_recurse(dir, prot);
14977 }
14978 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014979 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014980}
14981#endif
14982
Bram Moolenaar0d660222005-01-07 21:51:51 +000014983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 * "mode()" function
14985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014987f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014989 char_u buf[3];
14990
14991 buf[1] = NUL;
14992 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 if (VIsual_active)
14995 {
14996 if (VIsual_select)
14997 buf[0] = VIsual_mode + 's' - 'v';
14998 else
14999 buf[0] = VIsual_mode;
15000 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015001 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015002 || State == CONFIRM)
15003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015005 if (State == ASKMORE)
15006 buf[1] = 'm';
15007 else if (State == CONFIRM)
15008 buf[1] = '?';
15009 }
15010 else if (State == EXTERNCMD)
15011 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 else if (State & INSERT)
15013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015014#ifdef FEAT_VREPLACE
15015 if (State & VREPLACE_FLAG)
15016 {
15017 buf[0] = 'R';
15018 buf[1] = 'v';
15019 }
15020 else
15021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 if (State & REPLACE_FLAG)
15023 buf[0] = 'R';
15024 else
15025 buf[0] = 'i';
15026 }
15027 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015030 if (exmode_active)
15031 buf[1] = 'v';
15032 }
15033 else if (exmode_active)
15034 {
15035 buf[0] = 'c';
15036 buf[1] = 'e';
15037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015041 if (finish_op)
15042 buf[1] = 'o';
15043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015045 /* Clear out the minor mode when the argument is not a non-zero number or
15046 * non-empty string. */
15047 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015048 buf[1] = NUL;
15049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 rettv->vval.v_string = vim_strsave(buf);
15051 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052}
15053
Bram Moolenaar429fa852013-04-15 12:27:36 +020015054#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015055/*
15056 * "mzeval()" function
15057 */
15058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015059f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015060{
15061 char_u *str;
15062 char_u buf[NUMBUFLEN];
15063
15064 str = get_tv_string_buf(&argvars[0], buf);
15065 do_mzeval(str, rettv);
15066}
Bram Moolenaar75676462013-01-30 14:55:42 +010015067
15068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015069mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015070{
15071 typval_T argvars[3];
15072
15073 argvars[0].v_type = VAR_STRING;
15074 argvars[0].vval.v_string = name;
15075 copy_tv(args, &argvars[1]);
15076 argvars[2].v_type = VAR_UNKNOWN;
15077 f_call(argvars, rettv);
15078 clear_tv(&argvars[1]);
15079}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015080#endif
15081
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 * "nextnonblank()" function
15084 */
15085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015086f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087{
15088 linenr_T lnum;
15089
15090 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 {
15094 lnum = 0;
15095 break;
15096 }
15097 if (*skipwhite(ml_get(lnum)) != NUL)
15098 break;
15099 }
15100 rettv->vval.v_number = lnum;
15101}
15102
15103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 * "nr2char()" function
15105 */
15106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015107f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108{
15109 char_u buf[NUMBUFLEN];
15110
15111#ifdef FEAT_MBYTE
15112 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015113 {
15114 int utf8 = 0;
15115
15116 if (argvars[1].v_type != VAR_UNKNOWN)
15117 utf8 = get_tv_number_chk(&argvars[1], NULL);
15118 if (utf8)
15119 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15120 else
15121 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 else
15124#endif
15125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015126 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 buf[1] = NUL;
15128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129 rettv->v_type = VAR_STRING;
15130 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015131}
15132
15133/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015134 * "or(expr, expr)" function
15135 */
15136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015137f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015138{
15139 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15140 | get_tv_number_chk(&argvars[1], NULL);
15141}
15142
15143/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015144 * "pathshorten()" function
15145 */
15146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015147f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015148{
15149 char_u *p;
15150
15151 rettv->v_type = VAR_STRING;
15152 p = get_tv_string_chk(&argvars[0]);
15153 if (p == NULL)
15154 rettv->vval.v_string = NULL;
15155 else
15156 {
15157 p = vim_strsave(p);
15158 rettv->vval.v_string = p;
15159 if (p != NULL)
15160 shorten_dir(p);
15161 }
15162}
15163
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015164#ifdef FEAT_PERL
15165/*
15166 * "perleval()" function
15167 */
15168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015169f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015170{
15171 char_u *str;
15172 char_u buf[NUMBUFLEN];
15173
15174 str = get_tv_string_buf(&argvars[0], buf);
15175 do_perleval(str, rettv);
15176}
15177#endif
15178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015179#ifdef FEAT_FLOAT
15180/*
15181 * "pow()" function
15182 */
15183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015184f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015185{
15186 float_T fx, fy;
15187
15188 rettv->v_type = VAR_FLOAT;
15189 if (get_float_arg(argvars, &fx) == OK
15190 && get_float_arg(&argvars[1], &fy) == OK)
15191 rettv->vval.v_float = pow(fx, fy);
15192 else
15193 rettv->vval.v_float = 0.0;
15194}
15195#endif
15196
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 * "prevnonblank()" function
15199 */
15200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015201f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202{
15203 linenr_T lnum;
15204
15205 lnum = get_tv_lnum(argvars);
15206 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15207 lnum = 0;
15208 else
15209 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15210 --lnum;
15211 rettv->vval.v_number = lnum;
15212}
15213
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015214/* This dummy va_list is here because:
15215 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15216 * - locally in the function results in a "used before set" warning
15217 * - using va_start() to initialize it gives "function with fixed args" error */
15218static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015219
Bram Moolenaar8c711452005-01-14 21:53:12 +000015220/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015221 * "printf()" function
15222 */
15223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015224f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015225{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015226 char_u buf[NUMBUFLEN];
15227 int len;
15228 char_u *s;
15229 int saved_did_emsg = did_emsg;
15230 char *fmt;
15231
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015232 rettv->v_type = VAR_STRING;
15233 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015234
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015235 /* Get the required length, allocate the buffer and do it for real. */
15236 did_emsg = FALSE;
15237 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15238 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15239 if (!did_emsg)
15240 {
15241 s = alloc(len + 1);
15242 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015243 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015244 rettv->vval.v_string = s;
15245 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015246 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015247 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015248 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015249}
15250
15251/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015252 * "pumvisible()" function
15253 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015255f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257#ifdef FEAT_INS_EXPAND
15258 if (pum_visible())
15259 rettv->vval.v_number = 1;
15260#endif
15261}
15262
Bram Moolenaardb913952012-06-29 12:54:53 +020015263#ifdef FEAT_PYTHON3
15264/*
15265 * "py3eval()" function
15266 */
15267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015268f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015269{
15270 char_u *str;
15271 char_u buf[NUMBUFLEN];
15272
15273 str = get_tv_string_buf(&argvars[0], buf);
15274 do_py3eval(str, rettv);
15275}
15276#endif
15277
15278#ifdef FEAT_PYTHON
15279/*
15280 * "pyeval()" function
15281 */
15282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015283f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015284{
15285 char_u *str;
15286 char_u buf[NUMBUFLEN];
15287
15288 str = get_tv_string_buf(&argvars[0], buf);
15289 do_pyeval(str, rettv);
15290}
15291#endif
15292
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015293/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015294 * "range()" function
15295 */
15296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015297f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015298{
15299 long start;
15300 long end;
15301 long stride = 1;
15302 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015303 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015306 if (argvars[1].v_type == VAR_UNKNOWN)
15307 {
15308 end = start - 1;
15309 start = 0;
15310 }
15311 else
15312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015314 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015316 }
15317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 if (error)
15319 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015320 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015321 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015322 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015323 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015324 else
15325 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015326 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015327 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015328 if (list_append_number(rettv->vval.v_list,
15329 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015331 }
15332}
15333
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015334/*
15335 * "readfile()" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015339{
15340 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015341 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015342 char_u *fname;
15343 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015344 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15345 int io_size = sizeof(buf);
15346 int readlen; /* size of last fread() */
15347 char_u *prev = NULL; /* previously read bytes, if any */
15348 long prevlen = 0; /* length of data in prev */
15349 long prevsize = 0; /* size of prev buffer */
15350 long maxline = MAXLNUM;
15351 long cnt = 0;
15352 char_u *p; /* position in buf */
15353 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015354
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015355 if (argvars[1].v_type != VAR_UNKNOWN)
15356 {
15357 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15358 binary = TRUE;
15359 if (argvars[2].v_type != VAR_UNKNOWN)
15360 maxline = get_tv_number(&argvars[2]);
15361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015363 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015365
15366 /* Always open the file in binary mode, library functions have a mind of
15367 * their own about CR-LF conversion. */
15368 fname = get_tv_string(&argvars[0]);
15369 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15370 {
15371 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15372 return;
15373 }
15374
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015375 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015376 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015377 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015379 /* This for loop processes what was read, but is also entered at end
15380 * of file so that either:
15381 * - an incomplete line gets written
15382 * - a "binary" file gets an empty line at the end if it ends in a
15383 * newline. */
15384 for (p = buf, start = buf;
15385 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15386 ++p)
15387 {
15388 if (*p == '\n' || readlen <= 0)
15389 {
15390 listitem_T *li;
15391 char_u *s = NULL;
15392 long_u len = p - start;
15393
15394 /* Finished a line. Remove CRs before NL. */
15395 if (readlen > 0 && !binary)
15396 {
15397 while (len > 0 && start[len - 1] == '\r')
15398 --len;
15399 /* removal may cross back to the "prev" string */
15400 if (len == 0)
15401 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15402 --prevlen;
15403 }
15404 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015405 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015406 else
15407 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015408 /* Change "prev" buffer to be the right size. This way
15409 * the bytes are only copied once, and very long lines are
15410 * allocated only once. */
15411 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015412 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015413 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015415 prev = NULL; /* the list will own the string */
15416 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015417 }
15418 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015419 if (s == NULL)
15420 {
15421 do_outofmem_msg((long_u) prevlen + len + 1);
15422 failed = TRUE;
15423 break;
15424 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015426 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015427 {
15428 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015429 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015430 break;
15431 }
15432 li->li_tv.v_type = VAR_STRING;
15433 li->li_tv.v_lock = 0;
15434 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015435 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015436
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015437 start = p + 1; /* step over newline */
15438 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015439 break;
15440 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015441 else if (*p == NUL)
15442 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015443#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015444 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15445 * when finding the BF and check the previous two bytes. */
15446 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015447 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015448 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15449 * + 1, these may be in the "prev" string. */
15450 char_u back1 = p >= buf + 1 ? p[-1]
15451 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15452 char_u back2 = p >= buf + 2 ? p[-2]
15453 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15454 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015455
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015457 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015458 char_u *dest = p - 2;
15459
15460 /* Usually a BOM is at the beginning of a file, and so at
15461 * the beginning of a line; then we can just step over it.
15462 */
15463 if (start == dest)
15464 start = p + 1;
15465 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015466 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015467 /* have to shuffle buf to close gap */
15468 int adjust_prevlen = 0;
15469
15470 if (dest < buf)
15471 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015472 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015473 dest = buf;
15474 }
15475 if (readlen > p - buf + 1)
15476 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15477 readlen -= 3 - adjust_prevlen;
15478 prevlen -= adjust_prevlen;
15479 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015480 }
15481 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015482 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015483#endif
15484 } /* for */
15485
15486 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15487 break;
15488 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015489 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015490 /* There's part of a line in buf, store it in "prev". */
15491 if (p - start + prevlen >= prevsize)
15492 {
15493 /* need bigger "prev" buffer */
15494 char_u *newprev;
15495
15496 /* A common use case is ordinary text files and "prev" gets a
15497 * fragment of a line, so the first allocation is made
15498 * small, to avoid repeatedly 'allocing' large and
15499 * 'reallocing' small. */
15500 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015501 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015502 else
15503 {
15504 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015505 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015506 prevsize = grow50pc > growmin ? grow50pc : growmin;
15507 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015508 newprev = prev == NULL ? alloc(prevsize)
15509 : vim_realloc(prev, prevsize);
15510 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015511 {
15512 do_outofmem_msg((long_u)prevsize);
15513 failed = TRUE;
15514 break;
15515 }
15516 prev = newprev;
15517 }
15518 /* Add the line part to end of "prev". */
15519 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015520 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015521 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015522 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015523
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015524 /*
15525 * For a negative line count use only the lines at the end of the file,
15526 * free the rest.
15527 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015528 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015529 while (cnt > -maxline)
15530 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015531 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015532 --cnt;
15533 }
15534
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015535 if (failed)
15536 {
15537 list_free(rettv->vval.v_list, TRUE);
15538 /* readfile doc says an empty list is returned on error */
15539 rettv->vval.v_list = list_alloc();
15540 }
15541
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015542 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 fclose(fd);
15544}
15545
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015546#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015547static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015548
15549/*
15550 * Convert a List to proftime_T.
15551 * Return FAIL when there is something wrong.
15552 */
15553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015554list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015555{
15556 long n1, n2;
15557 int error = FALSE;
15558
15559 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15560 || arg->vval.v_list->lv_len != 2)
15561 return FAIL;
15562 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15563 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15564# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015565 tm->HighPart = n1;
15566 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015567# else
15568 tm->tv_sec = n1;
15569 tm->tv_usec = n2;
15570# endif
15571 return error ? FAIL : OK;
15572}
15573#endif /* FEAT_RELTIME */
15574
15575/*
15576 * "reltime()" function
15577 */
15578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015579f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015580{
15581#ifdef FEAT_RELTIME
15582 proftime_T res;
15583 proftime_T start;
15584
15585 if (argvars[0].v_type == VAR_UNKNOWN)
15586 {
15587 /* No arguments: get current time. */
15588 profile_start(&res);
15589 }
15590 else if (argvars[1].v_type == VAR_UNKNOWN)
15591 {
15592 if (list2proftime(&argvars[0], &res) == FAIL)
15593 return;
15594 profile_end(&res);
15595 }
15596 else
15597 {
15598 /* Two arguments: compute the difference. */
15599 if (list2proftime(&argvars[0], &start) == FAIL
15600 || list2proftime(&argvars[1], &res) == FAIL)
15601 return;
15602 profile_sub(&res, &start);
15603 }
15604
15605 if (rettv_list_alloc(rettv) == OK)
15606 {
15607 long n1, n2;
15608
15609# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015610 n1 = res.HighPart;
15611 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015612# else
15613 n1 = res.tv_sec;
15614 n2 = res.tv_usec;
15615# endif
15616 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15617 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15618 }
15619#endif
15620}
15621
15622/*
15623 * "reltimestr()" function
15624 */
15625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015626f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015627{
15628#ifdef FEAT_RELTIME
15629 proftime_T tm;
15630#endif
15631
15632 rettv->v_type = VAR_STRING;
15633 rettv->vval.v_string = NULL;
15634#ifdef FEAT_RELTIME
15635 if (list2proftime(&argvars[0], &tm) == OK)
15636 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15637#endif
15638}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015639
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015641static void make_connection(void);
15642static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015643
15644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015646{
15647 if (X_DISPLAY == NULL
15648# ifdef FEAT_GUI
15649 && !gui.in_use
15650# endif
15651 )
15652 {
15653 x_force_connect = TRUE;
15654 setup_term_clip();
15655 x_force_connect = FALSE;
15656 }
15657}
15658
15659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015660check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661{
15662 make_connection();
15663 if (X_DISPLAY == NULL)
15664 {
15665 EMSG(_("E240: No connection to Vim server"));
15666 return FAIL;
15667 }
15668 return OK;
15669}
15670#endif
15671
15672#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015673static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015674
15675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015676remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677{
15678 char_u *server_name;
15679 char_u *keys;
15680 char_u *r = NULL;
15681 char_u buf[NUMBUFLEN];
15682# ifdef WIN32
15683 HWND w;
15684# else
15685 Window w;
15686# endif
15687
15688 if (check_restricted() || check_secure())
15689 return;
15690
15691# ifdef FEAT_X11
15692 if (check_connection() == FAIL)
15693 return;
15694# endif
15695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 server_name = get_tv_string_chk(&argvars[0]);
15697 if (server_name == NULL)
15698 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 keys = get_tv_string_buf(&argvars[1], buf);
15700# ifdef WIN32
15701 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15702# else
15703 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15704 < 0)
15705# endif
15706 {
15707 if (r != NULL)
15708 EMSG(r); /* sending worked but evaluation failed */
15709 else
15710 EMSG2(_("E241: Unable to send to %s"), server_name);
15711 return;
15712 }
15713
15714 rettv->vval.v_string = r;
15715
15716 if (argvars[2].v_type != VAR_UNKNOWN)
15717 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015719 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015720 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015722 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 v.di_tv.v_type = VAR_STRING;
15724 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 idvar = get_tv_string_chk(&argvars[2]);
15726 if (idvar != NULL)
15727 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015728 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729 }
15730}
15731#endif
15732
15733/*
15734 * "remote_expr()" function
15735 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015737f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738{
15739 rettv->v_type = VAR_STRING;
15740 rettv->vval.v_string = NULL;
15741#ifdef FEAT_CLIENTSERVER
15742 remote_common(argvars, rettv, TRUE);
15743#endif
15744}
15745
15746/*
15747 * "remote_foreground()" function
15748 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015750f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752#ifdef FEAT_CLIENTSERVER
15753# ifdef WIN32
15754 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015755 {
15756 char_u *server_name = get_tv_string_chk(&argvars[0]);
15757
15758 if (server_name != NULL)
15759 serverForeground(server_name);
15760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761# else
15762 /* Send a foreground() expression to the server. */
15763 argvars[1].v_type = VAR_STRING;
15764 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15765 argvars[2].v_type = VAR_UNKNOWN;
15766 remote_common(argvars, rettv, TRUE);
15767 vim_free(argvars[1].vval.v_string);
15768# endif
15769#endif
15770}
15771
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015773f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774{
15775#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777 char_u *s = NULL;
15778# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015779 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015781 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782
15783 if (check_restricted() || check_secure())
15784 {
15785 rettv->vval.v_number = -1;
15786 return;
15787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015788 serverid = get_tv_string_chk(&argvars[0]);
15789 if (serverid == NULL)
15790 {
15791 rettv->vval.v_number = -1;
15792 return; /* type error; errmsg already given */
15793 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015795 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796 if (n == 0)
15797 rettv->vval.v_number = -1;
15798 else
15799 {
15800 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15801 rettv->vval.v_number = (s != NULL);
15802 }
15803# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 if (check_connection() == FAIL)
15805 return;
15806
15807 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015808 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809# endif
15810
15811 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813 char_u *retvar;
15814
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 v.di_tv.v_type = VAR_STRING;
15816 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015817 retvar = get_tv_string_chk(&argvars[1]);
15818 if (retvar != NULL)
15819 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 }
15822#else
15823 rettv->vval.v_number = -1;
15824#endif
15825}
15826
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015828f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829{
15830 char_u *r = NULL;
15831
15832#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015833 char_u *serverid = get_tv_string_chk(&argvars[0]);
15834
15835 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015836 {
15837# ifdef WIN32
15838 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015839 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015840
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015841 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015842 if (n != 0)
15843 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15844 if (r == NULL)
15845# else
15846 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848# endif
15849 EMSG(_("E277: Unable to read a server reply"));
15850 }
15851#endif
15852 rettv->v_type = VAR_STRING;
15853 rettv->vval.v_string = r;
15854}
15855
15856/*
15857 * "remote_send()" function
15858 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015860f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861{
15862 rettv->v_type = VAR_STRING;
15863 rettv->vval.v_string = NULL;
15864#ifdef FEAT_CLIENTSERVER
15865 remote_common(argvars, rettv, FALSE);
15866#endif
15867}
15868
15869/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015870 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015871 */
15872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015873f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015874{
Bram Moolenaar33570922005-01-25 22:26:29 +000015875 list_T *l;
15876 listitem_T *item, *item2;
15877 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015878 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015879 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015880 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015881 dict_T *d;
15882 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015883 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015884
Bram Moolenaar8c711452005-01-14 21:53:12 +000015885 if (argvars[0].v_type == VAR_DICT)
15886 {
15887 if (argvars[2].v_type != VAR_UNKNOWN)
15888 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015889 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015890 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015892 key = get_tv_string_chk(&argvars[1]);
15893 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 di = dict_find(d, key, -1);
15896 if (di == NULL)
15897 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015898 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15899 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 {
15901 *rettv = di->di_tv;
15902 init_tv(&di->di_tv);
15903 dictitem_remove(d, di);
15904 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015905 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015906 }
15907 }
15908 else if (argvars[0].v_type != VAR_LIST)
15909 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015910 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015911 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015913 int error = FALSE;
15914
15915 idx = get_tv_number_chk(&argvars[1], &error);
15916 if (error)
15917 ; /* type error: do nothing, errmsg already given */
15918 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015919 EMSGN(_(e_listidx), idx);
15920 else
15921 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015922 if (argvars[2].v_type == VAR_UNKNOWN)
15923 {
15924 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015925 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015926 *rettv = item->li_tv;
15927 vim_free(item);
15928 }
15929 else
15930 {
15931 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015932 end = get_tv_number_chk(&argvars[2], &error);
15933 if (error)
15934 ; /* type error: do nothing */
15935 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015936 EMSGN(_(e_listidx), end);
15937 else
15938 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015939 int cnt = 0;
15940
15941 for (li = item; li != NULL; li = li->li_next)
15942 {
15943 ++cnt;
15944 if (li == item2)
15945 break;
15946 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015947 if (li == NULL) /* didn't find "item2" after "item" */
15948 EMSG(_(e_invrange));
15949 else
15950 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015951 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015952 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015953 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015954 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955 l->lv_first = item;
15956 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015957 item->li_prev = NULL;
15958 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015959 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015960 }
15961 }
15962 }
15963 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015964 }
15965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966}
15967
15968/*
15969 * "rename({from}, {to})" function
15970 */
15971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015972f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973{
15974 char_u buf[NUMBUFLEN];
15975
15976 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015979 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15980 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981}
15982
15983/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015984 * "repeat()" function
15985 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015987f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015988{
15989 char_u *p;
15990 int n;
15991 int slen;
15992 int len;
15993 char_u *r;
15994 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015995
15996 n = get_tv_number(&argvars[1]);
15997 if (argvars[0].v_type == VAR_LIST)
15998 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015999 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016000 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016001 if (list_extend(rettv->vval.v_list,
16002 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016003 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016004 }
16005 else
16006 {
16007 p = get_tv_string(&argvars[0]);
16008 rettv->v_type = VAR_STRING;
16009 rettv->vval.v_string = NULL;
16010
16011 slen = (int)STRLEN(p);
16012 len = slen * n;
16013 if (len <= 0)
16014 return;
16015
16016 r = alloc(len + 1);
16017 if (r != NULL)
16018 {
16019 for (i = 0; i < n; i++)
16020 mch_memmove(r + i * slen, p, (size_t)slen);
16021 r[len] = NUL;
16022 }
16023
16024 rettv->vval.v_string = r;
16025 }
16026}
16027
16028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 * "resolve()" function
16030 */
16031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016032f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033{
16034 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016035#ifdef HAVE_READLINK
16036 char_u *buf = NULL;
16037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040#ifdef FEAT_SHORTCUT
16041 {
16042 char_u *v = NULL;
16043
16044 v = mch_resolve_shortcut(p);
16045 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016048 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 }
16050#else
16051# ifdef HAVE_READLINK
16052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 char_u *cpy;
16054 int len;
16055 char_u *remain = NULL;
16056 char_u *q;
16057 int is_relative_to_current = FALSE;
16058 int has_trailing_pathsep = FALSE;
16059 int limit = 100;
16060
16061 p = vim_strsave(p);
16062
16063 if (p[0] == '.' && (vim_ispathsep(p[1])
16064 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16065 is_relative_to_current = TRUE;
16066
16067 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016068 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016071 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073
16074 q = getnextcomp(p);
16075 if (*q != NUL)
16076 {
16077 /* Separate the first path component in "p", and keep the
16078 * remainder (beginning with the path separator). */
16079 remain = vim_strsave(q - 1);
16080 q[-1] = NUL;
16081 }
16082
Bram Moolenaard9462e32011-04-11 21:35:11 +020016083 buf = alloc(MAXPATHL + 1);
16084 if (buf == NULL)
16085 goto fail;
16086
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 for (;;)
16088 {
16089 for (;;)
16090 {
16091 len = readlink((char *)p, (char *)buf, MAXPATHL);
16092 if (len <= 0)
16093 break;
16094 buf[len] = NUL;
16095
16096 if (limit-- == 0)
16097 {
16098 vim_free(p);
16099 vim_free(remain);
16100 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016101 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 goto fail;
16103 }
16104
16105 /* Ensure that the result will have a trailing path separator
16106 * if the argument has one. */
16107 if (remain == NULL && has_trailing_pathsep)
16108 add_pathsep(buf);
16109
16110 /* Separate the first path component in the link value and
16111 * concatenate the remainders. */
16112 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16113 if (*q != NUL)
16114 {
16115 if (remain == NULL)
16116 remain = vim_strsave(q - 1);
16117 else
16118 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016119 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 if (cpy != NULL)
16121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 vim_free(remain);
16123 remain = cpy;
16124 }
16125 }
16126 q[-1] = NUL;
16127 }
16128
16129 q = gettail(p);
16130 if (q > p && *q == NUL)
16131 {
16132 /* Ignore trailing path separator. */
16133 q[-1] = NUL;
16134 q = gettail(p);
16135 }
16136 if (q > p && !mch_isFullName(buf))
16137 {
16138 /* symlink is relative to directory of argument */
16139 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16140 if (cpy != NULL)
16141 {
16142 STRCPY(cpy, p);
16143 STRCPY(gettail(cpy), buf);
16144 vim_free(p);
16145 p = cpy;
16146 }
16147 }
16148 else
16149 {
16150 vim_free(p);
16151 p = vim_strsave(buf);
16152 }
16153 }
16154
16155 if (remain == NULL)
16156 break;
16157
16158 /* Append the first path component of "remain" to "p". */
16159 q = getnextcomp(remain + 1);
16160 len = q - remain - (*q != NUL);
16161 cpy = vim_strnsave(p, STRLEN(p) + len);
16162 if (cpy != NULL)
16163 {
16164 STRNCAT(cpy, remain, len);
16165 vim_free(p);
16166 p = cpy;
16167 }
16168 /* Shorten "remain". */
16169 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016170 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 else
16172 {
16173 vim_free(remain);
16174 remain = NULL;
16175 }
16176 }
16177
16178 /* If the result is a relative path name, make it explicitly relative to
16179 * the current directory if and only if the argument had this form. */
16180 if (!vim_ispathsep(*p))
16181 {
16182 if (is_relative_to_current
16183 && *p != NUL
16184 && !(p[0] == '.'
16185 && (p[1] == NUL
16186 || vim_ispathsep(p[1])
16187 || (p[1] == '.'
16188 && (p[2] == NUL
16189 || vim_ispathsep(p[2]))))))
16190 {
16191 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016192 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 if (cpy != NULL)
16194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 vim_free(p);
16196 p = cpy;
16197 }
16198 }
16199 else if (!is_relative_to_current)
16200 {
16201 /* Strip leading "./". */
16202 q = p;
16203 while (q[0] == '.' && vim_ispathsep(q[1]))
16204 q += 2;
16205 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016206 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 }
16208 }
16209
16210 /* Ensure that the result will have no trailing path separator
16211 * if the argument had none. But keep "/" or "//". */
16212 if (!has_trailing_pathsep)
16213 {
16214 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016215 if (after_pathsep(p, q))
16216 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 }
16218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 }
16221# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223# endif
16224#endif
16225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227
16228#ifdef HAVE_READLINK
16229fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016230 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016232 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233}
16234
16235/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 */
16238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016239f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240{
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 list_T *l;
16242 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244 if (argvars[0].v_type != VAR_LIST)
16245 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016246 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016247 && !tv_check_lock(l->lv_lock,
16248 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 {
16250 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016251 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016252 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253 while (li != NULL)
16254 {
16255 ni = li->li_prev;
16256 list_append(l, li);
16257 li = ni;
16258 }
16259 rettv->vval.v_list = l;
16260 rettv->v_type = VAR_LIST;
16261 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016262 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264}
16265
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016266#define SP_NOMOVE 0x01 /* don't move cursor */
16267#define SP_REPEAT 0x02 /* repeat to find outer pair */
16268#define SP_RETCOUNT 0x04 /* return matchcount */
16269#define SP_SETPCMARK 0x08 /* set previous context mark */
16270#define SP_START 0x10 /* accept match at start position */
16271#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16272#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016273#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016274
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016275static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016276
16277/*
16278 * Get flags for a search function.
16279 * Possibly sets "p_ws".
16280 * Returns BACKWARD, FORWARD or zero (for an error).
16281 */
16282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016283get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284{
16285 int dir = FORWARD;
16286 char_u *flags;
16287 char_u nbuf[NUMBUFLEN];
16288 int mask;
16289
16290 if (varp->v_type != VAR_UNKNOWN)
16291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 flags = get_tv_string_buf_chk(varp, nbuf);
16293 if (flags == NULL)
16294 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 while (*flags != NUL)
16296 {
16297 switch (*flags)
16298 {
16299 case 'b': dir = BACKWARD; break;
16300 case 'w': p_ws = TRUE; break;
16301 case 'W': p_ws = FALSE; break;
16302 default: mask = 0;
16303 if (flagsp != NULL)
16304 switch (*flags)
16305 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016306 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016307 case 'e': mask = SP_END; break;
16308 case 'm': mask = SP_RETCOUNT; break;
16309 case 'n': mask = SP_NOMOVE; break;
16310 case 'p': mask = SP_SUBPAT; break;
16311 case 'r': mask = SP_REPEAT; break;
16312 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016313 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 }
16315 if (mask == 0)
16316 {
16317 EMSG2(_(e_invarg2), flags);
16318 dir = 0;
16319 }
16320 else
16321 *flagsp |= mask;
16322 }
16323 if (dir == 0)
16324 break;
16325 ++flags;
16326 }
16327 }
16328 return dir;
16329}
16330
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016332 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016335search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016337 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 char_u *pat;
16339 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016340 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 int save_p_ws = p_ws;
16342 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016343 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016344 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016345 proftime_T tm;
16346#ifdef FEAT_RELTIME
16347 long time_limit = 0;
16348#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016349 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016350 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016353 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016354 if (dir == 0)
16355 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016356 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016357 if (flags & SP_START)
16358 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016359 if (flags & SP_END)
16360 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016361 if (flags & SP_COLUMN)
16362 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016363
Bram Moolenaar76929292008-01-06 19:07:36 +000016364 /* Optional arguments: line number to stop searching and timeout. */
16365 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016366 {
16367 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16368 if (lnum_stop < 0)
16369 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016370#ifdef FEAT_RELTIME
16371 if (argvars[3].v_type != VAR_UNKNOWN)
16372 {
16373 time_limit = get_tv_number_chk(&argvars[3], NULL);
16374 if (time_limit < 0)
16375 goto theend;
16376 }
16377#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016378 }
16379
Bram Moolenaar76929292008-01-06 19:07:36 +000016380#ifdef FEAT_RELTIME
16381 /* Set the time limit, if there is one. */
16382 profile_setlimit(time_limit, &tm);
16383#endif
16384
Bram Moolenaar231334e2005-07-25 20:46:57 +000016385 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016386 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016387 * Check to make sure only those flags are set.
16388 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16389 * flags cannot be set. Check for that condition also.
16390 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016391 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016392 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016394 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016395 goto theend;
16396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016398 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016399 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016400 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016401 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016403 if (flags & SP_SUBPAT)
16404 retval = subpatnum;
16405 else
16406 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016407 if (flags & SP_SETPCMARK)
16408 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410 if (match_pos != NULL)
16411 {
16412 /* Store the match cursor position */
16413 match_pos->lnum = pos.lnum;
16414 match_pos->col = pos.col + 1;
16415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 /* "/$" will put the cursor after the end of the line, may need to
16417 * correct that here */
16418 check_cursor();
16419 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016420
16421 /* If 'n' flag is used: restore cursor position. */
16422 if (flags & SP_NOMOVE)
16423 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016424 else
16425 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016426theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016428
16429 return retval;
16430}
16431
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016432#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016433
16434/*
16435 * round() is not in C90, use ceil() or floor() instead.
16436 */
16437 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016438vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016439{
16440 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16441}
16442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016443/*
16444 * "round({float})" function
16445 */
16446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016448{
16449 float_T f;
16450
16451 rettv->v_type = VAR_FLOAT;
16452 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016453 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016454 else
16455 rettv->vval.v_float = 0.0;
16456}
16457#endif
16458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016459/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016460 * "screenattr()" function
16461 */
16462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016463f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016464{
16465 int row;
16466 int col;
16467 int c;
16468
16469 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16470 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16471 if (row < 0 || row >= screen_Rows
16472 || col < 0 || col >= screen_Columns)
16473 c = -1;
16474 else
16475 c = ScreenAttrs[LineOffset[row] + col];
16476 rettv->vval.v_number = c;
16477}
16478
16479/*
16480 * "screenchar()" function
16481 */
16482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016483f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016484{
16485 int row;
16486 int col;
16487 int off;
16488 int c;
16489
16490 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16491 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16492 if (row < 0 || row >= screen_Rows
16493 || col < 0 || col >= screen_Columns)
16494 c = -1;
16495 else
16496 {
16497 off = LineOffset[row] + col;
16498#ifdef FEAT_MBYTE
16499 if (enc_utf8 && ScreenLinesUC[off] != 0)
16500 c = ScreenLinesUC[off];
16501 else
16502#endif
16503 c = ScreenLines[off];
16504 }
16505 rettv->vval.v_number = c;
16506}
16507
16508/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016509 * "screencol()" function
16510 *
16511 * First column is 1 to be consistent with virtcol().
16512 */
16513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016514f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016515{
16516 rettv->vval.v_number = screen_screencol() + 1;
16517}
16518
16519/*
16520 * "screenrow()" function
16521 */
16522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016523f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016524{
16525 rettv->vval.v_number = screen_screenrow() + 1;
16526}
16527
16528/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016529 * "search()" function
16530 */
16531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016532f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016533{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016534 int flags = 0;
16535
16536 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537}
16538
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016540 * "searchdecl()" function
16541 */
16542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016543f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016544{
16545 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016546 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016547 int error = FALSE;
16548 char_u *name;
16549
16550 rettv->vval.v_number = 1; /* default: FAIL */
16551
16552 name = get_tv_string_chk(&argvars[0]);
16553 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016554 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016555 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016556 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16557 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16558 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016559 if (!error && name != NULL)
16560 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016561 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016562}
16563
16564/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016565 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016567 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016568searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569{
16570 char_u *spat, *mpat, *epat;
16571 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 int dir;
16574 int flags = 0;
16575 char_u nbuf1[NUMBUFLEN];
16576 char_u nbuf2[NUMBUFLEN];
16577 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016578 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016579 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016580 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 spat = get_tv_string_chk(&argvars[0]);
16584 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16585 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16586 if (spat == NULL || mpat == NULL || epat == NULL)
16587 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 /* Handle the optional fourth argument: flags */
16590 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016591 if (dir == 0)
16592 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016593
16594 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016595 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16596 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016597 if ((flags & (SP_END | SP_SUBPAT)) != 0
16598 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016599 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016600 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016601 goto theend;
16602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016604 /* Using 'r' implies 'W', otherwise it doesn't work. */
16605 if (flags & SP_REPEAT)
16606 p_ws = FALSE;
16607
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016608 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016609 if (argvars[3].v_type == VAR_UNKNOWN
16610 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 skip = (char_u *)"";
16612 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016615 if (argvars[5].v_type != VAR_UNKNOWN)
16616 {
16617 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16618 if (lnum_stop < 0)
16619 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016620#ifdef FEAT_RELTIME
16621 if (argvars[6].v_type != VAR_UNKNOWN)
16622 {
16623 time_limit = get_tv_number_chk(&argvars[6], NULL);
16624 if (time_limit < 0)
16625 goto theend;
16626 }
16627#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016628 }
16629 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 if (skip == NULL)
16631 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016633 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016634 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016635
16636theend:
16637 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016638
16639 return retval;
16640}
16641
16642/*
16643 * "searchpair()" function
16644 */
16645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016646f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016647{
16648 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16649}
16650
16651/*
16652 * "searchpairpos()" function
16653 */
16654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016655f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016656{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016657 pos_T match_pos;
16658 int lnum = 0;
16659 int col = 0;
16660
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016661 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016662 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663
16664 if (searchpair_cmn(argvars, &match_pos) > 0)
16665 {
16666 lnum = match_pos.lnum;
16667 col = match_pos.col;
16668 }
16669
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016670 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16671 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016672}
16673
16674/*
16675 * Search for a start/middle/end thing.
16676 * Used by searchpair(), see its documentation for the details.
16677 * Returns 0 or -1 for no match,
16678 */
16679 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016680do_searchpair(
16681 char_u *spat, /* start pattern */
16682 char_u *mpat, /* middle pattern */
16683 char_u *epat, /* end pattern */
16684 int dir, /* BACKWARD or FORWARD */
16685 char_u *skip, /* skip expression */
16686 int flags, /* SP_SETPCMARK and other SP_ values */
16687 pos_T *match_pos,
16688 linenr_T lnum_stop, /* stop at this line if not zero */
16689 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016690{
16691 char_u *save_cpo;
16692 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16693 long retval = 0;
16694 pos_T pos;
16695 pos_T firstpos;
16696 pos_T foundpos;
16697 pos_T save_cursor;
16698 pos_T save_pos;
16699 int n;
16700 int r;
16701 int nest = 1;
16702 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016703 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016704 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016705
16706 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16707 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016708 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016709
Bram Moolenaar76929292008-01-06 19:07:36 +000016710#ifdef FEAT_RELTIME
16711 /* Set the time limit, if there is one. */
16712 profile_setlimit(time_limit, &tm);
16713#endif
16714
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016715 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16716 * start/middle/end (pat3, for the top pair). */
16717 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16718 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16719 if (pat2 == NULL || pat3 == NULL)
16720 goto theend;
16721 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16722 if (*mpat == NUL)
16723 STRCPY(pat3, pat2);
16724 else
16725 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16726 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016727 if (flags & SP_START)
16728 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016729
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 save_cursor = curwin->w_cursor;
16731 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016732 clearpos(&firstpos);
16733 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 pat = pat3;
16735 for (;;)
16736 {
16737 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016738 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16740 /* didn't find it or found the first match again: FAIL */
16741 break;
16742
16743 if (firstpos.lnum == 0)
16744 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016745 if (equalpos(pos, foundpos))
16746 {
16747 /* Found the same position again. Can happen with a pattern that
16748 * has "\zs" at the end and searching backwards. Advance one
16749 * character and try again. */
16750 if (dir == BACKWARD)
16751 decl(&pos);
16752 else
16753 incl(&pos);
16754 }
16755 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016757 /* clear the start flag to avoid getting stuck here */
16758 options &= ~SEARCH_START;
16759
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 /* If the skip pattern matches, ignore this match. */
16761 if (*skip != NUL)
16762 {
16763 save_pos = curwin->w_cursor;
16764 curwin->w_cursor = pos;
16765 r = eval_to_bool(skip, &err, NULL, FALSE);
16766 curwin->w_cursor = save_pos;
16767 if (err)
16768 {
16769 /* Evaluating {skip} caused an error, break here. */
16770 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016771 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 break;
16773 }
16774 if (r)
16775 continue;
16776 }
16777
16778 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16779 {
16780 /* Found end when searching backwards or start when searching
16781 * forward: nested pair. */
16782 ++nest;
16783 pat = pat2; /* nested, don't search for middle */
16784 }
16785 else
16786 {
16787 /* Found end when searching forward or start when searching
16788 * backward: end of (nested) pair; or found middle in outer pair. */
16789 if (--nest == 1)
16790 pat = pat3; /* outer level, search for middle */
16791 }
16792
16793 if (nest == 0)
16794 {
16795 /* Found the match: return matchcount or line number. */
16796 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016797 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016799 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016800 if (flags & SP_SETPCMARK)
16801 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 curwin->w_cursor = pos;
16803 if (!(flags & SP_REPEAT))
16804 break;
16805 nest = 1; /* search for next unmatched */
16806 }
16807 }
16808
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016809 if (match_pos != NULL)
16810 {
16811 /* Store the match cursor position */
16812 match_pos->lnum = curwin->w_cursor.lnum;
16813 match_pos->col = curwin->w_cursor.col + 1;
16814 }
16815
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016817 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 curwin->w_cursor = save_cursor;
16819
16820theend:
16821 vim_free(pat2);
16822 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016823 if (p_cpo == empty_option)
16824 p_cpo = save_cpo;
16825 else
16826 /* Darn, evaluating the {skip} expression changed the value. */
16827 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016828
16829 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830}
16831
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016832/*
16833 * "searchpos()" function
16834 */
16835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016836f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016837{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016838 pos_T match_pos;
16839 int lnum = 0;
16840 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016841 int n;
16842 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016843
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016844 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016845 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016846
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016847 n = search_cmn(argvars, &match_pos, &flags);
16848 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016849 {
16850 lnum = match_pos.lnum;
16851 col = match_pos.col;
16852 }
16853
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016854 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16855 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016856 if (flags & SP_SUBPAT)
16857 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016858}
16859
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016860#ifdef FEAT_CHANNEL
16861/*
16862 * common for "sendexpr()" and "sendraw()"
16863 * Returns the channel index if the caller should read the response.
16864 * Otherwise returns -1.
16865 */
16866 static int
16867send_common(typval_T *argvars, char_u *text, char *fun)
16868{
16869 int ch_idx;
16870 char_u *callback = NULL;
16871
16872 ch_idx = get_channel_arg(&argvars[0]);
16873 if (ch_idx < 0)
16874 return -1;
16875
16876 if (argvars[2].v_type != VAR_UNKNOWN)
16877 {
16878 callback = get_callback(&argvars[2]);
16879 if (callback == NULL)
16880 return -1;
16881 }
16882 /* Set the callback or clear it. An empty callback means no callback and
16883 * not reading the response. */
16884 channel_set_req_callback(ch_idx,
16885 callback != NULL && *callback == NUL ? NULL : callback);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016886
16887 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
16888 return ch_idx;
16889 return -1;
16890}
16891
16892/*
16893 * "sendexpr()" function
16894 */
16895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016896f_sendexpr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016897{
16898 char_u *text;
16899 char_u *resp;
16900 typval_T nrtv;
16901 typval_T listtv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016902 typval_T typetv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016903 int ch_idx;
16904
16905 /* return an empty string by default */
16906 rettv->v_type = VAR_STRING;
16907 rettv->vval.v_string = NULL;
16908
16909 nrtv.v_type = VAR_NUMBER;
16910 nrtv.vval.v_number = channel_get_id();
16911 if (rettv_list_alloc(&listtv) == FAIL)
16912 return;
16913 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
16914 || list_append_tv(listtv.vval.v_list, &argvars[1]) == FAIL)
16915 {
16916 list_unref(listtv.vval.v_list);
16917 return;
16918 }
16919
16920 text = json_encode(&listtv);
16921 list_unref(listtv.vval.v_list);
16922
16923 ch_idx = send_common(argvars, text, "sendexpr");
16924 if (ch_idx >= 0)
16925 {
16926 /* TODO: read until the whole JSON message is received */
16927 /* TODO: only use the message with the right message ID */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016928 /* TODO: check sequence number */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016929 resp = channel_read_block(ch_idx);
16930 if (resp != NULL)
16931 {
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016932 channel_decode_json(resp, &typetv, rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016933 vim_free(resp);
16934 }
16935 }
16936}
16937
16938/*
16939 * "sendraw()" function
16940 */
16941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016942f_sendraw(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016943{
16944 char_u buf[NUMBUFLEN];
16945 char_u *text;
16946 int ch_idx;
16947
16948 /* return an empty string by default */
16949 rettv->v_type = VAR_STRING;
16950 rettv->vval.v_string = NULL;
16951
16952 text = get_tv_string_buf(&argvars[1], buf);
16953 ch_idx = send_common(argvars, text, "sendraw");
16954 if (ch_idx >= 0)
16955 rettv->vval.v_string = channel_read_block(ch_idx);
16956}
16957#endif
16958
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016959
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016961f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963#ifdef FEAT_CLIENTSERVER
16964 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016965 char_u *server = get_tv_string_chk(&argvars[0]);
16966 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016969 if (server == NULL || reply == NULL)
16970 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 if (check_restricted() || check_secure())
16972 return;
16973# ifdef FEAT_X11
16974 if (check_connection() == FAIL)
16975 return;
16976# endif
16977
16978 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 EMSG(_("E258: Unable to send to client"));
16981 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 rettv->vval.v_number = 0;
16984#else
16985 rettv->vval.v_number = -1;
16986#endif
16987}
16988
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016990f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991{
16992 char_u *r = NULL;
16993
16994#ifdef FEAT_CLIENTSERVER
16995# ifdef WIN32
16996 r = serverGetVimNames();
16997# else
16998 make_connection();
16999 if (X_DISPLAY != NULL)
17000 r = serverGetVimNames(X_DISPLAY);
17001# endif
17002#endif
17003 rettv->v_type = VAR_STRING;
17004 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005}
17006
17007/*
17008 * "setbufvar()" function
17009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017011f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012{
17013 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017016 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 char_u nbuf[NUMBUFLEN];
17018
17019 if (check_restricted() || check_secure())
17020 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17022 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017023 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 varp = &argvars[2];
17025
17026 if (buf != NULL && varname != NULL && varp != NULL)
17027 {
17028 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030
17031 if (*varname == '&')
17032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 long numval;
17034 char_u *strval;
17035 int error = FALSE;
17036
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017038 numval = get_tv_number_chk(varp, &error);
17039 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 if (!error && strval != NULL)
17041 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 }
17043 else
17044 {
17045 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17046 if (bufvarname != NULL)
17047 {
17048 STRCPY(bufvarname, "b:");
17049 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017050 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 vim_free(bufvarname);
17052 }
17053 }
17054
17055 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058}
17059
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017061f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017062{
17063 dict_T *d;
17064 dictitem_T *di;
17065 char_u *csearch;
17066
17067 if (argvars[0].v_type != VAR_DICT)
17068 {
17069 EMSG(_(e_dictreq));
17070 return;
17071 }
17072
17073 if ((d = argvars[0].vval.v_dict) != NULL)
17074 {
17075 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17076 if (csearch != NULL)
17077 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017078#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017079 if (enc_utf8)
17080 {
17081 int pcc[MAX_MCO];
17082 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017083
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017084 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17085 }
17086 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017087#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017088 set_last_csearch(PTR2CHAR(csearch),
17089 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017090 }
17091
17092 di = dict_find(d, (char_u *)"forward", -1);
17093 if (di != NULL)
17094 set_csearch_direction(get_tv_number(&di->di_tv)
17095 ? FORWARD : BACKWARD);
17096
17097 di = dict_find(d, (char_u *)"until", -1);
17098 if (di != NULL)
17099 set_csearch_until(!!get_tv_number(&di->di_tv));
17100 }
17101}
17102
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103/*
17104 * "setcmdpos()" function
17105 */
17106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017107f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017109 int pos = (int)get_tv_number(&argvars[0]) - 1;
17110
17111 if (pos >= 0)
17112 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113}
17114
17115/*
17116 * "setline()" function
17117 */
17118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017119f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120{
17121 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017122 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017123 list_T *l = NULL;
17124 listitem_T *li = NULL;
17125 long added = 0;
17126 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017128 lnum = get_tv_lnum(&argvars[0]);
17129 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017131 l = argvars[1].vval.v_list;
17132 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017134 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017136
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017137 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017138 for (;;)
17139 {
17140 if (l != NULL)
17141 {
17142 /* list argument, get next string */
17143 if (li == NULL)
17144 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017145 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017146 li = li->li_next;
17147 }
17148
17149 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017150 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017151 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017152
17153 /* When coming here from Insert mode, sync undo, so that this can be
17154 * undone separately from what was previously inserted. */
17155 if (u_sync_once == 2)
17156 {
17157 u_sync_once = 1; /* notify that u_sync() was called */
17158 u_sync(TRUE);
17159 }
17160
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017161 if (lnum <= curbuf->b_ml.ml_line_count)
17162 {
17163 /* existing line, replace it */
17164 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17165 {
17166 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017167 if (lnum == curwin->w_cursor.lnum)
17168 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017169 rettv->vval.v_number = 0; /* OK */
17170 }
17171 }
17172 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17173 {
17174 /* lnum is one past the last line, append the line */
17175 ++added;
17176 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17177 rettv->vval.v_number = 0; /* OK */
17178 }
17179
17180 if (l == NULL) /* only one string argument */
17181 break;
17182 ++lnum;
17183 }
17184
17185 if (added > 0)
17186 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187}
17188
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017189static 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 +000017190
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017192 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017193 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017195set_qf_ll_list(
17196 win_T *wp UNUSED,
17197 typval_T *list_arg UNUSED,
17198 typval_T *action_arg UNUSED,
17199 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017200{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017201#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017202 char_u *act;
17203 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017204#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017205
Bram Moolenaar2641f772005-03-25 21:58:17 +000017206 rettv->vval.v_number = -1;
17207
17208#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017209 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017210 EMSG(_(e_listreq));
17211 else
17212 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017213 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017214
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017215 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017216 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017217 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017218 if (act == NULL)
17219 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017220 if (*act == 'a' || *act == 'r')
17221 action = *act;
17222 }
17223
Bram Moolenaar81484f42012-12-05 15:16:47 +010017224 if (l != NULL && set_errorlist(wp, l, action,
17225 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017226 rettv->vval.v_number = 0;
17227 }
17228#endif
17229}
17230
17231/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017232 * "setloclist()" function
17233 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017235f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017236{
17237 win_T *win;
17238
17239 rettv->vval.v_number = -1;
17240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017241 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017242 if (win != NULL)
17243 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17244}
17245
17246/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017247 * "setmatches()" function
17248 */
17249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017250f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017251{
17252#ifdef FEAT_SEARCH_EXTRA
17253 list_T *l;
17254 listitem_T *li;
17255 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017256 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017257
17258 rettv->vval.v_number = -1;
17259 if (argvars[0].v_type != VAR_LIST)
17260 {
17261 EMSG(_(e_listreq));
17262 return;
17263 }
17264 if ((l = argvars[0].vval.v_list) != NULL)
17265 {
17266
17267 /* To some extent make sure that we are dealing with a list from
17268 * "getmatches()". */
17269 li = l->lv_first;
17270 while (li != NULL)
17271 {
17272 if (li->li_tv.v_type != VAR_DICT
17273 || (d = li->li_tv.vval.v_dict) == NULL)
17274 {
17275 EMSG(_(e_invarg));
17276 return;
17277 }
17278 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017279 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17280 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017281 && dict_find(d, (char_u *)"priority", -1) != NULL
17282 && dict_find(d, (char_u *)"id", -1) != NULL))
17283 {
17284 EMSG(_(e_invarg));
17285 return;
17286 }
17287 li = li->li_next;
17288 }
17289
17290 clear_matches(curwin);
17291 li = l->lv_first;
17292 while (li != NULL)
17293 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017294 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017295 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017296 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017297 char_u *group;
17298 int priority;
17299 int id;
17300 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017301
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017302 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017303 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17304 {
17305 if (s == NULL)
17306 {
17307 s = list_alloc();
17308 if (s == NULL)
17309 return;
17310 }
17311
17312 /* match from matchaddpos() */
17313 for (i = 1; i < 9; i++)
17314 {
17315 sprintf((char *)buf, (char *)"pos%d", i);
17316 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17317 {
17318 if (di->di_tv.v_type != VAR_LIST)
17319 return;
17320
17321 list_append_tv(s, &di->di_tv);
17322 s->lv_refcount++;
17323 }
17324 else
17325 break;
17326 }
17327 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017328
17329 group = get_dict_string(d, (char_u *)"group", FALSE);
17330 priority = (int)get_dict_number(d, (char_u *)"priority");
17331 id = (int)get_dict_number(d, (char_u *)"id");
17332 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17333 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17334 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017335 if (i == 0)
17336 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017337 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017338 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017339 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017340 }
17341 else
17342 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017343 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017344 list_unref(s);
17345 s = NULL;
17346 }
17347
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017348 li = li->li_next;
17349 }
17350 rettv->vval.v_number = 0;
17351 }
17352#endif
17353}
17354
17355/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017356 * "setpos()" function
17357 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017359f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017360{
17361 pos_T pos;
17362 int fnum;
17363 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017364 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017365
Bram Moolenaar08250432008-02-13 11:42:46 +000017366 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017367 name = get_tv_string_chk(argvars);
17368 if (name != NULL)
17369 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017370 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017371 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017372 if (--pos.col < 0)
17373 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017374 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017375 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017376 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017377 if (fnum == curbuf->b_fnum)
17378 {
17379 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017380 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017381 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017382 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017383 curwin->w_set_curswant = FALSE;
17384 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017385 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017386 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017387 }
17388 else
17389 EMSG(_(e_invarg));
17390 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017391 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17392 {
17393 /* set mark */
17394 if (setmark_pos(name[1], &pos, fnum) == OK)
17395 rettv->vval.v_number = 0;
17396 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017397 else
17398 EMSG(_(e_invarg));
17399 }
17400 }
17401}
17402
17403/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017404 * "setqflist()" function
17405 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017407f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017408{
17409 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17410}
17411
17412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 * "setreg()" function
17414 */
17415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017416f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417{
17418 int regname;
17419 char_u *strregname;
17420 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 int append;
17423 char_u yank_type;
17424 long block_len;
17425
17426 block_len = -1;
17427 yank_type = MAUTO;
17428 append = FALSE;
17429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017431 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017433 if (strregname == NULL)
17434 return; /* type error; errmsg already given */
17435 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 if (regname == 0 || regname == '@')
17437 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 stropt = get_tv_string_chk(&argvars[2]);
17442 if (stropt == NULL)
17443 return; /* type error */
17444 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 switch (*stropt)
17446 {
17447 case 'a': case 'A': /* append */
17448 append = TRUE;
17449 break;
17450 case 'v': case 'c': /* character-wise selection */
17451 yank_type = MCHAR;
17452 break;
17453 case 'V': case 'l': /* line-wise selection */
17454 yank_type = MLINE;
17455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 case 'b': case Ctrl_V: /* block-wise selection */
17457 yank_type = MBLOCK;
17458 if (VIM_ISDIGIT(stropt[1]))
17459 {
17460 ++stropt;
17461 block_len = getdigits(&stropt) - 1;
17462 --stropt;
17463 }
17464 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 }
17466 }
17467
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017468 if (argvars[1].v_type == VAR_LIST)
17469 {
17470 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017471 char_u **allocval;
17472 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017473 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017474 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017475 int len = argvars[1].vval.v_list->lv_len;
17476 listitem_T *li;
17477
Bram Moolenaar7d647822014-04-05 21:28:56 +020017478 /* First half: use for pointers to result lines; second half: use for
17479 * pointers to allocated copies. */
17480 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017481 if (lstval == NULL)
17482 return;
17483 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017484 allocval = lstval + len + 2;
17485 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017486
17487 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17488 li = li->li_next)
17489 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017490 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017491 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017492 goto free_lstval;
17493 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017494 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017495 /* Need to make a copy, next get_tv_string_buf_chk() will
17496 * overwrite the string. */
17497 strval = vim_strsave(buf);
17498 if (strval == NULL)
17499 goto free_lstval;
17500 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017501 }
17502 *curval++ = strval;
17503 }
17504 *curval++ = NULL;
17505
17506 write_reg_contents_lst(regname, lstval, -1,
17507 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017508free_lstval:
17509 while (curallocval > allocval)
17510 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017511 vim_free(lstval);
17512 }
17513 else
17514 {
17515 strval = get_tv_string_chk(&argvars[1]);
17516 if (strval == NULL)
17517 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017518 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522}
17523
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017524/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017525 * "settabvar()" function
17526 */
17527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017528f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017529{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017530#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017531 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017532 tabpage_T *tp;
17533#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017534 char_u *varname, *tabvarname;
17535 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017536
17537 rettv->vval.v_number = 0;
17538
17539 if (check_restricted() || check_secure())
17540 return;
17541
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017542#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017543 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017544#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017545 varname = get_tv_string_chk(&argvars[1]);
17546 varp = &argvars[2];
17547
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017548 if (varname != NULL && varp != NULL
17549#ifdef FEAT_WINDOWS
17550 && tp != NULL
17551#endif
17552 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017553 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017554#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017555 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017556 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017557#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017558
17559 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17560 if (tabvarname != NULL)
17561 {
17562 STRCPY(tabvarname, "t:");
17563 STRCPY(tabvarname + 2, varname);
17564 set_var(tabvarname, varp, TRUE);
17565 vim_free(tabvarname);
17566 }
17567
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017568#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017569 /* Restore current tabpage */
17570 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017571 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017572#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017573 }
17574}
17575
17576/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017577 * "settabwinvar()" function
17578 */
17579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017580f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017581{
17582 setwinvar(argvars, rettv, 1);
17583}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584
17585/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017586 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017589f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017591 setwinvar(argvars, rettv, 0);
17592}
17593
17594/*
17595 * "setwinvar()" and "settabwinvar()" functions
17596 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017597
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017599setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017600{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 win_T *win;
17602#ifdef FEAT_WINDOWS
17603 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017604 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017605 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606#endif
17607 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017610 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611
17612 if (check_restricted() || check_secure())
17613 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017614
17615#ifdef FEAT_WINDOWS
17616 if (off == 1)
17617 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17618 else
17619 tp = curtab;
17620#endif
17621 win = find_win_by_nr(&argvars[off], tp);
17622 varname = get_tv_string_chk(&argvars[off + 1]);
17623 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624
17625 if (win != NULL && varname != NULL && varp != NULL)
17626 {
17627#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017628 need_switch_win = !(tp == curtab && win == curwin);
17629 if (!need_switch_win
17630 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017633 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017635 long numval;
17636 char_u *strval;
17637 int error = FALSE;
17638
17639 ++varname;
17640 numval = get_tv_number_chk(varp, &error);
17641 strval = get_tv_string_buf_chk(varp, nbuf);
17642 if (!error && strval != NULL)
17643 set_option_value(varname, numval, strval, OPT_LOCAL);
17644 }
17645 else
17646 {
17647 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17648 if (winvarname != NULL)
17649 {
17650 STRCPY(winvarname, "w:");
17651 STRCPY(winvarname + 2, varname);
17652 set_var(winvarname, varp, TRUE);
17653 vim_free(winvarname);
17654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 }
17656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017658 if (need_switch_win)
17659 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660#endif
17661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662}
17663
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017664#ifdef FEAT_CRYPT
17665/*
17666 * "sha256({string})" function
17667 */
17668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017669f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017670{
17671 char_u *p;
17672
17673 p = get_tv_string(&argvars[0]);
17674 rettv->vval.v_string = vim_strsave(
17675 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17676 rettv->v_type = VAR_STRING;
17677}
17678#endif /* FEAT_CRYPT */
17679
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017681 * "shellescape({string})" function
17682 */
17683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017684f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017685{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017686 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017687 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017688 rettv->v_type = VAR_STRING;
17689}
17690
17691/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017692 * shiftwidth() function
17693 */
17694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017695f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017696{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017697 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017698}
17699
17700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017701 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 */
17703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017704f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017706 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707
Bram Moolenaar0d660222005-01-07 21:51:51 +000017708 p = get_tv_string(&argvars[0]);
17709 rettv->vval.v_string = vim_strsave(p);
17710 simplify_filename(rettv->vval.v_string); /* simplify in place */
17711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712}
17713
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017714#ifdef FEAT_FLOAT
17715/*
17716 * "sin()" function
17717 */
17718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017719f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017720{
17721 float_T f;
17722
17723 rettv->v_type = VAR_FLOAT;
17724 if (get_float_arg(argvars, &f) == OK)
17725 rettv->vval.v_float = sin(f);
17726 else
17727 rettv->vval.v_float = 0.0;
17728}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017729
17730/*
17731 * "sinh()" function
17732 */
17733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017734f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017735{
17736 float_T f;
17737
17738 rettv->v_type = VAR_FLOAT;
17739 if (get_float_arg(argvars, &f) == OK)
17740 rettv->vval.v_float = sinh(f);
17741 else
17742 rettv->vval.v_float = 0.0;
17743}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017744#endif
17745
Bram Moolenaar0d660222005-01-07 21:51:51 +000017746static int
17747#ifdef __BORLANDC__
17748 _RTLENTRYF
17749#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017750 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017751static int
17752#ifdef __BORLANDC__
17753 _RTLENTRYF
17754#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017755 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017756
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017757/* struct used in the array that's given to qsort() */
17758typedef struct
17759{
17760 listitem_T *item;
17761 int idx;
17762} sortItem_T;
17763
Bram Moolenaar0d660222005-01-07 21:51:51 +000017764static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017765static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017766static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017767#ifdef FEAT_FLOAT
17768static int item_compare_float;
17769#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017770static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017771static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017772static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017773static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017774static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017775#define ITEM_COMPARE_FAIL 999
17776
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017778 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017780 static int
17781#ifdef __BORLANDC__
17782_RTLENTRYF
17783#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017784item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017786 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017787 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017788 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017789 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790 int res;
17791 char_u numbuf1[NUMBUFLEN];
17792 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017794 si1 = (sortItem_T *)s1;
17795 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017796 tv1 = &si1->item->li_tv;
17797 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017798
17799 if (item_compare_numbers)
17800 {
17801 long v1 = get_tv_number(tv1);
17802 long v2 = get_tv_number(tv2);
17803
17804 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17805 }
17806
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017807#ifdef FEAT_FLOAT
17808 if (item_compare_float)
17809 {
17810 float_T v1 = get_tv_float(tv1);
17811 float_T v2 = get_tv_float(tv2);
17812
17813 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17814 }
17815#endif
17816
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017817 /* tv2string() puts quotes around a string and allocates memory. Don't do
17818 * that for string variables. Use a single quote when comparing with a
17819 * non-string to do what the docs promise. */
17820 if (tv1->v_type == VAR_STRING)
17821 {
17822 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17823 p1 = (char_u *)"'";
17824 else
17825 p1 = tv1->vval.v_string;
17826 }
17827 else
17828 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17829 if (tv2->v_type == VAR_STRING)
17830 {
17831 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17832 p2 = (char_u *)"'";
17833 else
17834 p2 = tv2->vval.v_string;
17835 }
17836 else
17837 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017838 if (p1 == NULL)
17839 p1 = (char_u *)"";
17840 if (p2 == NULL)
17841 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017842 if (!item_compare_numeric)
17843 {
17844 if (item_compare_ic)
17845 res = STRICMP(p1, p2);
17846 else
17847 res = STRCMP(p1, p2);
17848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017850 {
17851 double n1, n2;
17852 n1 = strtod((char *)p1, (char **)&p1);
17853 n2 = strtod((char *)p2, (char **)&p2);
17854 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17855 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017856
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017857 /* When the result would be zero, compare the item indexes. Makes the
17858 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017859 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017860 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017861
Bram Moolenaar0d660222005-01-07 21:51:51 +000017862 vim_free(tofree1);
17863 vim_free(tofree2);
17864 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865}
17866
17867 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017868#ifdef __BORLANDC__
17869_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017871item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017872{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017873 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017876 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017877 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017879 /* shortcut after failure in previous call; compare all items equal */
17880 if (item_compare_func_err)
17881 return 0;
17882
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017883 si1 = (sortItem_T *)s1;
17884 si2 = (sortItem_T *)s2;
17885
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017886 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017887 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017888 copy_tv(&si1->item->li_tv, &argv[0]);
17889 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017890
17891 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017892 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017893 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17894 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895 clear_tv(&argv[0]);
17896 clear_tv(&argv[1]);
17897
17898 if (res == FAIL)
17899 res = ITEM_COMPARE_FAIL;
17900 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017901 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17902 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017903 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017904 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017905
17906 /* When the result would be zero, compare the pointers themselves. Makes
17907 * the sort stable. */
17908 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017909 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017910
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911 return res;
17912}
17913
17914/*
17915 * "sort({list})" function
17916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017918do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919{
Bram Moolenaar33570922005-01-25 22:26:29 +000017920 list_T *l;
17921 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017922 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017923 long len;
17924 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017927 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 else
17929 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017931 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017932 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17933 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017934 return;
17935 rettv->vval.v_list = l;
17936 rettv->v_type = VAR_LIST;
17937 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938
Bram Moolenaar0d660222005-01-07 21:51:51 +000017939 len = list_len(l);
17940 if (len <= 1)
17941 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942
Bram Moolenaar0d660222005-01-07 21:51:51 +000017943 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017944 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017945 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017946#ifdef FEAT_FLOAT
17947 item_compare_float = FALSE;
17948#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017949 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017950 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951 if (argvars[1].v_type != VAR_UNKNOWN)
17952 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017953 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017954 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017955 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956 else
17957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017958 int error = FALSE;
17959
17960 i = get_tv_number_chk(&argvars[1], &error);
17961 if (error)
17962 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017963 if (i == 1)
17964 item_compare_ic = TRUE;
17965 else
17966 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017967 if (item_compare_func != NULL)
17968 {
17969 if (STRCMP(item_compare_func, "n") == 0)
17970 {
17971 item_compare_func = NULL;
17972 item_compare_numeric = TRUE;
17973 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017974 else if (STRCMP(item_compare_func, "N") == 0)
17975 {
17976 item_compare_func = NULL;
17977 item_compare_numbers = TRUE;
17978 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017979#ifdef FEAT_FLOAT
17980 else if (STRCMP(item_compare_func, "f") == 0)
17981 {
17982 item_compare_func = NULL;
17983 item_compare_float = TRUE;
17984 }
17985#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017986 else if (STRCMP(item_compare_func, "i") == 0)
17987 {
17988 item_compare_func = NULL;
17989 item_compare_ic = TRUE;
17990 }
17991 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017993
17994 if (argvars[2].v_type != VAR_UNKNOWN)
17995 {
17996 /* optional third argument: {dict} */
17997 if (argvars[2].v_type != VAR_DICT)
17998 {
17999 EMSG(_(e_dictreq));
18000 return;
18001 }
18002 item_compare_selfdict = argvars[2].vval.v_dict;
18003 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018007 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018008 if (ptrs == NULL)
18009 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010
Bram Moolenaar327aa022014-03-25 18:24:23 +010018011 i = 0;
18012 if (sort)
18013 {
18014 /* sort(): ptrs will be the list to sort */
18015 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018016 {
18017 ptrs[i].item = li;
18018 ptrs[i].idx = i;
18019 ++i;
18020 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018021
18022 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018023 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018024 /* test the compare function */
18025 if (item_compare_func != NULL
18026 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018027 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018028 EMSG(_("E702: Sort compare function failed"));
18029 else
18030 {
18031 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018032 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018033 item_compare_func == NULL ? item_compare : item_compare2);
18034
18035 if (!item_compare_func_err)
18036 {
18037 /* Clear the List and append the items in sorted order. */
18038 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18039 l->lv_len = 0;
18040 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018041 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018042 }
18043 }
18044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018047 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018048
18049 /* f_uniq(): ptrs will be a stack of items to remove */
18050 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018051 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018052 item_compare_func_ptr = item_compare_func
18053 ? item_compare2 : item_compare;
18054
18055 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18056 li = li->li_next)
18057 {
18058 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18059 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018060 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018061 if (item_compare_func_err)
18062 {
18063 EMSG(_("E882: Uniq compare function failed"));
18064 break;
18065 }
18066 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018068 if (!item_compare_func_err)
18069 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018070 while (--i >= 0)
18071 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018072 li = ptrs[i].item->li_next;
18073 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018074 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018075 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018076 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018077 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018078 list_fix_watch(l, li);
18079 listitem_free(li);
18080 l->lv_len--;
18081 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018082 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083 }
18084
18085 vim_free(ptrs);
18086 }
18087}
18088
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018089/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018090 * "sort({list})" function
18091 */
18092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018093f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018094{
18095 do_sort_uniq(argvars, rettv, TRUE);
18096}
18097
18098/*
18099 * "uniq({list})" function
18100 */
18101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018102f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018103{
18104 do_sort_uniq(argvars, rettv, FALSE);
18105}
18106
18107/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018108 * "soundfold({word})" function
18109 */
18110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018111f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018112{
18113 char_u *s;
18114
18115 rettv->v_type = VAR_STRING;
18116 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018117#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018118 rettv->vval.v_string = eval_soundfold(s);
18119#else
18120 rettv->vval.v_string = vim_strsave(s);
18121#endif
18122}
18123
18124/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018125 * "spellbadword()" function
18126 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018128f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018129{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018130 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018131 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018132 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018133
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018134 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018135 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018136
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018137#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018138 if (argvars[0].v_type == VAR_UNKNOWN)
18139 {
18140 /* Find the start and length of the badly spelled word. */
18141 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18142 if (len != 0)
18143 word = ml_get_cursor();
18144 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018145 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018146 {
18147 char_u *str = get_tv_string_chk(&argvars[0]);
18148 int capcol = -1;
18149
18150 if (str != NULL)
18151 {
18152 /* Check the argument for spelling. */
18153 while (*str != NUL)
18154 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018155 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018156 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018157 {
18158 word = str;
18159 break;
18160 }
18161 str += len;
18162 }
18163 }
18164 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018165#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018166
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018167 list_append_string(rettv->vval.v_list, word, len);
18168 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018169 attr == HLF_SPB ? "bad" :
18170 attr == HLF_SPR ? "rare" :
18171 attr == HLF_SPL ? "local" :
18172 attr == HLF_SPC ? "caps" :
18173 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018174}
18175
18176/*
18177 * "spellsuggest()" function
18178 */
18179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018180f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018181{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018182#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018183 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018184 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018185 int maxcount;
18186 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018187 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018188 listitem_T *li;
18189 int need_capital = FALSE;
18190#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018192 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018193 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018194
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018195#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018196 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018197 {
18198 str = get_tv_string(&argvars[0]);
18199 if (argvars[1].v_type != VAR_UNKNOWN)
18200 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018201 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018202 if (maxcount <= 0)
18203 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018204 if (argvars[2].v_type != VAR_UNKNOWN)
18205 {
18206 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18207 if (typeerr)
18208 return;
18209 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018210 }
18211 else
18212 maxcount = 25;
18213
Bram Moolenaar4770d092006-01-12 23:22:24 +000018214 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018215
18216 for (i = 0; i < ga.ga_len; ++i)
18217 {
18218 str = ((char_u **)ga.ga_data)[i];
18219
18220 li = listitem_alloc();
18221 if (li == NULL)
18222 vim_free(str);
18223 else
18224 {
18225 li->li_tv.v_type = VAR_STRING;
18226 li->li_tv.v_lock = 0;
18227 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018228 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018229 }
18230 }
18231 ga_clear(&ga);
18232 }
18233#endif
18234}
18235
Bram Moolenaar0d660222005-01-07 21:51:51 +000018236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018238{
18239 char_u *str;
18240 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018241 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 regmatch_T regmatch;
18243 char_u patbuf[NUMBUFLEN];
18244 char_u *save_cpo;
18245 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018246 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018247 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018248 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018249
18250 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18251 save_cpo = p_cpo;
18252 p_cpo = (char_u *)"";
18253
18254 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018255 if (argvars[1].v_type != VAR_UNKNOWN)
18256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018257 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18258 if (pat == NULL)
18259 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018260 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018261 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018262 }
18263 if (pat == NULL || *pat == NUL)
18264 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018265
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018266 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018268 if (typeerr)
18269 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18272 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018275 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018277 if (*str == NUL)
18278 match = FALSE; /* empty item at the end */
18279 else
18280 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 if (match)
18282 end = regmatch.startp[0];
18283 else
18284 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018285 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18286 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018288 if (list_append_string(rettv->vval.v_list, str,
18289 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018291 }
18292 if (!match)
18293 break;
18294 /* Advance to just after the match. */
18295 if (regmatch.endp[0] > str)
18296 col = 0;
18297 else
18298 {
18299 /* Don't get stuck at the same match. */
18300#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018301 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018302#else
18303 col = 1;
18304#endif
18305 }
18306 str = regmatch.endp[0];
18307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308
Bram Moolenaar473de612013-06-08 18:19:48 +020018309 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311
Bram Moolenaar0d660222005-01-07 21:51:51 +000018312 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313}
18314
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018315#ifdef FEAT_FLOAT
18316/*
18317 * "sqrt()" function
18318 */
18319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018320f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018321{
18322 float_T f;
18323
18324 rettv->v_type = VAR_FLOAT;
18325 if (get_float_arg(argvars, &f) == OK)
18326 rettv->vval.v_float = sqrt(f);
18327 else
18328 rettv->vval.v_float = 0.0;
18329}
18330
18331/*
18332 * "str2float()" function
18333 */
18334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018335f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018336{
18337 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18338
18339 if (*p == '+')
18340 p = skipwhite(p + 1);
18341 (void)string2float(p, &rettv->vval.v_float);
18342 rettv->v_type = VAR_FLOAT;
18343}
18344#endif
18345
Bram Moolenaar2c932302006-03-18 21:42:09 +000018346/*
18347 * "str2nr()" function
18348 */
18349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018350f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018351{
18352 int base = 10;
18353 char_u *p;
18354 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018355 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018356
18357 if (argvars[1].v_type != VAR_UNKNOWN)
18358 {
18359 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018360 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018361 {
18362 EMSG(_(e_invarg));
18363 return;
18364 }
18365 }
18366
18367 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018368 if (*p == '+')
18369 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018370 switch (base)
18371 {
18372 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18373 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18374 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18375 default: what = 0;
18376 }
18377 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018378 rettv->vval.v_number = n;
18379}
18380
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381#ifdef HAVE_STRFTIME
18382/*
18383 * "strftime({format}[, {time}])" function
18384 */
18385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018386f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387{
18388 char_u result_buf[256];
18389 struct tm *curtime;
18390 time_t seconds;
18391 char_u *p;
18392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018393 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018395 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018396 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 seconds = time(NULL);
18398 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 curtime = localtime(&seconds);
18401 /* MSVC returns NULL for an invalid value of seconds. */
18402 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018403 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 else
18405 {
18406# ifdef FEAT_MBYTE
18407 vimconv_T conv;
18408 char_u *enc;
18409
18410 conv.vc_type = CONV_NONE;
18411 enc = enc_locale();
18412 convert_setup(&conv, p_enc, enc);
18413 if (conv.vc_type != CONV_NONE)
18414 p = string_convert(&conv, p, NULL);
18415# endif
18416 if (p != NULL)
18417 (void)strftime((char *)result_buf, sizeof(result_buf),
18418 (char *)p, curtime);
18419 else
18420 result_buf[0] = NUL;
18421
18422# ifdef FEAT_MBYTE
18423 if (conv.vc_type != CONV_NONE)
18424 vim_free(p);
18425 convert_setup(&conv, enc, p_enc);
18426 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018427 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 else
18429# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018430 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
18432# ifdef FEAT_MBYTE
18433 /* Release conversion descriptors */
18434 convert_setup(&conv, NULL, NULL);
18435 vim_free(enc);
18436# endif
18437 }
18438}
18439#endif
18440
18441/*
18442 * "stridx()" function
18443 */
18444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018445f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446{
18447 char_u buf[NUMBUFLEN];
18448 char_u *needle;
18449 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018454 needle = get_tv_string_chk(&argvars[1]);
18455 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018456 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018457 if (needle == NULL || haystack == NULL)
18458 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459
Bram Moolenaar33570922005-01-25 22:26:29 +000018460 if (argvars[2].v_type != VAR_UNKNOWN)
18461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018462 int error = FALSE;
18463
18464 start_idx = get_tv_number_chk(&argvars[2], &error);
18465 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018466 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018467 if (start_idx >= 0)
18468 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018469 }
18470
18471 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18472 if (pos != NULL)
18473 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474}
18475
18476/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018477 * "string()" function
18478 */
18479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018480f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018481{
18482 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018483 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018485 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018486 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018487 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018488 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018489 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490}
18491
18492/*
18493 * "strlen()" function
18494 */
18495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018496f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018498 rettv->vval.v_number = (varnumber_T)(STRLEN(
18499 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500}
18501
18502/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018503 * "strchars()" function
18504 */
18505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018506f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018507{
18508 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018509 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018510#ifdef FEAT_MBYTE
18511 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018512 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018513#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018514
18515 if (argvars[1].v_type != VAR_UNKNOWN)
18516 skipcc = get_tv_number_chk(&argvars[1], NULL);
18517 if (skipcc < 0 || skipcc > 1)
18518 EMSG(_(e_invarg));
18519 else
18520 {
18521#ifdef FEAT_MBYTE
18522 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18523 while (*s != NUL)
18524 {
18525 func_mb_ptr2char_adv(&s);
18526 ++len;
18527 }
18528 rettv->vval.v_number = len;
18529#else
18530 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18531#endif
18532 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018533}
18534
18535/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018536 * "strdisplaywidth()" function
18537 */
18538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018539f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018540{
18541 char_u *s = get_tv_string(&argvars[0]);
18542 int col = 0;
18543
18544 if (argvars[1].v_type != VAR_UNKNOWN)
18545 col = get_tv_number(&argvars[1]);
18546
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018547 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018548}
18549
18550/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018551 * "strwidth()" function
18552 */
18553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018554f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018555{
18556 char_u *s = get_tv_string(&argvars[0]);
18557
18558 rettv->vval.v_number = (varnumber_T)(
18559#ifdef FEAT_MBYTE
18560 mb_string2cells(s, -1)
18561#else
18562 STRLEN(s)
18563#endif
18564 );
18565}
18566
18567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 * "strpart()" function
18569 */
18570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018571f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572{
18573 char_u *p;
18574 int n;
18575 int len;
18576 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018577 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 slen = (int)STRLEN(p);
18581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018582 n = get_tv_number_chk(&argvars[1], &error);
18583 if (error)
18584 len = 0;
18585 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018586 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 else
18588 len = slen - n; /* default len: all bytes that are available. */
18589
18590 /*
18591 * Only return the overlap between the specified part and the actual
18592 * string.
18593 */
18594 if (n < 0)
18595 {
18596 len += n;
18597 n = 0;
18598 }
18599 else if (n > slen)
18600 n = slen;
18601 if (len < 0)
18602 len = 0;
18603 else if (n + len > slen)
18604 len = slen - n;
18605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018606 rettv->v_type = VAR_STRING;
18607 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608}
18609
18610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611 * "strridx()" function
18612 */
18613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018614f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018615{
18616 char_u buf[NUMBUFLEN];
18617 char_u *needle;
18618 char_u *haystack;
18619 char_u *rest;
18620 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018621 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018623 needle = get_tv_string_chk(&argvars[1]);
18624 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018625
18626 rettv->vval.v_number = -1;
18627 if (needle == NULL || haystack == NULL)
18628 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018629
18630 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018631 if (argvars[2].v_type != VAR_UNKNOWN)
18632 {
18633 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018634 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018635 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018636 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018637 }
18638 else
18639 end_idx = haystack_len;
18640
Bram Moolenaar0d660222005-01-07 21:51:51 +000018641 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018642 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018644 lastmatch = haystack + end_idx;
18645 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018646 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018647 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 for (rest = haystack; *rest != '\0'; ++rest)
18649 {
18650 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018651 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652 break;
18653 lastmatch = rest;
18654 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018655 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656
18657 if (lastmatch == NULL)
18658 rettv->vval.v_number = -1;
18659 else
18660 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18661}
18662
18663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 * "strtrans()" function
18665 */
18666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018667f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 rettv->v_type = VAR_STRING;
18670 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671}
18672
18673/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018674 * "submatch()" function
18675 */
18676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018677f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018678{
Bram Moolenaar41571762014-04-02 19:00:58 +020018679 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018680 int no;
18681 int retList = 0;
18682
18683 no = (int)get_tv_number_chk(&argvars[0], &error);
18684 if (error)
18685 return;
18686 error = FALSE;
18687 if (argvars[1].v_type != VAR_UNKNOWN)
18688 retList = get_tv_number_chk(&argvars[1], &error);
18689 if (error)
18690 return;
18691
18692 if (retList == 0)
18693 {
18694 rettv->v_type = VAR_STRING;
18695 rettv->vval.v_string = reg_submatch(no);
18696 }
18697 else
18698 {
18699 rettv->v_type = VAR_LIST;
18700 rettv->vval.v_list = reg_submatch_list(no);
18701 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018702}
18703
18704/*
18705 * "substitute()" function
18706 */
18707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018708f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018709{
18710 char_u patbuf[NUMBUFLEN];
18711 char_u subbuf[NUMBUFLEN];
18712 char_u flagsbuf[NUMBUFLEN];
18713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018714 char_u *str = get_tv_string_chk(&argvars[0]);
18715 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18716 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18717 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18718
Bram Moolenaar0d660222005-01-07 21:51:51 +000018719 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018720 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18721 rettv->vval.v_string = NULL;
18722 else
18723 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018724}
18725
18726/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018727 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018730f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731{
18732 int id = 0;
18733#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018734 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 long col;
18736 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018737 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018739 lnum = get_tv_lnum(argvars); /* -1 on type error */
18740 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18741 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018743 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018744 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018745 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746#endif
18747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018748 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749}
18750
18751/*
18752 * "synIDattr(id, what [, mode])" function
18753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018755f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756{
18757 char_u *p = NULL;
18758#ifdef FEAT_SYN_HL
18759 int id;
18760 char_u *what;
18761 char_u *mode;
18762 char_u modebuf[NUMBUFLEN];
18763 int modec;
18764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018765 id = get_tv_number(&argvars[0]);
18766 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018769 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018771 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 modec = 0; /* replace invalid with current */
18773 }
18774 else
18775 {
18776#ifdef FEAT_GUI
18777 if (gui.in_use)
18778 modec = 'g';
18779 else
18780#endif
18781 if (t_colors > 1)
18782 modec = 'c';
18783 else
18784 modec = 't';
18785 }
18786
18787
18788 switch (TOLOWER_ASC(what[0]))
18789 {
18790 case 'b':
18791 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18792 p = highlight_color(id, what, modec);
18793 else /* bold */
18794 p = highlight_has_attr(id, HL_BOLD, modec);
18795 break;
18796
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018797 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 p = highlight_color(id, what, modec);
18799 break;
18800
18801 case 'i':
18802 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18803 p = highlight_has_attr(id, HL_INVERSE, modec);
18804 else /* italic */
18805 p = highlight_has_attr(id, HL_ITALIC, modec);
18806 break;
18807
18808 case 'n': /* name */
18809 p = get_highlight_name(NULL, id - 1);
18810 break;
18811
18812 case 'r': /* reverse */
18813 p = highlight_has_attr(id, HL_INVERSE, modec);
18814 break;
18815
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018816 case 's':
18817 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18818 p = highlight_color(id, what, modec);
18819 else /* standout */
18820 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 break;
18822
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018823 case 'u':
18824 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18825 /* underline */
18826 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18827 else
18828 /* undercurl */
18829 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830 break;
18831 }
18832
18833 if (p != NULL)
18834 p = vim_strsave(p);
18835#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018836 rettv->v_type = VAR_STRING;
18837 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838}
18839
18840/*
18841 * "synIDtrans(id)" function
18842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018844f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845{
18846 int id;
18847
18848#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018849 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850
18851 if (id > 0)
18852 id = syn_get_final_id(id);
18853 else
18854#endif
18855 id = 0;
18856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018857 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858}
18859
18860/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018861 * "synconcealed(lnum, col)" function
18862 */
18863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018864f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018865{
18866#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18867 long lnum;
18868 long col;
18869 int syntax_flags = 0;
18870 int cchar;
18871 int matchid = 0;
18872 char_u str[NUMBUFLEN];
18873#endif
18874
18875 rettv->v_type = VAR_LIST;
18876 rettv->vval.v_list = NULL;
18877
18878#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18879 lnum = get_tv_lnum(argvars); /* -1 on type error */
18880 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18881
18882 vim_memset(str, NUL, sizeof(str));
18883
18884 if (rettv_list_alloc(rettv) != FAIL)
18885 {
18886 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18887 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18888 && curwin->w_p_cole > 0)
18889 {
18890 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18891 syntax_flags = get_syntax_info(&matchid);
18892
18893 /* get the conceal character */
18894 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18895 {
18896 cchar = syn_get_sub_char();
18897 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18898 cchar = lcs_conceal;
18899 if (cchar != NUL)
18900 {
18901# ifdef FEAT_MBYTE
18902 if (has_mbyte)
18903 (*mb_char2bytes)(cchar, str);
18904 else
18905# endif
18906 str[0] = cchar;
18907 }
18908 }
18909 }
18910
18911 list_append_number(rettv->vval.v_list,
18912 (syntax_flags & HL_CONCEAL) != 0);
18913 /* -1 to auto-determine strlen */
18914 list_append_string(rettv->vval.v_list, str, -1);
18915 list_append_number(rettv->vval.v_list, matchid);
18916 }
18917#endif
18918}
18919
18920/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018921 * "synstack(lnum, col)" function
18922 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018924f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018925{
18926#ifdef FEAT_SYN_HL
18927 long lnum;
18928 long col;
18929 int i;
18930 int id;
18931#endif
18932
18933 rettv->v_type = VAR_LIST;
18934 rettv->vval.v_list = NULL;
18935
18936#ifdef FEAT_SYN_HL
18937 lnum = get_tv_lnum(argvars); /* -1 on type error */
18938 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18939
18940 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018941 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018942 && rettv_list_alloc(rettv) != FAIL)
18943 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018944 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018945 for (i = 0; ; ++i)
18946 {
18947 id = syn_get_stack_item(i);
18948 if (id < 0)
18949 break;
18950 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18951 break;
18952 }
18953 }
18954#endif
18955}
18956
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018958get_cmd_output_as_rettv(
18959 typval_T *argvars,
18960 typval_T *rettv,
18961 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018963 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018965 char_u *infile = NULL;
18966 char_u buf[NUMBUFLEN];
18967 int err = FALSE;
18968 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018969 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018970 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018972 rettv->v_type = VAR_STRING;
18973 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018974 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018975 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018976
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018977 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018978 {
18979 /*
18980 * Write the string to a temp file, to be used for input of the shell
18981 * command.
18982 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018983 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018984 {
18985 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018986 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018987 }
18988
18989 fd = mch_fopen((char *)infile, WRITEBIN);
18990 if (fd == NULL)
18991 {
18992 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018993 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018994 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018995 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018996 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018997 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18998 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018999 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019000 else
19001 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019002 size_t len;
19003
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019004 p = get_tv_string_buf_chk(&argvars[1], buf);
19005 if (p == NULL)
19006 {
19007 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019008 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019009 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019010 len = STRLEN(p);
19011 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019012 err = TRUE;
19013 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019014 if (fclose(fd) != 0)
19015 err = TRUE;
19016 if (err)
19017 {
19018 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019019 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019020 }
19021 }
19022
Bram Moolenaar52a72462014-08-29 15:53:52 +020019023 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19024 * echoes typeahead, that messes up the display. */
19025 if (!msg_silent)
19026 flags += SHELL_COOKED;
19027
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019028 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019030 int len;
19031 listitem_T *li;
19032 char_u *s = NULL;
19033 char_u *start;
19034 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019035 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036
Bram Moolenaar52a72462014-08-29 15:53:52 +020019037 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019038 if (res == NULL)
19039 goto errret;
19040
19041 list = list_alloc();
19042 if (list == NULL)
19043 goto errret;
19044
19045 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019047 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019048 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019049 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019050 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019051
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019052 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019053 if (s == NULL)
19054 goto errret;
19055
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019056 for (p = s; start < end; ++p, ++start)
19057 *p = *start == NUL ? NL : *start;
19058 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059
19060 li = listitem_alloc();
19061 if (li == NULL)
19062 {
19063 vim_free(s);
19064 goto errret;
19065 }
19066 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019067 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019068 li->li_tv.vval.v_string = s;
19069 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019071
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019072 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019073 rettv->v_type = VAR_LIST;
19074 rettv->vval.v_list = list;
19075 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019077 else
19078 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019079 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019080#ifdef USE_CR
19081 /* translate <CR> into <NL> */
19082 if (res != NULL)
19083 {
19084 char_u *s;
19085
19086 for (s = res; *s; ++s)
19087 {
19088 if (*s == CAR)
19089 *s = NL;
19090 }
19091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092#else
19093# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019094 /* translate <CR><NL> into <NL> */
19095 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019097 char_u *s, *d;
19098
19099 d = res;
19100 for (s = res; *s; ++s)
19101 {
19102 if (s[0] == CAR && s[1] == NL)
19103 ++s;
19104 *d++ = *s;
19105 }
19106 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108# endif
19109#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019110 rettv->vval.v_string = res;
19111 res = NULL;
19112 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019113
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019114errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019115 if (infile != NULL)
19116 {
19117 mch_remove(infile);
19118 vim_free(infile);
19119 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019120 if (res != NULL)
19121 vim_free(res);
19122 if (list != NULL)
19123 list_free(list, TRUE);
19124}
19125
19126/*
19127 * "system()" function
19128 */
19129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019130f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019131{
19132 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19133}
19134
19135/*
19136 * "systemlist()" function
19137 */
19138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019139f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019140{
19141 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142}
19143
19144/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019145 * "tabpagebuflist()" function
19146 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019148f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019149{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019150#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019151 tabpage_T *tp;
19152 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019153
19154 if (argvars[0].v_type == VAR_UNKNOWN)
19155 wp = firstwin;
19156 else
19157 {
19158 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19159 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019160 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019161 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019162 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019163 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019164 for (; wp != NULL; wp = wp->w_next)
19165 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019166 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019167 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019168 }
19169#endif
19170}
19171
19172
19173/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019174 * "tabpagenr()" function
19175 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019177f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019178{
19179 int nr = 1;
19180#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019181 char_u *arg;
19182
19183 if (argvars[0].v_type != VAR_UNKNOWN)
19184 {
19185 arg = get_tv_string_chk(&argvars[0]);
19186 nr = 0;
19187 if (arg != NULL)
19188 {
19189 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019190 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019191 else
19192 EMSG2(_(e_invexpr2), arg);
19193 }
19194 }
19195 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019196 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019197#endif
19198 rettv->vval.v_number = nr;
19199}
19200
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019201
19202#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019203static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019204
19205/*
19206 * Common code for tabpagewinnr() and winnr().
19207 */
19208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019209get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019210{
19211 win_T *twin;
19212 int nr = 1;
19213 win_T *wp;
19214 char_u *arg;
19215
19216 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19217 if (argvar->v_type != VAR_UNKNOWN)
19218 {
19219 arg = get_tv_string_chk(argvar);
19220 if (arg == NULL)
19221 nr = 0; /* type error; errmsg already given */
19222 else if (STRCMP(arg, "$") == 0)
19223 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19224 else if (STRCMP(arg, "#") == 0)
19225 {
19226 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19227 if (twin == NULL)
19228 nr = 0;
19229 }
19230 else
19231 {
19232 EMSG2(_(e_invexpr2), arg);
19233 nr = 0;
19234 }
19235 }
19236
19237 if (nr > 0)
19238 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19239 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019240 {
19241 if (wp == NULL)
19242 {
19243 /* didn't find it in this tabpage */
19244 nr = 0;
19245 break;
19246 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019247 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019248 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019249 return nr;
19250}
19251#endif
19252
19253/*
19254 * "tabpagewinnr()" function
19255 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019257f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019258{
19259 int nr = 1;
19260#ifdef FEAT_WINDOWS
19261 tabpage_T *tp;
19262
19263 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19264 if (tp == NULL)
19265 nr = 0;
19266 else
19267 nr = get_winnr(tp, &argvars[1]);
19268#endif
19269 rettv->vval.v_number = nr;
19270}
19271
19272
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019273/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019274 * "tagfiles()" function
19275 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019277f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019278{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019279 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019280 tagname_T tn;
19281 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019282
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019283 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019284 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019285 fname = alloc(MAXPATHL);
19286 if (fname == NULL)
19287 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019288
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019289 for (first = TRUE; ; first = FALSE)
19290 if (get_tagfname(&tn, first, fname) == FAIL
19291 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019292 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019293 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019294 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019295}
19296
19297/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019298 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019299 */
19300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019301f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019302{
19303 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019304
19305 tag_pattern = get_tv_string(&argvars[0]);
19306
19307 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019308 if (*tag_pattern == NUL)
19309 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019311 if (rettv_list_alloc(rettv) == OK)
19312 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019313}
19314
19315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 * "tempname()" function
19317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019319f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320{
19321 static int x = 'A';
19322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019323 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019324 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325
19326 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19327 * names. Skip 'I' and 'O', they are used for shell redirection. */
19328 do
19329 {
19330 if (x == 'Z')
19331 x = '0';
19332 else if (x == '9')
19333 x = 'A';
19334 else
19335 {
19336#ifdef EBCDIC
19337 if (x == 'I')
19338 x = 'J';
19339 else if (x == 'R')
19340 x = 'S';
19341 else
19342#endif
19343 ++x;
19344 }
19345 } while (x == 'I' || x == 'O');
19346}
19347
19348/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019349 * "test(list)" function: Just checking the walls...
19350 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019352f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019353{
19354 /* Used for unit testing. Change the code below to your liking. */
19355#if 0
19356 listitem_T *li;
19357 list_T *l;
19358 char_u *bad, *good;
19359
19360 if (argvars[0].v_type != VAR_LIST)
19361 return;
19362 l = argvars[0].vval.v_list;
19363 if (l == NULL)
19364 return;
19365 li = l->lv_first;
19366 if (li == NULL)
19367 return;
19368 bad = get_tv_string(&li->li_tv);
19369 li = li->li_next;
19370 if (li == NULL)
19371 return;
19372 good = get_tv_string(&li->li_tv);
19373 rettv->vval.v_number = test_edit_score(bad, good);
19374#endif
19375}
19376
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019377#ifdef FEAT_FLOAT
19378/*
19379 * "tan()" function
19380 */
19381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019382f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019383{
19384 float_T f;
19385
19386 rettv->v_type = VAR_FLOAT;
19387 if (get_float_arg(argvars, &f) == OK)
19388 rettv->vval.v_float = tan(f);
19389 else
19390 rettv->vval.v_float = 0.0;
19391}
19392
19393/*
19394 * "tanh()" function
19395 */
19396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019397f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019398{
19399 float_T f;
19400
19401 rettv->v_type = VAR_FLOAT;
19402 if (get_float_arg(argvars, &f) == OK)
19403 rettv->vval.v_float = tanh(f);
19404 else
19405 rettv->vval.v_float = 0.0;
19406}
19407#endif
19408
Bram Moolenaard52d9742005-08-21 22:20:28 +000019409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 * "tolower(string)" function
19411 */
19412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019413f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414{
19415 char_u *p;
19416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019417 p = vim_strsave(get_tv_string(&argvars[0]));
19418 rettv->v_type = VAR_STRING;
19419 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
19421 if (p != NULL)
19422 while (*p != NUL)
19423 {
19424#ifdef FEAT_MBYTE
19425 int l;
19426
19427 if (enc_utf8)
19428 {
19429 int c, lc;
19430
19431 c = utf_ptr2char(p);
19432 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019433 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 /* TODO: reallocate string when byte count changes. */
19435 if (utf_char2len(lc) == l)
19436 utf_char2bytes(lc, p);
19437 p += l;
19438 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019439 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 p += l; /* skip multi-byte character */
19441 else
19442#endif
19443 {
19444 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19445 ++p;
19446 }
19447 }
19448}
19449
19450/*
19451 * "toupper(string)" function
19452 */
19453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019454f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019456 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019457 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458}
19459
19460/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019461 * "tr(string, fromstr, tostr)" function
19462 */
19463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019464f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019465{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019466 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019467 char_u *fromstr;
19468 char_u *tostr;
19469 char_u *p;
19470#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019471 int inlen;
19472 int fromlen;
19473 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019474 int idx;
19475 char_u *cpstr;
19476 int cplen;
19477 int first = TRUE;
19478#endif
19479 char_u buf[NUMBUFLEN];
19480 char_u buf2[NUMBUFLEN];
19481 garray_T ga;
19482
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019483 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019484 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19485 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019486
19487 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019488 rettv->v_type = VAR_STRING;
19489 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019490 if (fromstr == NULL || tostr == NULL)
19491 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019492 ga_init2(&ga, (int)sizeof(char), 80);
19493
19494#ifdef FEAT_MBYTE
19495 if (!has_mbyte)
19496#endif
19497 /* not multi-byte: fromstr and tostr must be the same length */
19498 if (STRLEN(fromstr) != STRLEN(tostr))
19499 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019500#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019501error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019502#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019503 EMSG2(_(e_invarg2), fromstr);
19504 ga_clear(&ga);
19505 return;
19506 }
19507
19508 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019509 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019510 {
19511#ifdef FEAT_MBYTE
19512 if (has_mbyte)
19513 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019514 inlen = (*mb_ptr2len)(in_str);
19515 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019516 cplen = inlen;
19517 idx = 0;
19518 for (p = fromstr; *p != NUL; p += fromlen)
19519 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019520 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019521 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019522 {
19523 for (p = tostr; *p != NUL; p += tolen)
19524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019525 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019526 if (idx-- == 0)
19527 {
19528 cplen = tolen;
19529 cpstr = p;
19530 break;
19531 }
19532 }
19533 if (*p == NUL) /* tostr is shorter than fromstr */
19534 goto error;
19535 break;
19536 }
19537 ++idx;
19538 }
19539
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019540 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019541 {
19542 /* Check that fromstr and tostr have the same number of
19543 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019544 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019545 first = FALSE;
19546 for (p = tostr; *p != NUL; p += tolen)
19547 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019548 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019549 --idx;
19550 }
19551 if (idx != 0)
19552 goto error;
19553 }
19554
Bram Moolenaarcde88542015-08-11 19:14:00 +020019555 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019556 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019557 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019558
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019559 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019560 }
19561 else
19562#endif
19563 {
19564 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019565 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019566 if (p != NULL)
19567 ga_append(&ga, tostr[p - fromstr]);
19568 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019569 ga_append(&ga, *in_str);
19570 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019571 }
19572 }
19573
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019574 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019575 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019576 ga_append(&ga, NUL);
19577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019578 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019579}
19580
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019581#ifdef FEAT_FLOAT
19582/*
19583 * "trunc({float})" function
19584 */
19585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019586f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019587{
19588 float_T f;
19589
19590 rettv->v_type = VAR_FLOAT;
19591 if (get_float_arg(argvars, &f) == OK)
19592 /* trunc() is not in C90, use floor() or ceil() instead. */
19593 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19594 else
19595 rettv->vval.v_float = 0.0;
19596}
19597#endif
19598
Bram Moolenaar8299df92004-07-10 09:47:34 +000019599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 * "type(expr)" function
19601 */
19602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019603f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019605 int n;
19606
19607 switch (argvars[0].v_type)
19608 {
19609 case VAR_NUMBER: n = 0; break;
19610 case VAR_STRING: n = 1; break;
19611 case VAR_FUNC: n = 2; break;
19612 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019613 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019614#ifdef FEAT_FLOAT
19615 case VAR_FLOAT: n = 5; break;
19616#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019617 case VAR_SPECIAL:
19618 if (argvars[0].vval.v_number == VVAL_FALSE
19619 || argvars[0].vval.v_number == VVAL_TRUE)
19620 n = 6;
19621 else
19622 n = 7;
19623 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019624 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19625 }
19626 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627}
19628
19629/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019630 * "undofile(name)" function
19631 */
19632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019633f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019634{
19635 rettv->v_type = VAR_STRING;
19636#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019637 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019638 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019639
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019640 if (*fname == NUL)
19641 {
19642 /* If there is no file name there will be no undo file. */
19643 rettv->vval.v_string = NULL;
19644 }
19645 else
19646 {
19647 char_u *ffname = FullName_save(fname, FALSE);
19648
19649 if (ffname != NULL)
19650 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19651 vim_free(ffname);
19652 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019653 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019654#else
19655 rettv->vval.v_string = NULL;
19656#endif
19657}
19658
19659/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019660 * "undotree()" function
19661 */
19662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019663f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019664{
19665 if (rettv_dict_alloc(rettv) == OK)
19666 {
19667 dict_T *dict = rettv->vval.v_dict;
19668 list_T *list;
19669
Bram Moolenaar730cde92010-06-27 05:18:54 +020019670 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019671 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019672 dict_add_nr_str(dict, "save_last",
19673 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019674 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19675 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019676 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019677
19678 list = list_alloc();
19679 if (list != NULL)
19680 {
19681 u_eval_tree(curbuf->b_u_oldhead, list);
19682 dict_add_list(dict, "entries", list);
19683 }
19684 }
19685}
19686
19687/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019688 * "values(dict)" function
19689 */
19690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019691f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019692{
19693 dict_list(argvars, rettv, 1);
19694}
19695
19696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 * "virtcol(string)" function
19698 */
19699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019700f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701{
19702 colnr_T vcol = 0;
19703 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019704 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019706 fp = var2fpos(&argvars[0], FALSE, &fnum);
19707 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19708 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 {
19710 getvvcol(curwin, fp, NULL, NULL, &vcol);
19711 ++vcol;
19712 }
19713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715}
19716
19717/*
19718 * "visualmode()" function
19719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019721f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 char_u str[2];
19724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 str[0] = curbuf->b_visual_mode_eval;
19727 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729
19730 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019731 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733}
19734
19735/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019736 * "wildmenumode()" function
19737 */
19738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019739f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019740{
19741#ifdef FEAT_WILDMENU
19742 if (wild_menu_showing)
19743 rettv->vval.v_number = 1;
19744#endif
19745}
19746
19747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 * "winbufnr(nr)" function
19749 */
19750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019751f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752{
19753 win_T *wp;
19754
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019755 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019759 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760}
19761
19762/*
19763 * "wincol()" function
19764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019766f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767{
19768 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770}
19771
19772/*
19773 * "winheight(nr)" function
19774 */
19775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019776f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777{
19778 win_T *wp;
19779
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019780 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785}
19786
19787/*
19788 * "winline()" function
19789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019791f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792{
19793 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019794 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795}
19796
19797/*
19798 * "winnr()" function
19799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019801f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802{
19803 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019804
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019806 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019808 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809}
19810
19811/*
19812 * "winrestcmd()" function
19813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019815f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816{
19817#ifdef FEAT_WINDOWS
19818 win_T *wp;
19819 int winnr = 1;
19820 garray_T ga;
19821 char_u buf[50];
19822
19823 ga_init2(&ga, (int)sizeof(char), 70);
19824 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19825 {
19826 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19827 ga_concat(&ga, buf);
19828# ifdef FEAT_VERTSPLIT
19829 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19830 ga_concat(&ga, buf);
19831# endif
19832 ++winnr;
19833 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019834 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841}
19842
19843/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019844 * "winrestview()" function
19845 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019847f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019848{
19849 dict_T *dict;
19850
19851 if (argvars[0].v_type != VAR_DICT
19852 || (dict = argvars[0].vval.v_dict) == NULL)
19853 EMSG(_(e_invarg));
19854 else
19855 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019856 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19857 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19858 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19859 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019860#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019861 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19862 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019863#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019864 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19865 {
19866 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19867 curwin->w_set_curswant = FALSE;
19868 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019869
Bram Moolenaar82c25852014-05-28 16:47:16 +020019870 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19871 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019872#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019873 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19874 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019875#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019876 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19877 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19878 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19879 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019880
19881 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019882 win_new_height(curwin, curwin->w_height);
19883# ifdef FEAT_VERTSPLIT
19884 win_new_width(curwin, W_WIDTH(curwin));
19885# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019886 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019887
Bram Moolenaarb851a962014-10-31 15:45:52 +010019888 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019889 curwin->w_topline = 1;
19890 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19891 curwin->w_topline = curbuf->b_ml.ml_line_count;
19892#ifdef FEAT_DIFF
19893 check_topfill(curwin, TRUE);
19894#endif
19895 }
19896}
19897
19898/*
19899 * "winsaveview()" function
19900 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019902f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019903{
19904 dict_T *dict;
19905
Bram Moolenaara800b422010-06-27 01:15:55 +020019906 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019907 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019908 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019909
19910 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19911 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19912#ifdef FEAT_VIRTUALEDIT
19913 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19914#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019915 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019916 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19917
19918 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19919#ifdef FEAT_DIFF
19920 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19921#endif
19922 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19923 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19924}
19925
19926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927 * "winwidth(nr)" function
19928 */
19929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019930f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931{
19932 win_T *wp;
19933
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019934 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019936 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 else
19938#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942#endif
19943}
19944
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019946 * "wordcount()" function
19947 */
19948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019949f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019950{
19951 if (rettv_dict_alloc(rettv) == FAIL)
19952 return;
19953 cursor_pos_info(rettv->vval.v_dict);
19954}
19955
19956/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019957 * Write list of strings to file
19958 */
19959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019960write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019961{
19962 listitem_T *li;
19963 int c;
19964 int ret = OK;
19965 char_u *s;
19966
19967 for (li = list->lv_first; li != NULL; li = li->li_next)
19968 {
19969 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19970 {
19971 if (*s == '\n')
19972 c = putc(NUL, fd);
19973 else
19974 c = putc(*s, fd);
19975 if (c == EOF)
19976 {
19977 ret = FAIL;
19978 break;
19979 }
19980 }
19981 if (!binary || li->li_next != NULL)
19982 if (putc('\n', fd) == EOF)
19983 {
19984 ret = FAIL;
19985 break;
19986 }
19987 if (ret == FAIL)
19988 {
19989 EMSG(_(e_write));
19990 break;
19991 }
19992 }
19993 return ret;
19994}
19995
19996/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019997 * "writefile()" function
19998 */
19999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020000f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020001{
20002 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020003 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020004 char_u *fname;
20005 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020006 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020007
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020008 if (check_restricted() || check_secure())
20009 return;
20010
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020011 if (argvars[0].v_type != VAR_LIST)
20012 {
20013 EMSG2(_(e_listarg), "writefile()");
20014 return;
20015 }
20016 if (argvars[0].vval.v_list == NULL)
20017 return;
20018
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020019 if (argvars[2].v_type != VAR_UNKNOWN)
20020 {
20021 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20022 binary = TRUE;
20023 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20024 append = TRUE;
20025 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020026
20027 /* Always open the file in binary mode, library functions have a mind of
20028 * their own about CR-LF conversion. */
20029 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020030 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20031 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020032 {
20033 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20034 ret = -1;
20035 }
20036 else
20037 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020038 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20039 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020040 fclose(fd);
20041 }
20042
20043 rettv->vval.v_number = ret;
20044}
20045
20046/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020047 * "xor(expr, expr)" function
20048 */
20049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020050f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020051{
20052 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20053 ^ get_tv_number_chk(&argvars[1], NULL);
20054}
20055
20056
20057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020059 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 */
20061 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020062var2fpos(
20063 typval_T *varp,
20064 int dollar_lnum, /* TRUE when $ is last line */
20065 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020067 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020069 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070
Bram Moolenaara5525202006-03-02 22:52:09 +000020071 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020072 if (varp->v_type == VAR_LIST)
20073 {
20074 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020075 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020076 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020077 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020078
20079 l = varp->vval.v_list;
20080 if (l == NULL)
20081 return NULL;
20082
20083 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020084 pos.lnum = list_find_nr(l, 0L, &error);
20085 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020086 return NULL; /* invalid line number */
20087
20088 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020089 pos.col = list_find_nr(l, 1L, &error);
20090 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020091 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020092 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020093
20094 /* We accept "$" for the column number: last column. */
20095 li = list_find(l, 1L);
20096 if (li != NULL && li->li_tv.v_type == VAR_STRING
20097 && li->li_tv.vval.v_string != NULL
20098 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20099 pos.col = len + 1;
20100
Bram Moolenaara5525202006-03-02 22:52:09 +000020101 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020102 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020103 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020104 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020105
Bram Moolenaara5525202006-03-02 22:52:09 +000020106#ifdef FEAT_VIRTUALEDIT
20107 /* Get the virtual offset. Defaults to zero. */
20108 pos.coladd = list_find_nr(l, 2L, &error);
20109 if (error)
20110 pos.coladd = 0;
20111#endif
20112
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020113 return &pos;
20114 }
20115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020116 name = get_tv_string_chk(varp);
20117 if (name == NULL)
20118 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020119 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020121 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20122 {
20123 if (VIsual_active)
20124 return &VIsual;
20125 return &curwin->w_cursor;
20126 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020127 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020129 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20131 return NULL;
20132 return pp;
20133 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020134
20135#ifdef FEAT_VIRTUALEDIT
20136 pos.coladd = 0;
20137#endif
20138
Bram Moolenaar477933c2007-07-17 14:32:23 +000020139 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020140 {
20141 pos.col = 0;
20142 if (name[1] == '0') /* "w0": first visible line */
20143 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020144 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020145 pos.lnum = curwin->w_topline;
20146 return &pos;
20147 }
20148 else if (name[1] == '$') /* "w$": last visible line */
20149 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020150 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020151 pos.lnum = curwin->w_botline - 1;
20152 return &pos;
20153 }
20154 }
20155 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020157 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 {
20159 pos.lnum = curbuf->b_ml.ml_line_count;
20160 pos.col = 0;
20161 }
20162 else
20163 {
20164 pos.lnum = curwin->w_cursor.lnum;
20165 pos.col = (colnr_T)STRLEN(ml_get_curline());
20166 }
20167 return &pos;
20168 }
20169 return NULL;
20170}
20171
20172/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020173 * Convert list in "arg" into a position and optional file number.
20174 * When "fnump" is NULL there is no file number, only 3 items.
20175 * Note that the column is passed on as-is, the caller may want to decrement
20176 * it to use 1 for the first column.
20177 * Return FAIL when conversion is not possible, doesn't check the position for
20178 * validity.
20179 */
20180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020181list2fpos(
20182 typval_T *arg,
20183 pos_T *posp,
20184 int *fnump,
20185 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020186{
20187 list_T *l = arg->vval.v_list;
20188 long i = 0;
20189 long n;
20190
Bram Moolenaar493c1782014-05-28 14:34:46 +020020191 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20192 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020193 if (arg->v_type != VAR_LIST
20194 || l == NULL
20195 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020196 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020197 return FAIL;
20198
20199 if (fnump != NULL)
20200 {
20201 n = list_find_nr(l, i++, NULL); /* fnum */
20202 if (n < 0)
20203 return FAIL;
20204 if (n == 0)
20205 n = curbuf->b_fnum; /* current buffer */
20206 *fnump = n;
20207 }
20208
20209 n = list_find_nr(l, i++, NULL); /* lnum */
20210 if (n < 0)
20211 return FAIL;
20212 posp->lnum = n;
20213
20214 n = list_find_nr(l, i++, NULL); /* col */
20215 if (n < 0)
20216 return FAIL;
20217 posp->col = n;
20218
20219#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020220 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020221 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020222 posp->coladd = 0;
20223 else
20224 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020225#endif
20226
Bram Moolenaar493c1782014-05-28 14:34:46 +020020227 if (curswantp != NULL)
20228 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20229
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020230 return OK;
20231}
20232
20233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 * Get the length of an environment variable name.
20235 * Advance "arg" to the first character after the name.
20236 * Return 0 for error.
20237 */
20238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020239get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240{
20241 char_u *p;
20242 int len;
20243
20244 for (p = *arg; vim_isIDc(*p); ++p)
20245 ;
20246 if (p == *arg) /* no name found */
20247 return 0;
20248
20249 len = (int)(p - *arg);
20250 *arg = p;
20251 return len;
20252}
20253
20254/*
20255 * Get the length of the name of a function or internal variable.
20256 * "arg" is advanced to the first non-white character after the name.
20257 * Return 0 if something is wrong.
20258 */
20259 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020260get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261{
20262 char_u *p;
20263 int len;
20264
20265 /* Find the end of the name. */
20266 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020267 {
20268 if (*p == ':')
20269 {
20270 /* "s:" is start of "s:var", but "n:" is not and can be used in
20271 * slice "[n:]". Also "xx:" is not a namespace. */
20272 len = (int)(p - *arg);
20273 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20274 || len > 1)
20275 break;
20276 }
20277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 if (p == *arg) /* no name found */
20279 return 0;
20280
20281 len = (int)(p - *arg);
20282 *arg = skipwhite(p);
20283
20284 return len;
20285}
20286
20287/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020288 * Get the length of the name of a variable or function.
20289 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020291 * Return -1 if curly braces expansion failed.
20292 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 * If the name contains 'magic' {}'s, expand them and return the
20294 * expanded name in an allocated string via 'alias' - caller must free.
20295 */
20296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020297get_name_len(
20298 char_u **arg,
20299 char_u **alias,
20300 int evaluate,
20301 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302{
20303 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 char_u *p;
20305 char_u *expr_start;
20306 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307
20308 *alias = NULL; /* default to no alias */
20309
20310 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20311 && (*arg)[2] == (int)KE_SNR)
20312 {
20313 /* hard coded <SNR>, already translated */
20314 *arg += 3;
20315 return get_id_len(arg) + 3;
20316 }
20317 len = eval_fname_script(*arg);
20318 if (len > 0)
20319 {
20320 /* literal "<SID>", "s:" or "<SNR>" */
20321 *arg += len;
20322 }
20323
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020325 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020327 p = find_name_end(*arg, &expr_start, &expr_end,
20328 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 if (expr_start != NULL)
20330 {
20331 char_u *temp_string;
20332
20333 if (!evaluate)
20334 {
20335 len += (int)(p - *arg);
20336 *arg = skipwhite(p);
20337 return len;
20338 }
20339
20340 /*
20341 * Include any <SID> etc in the expanded string:
20342 * Thus the -len here.
20343 */
20344 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20345 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020346 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 *alias = temp_string;
20348 *arg = skipwhite(p);
20349 return (int)STRLEN(temp_string);
20350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351
20352 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020353 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 EMSG2(_(e_invexpr2), *arg);
20355
20356 return len;
20357}
20358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359/*
20360 * Find the end of a variable or function name, taking care of magic braces.
20361 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20362 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020363 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 * Return a pointer to just after the name. Equal to "arg" if there is no
20365 * valid name.
20366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020368find_name_end(
20369 char_u *arg,
20370 char_u **expr_start,
20371 char_u **expr_end,
20372 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020374 int mb_nest = 0;
20375 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020377 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 *expr_start = NULL;
20382 *expr_end = NULL;
20383 }
20384
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020385 /* Quick check for valid starting character. */
20386 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20387 return arg;
20388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020389 for (p = arg; *p != NUL
20390 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020391 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020392 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020393 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020394 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020396 if (*p == '\'')
20397 {
20398 /* skip over 'string' to avoid counting [ and ] inside it. */
20399 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20400 ;
20401 if (*p == NUL)
20402 break;
20403 }
20404 else if (*p == '"')
20405 {
20406 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20407 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20408 if (*p == '\\' && p[1] != NUL)
20409 ++p;
20410 if (*p == NUL)
20411 break;
20412 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020413 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20414 {
20415 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020416 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020417 len = (int)(p - arg);
20418 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020419 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020420 break;
20421 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020425 if (*p == '[')
20426 ++br_nest;
20427 else if (*p == ']')
20428 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020433 if (*p == '{')
20434 {
20435 mb_nest++;
20436 if (expr_start != NULL && *expr_start == NULL)
20437 *expr_start = p;
20438 }
20439 else if (*p == '}')
20440 {
20441 mb_nest--;
20442 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20443 *expr_end = p;
20444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 }
20447
20448 return p;
20449}
20450
20451/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020452 * Expands out the 'magic' {}'s in a variable/function name.
20453 * Note that this can call itself recursively, to deal with
20454 * constructs like foo{bar}{baz}{bam}
20455 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20456 * "in_start" ^
20457 * "expr_start" ^
20458 * "expr_end" ^
20459 * "in_end" ^
20460 *
20461 * Returns a new allocated string, which the caller must free.
20462 * Returns NULL for failure.
20463 */
20464 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020465make_expanded_name(
20466 char_u *in_start,
20467 char_u *expr_start,
20468 char_u *expr_end,
20469 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020470{
20471 char_u c1;
20472 char_u *retval = NULL;
20473 char_u *temp_result;
20474 char_u *nextcmd = NULL;
20475
20476 if (expr_end == NULL || in_end == NULL)
20477 return NULL;
20478 *expr_start = NUL;
20479 *expr_end = NUL;
20480 c1 = *in_end;
20481 *in_end = NUL;
20482
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020483 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020484 if (temp_result != NULL && nextcmd == NULL)
20485 {
20486 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20487 + (in_end - expr_end) + 1));
20488 if (retval != NULL)
20489 {
20490 STRCPY(retval, in_start);
20491 STRCAT(retval, temp_result);
20492 STRCAT(retval, expr_end + 1);
20493 }
20494 }
20495 vim_free(temp_result);
20496
20497 *in_end = c1; /* put char back for error messages */
20498 *expr_start = '{';
20499 *expr_end = '}';
20500
20501 if (retval != NULL)
20502 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020503 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020504 if (expr_start != NULL)
20505 {
20506 /* Further expansion! */
20507 temp_result = make_expanded_name(retval, expr_start,
20508 expr_end, temp_result);
20509 vim_free(retval);
20510 retval = temp_result;
20511 }
20512 }
20513
20514 return retval;
20515}
20516
20517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020519 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 */
20521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020522eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020524 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20525}
20526
20527/*
20528 * Return TRUE if character "c" can be used as the first character in a
20529 * variable or function name (excluding '{' and '}').
20530 */
20531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020532eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020533{
20534 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535}
20536
20537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 * Set number v: variable to "val".
20539 */
20540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020541set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020543 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544}
20545
20546/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020547 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 */
20549 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020550get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020552 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553}
20554
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020555/*
20556 * Get string v: variable value. Uses a static buffer, can only be used once.
20557 */
20558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020559get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020560{
20561 return get_tv_string(&vimvars[idx].vv_tv);
20562}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020563
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020565 * Get List v: variable value. Caller must take care of reference count when
20566 * needed.
20567 */
20568 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020569get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020570{
20571 return vimvars[idx].vv_list;
20572}
20573
20574/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020575 * Set v:char to character "c".
20576 */
20577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020578set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020579{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020580 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020581
20582#ifdef FEAT_MBYTE
20583 if (has_mbyte)
20584 buf[(*mb_char2bytes)(c, buf)] = NUL;
20585 else
20586#endif
20587 {
20588 buf[0] = c;
20589 buf[1] = NUL;
20590 }
20591 set_vim_var_string(VV_CHAR, buf, -1);
20592}
20593
20594/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020595 * Set v:count to "count" and v:count1 to "count1".
20596 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 */
20598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020599set_vcount(
20600 long count,
20601 long count1,
20602 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020604 if (set_prevcount)
20605 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020606 vimvars[VV_COUNT].vv_nr = count;
20607 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608}
20609
20610/*
20611 * Set string v: variable to a copy of "val".
20612 */
20613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020614set_vim_var_string(
20615 int idx,
20616 char_u *val,
20617 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020619 /* Need to do this (at least) once, since we can't initialize a union.
20620 * Will always be invoked when "v:progname" is set. */
20621 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20622
Bram Moolenaare9a41262005-01-15 22:18:47 +000020623 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020625 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020627 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020629 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630}
20631
20632/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020633 * Set List v: variable to "val".
20634 */
20635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020636set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020637{
20638 list_unref(vimvars[idx].vv_list);
20639 vimvars[idx].vv_list = val;
20640 if (val != NULL)
20641 ++val->lv_refcount;
20642}
20643
20644/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020645 * Set Dictionary v: variable to "val".
20646 */
20647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020648set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020649{
20650 int todo;
20651 hashitem_T *hi;
20652
20653 dict_unref(vimvars[idx].vv_dict);
20654 vimvars[idx].vv_dict = val;
20655 if (val != NULL)
20656 {
20657 ++val->dv_refcount;
20658
20659 /* Set readonly */
20660 todo = (int)val->dv_hashtab.ht_used;
20661 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20662 {
20663 if (HASHITEM_EMPTY(hi))
20664 continue;
20665 --todo;
20666 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20667 }
20668 }
20669}
20670
20671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 * Set v:register if needed.
20673 */
20674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020675set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676{
20677 char_u regname;
20678
20679 if (c == 0 || c == ' ')
20680 regname = '"';
20681 else
20682 regname = c;
20683 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020684 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 set_vim_var_string(VV_REG, &regname, 1);
20686}
20687
20688/*
20689 * Get or set v:exception. If "oldval" == NULL, return the current value.
20690 * Otherwise, restore the value to "oldval" and return NULL.
20691 * Must always be called in pairs to save and restore v:exception! Does not
20692 * take care of memory allocations.
20693 */
20694 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020695v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696{
20697 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020698 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699
Bram Moolenaare9a41262005-01-15 22:18:47 +000020700 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 return NULL;
20702}
20703
20704/*
20705 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20706 * Otherwise, restore the value to "oldval" and return NULL.
20707 * Must always be called in pairs to save and restore v:throwpoint! Does not
20708 * take care of memory allocations.
20709 */
20710 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020711v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712{
20713 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020714 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715
Bram Moolenaare9a41262005-01-15 22:18:47 +000020716 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 return NULL;
20718}
20719
20720#if defined(FEAT_AUTOCMD) || defined(PROTO)
20721/*
20722 * Set v:cmdarg.
20723 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20724 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20725 * Must always be called in pairs!
20726 */
20727 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020728set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729{
20730 char_u *oldval;
20731 char_u *newval;
20732 unsigned len;
20733
Bram Moolenaare9a41262005-01-15 22:18:47 +000020734 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020735 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020737 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020738 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020739 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 }
20741
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020742 if (eap->force_bin == FORCE_BIN)
20743 len = 6;
20744 else if (eap->force_bin == FORCE_NOBIN)
20745 len = 8;
20746 else
20747 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020748
20749 if (eap->read_edit)
20750 len += 7;
20751
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020752 if (eap->force_ff != 0)
20753 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20754# ifdef FEAT_MBYTE
20755 if (eap->force_enc != 0)
20756 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020757 if (eap->bad_char != 0)
20758 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020759# endif
20760
20761 newval = alloc(len + 1);
20762 if (newval == NULL)
20763 return NULL;
20764
20765 if (eap->force_bin == FORCE_BIN)
20766 sprintf((char *)newval, " ++bin");
20767 else if (eap->force_bin == FORCE_NOBIN)
20768 sprintf((char *)newval, " ++nobin");
20769 else
20770 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020771
20772 if (eap->read_edit)
20773 STRCAT(newval, " ++edit");
20774
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020775 if (eap->force_ff != 0)
20776 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20777 eap->cmd + eap->force_ff);
20778# ifdef FEAT_MBYTE
20779 if (eap->force_enc != 0)
20780 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20781 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020782 if (eap->bad_char == BAD_KEEP)
20783 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20784 else if (eap->bad_char == BAD_DROP)
20785 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20786 else if (eap->bad_char != 0)
20787 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020788# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020789 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020790 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791}
20792#endif
20793
20794/*
20795 * Get the value of internal variable "name".
20796 * Return OK or FAIL.
20797 */
20798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020799get_var_tv(
20800 char_u *name,
20801 int len, /* length of "name" */
20802 typval_T *rettv, /* NULL when only checking existence */
20803 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20804 int verbose, /* may give error message */
20805 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806{
20807 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020808 typval_T *tv = NULL;
20809 typval_T atv;
20810 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812
20813 /* truncate the name, so that we can use strcmp() */
20814 cc = name[len];
20815 name[len] = NUL;
20816
20817 /*
20818 * Check for "b:changedtick".
20819 */
20820 if (STRCMP(name, "b:changedtick") == 0)
20821 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020822 atv.v_type = VAR_NUMBER;
20823 atv.vval.v_number = curbuf->b_changedtick;
20824 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 }
20826
20827 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 * Check for user-defined variables.
20829 */
20830 else
20831 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020832 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020835 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020836 if (dip != NULL)
20837 *dip = v;
20838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 }
20840
Bram Moolenaare9a41262005-01-15 22:18:47 +000020841 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020843 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020844 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 ret = FAIL;
20846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020847 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020848 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849
20850 name[len] = cc;
20851
20852 return ret;
20853}
20854
20855/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020856 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20857 * Also handle function call with Funcref variable: func(expr)
20858 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20859 */
20860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020861handle_subscript(
20862 char_u **arg,
20863 typval_T *rettv,
20864 int evaluate, /* do more than finding the end */
20865 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020866{
20867 int ret = OK;
20868 dict_T *selfdict = NULL;
20869 char_u *s;
20870 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020871 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020872
20873 while (ret == OK
20874 && (**arg == '['
20875 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020876 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020877 && !vim_iswhite(*(*arg - 1)))
20878 {
20879 if (**arg == '(')
20880 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020881 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020882 if (evaluate)
20883 {
20884 functv = *rettv;
20885 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020886
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020887 /* Invoke the function. Recursive! */
20888 s = functv.vval.v_string;
20889 }
20890 else
20891 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020892 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020893 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20894 &len, evaluate, selfdict);
20895
20896 /* Clear the funcref afterwards, so that deleting it while
20897 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020898 if (evaluate)
20899 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020900
20901 /* Stop the expression evaluation when immediately aborting on
20902 * error, or when an interrupt occurred or an exception was thrown
20903 * but not caught. */
20904 if (aborting())
20905 {
20906 if (ret == OK)
20907 clear_tv(rettv);
20908 ret = FAIL;
20909 }
20910 dict_unref(selfdict);
20911 selfdict = NULL;
20912 }
20913 else /* **arg == '[' || **arg == '.' */
20914 {
20915 dict_unref(selfdict);
20916 if (rettv->v_type == VAR_DICT)
20917 {
20918 selfdict = rettv->vval.v_dict;
20919 if (selfdict != NULL)
20920 ++selfdict->dv_refcount;
20921 }
20922 else
20923 selfdict = NULL;
20924 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20925 {
20926 clear_tv(rettv);
20927 ret = FAIL;
20928 }
20929 }
20930 }
20931 dict_unref(selfdict);
20932 return ret;
20933}
20934
20935/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020936 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020937 * value).
20938 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020939 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020940alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020941{
Bram Moolenaar33570922005-01-25 22:26:29 +000020942 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020943}
20944
20945/*
20946 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 * The string "s" must have been allocated, it is consumed.
20948 * Return NULL for out of memory, the variable otherwise.
20949 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020950 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020951alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952{
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020955 rettv = alloc_tv();
20956 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020958 rettv->v_type = VAR_STRING;
20959 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 }
20961 else
20962 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020963 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964}
20965
20966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020967 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020970free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971{
20972 if (varp != NULL)
20973 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020974 switch (varp->v_type)
20975 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020976 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020977 func_unref(varp->vval.v_string);
20978 /*FALLTHROUGH*/
20979 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020980 vim_free(varp->vval.v_string);
20981 break;
20982 case VAR_LIST:
20983 list_unref(varp->vval.v_list);
20984 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020985 case VAR_DICT:
20986 dict_unref(varp->vval.v_dict);
20987 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020988 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020989#ifdef FEAT_FLOAT
20990 case VAR_FLOAT:
20991#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020992 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010020993 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020994 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020995 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020996 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020997 break;
20998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 vim_free(varp);
21000 }
21001}
21002
21003/*
21004 * Free the memory for a variable value and set the value to NULL or 0.
21005 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021007clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008{
21009 if (varp != NULL)
21010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021011 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021013 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021014 func_unref(varp->vval.v_string);
21015 /*FALLTHROUGH*/
21016 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021017 vim_free(varp->vval.v_string);
21018 varp->vval.v_string = NULL;
21019 break;
21020 case VAR_LIST:
21021 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021022 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021023 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021024 case VAR_DICT:
21025 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021026 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021027 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021029 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021030 varp->vval.v_number = 0;
21031 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021032#ifdef FEAT_FLOAT
21033 case VAR_FLOAT:
21034 varp->vval.v_float = 0.0;
21035 break;
21036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021037 case VAR_UNKNOWN:
21038 break;
21039 default:
21040 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021042 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 }
21044}
21045
21046/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021047 * Set the value of a variable to NULL without freeing items.
21048 */
21049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021050init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021051{
21052 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021054}
21055
21056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057 * Get the number value of a variable.
21058 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021059 * For incompatible types, return 0.
21060 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21061 * caller of incompatible types: it sets *denote to TRUE if "denote"
21062 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 */
21064 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021065get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021067 int error = FALSE;
21068
21069 return get_tv_number_chk(varp, &error); /* return 0L on error */
21070}
21071
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021072 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021073get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021074{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021075 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021077 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021079 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021080 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021081#ifdef FEAT_FLOAT
21082 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021083 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021084 break;
21085#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021086 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021087 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021088 break;
21089 case VAR_STRING:
21090 if (varp->vval.v_string != NULL)
21091 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021092 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021093 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021094 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021095 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021096 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021097 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021098 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021099 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021100 case VAR_SPECIAL:
21101 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21102 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021103 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021104 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021107 if (denote == NULL) /* useful for values that must be unsigned */
21108 n = -1;
21109 else
21110 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021111 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112}
21113
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021114#ifdef FEAT_FLOAT
21115 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021116get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021117{
21118 switch (varp->v_type)
21119 {
21120 case VAR_NUMBER:
21121 return (float_T)(varp->vval.v_number);
21122#ifdef FEAT_FLOAT
21123 case VAR_FLOAT:
21124 return varp->vval.v_float;
21125 break;
21126#endif
21127 case VAR_FUNC:
21128 EMSG(_("E891: Using a Funcref as a Float"));
21129 break;
21130 case VAR_STRING:
21131 EMSG(_("E892: Using a String as a Float"));
21132 break;
21133 case VAR_LIST:
21134 EMSG(_("E893: Using a List as a Float"));
21135 break;
21136 case VAR_DICT:
21137 EMSG(_("E894: Using a Dictionary as a Float"));
21138 break;
21139 default:
21140 EMSG2(_(e_intern2), "get_tv_float()");
21141 break;
21142 }
21143 return 0;
21144}
21145#endif
21146
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021148 * Get the lnum from the first argument.
21149 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021150 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 */
21152 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021153get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154{
Bram Moolenaar33570922005-01-25 22:26:29 +000021155 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 linenr_T lnum;
21157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021158 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 if (lnum == 0) /* no valid number, try using line() */
21160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021161 rettv.v_type = VAR_NUMBER;
21162 f_line(argvars, &rettv);
21163 lnum = rettv.vval.v_number;
21164 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 }
21166 return lnum;
21167}
21168
21169/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021170 * Get the lnum from the first argument.
21171 * Also accepts "$", then "buf" is used.
21172 * Returns 0 on error.
21173 */
21174 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021175get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021176{
21177 if (argvars[0].v_type == VAR_STRING
21178 && argvars[0].vval.v_string != NULL
21179 && argvars[0].vval.v_string[0] == '$'
21180 && buf != NULL)
21181 return buf->b_ml.ml_line_count;
21182 return get_tv_number_chk(&argvars[0], NULL);
21183}
21184
21185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 * Get the string value of a variable.
21187 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021188 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21189 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 * If the String variable has never been set, return an empty string.
21191 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021192 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21193 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 */
21195 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021196get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197{
21198 static char_u mybuf[NUMBUFLEN];
21199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021200 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201}
21202
21203 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021204get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021206 char_u *res = get_tv_string_buf_chk(varp, buf);
21207
21208 return res != NULL ? res : (char_u *)"";
21209}
21210
Bram Moolenaar7d647822014-04-05 21:28:56 +020021211/*
21212 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21213 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021214 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021215get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021216{
21217 static char_u mybuf[NUMBUFLEN];
21218
21219 return get_tv_string_buf_chk(varp, mybuf);
21220}
21221
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021222 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021223get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021224{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021225 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021227 case VAR_NUMBER:
21228 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21229 return buf;
21230 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021231 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021232 break;
21233 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021234 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021235 break;
21236 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021237 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021238 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021239#ifdef FEAT_FLOAT
21240 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021241 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021242 break;
21243#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021244 case VAR_STRING:
21245 if (varp->vval.v_string != NULL)
21246 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021247 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021248 case VAR_SPECIAL:
21249 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21250 return buf;
21251
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021252 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021253 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021256 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257}
21258
21259/*
21260 * Find variable "name" in the list of variables.
21261 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021262 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021263 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021264 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021266 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021267find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021270 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271
Bram Moolenaara7043832005-01-21 11:56:39 +000021272 ht = find_var_ht(name, &varname);
21273 if (htp != NULL)
21274 *htp = ht;
21275 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021277 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278}
21279
21280/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021281 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021282 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021285find_var_in_ht(
21286 hashtab_T *ht,
21287 int htname,
21288 char_u *varname,
21289 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021290{
Bram Moolenaar33570922005-01-25 22:26:29 +000021291 hashitem_T *hi;
21292
21293 if (*varname == NUL)
21294 {
21295 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021296 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021297 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021298 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021299 case 'g': return &globvars_var;
21300 case 'v': return &vimvars_var;
21301 case 'b': return &curbuf->b_bufvar;
21302 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021303#ifdef FEAT_WINDOWS
21304 case 't': return &curtab->tp_winvar;
21305#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021306 case 'l': return current_funccal == NULL
21307 ? NULL : &current_funccal->l_vars_var;
21308 case 'a': return current_funccal == NULL
21309 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021310 }
21311 return NULL;
21312 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021313
21314 hi = hash_find(ht, varname);
21315 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021316 {
21317 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021318 * worked find the variable again. Don't auto-load a script if it was
21319 * loaded already, otherwise it would be loaded every time when
21320 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021321 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021322 {
21323 /* Note: script_autoload() may make "hi" invalid. It must either
21324 * be obtained again or not used. */
21325 if (!script_autoload(varname, FALSE) || aborting())
21326 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021327 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021328 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021329 if (HASHITEM_EMPTY(hi))
21330 return NULL;
21331 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021332 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021333}
21334
21335/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021336 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021337 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021338 * Set "varname" to the start of name without ':'.
21339 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021340 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021341find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021343 hashitem_T *hi;
21344
Bram Moolenaar73627d02015-08-11 15:46:09 +020021345 if (name[0] == NUL)
21346 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 if (name[1] != ':')
21348 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021349 /* The name must not start with a colon or #. */
21350 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 return NULL;
21352 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021353
21354 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021355 hi = hash_find(&compat_hashtab, name);
21356 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021357 return &compat_hashtab;
21358
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021360 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021361 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 }
21363 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021364 if (*name == 'g') /* global variable */
21365 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021366 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21367 */
21368 if (vim_strchr(name + 2, ':') != NULL
21369 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021370 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021372 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021374 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021375#ifdef FEAT_WINDOWS
21376 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021377 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021378#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021379 if (*name == 'v') /* v: variable */
21380 return &vimvarht;
21381 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021382 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021383 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021384 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 if (*name == 's' /* script variable */
21386 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21387 return &SCRIPT_VARS(current_SID);
21388 return NULL;
21389}
21390
21391/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021392 * Get function call environment based on bactrace debug level
21393 */
21394 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021395get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021396{
21397 int i;
21398 funccall_T *funccal;
21399 funccall_T *temp_funccal;
21400
21401 funccal = current_funccal;
21402 if (debug_backtrace_level > 0)
21403 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021404 for (i = 0; i < debug_backtrace_level; i++)
21405 {
21406 temp_funccal = funccal->caller;
21407 if (temp_funccal)
21408 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021409 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021410 /* backtrace level overflow. reset to max */
21411 debug_backtrace_level = i;
21412 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021413 }
21414 return funccal;
21415}
21416
21417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021419 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 * Returns NULL when it doesn't exist.
21421 */
21422 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021423get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424{
Bram Moolenaar33570922005-01-25 22:26:29 +000021425 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021427 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 if (v == NULL)
21429 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021430 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431}
21432
21433/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021434 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 * sourcing this script and when executing functions defined in the script.
21436 */
21437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021438new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439{
Bram Moolenaara7043832005-01-21 11:56:39 +000021440 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021441 hashtab_T *ht;
21442 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021443
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21445 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021446 /* Re-allocating ga_data means that an ht_array pointing to
21447 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021449 for (i = 1; i <= ga_scripts.ga_len; ++i)
21450 {
21451 ht = &SCRIPT_VARS(i);
21452 if (ht->ht_mask == HT_INIT_SIZE - 1)
21453 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021454 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021456 }
21457
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 while (ga_scripts.ga_len < id)
21459 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021460 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021461 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021462 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 }
21465 }
21466}
21467
21468/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21470 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 */
21472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021473init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474{
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021476 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021477 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021478 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021479 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021480 dict_var->di_tv.vval.v_dict = dict;
21481 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021482 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021483 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21484 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485}
21486
21487/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021488 * Unreference a dictionary initialized by init_var_dict().
21489 */
21490 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021491unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021492{
21493 /* Now the dict needs to be freed if no one else is using it, go back to
21494 * normal reference counting. */
21495 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21496 dict_unref(dict);
21497}
21498
21499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021501 * Frees all allocated variables and the value they contain.
21502 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 */
21504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021505vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021506{
21507 vars_clear_ext(ht, TRUE);
21508}
21509
21510/*
21511 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21512 */
21513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021514vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515{
Bram Moolenaara7043832005-01-21 11:56:39 +000021516 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021517 hashitem_T *hi;
21518 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519
Bram Moolenaar33570922005-01-25 22:26:29 +000021520 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021521 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021522 for (hi = ht->ht_array; todo > 0; ++hi)
21523 {
21524 if (!HASHITEM_EMPTY(hi))
21525 {
21526 --todo;
21527
Bram Moolenaar33570922005-01-25 22:26:29 +000021528 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021529 * ht_array might change then. hash_clear() takes care of it
21530 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021531 v = HI2DI(hi);
21532 if (free_val)
21533 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021534 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021536 }
21537 }
21538 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021539 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540}
21541
Bram Moolenaara7043832005-01-21 11:56:39 +000021542/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 * Delete a variable from hashtab "ht" at item "hi".
21544 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021547delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548{
Bram Moolenaar33570922005-01-25 22:26:29 +000021549 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021550
21551 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 clear_tv(&di->di_tv);
21553 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554}
21555
21556/*
21557 * List the value of one internal variable.
21558 */
21559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021560list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021562 char_u *tofree;
21563 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021564 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021565
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021566 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021567 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021568 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021569 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570}
21571
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021573list_one_var_a(
21574 char_u *prefix,
21575 char_u *name,
21576 int type,
21577 char_u *string,
21578 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579{
Bram Moolenaar31859182007-08-14 20:41:13 +000021580 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21581 msg_start();
21582 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 if (name != NULL) /* "a:" vars don't have a name stored */
21584 msg_puts(name);
21585 msg_putchar(' ');
21586 msg_advance(22);
21587 if (type == VAR_NUMBER)
21588 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021589 else if (type == VAR_FUNC)
21590 msg_putchar('*');
21591 else if (type == VAR_LIST)
21592 {
21593 msg_putchar('[');
21594 if (*string == '[')
21595 ++string;
21596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021597 else if (type == VAR_DICT)
21598 {
21599 msg_putchar('{');
21600 if (*string == '{')
21601 ++string;
21602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 else
21604 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021605
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607
21608 if (type == VAR_FUNC)
21609 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021610 if (*first)
21611 {
21612 msg_clr_eos();
21613 *first = FALSE;
21614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615}
21616
21617/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021618 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 * If the variable already exists, the value is updated.
21620 * Otherwise the variable is created.
21621 */
21622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021623set_var(
21624 char_u *name,
21625 typval_T *tv,
21626 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627{
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021632 ht = find_var_ht(name, &varname);
21633 if (ht == NULL || *varname == NUL)
21634 {
21635 EMSG2(_(e_illvar), name);
21636 return;
21637 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021638 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021639
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021640 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21641 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021642
Bram Moolenaar33570922005-01-25 22:26:29 +000021643 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021646 if (var_check_ro(v->di_flags, name, FALSE)
21647 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021648 return;
21649 if (v->di_tv.v_type != tv->v_type
21650 && !((v->di_tv.v_type == VAR_STRING
21651 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021652 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021653 || tv->v_type == VAR_NUMBER))
21654#ifdef FEAT_FLOAT
21655 && !((v->di_tv.v_type == VAR_NUMBER
21656 || v->di_tv.v_type == VAR_FLOAT)
21657 && (tv->v_type == VAR_NUMBER
21658 || tv->v_type == VAR_FLOAT))
21659#endif
21660 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021661 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021662 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021663 return;
21664 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021665
21666 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021667 * Handle setting internal v: variables separately where needed to
21668 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021669 */
21670 if (ht == &vimvarht)
21671 {
21672 if (v->di_tv.v_type == VAR_STRING)
21673 {
21674 vim_free(v->di_tv.vval.v_string);
21675 if (copy || tv->v_type != VAR_STRING)
21676 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21677 else
21678 {
21679 /* Take over the string to avoid an extra alloc/free. */
21680 v->di_tv.vval.v_string = tv->vval.v_string;
21681 tv->vval.v_string = NULL;
21682 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021683 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021685 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021686 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021687 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021688 if (STRCMP(varname, "searchforward") == 0)
21689 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021690#ifdef FEAT_SEARCH_EXTRA
21691 else if (STRCMP(varname, "hlsearch") == 0)
21692 {
21693 no_hlsearch = !v->di_tv.vval.v_number;
21694 redraw_all_later(SOME_VALID);
21695 }
21696#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021697 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021698 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021699 else if (v->di_tv.v_type != tv->v_type)
21700 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021701 }
21702
21703 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 }
21705 else /* add a new variable */
21706 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021707 /* Can't add "v:" variable. */
21708 if (ht == &vimvarht)
21709 {
21710 EMSG2(_(e_illvar), name);
21711 return;
21712 }
21713
Bram Moolenaar92124a32005-06-17 22:03:40 +000021714 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021715 if (!valid_varname(varname))
21716 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021717
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021718 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21719 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021720 if (v == NULL)
21721 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021723 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021725 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 return;
21727 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021728 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021730
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021731 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021732 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021733 else
21734 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021735 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021736 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021737 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739}
21740
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021741/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021742 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 * Also give an error message.
21744 */
21745 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021746var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021747{
21748 if (flags & DI_FLAGS_RO)
21749 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021750 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 return TRUE;
21752 }
21753 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21754 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021755 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021756 return TRUE;
21757 }
21758 return FALSE;
21759}
21760
21761/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021762 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21763 * Also give an error message.
21764 */
21765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021766var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021767{
21768 if (flags & DI_FLAGS_FIX)
21769 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021770 EMSG2(_("E795: Cannot delete variable %s"),
21771 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021772 return TRUE;
21773 }
21774 return FALSE;
21775}
21776
21777/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021778 * Check if a funcref is assigned to a valid variable name.
21779 * Return TRUE and give an error if not.
21780 */
21781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021782var_check_func_name(
21783 char_u *name, /* points to start of variable name */
21784 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021785{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021786 /* Allow for w: b: s: and t:. */
21787 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021788 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21789 ? name[2] : name[0]))
21790 {
21791 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21792 name);
21793 return TRUE;
21794 }
21795 /* Don't allow hiding a function. When "v" is not NULL we might be
21796 * assigning another function to the same var, the type is checked
21797 * below. */
21798 if (new_var && function_exists(name))
21799 {
21800 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21801 name);
21802 return TRUE;
21803 }
21804 return FALSE;
21805}
21806
21807/*
21808 * Check if a variable name is valid.
21809 * Return FALSE and give an error if not.
21810 */
21811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021812valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021813{
21814 char_u *p;
21815
21816 for (p = varname; *p != NUL; ++p)
21817 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21818 && *p != AUTOLOAD_CHAR)
21819 {
21820 EMSG2(_(e_illvar), varname);
21821 return FALSE;
21822 }
21823 return TRUE;
21824}
21825
21826/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021827 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021828 * Also give an error message, using "name" or _("name") when use_gettext is
21829 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021830 */
21831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021832tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021833{
21834 if (lock & VAR_LOCKED)
21835 {
21836 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021837 name == NULL ? (char_u *)_("Unknown")
21838 : use_gettext ? (char_u *)_(name)
21839 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021840 return TRUE;
21841 }
21842 if (lock & VAR_FIXED)
21843 {
21844 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021845 name == NULL ? (char_u *)_("Unknown")
21846 : use_gettext ? (char_u *)_(name)
21847 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021848 return TRUE;
21849 }
21850 return FALSE;
21851}
21852
21853/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021854 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021855 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021856 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021857 * It is OK for "from" and "to" to point to the same item. This is used to
21858 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021859 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021860 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021861copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021863 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021864 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021865 switch (from->v_type)
21866 {
21867 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021868 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869 to->vval.v_number = from->vval.v_number;
21870 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021871#ifdef FEAT_FLOAT
21872 case VAR_FLOAT:
21873 to->vval.v_float = from->vval.v_float;
21874 break;
21875#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021876 case VAR_STRING:
21877 case VAR_FUNC:
21878 if (from->vval.v_string == NULL)
21879 to->vval.v_string = NULL;
21880 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021881 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021883 if (from->v_type == VAR_FUNC)
21884 func_ref(to->vval.v_string);
21885 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021886 break;
21887 case VAR_LIST:
21888 if (from->vval.v_list == NULL)
21889 to->vval.v_list = NULL;
21890 else
21891 {
21892 to->vval.v_list = from->vval.v_list;
21893 ++to->vval.v_list->lv_refcount;
21894 }
21895 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021896 case VAR_DICT:
21897 if (from->vval.v_dict == NULL)
21898 to->vval.v_dict = NULL;
21899 else
21900 {
21901 to->vval.v_dict = from->vval.v_dict;
21902 ++to->vval.v_dict->dv_refcount;
21903 }
21904 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021905 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021906 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021907 break;
21908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909}
21910
21911/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021912 * Make a copy of an item.
21913 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021914 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21915 * reference to an already copied list/dict can be used.
21916 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021917 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021918 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021919item_copy(
21920 typval_T *from,
21921 typval_T *to,
21922 int deep,
21923 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021924{
21925 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021926 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021927
Bram Moolenaar33570922005-01-25 22:26:29 +000021928 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021929 {
21930 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021931 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021932 }
21933 ++recurse;
21934
21935 switch (from->v_type)
21936 {
21937 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021938#ifdef FEAT_FLOAT
21939 case VAR_FLOAT:
21940#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021941 case VAR_STRING:
21942 case VAR_FUNC:
21943 copy_tv(from, to);
21944 break;
21945 case VAR_LIST:
21946 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021947 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021948 if (from->vval.v_list == NULL)
21949 to->vval.v_list = NULL;
21950 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21951 {
21952 /* use the copy made earlier */
21953 to->vval.v_list = from->vval.v_list->lv_copylist;
21954 ++to->vval.v_list->lv_refcount;
21955 }
21956 else
21957 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21958 if (to->vval.v_list == NULL)
21959 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021960 break;
21961 case VAR_DICT:
21962 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021963 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021964 if (from->vval.v_dict == NULL)
21965 to->vval.v_dict = NULL;
21966 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21967 {
21968 /* use the copy made earlier */
21969 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21970 ++to->vval.v_dict->dv_refcount;
21971 }
21972 else
21973 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21974 if (to->vval.v_dict == NULL)
21975 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021976 break;
21977 default:
21978 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021979 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021980 }
21981 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021982 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021983}
21984
21985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 * ":echo expr1 ..." print each argument separated with a space, add a
21987 * newline at the end.
21988 * ":echon expr1 ..." print each argument plain.
21989 */
21990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021991ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992{
21993 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021995 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 char_u *p;
21997 int needclr = TRUE;
21998 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021999 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000
22001 if (eap->skip)
22002 ++emsg_skip;
22003 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022005 /* If eval1() causes an error message the text from the command may
22006 * still need to be cleared. E.g., "echo 22,44". */
22007 need_clr_eos = needclr;
22008
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022010 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 {
22012 /*
22013 * Report the invalid expression unless the expression evaluation
22014 * has been cancelled due to an aborting error, an interrupt, or an
22015 * exception.
22016 */
22017 if (!aborting())
22018 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022019 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 break;
22021 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022022 need_clr_eos = FALSE;
22023
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 if (!eap->skip)
22025 {
22026 if (atstart)
22027 {
22028 atstart = FALSE;
22029 /* Call msg_start() after eval1(), evaluating the expression
22030 * may cause a message to appear. */
22031 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022032 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022033 /* Mark the saved text as finishing the line, so that what
22034 * follows is displayed on a new line when scrolling back
22035 * at the more prompt. */
22036 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 }
22040 else if (eap->cmdidx == CMD_echo)
22041 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022042 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022043 if (p != NULL)
22044 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022046 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022048 if (*p != TAB && needclr)
22049 {
22050 /* remove any text still there from the command */
22051 msg_clr_eos();
22052 needclr = FALSE;
22053 }
22054 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 }
22056 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022057 {
22058#ifdef FEAT_MBYTE
22059 if (has_mbyte)
22060 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022061 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022062
22063 (void)msg_outtrans_len_attr(p, i, echo_attr);
22064 p += i - 1;
22065 }
22066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022068 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022071 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022073 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 arg = skipwhite(arg);
22075 }
22076 eap->nextcmd = check_nextcmd(arg);
22077
22078 if (eap->skip)
22079 --emsg_skip;
22080 else
22081 {
22082 /* remove text that may still be there from the command */
22083 if (needclr)
22084 msg_clr_eos();
22085 if (eap->cmdidx == CMD_echo)
22086 msg_end();
22087 }
22088}
22089
22090/*
22091 * ":echohl {name}".
22092 */
22093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022094ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095{
22096 int id;
22097
22098 id = syn_name2id(eap->arg);
22099 if (id == 0)
22100 echo_attr = 0;
22101 else
22102 echo_attr = syn_id2attr(id);
22103}
22104
22105/*
22106 * ":execute expr1 ..." execute the result of an expression.
22107 * ":echomsg expr1 ..." Print a message
22108 * ":echoerr expr1 ..." Print an error
22109 * Each gets spaces around each argument and a newline at the end for
22110 * echo commands
22111 */
22112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022113ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114{
22115 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022116 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 int ret = OK;
22118 char_u *p;
22119 garray_T ga;
22120 int len;
22121 int save_did_emsg;
22122
22123 ga_init2(&ga, 1, 80);
22124
22125 if (eap->skip)
22126 ++emsg_skip;
22127 while (*arg != NUL && *arg != '|' && *arg != '\n')
22128 {
22129 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022130 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 {
22132 /*
22133 * Report the invalid expression unless the expression evaluation
22134 * has been cancelled due to an aborting error, an interrupt, or an
22135 * exception.
22136 */
22137 if (!aborting())
22138 EMSG2(_(e_invexpr2), p);
22139 ret = FAIL;
22140 break;
22141 }
22142
22143 if (!eap->skip)
22144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022145 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 len = (int)STRLEN(p);
22147 if (ga_grow(&ga, len + 2) == FAIL)
22148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022149 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 ret = FAIL;
22151 break;
22152 }
22153 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 ga.ga_len += len;
22157 }
22158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022159 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 arg = skipwhite(arg);
22161 }
22162
22163 if (ret != FAIL && ga.ga_data != NULL)
22164 {
22165 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022168 out_flush();
22169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 else if (eap->cmdidx == CMD_echoerr)
22171 {
22172 /* We don't want to abort following commands, restore did_emsg. */
22173 save_did_emsg = did_emsg;
22174 EMSG((char_u *)ga.ga_data);
22175 if (!force_abort)
22176 did_emsg = save_did_emsg;
22177 }
22178 else if (eap->cmdidx == CMD_execute)
22179 do_cmdline((char_u *)ga.ga_data,
22180 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22181 }
22182
22183 ga_clear(&ga);
22184
22185 if (eap->skip)
22186 --emsg_skip;
22187
22188 eap->nextcmd = check_nextcmd(arg);
22189}
22190
22191/*
22192 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22193 * "arg" points to the "&" or '+' when called, to "option" when returning.
22194 * Returns NULL when no option name found. Otherwise pointer to the char
22195 * after the option name.
22196 */
22197 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022198find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199{
22200 char_u *p = *arg;
22201
22202 ++p;
22203 if (*p == 'g' && p[1] == ':')
22204 {
22205 *opt_flags = OPT_GLOBAL;
22206 p += 2;
22207 }
22208 else if (*p == 'l' && p[1] == ':')
22209 {
22210 *opt_flags = OPT_LOCAL;
22211 p += 2;
22212 }
22213 else
22214 *opt_flags = 0;
22215
22216 if (!ASCII_ISALPHA(*p))
22217 return NULL;
22218 *arg = p;
22219
22220 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22221 p += 4; /* termcap option */
22222 else
22223 while (ASCII_ISALPHA(*p))
22224 ++p;
22225 return p;
22226}
22227
22228/*
22229 * ":function"
22230 */
22231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022232ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233{
22234 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022235 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 int j;
22237 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022239 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 char_u *name = NULL;
22241 char_u *p;
22242 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022243 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 garray_T newargs;
22245 garray_T newlines;
22246 int varargs = FALSE;
22247 int mustend = FALSE;
22248 int flags = 0;
22249 ufunc_T *fp;
22250 int indent;
22251 int nesting;
22252 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 dictitem_T *v;
22254 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022255 static int func_nr = 0; /* number for nameless function */
22256 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022257 hashtab_T *ht;
22258 int todo;
22259 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022260 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261
22262 /*
22263 * ":function" without argument: list functions.
22264 */
22265 if (ends_excmd(*eap->arg))
22266 {
22267 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022268 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022269 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022270 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022271 {
22272 if (!HASHITEM_EMPTY(hi))
22273 {
22274 --todo;
22275 fp = HI2UF(hi);
22276 if (!isdigit(*fp->uf_name))
22277 list_func_head(fp, FALSE);
22278 }
22279 }
22280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 eap->nextcmd = check_nextcmd(eap->arg);
22282 return;
22283 }
22284
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022285 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022286 * ":function /pat": list functions matching pattern.
22287 */
22288 if (*eap->arg == '/')
22289 {
22290 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22291 if (!eap->skip)
22292 {
22293 regmatch_T regmatch;
22294
22295 c = *p;
22296 *p = NUL;
22297 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22298 *p = c;
22299 if (regmatch.regprog != NULL)
22300 {
22301 regmatch.rm_ic = p_ic;
22302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022303 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022304 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22305 {
22306 if (!HASHITEM_EMPTY(hi))
22307 {
22308 --todo;
22309 fp = HI2UF(hi);
22310 if (!isdigit(*fp->uf_name)
22311 && vim_regexec(&regmatch, fp->uf_name, 0))
22312 list_func_head(fp, FALSE);
22313 }
22314 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022315 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022316 }
22317 }
22318 if (*p == '/')
22319 ++p;
22320 eap->nextcmd = check_nextcmd(p);
22321 return;
22322 }
22323
22324 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022325 * Get the function name. There are these situations:
22326 * func normal function name
22327 * "name" == func, "fudi.fd_dict" == NULL
22328 * dict.func new dictionary entry
22329 * "name" == NULL, "fudi.fd_dict" set,
22330 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22331 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022332 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022333 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22334 * dict.func existing dict entry that's not a Funcref
22335 * "name" == NULL, "fudi.fd_dict" set,
22336 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022337 * s:func script-local function name
22338 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022341 name = trans_function_name(&p, eap->skip, 0, &fudi);
22342 paren = (vim_strchr(p, '(') != NULL);
22343 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 {
22345 /*
22346 * Return on an invalid expression in braces, unless the expression
22347 * evaluation has been cancelled due to an aborting error, an
22348 * interrupt, or an exception.
22349 */
22350 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022351 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022352 if (!eap->skip && fudi.fd_newkey != NULL)
22353 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022354 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 else
22358 eap->skip = TRUE;
22359 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022360
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 /* An error in a function call during evaluation of an expression in magic
22362 * braces should not cause the function not to be defined. */
22363 saved_did_emsg = did_emsg;
22364 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365
22366 /*
22367 * ":function func" with only function name: list function.
22368 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022369 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 {
22371 if (!ends_excmd(*skipwhite(p)))
22372 {
22373 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022374 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 }
22376 eap->nextcmd = check_nextcmd(p);
22377 if (eap->nextcmd != NULL)
22378 *p = NUL;
22379 if (!eap->skip && !got_int)
22380 {
22381 fp = find_func(name);
22382 if (fp != NULL)
22383 {
22384 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022385 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022387 if (FUNCLINE(fp, j) == NULL)
22388 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 msg_putchar('\n');
22390 msg_outnum((long)(j + 1));
22391 if (j < 9)
22392 msg_putchar(' ');
22393 if (j < 99)
22394 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022395 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396 out_flush(); /* show a line at a time */
22397 ui_breakcheck();
22398 }
22399 if (!got_int)
22400 {
22401 msg_putchar('\n');
22402 msg_puts((char_u *)" endfunction");
22403 }
22404 }
22405 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022406 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022408 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 }
22410
22411 /*
22412 * ":function name(arg1, arg2)" Define function.
22413 */
22414 p = skipwhite(p);
22415 if (*p != '(')
22416 {
22417 if (!eap->skip)
22418 {
22419 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022420 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 }
22422 /* attempt to continue by skipping some text */
22423 if (vim_strchr(p, '(') != NULL)
22424 p = vim_strchr(p, '(');
22425 }
22426 p = skipwhite(p + 1);
22427
22428 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22429 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22430
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022431 if (!eap->skip)
22432 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022433 /* Check the name of the function. Unless it's a dictionary function
22434 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022435 if (name != NULL)
22436 arg = name;
22437 else
22438 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022439 if (arg != NULL && (fudi.fd_di == NULL
22440 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022441 {
22442 if (*arg == K_SPECIAL)
22443 j = 3;
22444 else
22445 j = 0;
22446 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22447 : eval_isnamec(arg[j])))
22448 ++j;
22449 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022450 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022451 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022452 /* Disallow using the g: dict. */
22453 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22454 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022455 }
22456
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 /*
22458 * Isolate the arguments: "arg1, arg2, ...)"
22459 */
22460 while (*p != ')')
22461 {
22462 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22463 {
22464 varargs = TRUE;
22465 p += 3;
22466 mustend = TRUE;
22467 }
22468 else
22469 {
22470 arg = p;
22471 while (ASCII_ISALNUM(*p) || *p == '_')
22472 ++p;
22473 if (arg == p || isdigit(*arg)
22474 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22475 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22476 {
22477 if (!eap->skip)
22478 EMSG2(_("E125: Illegal argument: %s"), arg);
22479 break;
22480 }
22481 if (ga_grow(&newargs, 1) == FAIL)
22482 goto erret;
22483 c = *p;
22484 *p = NUL;
22485 arg = vim_strsave(arg);
22486 if (arg == NULL)
22487 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022488
22489 /* Check for duplicate argument name. */
22490 for (i = 0; i < newargs.ga_len; ++i)
22491 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22492 {
22493 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022494 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022495 goto erret;
22496 }
22497
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22499 *p = c;
22500 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 if (*p == ',')
22502 ++p;
22503 else
22504 mustend = TRUE;
22505 }
22506 p = skipwhite(p);
22507 if (mustend && *p != ')')
22508 {
22509 if (!eap->skip)
22510 EMSG2(_(e_invarg2), eap->arg);
22511 break;
22512 }
22513 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022514 if (*p != ')')
22515 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 ++p; /* skip the ')' */
22517
Bram Moolenaare9a41262005-01-15 22:18:47 +000022518 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 for (;;)
22520 {
22521 p = skipwhite(p);
22522 if (STRNCMP(p, "range", 5) == 0)
22523 {
22524 flags |= FC_RANGE;
22525 p += 5;
22526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022527 else if (STRNCMP(p, "dict", 4) == 0)
22528 {
22529 flags |= FC_DICT;
22530 p += 4;
22531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 else if (STRNCMP(p, "abort", 5) == 0)
22533 {
22534 flags |= FC_ABORT;
22535 p += 5;
22536 }
22537 else
22538 break;
22539 }
22540
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022541 /* When there is a line break use what follows for the function body.
22542 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22543 if (*p == '\n')
22544 line_arg = p + 1;
22545 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 EMSG(_(e_trailing));
22547
22548 /*
22549 * Read the body of the function, until ":endfunction" is found.
22550 */
22551 if (KeyTyped)
22552 {
22553 /* Check if the function already exists, don't let the user type the
22554 * whole function before telling him it doesn't work! For a script we
22555 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022556 if (!eap->skip && !eap->forceit)
22557 {
22558 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22559 EMSG(_(e_funcdict));
22560 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022561 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022564 if (!eap->skip && did_emsg)
22565 goto erret;
22566
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 msg_putchar('\n'); /* don't overwrite the function name */
22568 cmdline_row = msg_row;
22569 }
22570
22571 indent = 2;
22572 nesting = 0;
22573 for (;;)
22574 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022575 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022576 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022577 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022578 saved_wait_return = FALSE;
22579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022581 sourcing_lnum_off = sourcing_lnum;
22582
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022583 if (line_arg != NULL)
22584 {
22585 /* Use eap->arg, split up in parts by line breaks. */
22586 theline = line_arg;
22587 p = vim_strchr(theline, '\n');
22588 if (p == NULL)
22589 line_arg += STRLEN(line_arg);
22590 else
22591 {
22592 *p = NUL;
22593 line_arg = p + 1;
22594 }
22595 }
22596 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 theline = getcmdline(':', 0L, indent);
22598 else
22599 theline = eap->getline(':', eap->cookie, indent);
22600 if (KeyTyped)
22601 lines_left = Rows - 1;
22602 if (theline == NULL)
22603 {
22604 EMSG(_("E126: Missing :endfunction"));
22605 goto erret;
22606 }
22607
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022608 /* Detect line continuation: sourcing_lnum increased more than one. */
22609 if (sourcing_lnum > sourcing_lnum_off + 1)
22610 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22611 else
22612 sourcing_lnum_off = 0;
22613
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 if (skip_until != NULL)
22615 {
22616 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22617 * don't check for ":endfunc". */
22618 if (STRCMP(theline, skip_until) == 0)
22619 {
22620 vim_free(skip_until);
22621 skip_until = NULL;
22622 }
22623 }
22624 else
22625 {
22626 /* skip ':' and blanks*/
22627 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22628 ;
22629
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022630 /* Check for "endfunction". */
22631 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022633 if (line_arg == NULL)
22634 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 break;
22636 }
22637
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022638 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 * at "end". */
22640 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22641 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022642 else if (STRNCMP(p, "if", 2) == 0
22643 || STRNCMP(p, "wh", 2) == 0
22644 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 || STRNCMP(p, "try", 3) == 0)
22646 indent += 2;
22647
22648 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022649 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022651 if (*p == '!')
22652 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022654 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22655 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022657 ++nesting;
22658 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 }
22660 }
22661
22662 /* Check for ":append" or ":insert". */
22663 p = skip_range(p, NULL);
22664 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22665 || (p[0] == 'i'
22666 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22667 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22668 skip_until = vim_strsave((char_u *)".");
22669
22670 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22671 arg = skipwhite(skiptowhite(p));
22672 if (arg[0] == '<' && arg[1] =='<'
22673 && ((p[0] == 'p' && p[1] == 'y'
22674 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22675 || (p[0] == 'p' && p[1] == 'e'
22676 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22677 || (p[0] == 't' && p[1] == 'c'
22678 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022679 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22680 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22682 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022683 || (p[0] == 'm' && p[1] == 'z'
22684 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 ))
22686 {
22687 /* ":python <<" continues until a dot, like ":append" */
22688 p = skipwhite(arg + 2);
22689 if (*p == NUL)
22690 skip_until = vim_strsave((char_u *)".");
22691 else
22692 skip_until = vim_strsave(p);
22693 }
22694 }
22695
22696 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022697 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022698 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022699 if (line_arg == NULL)
22700 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022702 }
22703
22704 /* Copy the line to newly allocated memory. get_one_sourceline()
22705 * allocates 250 bytes per line, this saves 80% on average. The cost
22706 * is an extra alloc/free. */
22707 p = vim_strsave(theline);
22708 if (p != NULL)
22709 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022710 if (line_arg == NULL)
22711 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022712 theline = p;
22713 }
22714
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022715 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22716
22717 /* Add NULL lines for continuation lines, so that the line count is
22718 * equal to the index in the growarray. */
22719 while (sourcing_lnum_off-- > 0)
22720 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022721
22722 /* Check for end of eap->arg. */
22723 if (line_arg != NULL && *line_arg == NUL)
22724 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 }
22726
22727 /* Don't define the function when skipping commands or when an error was
22728 * detected. */
22729 if (eap->skip || did_emsg)
22730 goto erret;
22731
22732 /*
22733 * If there are no errors, add the function
22734 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022735 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022736 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022737 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022739 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022740 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022741 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022742 goto erret;
22743 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022744
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 fp = find_func(name);
22746 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022748 if (!eap->forceit)
22749 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022750 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022751 goto erret;
22752 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022753 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022755 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022756 name);
22757 goto erret;
22758 }
22759 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022760 ga_clear_strings(&(fp->uf_args));
22761 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762 vim_free(name);
22763 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 }
22766 else
22767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022768 char numbuf[20];
22769
22770 fp = NULL;
22771 if (fudi.fd_newkey == NULL && !eap->forceit)
22772 {
22773 EMSG(_(e_funcdict));
22774 goto erret;
22775 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022776 if (fudi.fd_di == NULL)
22777 {
22778 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022779 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022780 goto erret;
22781 }
22782 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022783 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022784 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022785
22786 /* Give the function a sequential number. Can only be used with a
22787 * Funcref! */
22788 vim_free(name);
22789 sprintf(numbuf, "%d", ++func_nr);
22790 name = vim_strsave((char_u *)numbuf);
22791 if (name == NULL)
22792 goto erret;
22793 }
22794
22795 if (fp == NULL)
22796 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022797 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022798 {
22799 int slen, plen;
22800 char_u *scriptname;
22801
22802 /* Check that the autoload name matches the script name. */
22803 j = FAIL;
22804 if (sourcing_name != NULL)
22805 {
22806 scriptname = autoload_name(name);
22807 if (scriptname != NULL)
22808 {
22809 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022810 plen = (int)STRLEN(p);
22811 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022812 if (slen > plen && fnamecmp(p,
22813 sourcing_name + slen - plen) == 0)
22814 j = OK;
22815 vim_free(scriptname);
22816 }
22817 }
22818 if (j == FAIL)
22819 {
22820 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22821 goto erret;
22822 }
22823 }
22824
22825 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 if (fp == NULL)
22827 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022828
22829 if (fudi.fd_dict != NULL)
22830 {
22831 if (fudi.fd_di == NULL)
22832 {
22833 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022834 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022835 if (fudi.fd_di == NULL)
22836 {
22837 vim_free(fp);
22838 goto erret;
22839 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022840 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22841 {
22842 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022843 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022844 goto erret;
22845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022846 }
22847 else
22848 /* overwrite existing dict entry */
22849 clear_tv(&fudi.fd_di->di_tv);
22850 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022851 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022852 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022853 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022854
22855 /* behave like "dict" was used */
22856 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022857 }
22858
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022860 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022861 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22862 {
22863 vim_free(fp);
22864 goto erret;
22865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022867 fp->uf_args = newargs;
22868 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022869#ifdef FEAT_PROFILE
22870 fp->uf_tml_count = NULL;
22871 fp->uf_tml_total = NULL;
22872 fp->uf_tml_self = NULL;
22873 fp->uf_profiling = FALSE;
22874 if (prof_def_func())
22875 func_do_profile(fp);
22876#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022877 fp->uf_varargs = varargs;
22878 fp->uf_flags = flags;
22879 fp->uf_calls = 0;
22880 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022881 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882
22883erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 ga_clear_strings(&newargs);
22885 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022886ret_free:
22887 vim_free(skip_until);
22888 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022891 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892}
22893
22894/*
22895 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022896 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022899 * TFN_INT: internal function name OK
22900 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022901 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 * Advances "pp" to just after the function name (if no error).
22903 */
22904 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022905trans_function_name(
22906 char_u **pp,
22907 int skip, /* only find the end, don't evaluate */
22908 int flags,
22909 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022911 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 char_u *start;
22913 char_u *end;
22914 int lead;
22915 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022917 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022918
22919 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022920 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022922
22923 /* Check for hard coded <SNR>: already translated function ID (from a user
22924 * command). */
22925 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22926 && (*pp)[2] == (int)KE_SNR)
22927 {
22928 *pp += 3;
22929 len = get_id_len(pp) + 3;
22930 return vim_strnsave(start, len);
22931 }
22932
22933 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22934 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022936 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022938
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022939 /* Note that TFN_ flags use the same values as GLV_ flags. */
22940 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022941 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022942 if (end == start)
22943 {
22944 if (!skip)
22945 EMSG(_("E129: Function name required"));
22946 goto theend;
22947 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022948 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022949 {
22950 /*
22951 * Report an invalid expression in braces, unless the expression
22952 * evaluation has been cancelled due to an aborting error, an
22953 * interrupt, or an exception.
22954 */
22955 if (!aborting())
22956 {
22957 if (end != NULL)
22958 EMSG2(_(e_invarg2), start);
22959 }
22960 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022961 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022962 goto theend;
22963 }
22964
22965 if (lv.ll_tv != NULL)
22966 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022967 if (fdp != NULL)
22968 {
22969 fdp->fd_dict = lv.ll_dict;
22970 fdp->fd_newkey = lv.ll_newkey;
22971 lv.ll_newkey = NULL;
22972 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022973 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022974 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22975 {
22976 name = vim_strsave(lv.ll_tv->vval.v_string);
22977 *pp = end;
22978 }
22979 else
22980 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022981 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22982 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022983 EMSG(_(e_funcref));
22984 else
22985 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022986 name = NULL;
22987 }
22988 goto theend;
22989 }
22990
22991 if (lv.ll_name == NULL)
22992 {
22993 /* Error found, but continue after the function name. */
22994 *pp = end;
22995 goto theend;
22996 }
22997
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022998 /* Check if the name is a Funcref. If so, use the value. */
22999 if (lv.ll_exp_name != NULL)
23000 {
23001 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023002 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023003 if (name == lv.ll_exp_name)
23004 name = NULL;
23005 }
23006 else
23007 {
23008 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023009 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023010 if (name == *pp)
23011 name = NULL;
23012 }
23013 if (name != NULL)
23014 {
23015 name = vim_strsave(name);
23016 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023017 if (STRNCMP(name, "<SNR>", 5) == 0)
23018 {
23019 /* Change "<SNR>" to the byte sequence. */
23020 name[0] = K_SPECIAL;
23021 name[1] = KS_EXTRA;
23022 name[2] = (int)KE_SNR;
23023 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23024 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023025 goto theend;
23026 }
23027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023028 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023030 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023031 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23032 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23033 {
23034 /* When there was "s:" already or the name expanded to get a
23035 * leading "s:" then remove it. */
23036 lv.ll_name += 2;
23037 len -= 2;
23038 lead = 2;
23039 }
23040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023041 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023042 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023043 /* skip over "s:" and "g:" */
23044 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023045 lv.ll_name += 2;
23046 len = (int)(end - lv.ll_name);
23047 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023048
23049 /*
23050 * Copy the function name to allocated memory.
23051 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23052 * Accept <SNR>123_name() outside a script.
23053 */
23054 if (skip)
23055 lead = 0; /* do nothing */
23056 else if (lead > 0)
23057 {
23058 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023059 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23060 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023061 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023062 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023063 if (current_SID <= 0)
23064 {
23065 EMSG(_(e_usingsid));
23066 goto theend;
23067 }
23068 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23069 lead += (int)STRLEN(sid_buf);
23070 }
23071 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023072 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023073 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023074 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023075 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023076 goto theend;
23077 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023078 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023079 {
23080 char_u *cp = vim_strchr(lv.ll_name, ':');
23081
23082 if (cp != NULL && cp < end)
23083 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023084 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023085 goto theend;
23086 }
23087 }
23088
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023089 name = alloc((unsigned)(len + lead + 1));
23090 if (name != NULL)
23091 {
23092 if (lead > 0)
23093 {
23094 name[0] = K_SPECIAL;
23095 name[1] = KS_EXTRA;
23096 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023097 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023098 STRCPY(name + 3, sid_buf);
23099 }
23100 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023101 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023102 }
23103 *pp = end;
23104
23105theend:
23106 clear_lval(&lv);
23107 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108}
23109
23110/*
23111 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23112 * Return 2 if "p" starts with "s:".
23113 * Return 0 otherwise.
23114 */
23115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023116eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023118 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23119 * the standard library function. */
23120 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23121 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 return 5;
23123 if (p[0] == 's' && p[1] == ':')
23124 return 2;
23125 return 0;
23126}
23127
23128/*
23129 * Return TRUE if "p" starts with "<SID>" or "s:".
23130 * Only works if eval_fname_script() returned non-zero for "p"!
23131 */
23132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023133eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134{
23135 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23136}
23137
23138/*
23139 * List the head of the function: "name(arg1, arg2)".
23140 */
23141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023142list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143{
23144 int j;
23145
23146 msg_start();
23147 if (indent)
23148 MSG_PUTS(" ");
23149 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023150 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 {
23152 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023153 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 }
23155 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023156 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023158 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 {
23160 if (j)
23161 MSG_PUTS(", ");
23162 msg_puts(FUNCARG(fp, j));
23163 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023164 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 {
23166 if (j)
23167 MSG_PUTS(", ");
23168 MSG_PUTS("...");
23169 }
23170 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023171 if (fp->uf_flags & FC_ABORT)
23172 MSG_PUTS(" abort");
23173 if (fp->uf_flags & FC_RANGE)
23174 MSG_PUTS(" range");
23175 if (fp->uf_flags & FC_DICT)
23176 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023177 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023178 if (p_verbose > 0)
23179 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180}
23181
23182/*
23183 * Find a function by name, return pointer to it in ufuncs.
23184 * Return NULL for unknown function.
23185 */
23186 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023187find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023189 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023191 hi = hash_find(&func_hashtab, name);
23192 if (!HASHITEM_EMPTY(hi))
23193 return HI2UF(hi);
23194 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195}
23196
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023197#if defined(EXITFREE) || defined(PROTO)
23198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023199free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023200{
23201 hashitem_T *hi;
23202
23203 /* Need to start all over every time, because func_free() may change the
23204 * hash table. */
23205 while (func_hashtab.ht_used > 0)
23206 for (hi = func_hashtab.ht_array; ; ++hi)
23207 if (!HASHITEM_EMPTY(hi))
23208 {
23209 func_free(HI2UF(hi));
23210 break;
23211 }
23212}
23213#endif
23214
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023216translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023217{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023218 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023219 return find_internal_func(name) >= 0;
23220 return find_func(name) != NULL;
23221}
23222
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023223/*
23224 * Return TRUE if a function "name" exists.
23225 */
23226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023227function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023228{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023229 char_u *nm = name;
23230 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023231 int n = FALSE;
23232
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023233 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23234 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023235 nm = skipwhite(nm);
23236
23237 /* Only accept "funcname", "funcname ", "funcname (..." and
23238 * "funcname(...", not "funcname!...". */
23239 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023240 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023241 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023242 return n;
23243}
23244
Bram Moolenaara1544c02013-05-30 12:35:52 +020023245 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023246get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023247{
23248 char_u *nm = name;
23249 char_u *p;
23250
23251 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23252
23253 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023254 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023255 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023256
Bram Moolenaara1544c02013-05-30 12:35:52 +020023257 vim_free(p);
23258 return NULL;
23259}
23260
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023261/*
23262 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023263 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23264 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023265 */
23266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023267builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023268{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023269 char_u *p;
23270
23271 if (!ASCII_ISLOWER(name[0]))
23272 return FALSE;
23273 p = vim_strchr(name, AUTOLOAD_CHAR);
23274 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023275}
23276
Bram Moolenaar05159a02005-02-26 23:04:13 +000023277#if defined(FEAT_PROFILE) || defined(PROTO)
23278/*
23279 * Start profiling function "fp".
23280 */
23281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023282func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023283{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023284 int len = fp->uf_lines.ga_len;
23285
23286 if (len == 0)
23287 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023288 fp->uf_tm_count = 0;
23289 profile_zero(&fp->uf_tm_self);
23290 profile_zero(&fp->uf_tm_total);
23291 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023292 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023293 if (fp->uf_tml_total == NULL)
23294 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023295 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023296 if (fp->uf_tml_self == NULL)
23297 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023298 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023299 fp->uf_tml_idx = -1;
23300 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23301 || fp->uf_tml_self == NULL)
23302 return; /* out of memory */
23303
23304 fp->uf_profiling = TRUE;
23305}
23306
23307/*
23308 * Dump the profiling results for all functions in file "fd".
23309 */
23310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023311func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023312{
23313 hashitem_T *hi;
23314 int todo;
23315 ufunc_T *fp;
23316 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023317 ufunc_T **sorttab;
23318 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023319
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023320 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023321 if (todo == 0)
23322 return; /* nothing to dump */
23323
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023324 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023325
Bram Moolenaar05159a02005-02-26 23:04:13 +000023326 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23327 {
23328 if (!HASHITEM_EMPTY(hi))
23329 {
23330 --todo;
23331 fp = HI2UF(hi);
23332 if (fp->uf_profiling)
23333 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023334 if (sorttab != NULL)
23335 sorttab[st_len++] = fp;
23336
Bram Moolenaar05159a02005-02-26 23:04:13 +000023337 if (fp->uf_name[0] == K_SPECIAL)
23338 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23339 else
23340 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23341 if (fp->uf_tm_count == 1)
23342 fprintf(fd, "Called 1 time\n");
23343 else
23344 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23345 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23346 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23347 fprintf(fd, "\n");
23348 fprintf(fd, "count total (s) self (s)\n");
23349
23350 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23351 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023352 if (FUNCLINE(fp, i) == NULL)
23353 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023354 prof_func_line(fd, fp->uf_tml_count[i],
23355 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023356 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23357 }
23358 fprintf(fd, "\n");
23359 }
23360 }
23361 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023362
23363 if (sorttab != NULL && st_len > 0)
23364 {
23365 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23366 prof_total_cmp);
23367 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23368 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23369 prof_self_cmp);
23370 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23371 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023372
23373 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023374}
Bram Moolenaar73830342005-02-28 22:48:19 +000023375
23376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023377prof_sort_list(
23378 FILE *fd,
23379 ufunc_T **sorttab,
23380 int st_len,
23381 char *title,
23382 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023383{
23384 int i;
23385 ufunc_T *fp;
23386
23387 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23388 fprintf(fd, "count total (s) self (s) function\n");
23389 for (i = 0; i < 20 && i < st_len; ++i)
23390 {
23391 fp = sorttab[i];
23392 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23393 prefer_self);
23394 if (fp->uf_name[0] == K_SPECIAL)
23395 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23396 else
23397 fprintf(fd, " %s()\n", fp->uf_name);
23398 }
23399 fprintf(fd, "\n");
23400}
23401
23402/*
23403 * Print the count and times for one function or function line.
23404 */
23405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023406prof_func_line(
23407 FILE *fd,
23408 int count,
23409 proftime_T *total,
23410 proftime_T *self,
23411 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023412{
23413 if (count > 0)
23414 {
23415 fprintf(fd, "%5d ", count);
23416 if (prefer_self && profile_equal(total, self))
23417 fprintf(fd, " ");
23418 else
23419 fprintf(fd, "%s ", profile_msg(total));
23420 if (!prefer_self && profile_equal(total, self))
23421 fprintf(fd, " ");
23422 else
23423 fprintf(fd, "%s ", profile_msg(self));
23424 }
23425 else
23426 fprintf(fd, " ");
23427}
23428
23429/*
23430 * Compare function for total time sorting.
23431 */
23432 static int
23433#ifdef __BORLANDC__
23434_RTLENTRYF
23435#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023436prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023437{
23438 ufunc_T *p1, *p2;
23439
23440 p1 = *(ufunc_T **)s1;
23441 p2 = *(ufunc_T **)s2;
23442 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23443}
23444
23445/*
23446 * Compare function for self time sorting.
23447 */
23448 static int
23449#ifdef __BORLANDC__
23450_RTLENTRYF
23451#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023452prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023453{
23454 ufunc_T *p1, *p2;
23455
23456 p1 = *(ufunc_T **)s1;
23457 p2 = *(ufunc_T **)s2;
23458 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23459}
23460
Bram Moolenaar05159a02005-02-26 23:04:13 +000023461#endif
23462
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023463/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023464 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023465 * Return TRUE if a package was loaded.
23466 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023468script_autoload(
23469 char_u *name,
23470 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023471{
23472 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023473 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023474 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023475 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023476
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023477 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023478 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023479 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023480 return FALSE;
23481
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023482 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023483
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023484 /* Find the name in the list of previously loaded package names. Skip
23485 * "autoload/", it's always the same. */
23486 for (i = 0; i < ga_loaded.ga_len; ++i)
23487 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23488 break;
23489 if (!reload && i < ga_loaded.ga_len)
23490 ret = FALSE; /* was loaded already */
23491 else
23492 {
23493 /* Remember the name if it wasn't loaded already. */
23494 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23495 {
23496 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23497 tofree = NULL;
23498 }
23499
23500 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023501 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023502 ret = TRUE;
23503 }
23504
23505 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023506 return ret;
23507}
23508
23509/*
23510 * Return the autoload script name for a function or variable name.
23511 * Returns NULL when out of memory.
23512 */
23513 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023514autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023515{
23516 char_u *p;
23517 char_u *scriptname;
23518
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023519 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023520 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23521 if (scriptname == NULL)
23522 return FALSE;
23523 STRCPY(scriptname, "autoload/");
23524 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023525 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023526 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023527 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023528 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023529 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023530}
23531
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23533
23534/*
23535 * Function given to ExpandGeneric() to obtain the list of user defined
23536 * function names.
23537 */
23538 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023539get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023541 static long_u done;
23542 static hashitem_T *hi;
23543 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544
23545 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547 done = 0;
23548 hi = func_hashtab.ht_array;
23549 }
23550 if (done < func_hashtab.ht_used)
23551 {
23552 if (done++ > 0)
23553 ++hi;
23554 while (HASHITEM_EMPTY(hi))
23555 ++hi;
23556 fp = HI2UF(hi);
23557
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023558 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023559 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023560
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023561 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23562 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563
23564 cat_func_name(IObuff, fp);
23565 if (xp->xp_context != EXPAND_USER_FUNC)
23566 {
23567 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023568 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 STRCAT(IObuff, ")");
23570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 return IObuff;
23572 }
23573 return NULL;
23574}
23575
23576#endif /* FEAT_CMDL_COMPL */
23577
23578/*
23579 * Copy the function name of "fp" to buffer "buf".
23580 * "buf" must be able to hold the function name plus three bytes.
23581 * Takes care of script-local function names.
23582 */
23583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023584cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023586 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 {
23588 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023589 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 }
23591 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023592 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593}
23594
23595/*
23596 * ":delfunction {name}"
23597 */
23598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023599ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023601 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 char_u *p;
23603 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023604 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605
23606 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023607 name = trans_function_name(&p, eap->skip, 0, &fudi);
23608 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023610 {
23611 if (fudi.fd_dict != NULL && !eap->skip)
23612 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 if (!ends_excmd(*skipwhite(p)))
23616 {
23617 vim_free(name);
23618 EMSG(_(e_trailing));
23619 return;
23620 }
23621 eap->nextcmd = check_nextcmd(p);
23622 if (eap->nextcmd != NULL)
23623 *p = NUL;
23624
23625 if (!eap->skip)
23626 fp = find_func(name);
23627 vim_free(name);
23628
23629 if (!eap->skip)
23630 {
23631 if (fp == NULL)
23632 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023633 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 return;
23635 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023636 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 {
23638 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23639 return;
23640 }
23641
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023642 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023644 /* Delete the dict item that refers to the function, it will
23645 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023646 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648 else
23649 func_free(fp);
23650 }
23651}
23652
23653/*
23654 * Free a function and remove it from the list of functions.
23655 */
23656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023657func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023658{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023659 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023660
23661 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023662 ga_clear_strings(&(fp->uf_args));
23663 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023664#ifdef FEAT_PROFILE
23665 vim_free(fp->uf_tml_count);
23666 vim_free(fp->uf_tml_total);
23667 vim_free(fp->uf_tml_self);
23668#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023669
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023670 /* remove the function from the function hashtable */
23671 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23672 if (HASHITEM_EMPTY(hi))
23673 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023674 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023675 hash_remove(&func_hashtab, hi);
23676
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677 vim_free(fp);
23678}
23679
23680/*
23681 * Unreference a Function: decrement the reference count and free it when it
23682 * becomes zero. Only for numbered functions.
23683 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023685func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023686{
23687 ufunc_T *fp;
23688
23689 if (name != NULL && isdigit(*name))
23690 {
23691 fp = find_func(name);
23692 if (fp == NULL)
23693 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023694 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023695 {
23696 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023697 * when "uf_calls" becomes zero. */
23698 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023699 func_free(fp);
23700 }
23701 }
23702}
23703
23704/*
23705 * Count a reference to a Function.
23706 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023708func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023709{
23710 ufunc_T *fp;
23711
23712 if (name != NULL && isdigit(*name))
23713 {
23714 fp = find_func(name);
23715 if (fp == NULL)
23716 EMSG2(_(e_intern2), "func_ref()");
23717 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023718 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 }
23720}
23721
23722/*
23723 * Call a user function.
23724 */
23725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023726call_user_func(
23727 ufunc_T *fp, /* pointer to function */
23728 int argcount, /* nr of args */
23729 typval_T *argvars, /* arguments */
23730 typval_T *rettv, /* return value */
23731 linenr_T firstline, /* first line of range */
23732 linenr_T lastline, /* last line of range */
23733 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734{
Bram Moolenaar33570922005-01-25 22:26:29 +000023735 char_u *save_sourcing_name;
23736 linenr_T save_sourcing_lnum;
23737 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023738 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023739 int save_did_emsg;
23740 static int depth = 0;
23741 dictitem_T *v;
23742 int fixvar_idx = 0; /* index in fixvar[] */
23743 int i;
23744 int ai;
23745 char_u numbuf[NUMBUFLEN];
23746 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023747 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023748#ifdef FEAT_PROFILE
23749 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023750 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752
23753 /* If depth of calling is getting too high, don't execute the function */
23754 if (depth >= p_mfd)
23755 {
23756 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023757 rettv->v_type = VAR_NUMBER;
23758 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 return;
23760 }
23761 ++depth;
23762
23763 line_breakcheck(); /* check for CTRL-C hit */
23764
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023765 fc = (funccall_T *)alloc(sizeof(funccall_T));
23766 fc->caller = current_funccal;
23767 current_funccal = fc;
23768 fc->func = fp;
23769 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023770 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023771 fc->linenr = 0;
23772 fc->returned = FALSE;
23773 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023775 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23776 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777
Bram Moolenaar33570922005-01-25 22:26:29 +000023778 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023779 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023780 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23781 * each argument variable and saves a lot of time.
23782 */
23783 /*
23784 * Init l: variables.
23785 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023786 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023787 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023788 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023789 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23790 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023791 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023792 name = v->di_key;
23793 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023794 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023795 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023796 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023797 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023798 v->di_tv.vval.v_dict = selfdict;
23799 ++selfdict->dv_refcount;
23800 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023801
Bram Moolenaar33570922005-01-25 22:26:29 +000023802 /*
23803 * Init a: variables.
23804 * Set a:0 to "argcount".
23805 * Set a:000 to a list with room for the "..." arguments.
23806 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023807 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023808 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023809 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023810 /* Use "name" to avoid a warning from some compiler that checks the
23811 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023812 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023813 name = v->di_key;
23814 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023815 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023816 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023817 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023818 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023819 v->di_tv.vval.v_list = &fc->l_varlist;
23820 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23821 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23822 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023823
23824 /*
23825 * Set a:firstline to "firstline" and a:lastline to "lastline".
23826 * Set a:name to named arguments.
23827 * Set a:N to the "..." arguments.
23828 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023829 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023830 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023831 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023832 (varnumber_T)lastline);
23833 for (i = 0; i < argcount; ++i)
23834 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023835 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023836 if (ai < 0)
23837 /* named argument a:name */
23838 name = FUNCARG(fp, i);
23839 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023840 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023841 /* "..." argument a:1, a:2, etc. */
23842 sprintf((char *)numbuf, "%d", ai + 1);
23843 name = numbuf;
23844 }
23845 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23846 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023847 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023848 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23849 }
23850 else
23851 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023852 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23853 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023854 if (v == NULL)
23855 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023856 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023857 }
23858 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023859 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023860
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023861 /* Note: the values are copied directly to avoid alloc/free.
23862 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023864 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023865
23866 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23867 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023868 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23869 fc->l_listitems[ai].li_tv = argvars[i];
23870 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023871 }
23872 }
23873
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 /* Don't redraw while executing the function. */
23875 ++RedrawingDisabled;
23876 save_sourcing_name = sourcing_name;
23877 save_sourcing_lnum = sourcing_lnum;
23878 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023879 /* need space for function name + ("function " + 3) or "[number]" */
23880 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23881 + STRLEN(fp->uf_name) + 20;
23882 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 if (sourcing_name != NULL)
23884 {
23885 if (save_sourcing_name != NULL
23886 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023887 sprintf((char *)sourcing_name, "%s[%d]..",
23888 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 else
23890 STRCPY(sourcing_name, "function ");
23891 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23892
23893 if (p_verbose >= 12)
23894 {
23895 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023896 verbose_enter_scroll();
23897
Bram Moolenaar555b2802005-05-19 21:08:39 +000023898 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 if (p_verbose >= 14)
23900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023902 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023903 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023904 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905
23906 msg_puts((char_u *)"(");
23907 for (i = 0; i < argcount; ++i)
23908 {
23909 if (i > 0)
23910 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023911 if (argvars[i].v_type == VAR_NUMBER)
23912 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 else
23914 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023915 /* Do not want errors such as E724 here. */
23916 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023917 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023918 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023919 if (s != NULL)
23920 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023921 if (vim_strsize(s) > MSG_BUF_CLEN)
23922 {
23923 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23924 s = buf;
23925 }
23926 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023927 vim_free(tofree);
23928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 }
23930 }
23931 msg_puts((char_u *)")");
23932 }
23933 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023934
23935 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 --no_wait_return;
23937 }
23938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023939#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023940 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023941 {
23942 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23943 func_do_profile(fp);
23944 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023945 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023946 {
23947 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023948 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023949 profile_zero(&fp->uf_tm_children);
23950 }
23951 script_prof_save(&wait_start);
23952 }
23953#endif
23954
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023956 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 save_did_emsg = did_emsg;
23958 did_emsg = FALSE;
23959
23960 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023961 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23963
23964 --RedrawingDisabled;
23965
23966 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023967 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023969 clear_tv(rettv);
23970 rettv->v_type = VAR_NUMBER;
23971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 }
23973
Bram Moolenaar05159a02005-02-26 23:04:13 +000023974#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023975 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023976 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023977 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023978 profile_end(&call_start);
23979 profile_sub_wait(&wait_start, &call_start);
23980 profile_add(&fp->uf_tm_total, &call_start);
23981 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023982 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023983 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023984 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23985 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023986 }
23987 }
23988#endif
23989
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 /* when being verbose, mention the return value */
23991 if (p_verbose >= 12)
23992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023994 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023997 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023998 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023999 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024000 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024003 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024004 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024005 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024006 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024007
Bram Moolenaar555b2802005-05-19 21:08:39 +000024008 /* The value may be very long. Skip the middle part, so that we
24009 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024010 * truncate it at the end. Don't want errors such as E724 here. */
24011 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024012 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024013 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024014 if (s != NULL)
24015 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024016 if (vim_strsize(s) > MSG_BUF_CLEN)
24017 {
24018 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24019 s = buf;
24020 }
24021 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024022 vim_free(tofree);
24023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 }
24025 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024026
24027 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 --no_wait_return;
24029 }
24030
24031 vim_free(sourcing_name);
24032 sourcing_name = save_sourcing_name;
24033 sourcing_lnum = save_sourcing_lnum;
24034 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024035#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024036 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024037 script_prof_restore(&wait_start);
24038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039
24040 if (p_verbose >= 12 && sourcing_name != NULL)
24041 {
24042 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024043 verbose_enter_scroll();
24044
Bram Moolenaar555b2802005-05-19 21:08:39 +000024045 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024047
24048 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 --no_wait_return;
24050 }
24051
24052 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024053 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024055
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024056 /* If the a:000 list and the l: and a: dicts are not referenced we can
24057 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024058 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24059 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24060 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24061 {
24062 free_funccal(fc, FALSE);
24063 }
24064 else
24065 {
24066 hashitem_T *hi;
24067 listitem_T *li;
24068 int todo;
24069
24070 /* "fc" is still in use. This can happen when returning "a:000" or
24071 * assigning "l:" to a global variable.
24072 * Link "fc" in the list for garbage collection later. */
24073 fc->caller = previous_funccal;
24074 previous_funccal = fc;
24075
24076 /* Make a copy of the a: variables, since we didn't do that above. */
24077 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24078 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24079 {
24080 if (!HASHITEM_EMPTY(hi))
24081 {
24082 --todo;
24083 v = HI2DI(hi);
24084 copy_tv(&v->di_tv, &v->di_tv);
24085 }
24086 }
24087
24088 /* Make a copy of the a:000 items, since we didn't do that above. */
24089 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24090 copy_tv(&li->li_tv, &li->li_tv);
24091 }
24092}
24093
24094/*
24095 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024096 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024097 */
24098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024099can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024100{
24101 return (fc->l_varlist.lv_copyID != copyID
24102 && fc->l_vars.dv_copyID != copyID
24103 && fc->l_avars.dv_copyID != copyID);
24104}
24105
24106/*
24107 * Free "fc" and what it contains.
24108 */
24109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024110free_funccal(
24111 funccall_T *fc,
24112 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024113{
24114 listitem_T *li;
24115
24116 /* The a: variables typevals may not have been allocated, only free the
24117 * allocated variables. */
24118 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24119
24120 /* free all l: variables */
24121 vars_clear(&fc->l_vars.dv_hashtab);
24122
24123 /* Free the a:000 variables if they were allocated. */
24124 if (free_val)
24125 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24126 clear_tv(&li->li_tv);
24127
24128 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129}
24130
24131/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024132 * Add a number variable "name" to dict "dp" with value "nr".
24133 */
24134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024135add_nr_var(
24136 dict_T *dp,
24137 dictitem_T *v,
24138 char *name,
24139 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024140{
24141 STRCPY(v->di_key, name);
24142 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24143 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24144 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024145 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024146 v->di_tv.vval.v_number = nr;
24147}
24148
24149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 * ":return [expr]"
24151 */
24152 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024153ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154{
24155 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024156 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 int returning = FALSE;
24158
24159 if (current_funccal == NULL)
24160 {
24161 EMSG(_("E133: :return not inside a function"));
24162 return;
24163 }
24164
24165 if (eap->skip)
24166 ++emsg_skip;
24167
24168 eap->nextcmd = NULL;
24169 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024170 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 {
24172 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024173 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024175 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 }
24177 /* It's safer to return also on error. */
24178 else if (!eap->skip)
24179 {
24180 /*
24181 * Return unless the expression evaluation has been cancelled due to an
24182 * aborting error, an interrupt, or an exception.
24183 */
24184 if (!aborting())
24185 returning = do_return(eap, FALSE, TRUE, NULL);
24186 }
24187
24188 /* When skipping or the return gets pending, advance to the next command
24189 * in this line (!returning). Otherwise, ignore the rest of the line.
24190 * Following lines will be ignored by get_func_line(). */
24191 if (returning)
24192 eap->nextcmd = NULL;
24193 else if (eap->nextcmd == NULL) /* no argument */
24194 eap->nextcmd = check_nextcmd(arg);
24195
24196 if (eap->skip)
24197 --emsg_skip;
24198}
24199
24200/*
24201 * Return from a function. Possibly makes the return pending. Also called
24202 * for a pending return at the ":endtry" or after returning from an extra
24203 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024204 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024205 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 * FALSE when the return gets pending.
24207 */
24208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024209do_return(
24210 exarg_T *eap,
24211 int reanimate,
24212 int is_cmd,
24213 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214{
24215 int idx;
24216 struct condstack *cstack = eap->cstack;
24217
24218 if (reanimate)
24219 /* Undo the return. */
24220 current_funccal->returned = FALSE;
24221
24222 /*
24223 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24224 * not in its finally clause (which then is to be executed next) is found.
24225 * In this case, make the ":return" pending for execution at the ":endtry".
24226 * Otherwise, return normally.
24227 */
24228 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24229 if (idx >= 0)
24230 {
24231 cstack->cs_pending[idx] = CSTP_RETURN;
24232
24233 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024234 /* A pending return again gets pending. "rettv" points to an
24235 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024237 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 else
24239 {
24240 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024241 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024243 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024245 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 {
24247 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024248 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024249 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250 else
24251 EMSG(_(e_outofmem));
24252 }
24253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024254 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255
24256 if (reanimate)
24257 {
24258 /* The pending return value could be overwritten by a ":return"
24259 * without argument in a finally clause; reset the default
24260 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024261 current_funccal->rettv->v_type = VAR_NUMBER;
24262 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 }
24264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024265 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 }
24267 else
24268 {
24269 current_funccal->returned = TRUE;
24270
24271 /* If the return is carried out now, store the return value. For
24272 * a return immediately after reanimation, the value is already
24273 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024274 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024276 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024277 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024279 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280 }
24281 }
24282
24283 return idx < 0;
24284}
24285
24286/*
24287 * Free the variable with a pending return value.
24288 */
24289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024290discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291{
Bram Moolenaar33570922005-01-25 22:26:29 +000024292 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293}
24294
24295/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024296 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 * is an allocated string. Used by report_pending() for verbose messages.
24298 */
24299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024300get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024302 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024303 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024304 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024306 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024307 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024308 if (s == NULL)
24309 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024310
24311 STRCPY(IObuff, ":return ");
24312 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24313 if (STRLEN(s) + 8 >= IOSIZE)
24314 STRCPY(IObuff + IOSIZE - 4, "...");
24315 vim_free(tofree);
24316 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317}
24318
24319/*
24320 * Get next function line.
24321 * Called by do_cmdline() to get the next line.
24322 * Returns allocated string, or NULL for end of function.
24323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024325get_func_line(
24326 int c UNUSED,
24327 void *cookie,
24328 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329{
Bram Moolenaar33570922005-01-25 22:26:29 +000024330 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024331 ufunc_T *fp = fcp->func;
24332 char_u *retval;
24333 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334
24335 /* If breakpoints have been added/deleted need to check for it. */
24336 if (fcp->dbg_tick != debug_tick)
24337 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024338 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 sourcing_lnum);
24340 fcp->dbg_tick = debug_tick;
24341 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024342#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024343 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024344 func_line_end(cookie);
24345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346
Bram Moolenaar05159a02005-02-26 23:04:13 +000024347 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024348 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24349 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 retval = NULL;
24351 else
24352 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024353 /* Skip NULL lines (continuation lines). */
24354 while (fcp->linenr < gap->ga_len
24355 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24356 ++fcp->linenr;
24357 if (fcp->linenr >= gap->ga_len)
24358 retval = NULL;
24359 else
24360 {
24361 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24362 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024363#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024364 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024365 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024366#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368 }
24369
24370 /* Did we encounter a breakpoint? */
24371 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24372 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024373 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024375 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 sourcing_lnum);
24377 fcp->dbg_tick = debug_tick;
24378 }
24379
24380 return retval;
24381}
24382
Bram Moolenaar05159a02005-02-26 23:04:13 +000024383#if defined(FEAT_PROFILE) || defined(PROTO)
24384/*
24385 * Called when starting to read a function line.
24386 * "sourcing_lnum" must be correct!
24387 * When skipping lines it may not actually be executed, but we won't find out
24388 * until later and we need to store the time now.
24389 */
24390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024391func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024392{
24393 funccall_T *fcp = (funccall_T *)cookie;
24394 ufunc_T *fp = fcp->func;
24395
24396 if (fp->uf_profiling && sourcing_lnum >= 1
24397 && sourcing_lnum <= fp->uf_lines.ga_len)
24398 {
24399 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024400 /* Skip continuation lines. */
24401 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24402 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024403 fp->uf_tml_execed = FALSE;
24404 profile_start(&fp->uf_tml_start);
24405 profile_zero(&fp->uf_tml_children);
24406 profile_get_wait(&fp->uf_tml_wait);
24407 }
24408}
24409
24410/*
24411 * Called when actually executing a function line.
24412 */
24413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024414func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024415{
24416 funccall_T *fcp = (funccall_T *)cookie;
24417 ufunc_T *fp = fcp->func;
24418
24419 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24420 fp->uf_tml_execed = TRUE;
24421}
24422
24423/*
24424 * Called when done with a function line.
24425 */
24426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024427func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024428{
24429 funccall_T *fcp = (funccall_T *)cookie;
24430 ufunc_T *fp = fcp->func;
24431
24432 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24433 {
24434 if (fp->uf_tml_execed)
24435 {
24436 ++fp->uf_tml_count[fp->uf_tml_idx];
24437 profile_end(&fp->uf_tml_start);
24438 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024439 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024440 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24441 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024442 }
24443 fp->uf_tml_idx = -1;
24444 }
24445}
24446#endif
24447
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448/*
24449 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024450 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024451 */
24452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024453func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454{
Bram Moolenaar33570922005-01-25 22:26:29 +000024455 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024456
24457 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24458 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024459 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 || fcp->returned);
24461}
24462
24463/*
24464 * return TRUE if cookie indicates a function which "abort"s on errors.
24465 */
24466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024467func_has_abort(
24468 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024470 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471}
24472
24473#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24474typedef enum
24475{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024476 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24477 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24478 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479} var_flavour_T;
24480
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024481static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482
24483 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024484var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485{
24486 char_u *p = varname;
24487
24488 if (ASCII_ISUPPER(*p))
24489 {
24490 while (*(++p))
24491 if (ASCII_ISLOWER(*p))
24492 return VAR_FLAVOUR_SESSION;
24493 return VAR_FLAVOUR_VIMINFO;
24494 }
24495 else
24496 return VAR_FLAVOUR_DEFAULT;
24497}
24498#endif
24499
24500#if defined(FEAT_VIMINFO) || defined(PROTO)
24501/*
24502 * Restore global vars that start with a capital from the viminfo file
24503 */
24504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024505read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506{
24507 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024508 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024509 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024510 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511
24512 if (!writing && (find_viminfo_parameter('!') != NULL))
24513 {
24514 tab = vim_strchr(virp->vir_line + 1, '\t');
24515 if (tab != NULL)
24516 {
24517 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024518 switch (*tab)
24519 {
24520 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024521#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024522 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024523#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024524 case 'D': type = VAR_DICT; break;
24525 case 'L': type = VAR_LIST; break;
24526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527
24528 tab = vim_strchr(tab, '\t');
24529 if (tab != NULL)
24530 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024531 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024532 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024533 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024535#ifdef FEAT_FLOAT
24536 else if (type == VAR_FLOAT)
24537 (void)string2float(tab + 1, &tv.vval.v_float);
24538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024540 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024541 if (type == VAR_DICT || type == VAR_LIST)
24542 {
24543 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24544
24545 if (etv == NULL)
24546 /* Failed to parse back the dict or list, use it as a
24547 * string. */
24548 tv.v_type = VAR_STRING;
24549 else
24550 {
24551 vim_free(tv.vval.v_string);
24552 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024553 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024554 }
24555 }
24556
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024557 /* when in a function use global variables */
24558 save_funccal = current_funccal;
24559 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024560 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024561 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024562
24563 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024564 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024565 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24566 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 }
24568 }
24569 }
24570
24571 return viminfo_readline(virp);
24572}
24573
24574/*
24575 * Write global vars that start with a capital to the viminfo file
24576 */
24577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024578write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579{
Bram Moolenaar33570922005-01-25 22:26:29 +000024580 hashitem_T *hi;
24581 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024582 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024583 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024584 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024585 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024586 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587
24588 if (find_viminfo_parameter('!') == NULL)
24589 return;
24590
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024591 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024592
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024593 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024594 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024596 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024598 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024599 this_var = HI2DI(hi);
24600 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024601 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024602 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024603 {
24604 case VAR_STRING: s = "STR"; break;
24605 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024606#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024607 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024608#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024609 case VAR_DICT: s = "DIC"; break;
24610 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024611 default: continue;
24612 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024613 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024614 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024615 if (p != NULL)
24616 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024617 vim_free(tofree);
24618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619 }
24620 }
24621}
24622#endif
24623
24624#if defined(FEAT_SESSION) || defined(PROTO)
24625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024626store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627{
Bram Moolenaar33570922005-01-25 22:26:29 +000024628 hashitem_T *hi;
24629 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024630 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 char_u *p, *t;
24632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024633 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024634 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024638 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024639 this_var = HI2DI(hi);
24640 if ((this_var->di_tv.v_type == VAR_NUMBER
24641 || this_var->di_tv.v_type == VAR_STRING)
24642 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024643 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024644 /* Escape special characters with a backslash. Turn a LF and
24645 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024646 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024647 (char_u *)"\\\"\n\r");
24648 if (p == NULL) /* out of memory */
24649 break;
24650 for (t = p; *t != NUL; ++t)
24651 if (*t == '\n')
24652 *t = 'n';
24653 else if (*t == '\r')
24654 *t = 'r';
24655 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024656 this_var->di_key,
24657 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24658 : ' ',
24659 p,
24660 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24661 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024662 || put_eol(fd) == FAIL)
24663 {
24664 vim_free(p);
24665 return FAIL;
24666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 vim_free(p);
24668 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024669#ifdef FEAT_FLOAT
24670 else if (this_var->di_tv.v_type == VAR_FLOAT
24671 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24672 {
24673 float_T f = this_var->di_tv.vval.v_float;
24674 int sign = ' ';
24675
24676 if (f < 0)
24677 {
24678 f = -f;
24679 sign = '-';
24680 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024681 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024682 this_var->di_key, sign, f) < 0)
24683 || put_eol(fd) == FAIL)
24684 return FAIL;
24685 }
24686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687 }
24688 }
24689 return OK;
24690}
24691#endif
24692
Bram Moolenaar661b1822005-07-28 22:36:45 +000024693/*
24694 * Display script name where an item was last set.
24695 * Should only be invoked when 'verbose' is non-zero.
24696 */
24697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024698last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024699{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024700 char_u *p;
24701
Bram Moolenaar661b1822005-07-28 22:36:45 +000024702 if (scriptID != 0)
24703 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024704 p = home_replace_save(NULL, get_scriptname(scriptID));
24705 if (p != NULL)
24706 {
24707 verbose_enter();
24708 MSG_PUTS(_("\n\tLast set from "));
24709 MSG_PUTS(p);
24710 vim_free(p);
24711 verbose_leave();
24712 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024713 }
24714}
24715
Bram Moolenaard812df62008-11-09 12:46:09 +000024716/*
24717 * List v:oldfiles in a nice way.
24718 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024720ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024721{
24722 list_T *l = vimvars[VV_OLDFILES].vv_list;
24723 listitem_T *li;
24724 int nr = 0;
24725
24726 if (l == NULL)
24727 msg((char_u *)_("No old files"));
24728 else
24729 {
24730 msg_start();
24731 msg_scroll = TRUE;
24732 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24733 {
24734 msg_outnum((long)++nr);
24735 MSG_PUTS(": ");
24736 msg_outtrans(get_tv_string(&li->li_tv));
24737 msg_putchar('\n');
24738 out_flush(); /* output one line at a time */
24739 ui_breakcheck();
24740 }
24741 /* Assume "got_int" was set to truncate the listing. */
24742 got_int = FALSE;
24743
24744#ifdef FEAT_BROWSE_CMD
24745 if (cmdmod.browse)
24746 {
24747 quit_more = FALSE;
24748 nr = prompt_for_number(FALSE);
24749 msg_starthere();
24750 if (nr > 0)
24751 {
24752 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24753 (long)nr);
24754
24755 if (p != NULL)
24756 {
24757 p = expand_env_save(p);
24758 eap->arg = p;
24759 eap->cmdidx = CMD_edit;
24760 cmdmod.browse = FALSE;
24761 do_exedit(eap, NULL);
24762 vim_free(p);
24763 }
24764 }
24765 }
24766#endif
24767 }
24768}
24769
Bram Moolenaar53744302015-07-17 17:38:22 +020024770/* reset v:option_new, v:option_old and v:option_type */
24771 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024772reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024773{
24774 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24775 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24776 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24777}
24778
24779
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780#endif /* FEAT_EVAL */
24781
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024783#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784
24785#ifdef WIN3264
24786/*
24787 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24788 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024789static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24790static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24791static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792
24793/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024794 * Get the short path (8.3) for the filename in "fnamep".
24795 * Only works for a valid file name.
24796 * When the path gets longer "fnamep" is changed and the allocated buffer
24797 * is put in "bufp".
24798 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24799 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 */
24801 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024802get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024804 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024805 char_u *newbuf;
24806
24807 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808 l = GetShortPathName(*fnamep, *fnamep, len);
24809 if (l > len - 1)
24810 {
24811 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024812 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 newbuf = vim_strnsave(*fnamep, l);
24814 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816
24817 vim_free(*bufp);
24818 *fnamep = *bufp = newbuf;
24819
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024820 /* Really should always succeed, as the buffer is big enough. */
24821 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822 }
24823
24824 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024825 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826}
24827
24828/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024829 * Get the short path (8.3) for the filename in "fname". The converted
24830 * path is returned in "bufp".
24831 *
24832 * Some of the directories specified in "fname" may not exist. This function
24833 * will shorten the existing directories at the beginning of the path and then
24834 * append the remaining non-existing path.
24835 *
24836 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024837 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024838 * bufp - Pointer to an allocated buffer for the filename.
24839 * fnamelen - Length of the filename pointed to by fname
24840 *
24841 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842 */
24843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024844shortpath_for_invalid_fname(
24845 char_u **fname,
24846 char_u **bufp,
24847 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024849 char_u *short_fname, *save_fname, *pbuf_unused;
24850 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024852 int old_len, len;
24853 int new_len, sfx_len;
24854 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855
24856 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024857 old_len = *fnamelen;
24858 save_fname = vim_strnsave(*fname, old_len);
24859 pbuf_unused = NULL;
24860 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024862 endp = save_fname + old_len - 1; /* Find the end of the copy */
24863 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024865 /*
24866 * Try shortening the supplied path till it succeeds by removing one
24867 * directory at a time from the tail of the path.
24868 */
24869 len = 0;
24870 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024872 /* go back one path-separator */
24873 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24874 --endp;
24875 if (endp <= save_fname)
24876 break; /* processed the complete path */
24877
24878 /*
24879 * Replace the path separator with a NUL and try to shorten the
24880 * resulting path.
24881 */
24882 ch = *endp;
24883 *endp = 0;
24884 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024885 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024886 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24887 {
24888 retval = FAIL;
24889 goto theend;
24890 }
24891 *endp = ch; /* preserve the string */
24892
24893 if (len > 0)
24894 break; /* successfully shortened the path */
24895
24896 /* failed to shorten the path. Skip the path separator */
24897 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898 }
24899
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024900 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024902 /*
24903 * Succeeded in shortening the path. Now concatenate the shortened
24904 * path with the remaining path at the tail.
24905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024907 /* Compute the length of the new path. */
24908 sfx_len = (int)(save_endp - endp) + 1;
24909 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024911 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024913 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024915 /* There is not enough space in the currently allocated string,
24916 * copy it to a buffer big enough. */
24917 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024919 {
24920 retval = FAIL;
24921 goto theend;
24922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 }
24924 else
24925 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024926 /* Transfer short_fname to the main buffer (it's big enough),
24927 * unless get_short_pathname() did its work in-place. */
24928 *fname = *bufp = save_fname;
24929 if (short_fname != save_fname)
24930 vim_strncpy(save_fname, short_fname, len);
24931 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024933
24934 /* concat the not-shortened part of the path */
24935 vim_strncpy(*fname + len, endp, sfx_len);
24936 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024938
24939theend:
24940 vim_free(pbuf_unused);
24941 vim_free(save_fname);
24942
24943 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944}
24945
24946/*
24947 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024948 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949 */
24950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024951shortpath_for_partial(
24952 char_u **fnamep,
24953 char_u **bufp,
24954 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955{
24956 int sepcount, len, tflen;
24957 char_u *p;
24958 char_u *pbuf, *tfname;
24959 int hasTilde;
24960
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024961 /* Count up the path separators from the RHS.. so we know which part
24962 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024964 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965 if (vim_ispathsep(*p))
24966 ++sepcount;
24967
24968 /* Need full path first (use expand_env() to remove a "~/") */
24969 hasTilde = (**fnamep == '~');
24970 if (hasTilde)
24971 pbuf = tfname = expand_env_save(*fnamep);
24972 else
24973 pbuf = tfname = FullName_save(*fnamep, FALSE);
24974
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024975 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024977 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24978 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979
24980 if (len == 0)
24981 {
24982 /* Don't have a valid filename, so shorten the rest of the
24983 * path if we can. This CAN give us invalid 8.3 filenames, but
24984 * there's not a lot of point in guessing what it might be.
24985 */
24986 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024987 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24988 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024989 }
24990
24991 /* Count the paths backward to find the beginning of the desired string. */
24992 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024993 {
24994#ifdef FEAT_MBYTE
24995 if (has_mbyte)
24996 p -= mb_head_off(tfname, p);
24997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024998 if (vim_ispathsep(*p))
24999 {
25000 if (sepcount == 0 || (hasTilde && sepcount == 1))
25001 break;
25002 else
25003 sepcount --;
25004 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 if (hasTilde)
25007 {
25008 --p;
25009 if (p >= tfname)
25010 *p = '~';
25011 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025012 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 }
25014 else
25015 ++p;
25016
25017 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25018 vim_free(*bufp);
25019 *fnamelen = (int)STRLEN(p);
25020 *bufp = pbuf;
25021 *fnamep = p;
25022
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025023 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024}
25025#endif /* WIN3264 */
25026
25027/*
25028 * Adjust a filename, according to a string of modifiers.
25029 * *fnamep must be NUL terminated when called. When returning, the length is
25030 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025031 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 * When there is an error, *fnamep is set to NULL.
25033 */
25034 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025035modify_fname(
25036 char_u *src, /* string with modifiers */
25037 int *usedlen, /* characters after src that are used */
25038 char_u **fnamep, /* file name so far */
25039 char_u **bufp, /* buffer for allocated file name or NULL */
25040 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041{
25042 int valid = 0;
25043 char_u *tail;
25044 char_u *s, *p, *pbuf;
25045 char_u dirname[MAXPATHL];
25046 int c;
25047 int has_fullname = 0;
25048#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025049 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050 int has_shortname = 0;
25051#endif
25052
25053repeat:
25054 /* ":p" - full path/file_name */
25055 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25056 {
25057 has_fullname = 1;
25058
25059 valid |= VALID_PATH;
25060 *usedlen += 2;
25061
25062 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25063 if ((*fnamep)[0] == '~'
25064#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25065 && ((*fnamep)[1] == '/'
25066# ifdef BACKSLASH_IN_FILENAME
25067 || (*fnamep)[1] == '\\'
25068# endif
25069 || (*fnamep)[1] == NUL)
25070
25071#endif
25072 )
25073 {
25074 *fnamep = expand_env_save(*fnamep);
25075 vim_free(*bufp); /* free any allocated file name */
25076 *bufp = *fnamep;
25077 if (*fnamep == NULL)
25078 return -1;
25079 }
25080
25081 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025082 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083 {
25084 if (vim_ispathsep(*p)
25085 && p[1] == '.'
25086 && (p[2] == NUL
25087 || vim_ispathsep(p[2])
25088 || (p[2] == '.'
25089 && (p[3] == NUL || vim_ispathsep(p[3])))))
25090 break;
25091 }
25092
25093 /* FullName_save() is slow, don't use it when not needed. */
25094 if (*p != NUL || !vim_isAbsName(*fnamep))
25095 {
25096 *fnamep = FullName_save(*fnamep, *p != NUL);
25097 vim_free(*bufp); /* free any allocated file name */
25098 *bufp = *fnamep;
25099 if (*fnamep == NULL)
25100 return -1;
25101 }
25102
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025103#ifdef WIN3264
25104# if _WIN32_WINNT >= 0x0500
25105 if (vim_strchr(*fnamep, '~') != NULL)
25106 {
25107 /* Expand 8.3 filename to full path. Needed to make sure the same
25108 * file does not have two different names.
25109 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25110 p = alloc(_MAX_PATH + 1);
25111 if (p != NULL)
25112 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025113 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025114 {
25115 vim_free(*bufp);
25116 *bufp = *fnamep = p;
25117 }
25118 else
25119 vim_free(p);
25120 }
25121 }
25122# endif
25123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 /* Append a path separator to a directory. */
25125 if (mch_isdir(*fnamep))
25126 {
25127 /* Make room for one or two extra characters. */
25128 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25129 vim_free(*bufp); /* free any allocated file name */
25130 *bufp = *fnamep;
25131 if (*fnamep == NULL)
25132 return -1;
25133 add_pathsep(*fnamep);
25134 }
25135 }
25136
25137 /* ":." - path relative to the current directory */
25138 /* ":~" - path relative to the home directory */
25139 /* ":8" - shortname path - postponed till after */
25140 while (src[*usedlen] == ':'
25141 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25142 {
25143 *usedlen += 2;
25144 if (c == '8')
25145 {
25146#ifdef WIN3264
25147 has_shortname = 1; /* Postpone this. */
25148#endif
25149 continue;
25150 }
25151 pbuf = NULL;
25152 /* Need full path first (use expand_env() to remove a "~/") */
25153 if (!has_fullname)
25154 {
25155 if (c == '.' && **fnamep == '~')
25156 p = pbuf = expand_env_save(*fnamep);
25157 else
25158 p = pbuf = FullName_save(*fnamep, FALSE);
25159 }
25160 else
25161 p = *fnamep;
25162
25163 has_fullname = 0;
25164
25165 if (p != NULL)
25166 {
25167 if (c == '.')
25168 {
25169 mch_dirname(dirname, MAXPATHL);
25170 s = shorten_fname(p, dirname);
25171 if (s != NULL)
25172 {
25173 *fnamep = s;
25174 if (pbuf != NULL)
25175 {
25176 vim_free(*bufp); /* free any allocated file name */
25177 *bufp = pbuf;
25178 pbuf = NULL;
25179 }
25180 }
25181 }
25182 else
25183 {
25184 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25185 /* Only replace it when it starts with '~' */
25186 if (*dirname == '~')
25187 {
25188 s = vim_strsave(dirname);
25189 if (s != NULL)
25190 {
25191 *fnamep = s;
25192 vim_free(*bufp);
25193 *bufp = s;
25194 }
25195 }
25196 }
25197 vim_free(pbuf);
25198 }
25199 }
25200
25201 tail = gettail(*fnamep);
25202 *fnamelen = (int)STRLEN(*fnamep);
25203
25204 /* ":h" - head, remove "/file_name", can be repeated */
25205 /* Don't remove the first "/" or "c:\" */
25206 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25207 {
25208 valid |= VALID_HEAD;
25209 *usedlen += 2;
25210 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025211 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025212 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 *fnamelen = (int)(tail - *fnamep);
25214#ifdef VMS
25215 if (*fnamelen > 0)
25216 *fnamelen += 1; /* the path separator is part of the path */
25217#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025218 if (*fnamelen == 0)
25219 {
25220 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25221 p = vim_strsave((char_u *)".");
25222 if (p == NULL)
25223 return -1;
25224 vim_free(*bufp);
25225 *bufp = *fnamep = tail = p;
25226 *fnamelen = 1;
25227 }
25228 else
25229 {
25230 while (tail > s && !after_pathsep(s, tail))
25231 mb_ptr_back(*fnamep, tail);
25232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 }
25234
25235 /* ":8" - shortname */
25236 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25237 {
25238 *usedlen += 2;
25239#ifdef WIN3264
25240 has_shortname = 1;
25241#endif
25242 }
25243
25244#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025245 /*
25246 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 */
25248 if (has_shortname)
25249 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025250 /* Copy the string if it is shortened by :h and when it wasn't copied
25251 * yet, because we are going to change it in place. Avoids changing
25252 * the buffer name for "%:8". */
25253 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254 {
25255 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025256 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257 return -1;
25258 vim_free(*bufp);
25259 *bufp = *fnamep = p;
25260 }
25261
25262 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025263 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 if (!has_fullname && !vim_isAbsName(*fnamep))
25265 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025266 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 return -1;
25268 }
25269 else
25270 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025271 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272
Bram Moolenaardc935552011-08-17 15:23:23 +020025273 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025275 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 return -1;
25277
25278 if (l == 0)
25279 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025280 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025282 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283 return -1;
25284 }
25285 *fnamelen = l;
25286 }
25287 }
25288#endif /* WIN3264 */
25289
25290 /* ":t" - tail, just the basename */
25291 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25292 {
25293 *usedlen += 2;
25294 *fnamelen -= (int)(tail - *fnamep);
25295 *fnamep = tail;
25296 }
25297
25298 /* ":e" - extension, can be repeated */
25299 /* ":r" - root, without extension, can be repeated */
25300 while (src[*usedlen] == ':'
25301 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25302 {
25303 /* find a '.' in the tail:
25304 * - for second :e: before the current fname
25305 * - otherwise: The last '.'
25306 */
25307 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25308 s = *fnamep - 2;
25309 else
25310 s = *fnamep + *fnamelen - 1;
25311 for ( ; s > tail; --s)
25312 if (s[0] == '.')
25313 break;
25314 if (src[*usedlen + 1] == 'e') /* :e */
25315 {
25316 if (s > tail)
25317 {
25318 *fnamelen += (int)(*fnamep - (s + 1));
25319 *fnamep = s + 1;
25320#ifdef VMS
25321 /* cut version from the extension */
25322 s = *fnamep + *fnamelen - 1;
25323 for ( ; s > *fnamep; --s)
25324 if (s[0] == ';')
25325 break;
25326 if (s > *fnamep)
25327 *fnamelen = s - *fnamep;
25328#endif
25329 }
25330 else if (*fnamep <= tail)
25331 *fnamelen = 0;
25332 }
25333 else /* :r */
25334 {
25335 if (s > tail) /* remove one extension */
25336 *fnamelen = (int)(s - *fnamep);
25337 }
25338 *usedlen += 2;
25339 }
25340
25341 /* ":s?pat?foo?" - substitute */
25342 /* ":gs?pat?foo?" - global substitute */
25343 if (src[*usedlen] == ':'
25344 && (src[*usedlen + 1] == 's'
25345 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25346 {
25347 char_u *str;
25348 char_u *pat;
25349 char_u *sub;
25350 int sep;
25351 char_u *flags;
25352 int didit = FALSE;
25353
25354 flags = (char_u *)"";
25355 s = src + *usedlen + 2;
25356 if (src[*usedlen + 1] == 'g')
25357 {
25358 flags = (char_u *)"g";
25359 ++s;
25360 }
25361
25362 sep = *s++;
25363 if (sep)
25364 {
25365 /* find end of pattern */
25366 p = vim_strchr(s, sep);
25367 if (p != NULL)
25368 {
25369 pat = vim_strnsave(s, (int)(p - s));
25370 if (pat != NULL)
25371 {
25372 s = p + 1;
25373 /* find end of substitution */
25374 p = vim_strchr(s, sep);
25375 if (p != NULL)
25376 {
25377 sub = vim_strnsave(s, (int)(p - s));
25378 str = vim_strnsave(*fnamep, *fnamelen);
25379 if (sub != NULL && str != NULL)
25380 {
25381 *usedlen = (int)(p + 1 - src);
25382 s = do_string_sub(str, pat, sub, flags);
25383 if (s != NULL)
25384 {
25385 *fnamep = s;
25386 *fnamelen = (int)STRLEN(s);
25387 vim_free(*bufp);
25388 *bufp = s;
25389 didit = TRUE;
25390 }
25391 }
25392 vim_free(sub);
25393 vim_free(str);
25394 }
25395 vim_free(pat);
25396 }
25397 }
25398 /* after using ":s", repeat all the modifiers */
25399 if (didit)
25400 goto repeat;
25401 }
25402 }
25403
Bram Moolenaar26df0922014-02-23 23:39:13 +010025404 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25405 {
25406 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25407 if (p == NULL)
25408 return -1;
25409 vim_free(*bufp);
25410 *bufp = *fnamep = p;
25411 *fnamelen = (int)STRLEN(p);
25412 *usedlen += 2;
25413 }
25414
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415 return valid;
25416}
25417
25418/*
25419 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25420 * "flags" can be "g" to do a global substitute.
25421 * Returns an allocated string, NULL for error.
25422 */
25423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025424do_string_sub(
25425 char_u *str,
25426 char_u *pat,
25427 char_u *sub,
25428 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429{
25430 int sublen;
25431 regmatch_T regmatch;
25432 int i;
25433 int do_all;
25434 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025435 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436 garray_T ga;
25437 char_u *ret;
25438 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025439 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025440
25441 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25442 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025443 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444
25445 ga_init2(&ga, 1, 200);
25446
25447 do_all = (flags[0] == 'g');
25448
25449 regmatch.rm_ic = p_ic;
25450 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25451 if (regmatch.regprog != NULL)
25452 {
25453 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025454 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25456 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025457 /* Skip empty match except for first match. */
25458 if (regmatch.startp[0] == regmatch.endp[0])
25459 {
25460 if (zero_width == regmatch.startp[0])
25461 {
25462 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025463 i = MB_PTR2LEN(tail);
25464 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25465 (size_t)i);
25466 ga.ga_len += i;
25467 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025468 continue;
25469 }
25470 zero_width = regmatch.startp[0];
25471 }
25472
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473 /*
25474 * Get some space for a temporary buffer to do the substitution
25475 * into. It will contain:
25476 * - The text up to where the match is.
25477 * - The substituted text.
25478 * - The text after the match.
25479 */
25480 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025481 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25483 {
25484 ga_clear(&ga);
25485 break;
25486 }
25487
25488 /* copy the text up to where the match is */
25489 i = (int)(regmatch.startp[0] - tail);
25490 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25491 /* add the substituted text */
25492 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25493 + ga.ga_len + i, TRUE, TRUE, FALSE);
25494 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025495 tail = regmatch.endp[0];
25496 if (*tail == NUL)
25497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 if (!do_all)
25499 break;
25500 }
25501
25502 if (ga.ga_data != NULL)
25503 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25504
Bram Moolenaar473de612013-06-08 18:19:48 +020025505 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506 }
25507
25508 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25509 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025510 if (p_cpo == empty_option)
25511 p_cpo = save_cpo;
25512 else
25513 /* Darn, evaluating {sub} expression changed the value. */
25514 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515
25516 return ret;
25517}
25518
25519#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */