blob: f13486f227fe2ad984ae81a91795104ece91958b [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 Moolenaarf57969a2016-02-02 20:47:49 +0100502#ifdef FEAT_CHANNEL
503static void f_ch_open(typval_T *argvars, typval_T *rettv);
504static void f_ch_close(typval_T *argvars, typval_T *rettv);
505static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
506static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
507#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_changenr(typval_T *argvars, typval_T *rettv);
509static void f_char2nr(typval_T *argvars, typval_T *rettv);
510static void f_cindent(typval_T *argvars, typval_T *rettv);
511static void f_clearmatches(typval_T *argvars, typval_T *rettv);
512static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000513#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_complete(typval_T *argvars, typval_T *rettv);
515static void f_complete_add(typval_T *argvars, typval_T *rettv);
516static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000517#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100518static void f_confirm(typval_T *argvars, typval_T *rettv);
519static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_cos(typval_T *argvars, typval_T *rettv);
522static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_count(typval_T *argvars, typval_T *rettv);
525static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
526static void f_cursor(typval_T *argsvars, typval_T *rettv);
527static void f_deepcopy(typval_T *argvars, typval_T *rettv);
528static void f_delete(typval_T *argvars, typval_T *rettv);
529static void f_did_filetype(typval_T *argvars, typval_T *rettv);
530static void f_diff_filler(typval_T *argvars, typval_T *rettv);
531static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
532static 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);
706static void f_server2client(typval_T *argvars, typval_T *rettv);
707static void f_serverlist(typval_T *argvars, typval_T *rettv);
708static void f_setbufvar(typval_T *argvars, typval_T *rettv);
709static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
710static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
711static void f_setline(typval_T *argvars, typval_T *rettv);
712static void f_setloclist(typval_T *argvars, typval_T *rettv);
713static void f_setmatches(typval_T *argvars, typval_T *rettv);
714static void f_setpos(typval_T *argvars, typval_T *rettv);
715static void f_setqflist(typval_T *argvars, typval_T *rettv);
716static void f_setreg(typval_T *argvars, typval_T *rettv);
717static void f_settabvar(typval_T *argvars, typval_T *rettv);
718static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
719static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100720#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100722#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_shellescape(typval_T *argvars, typval_T *rettv);
724static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
725static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000726#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_sin(typval_T *argvars, typval_T *rettv);
728static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_sort(typval_T *argvars, typval_T *rettv);
731static void f_soundfold(typval_T *argvars, typval_T *rettv);
732static void f_spellbadword(typval_T *argvars, typval_T *rettv);
733static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
734static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_sqrt(typval_T *argvars, typval_T *rettv);
737static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_str2nr(typval_T *argvars, typval_T *rettv);
740static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000741#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100742static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000743#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_stridx(typval_T *argvars, typval_T *rettv);
745static void f_string(typval_T *argvars, typval_T *rettv);
746static void f_strlen(typval_T *argvars, typval_T *rettv);
747static void f_strpart(typval_T *argvars, typval_T *rettv);
748static void f_strridx(typval_T *argvars, typval_T *rettv);
749static void f_strtrans(typval_T *argvars, typval_T *rettv);
750static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
751static void f_strwidth(typval_T *argvars, typval_T *rettv);
752static void f_submatch(typval_T *argvars, typval_T *rettv);
753static void f_substitute(typval_T *argvars, typval_T *rettv);
754static void f_synID(typval_T *argvars, typval_T *rettv);
755static void f_synIDattr(typval_T *argvars, typval_T *rettv);
756static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
757static void f_synstack(typval_T *argvars, typval_T *rettv);
758static void f_synconcealed(typval_T *argvars, typval_T *rettv);
759static void f_system(typval_T *argvars, typval_T *rettv);
760static void f_systemlist(typval_T *argvars, typval_T *rettv);
761static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
762static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
763static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
764static void f_taglist(typval_T *argvars, typval_T *rettv);
765static void f_tagfiles(typval_T *argvars, typval_T *rettv);
766static void f_tempname(typval_T *argvars, typval_T *rettv);
767static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_tan(typval_T *argvars, typval_T *rettv);
770static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_tolower(typval_T *argvars, typval_T *rettv);
773static void f_toupper(typval_T *argvars, typval_T *rettv);
774static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_type(typval_T *argvars, typval_T *rettv);
779static void f_undofile(typval_T *argvars, typval_T *rettv);
780static void f_undotree(typval_T *argvars, typval_T *rettv);
781static void f_uniq(typval_T *argvars, typval_T *rettv);
782static void f_values(typval_T *argvars, typval_T *rettv);
783static void f_virtcol(typval_T *argvars, typval_T *rettv);
784static void f_visualmode(typval_T *argvars, typval_T *rettv);
785static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
786static void f_winbufnr(typval_T *argvars, typval_T *rettv);
787static void f_wincol(typval_T *argvars, typval_T *rettv);
788static void f_winheight(typval_T *argvars, typval_T *rettv);
789static void f_winline(typval_T *argvars, typval_T *rettv);
790static void f_winnr(typval_T *argvars, typval_T *rettv);
791static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
792static void f_winrestview(typval_T *argvars, typval_T *rettv);
793static void f_winsaveview(typval_T *argvars, typval_T *rettv);
794static void f_winwidth(typval_T *argvars, typval_T *rettv);
795static void f_writefile(typval_T *argvars, typval_T *rettv);
796static void f_wordcount(typval_T *argvars, typval_T *rettv);
797static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000798
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
800static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
801static int get_env_len(char_u **arg);
802static int get_id_len(char_u **arg);
803static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
804static 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 +0000805#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
806#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
807 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100808static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
809static int eval_isnamec(int c);
810static int eval_isnamec1(int c);
811static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
812static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static typval_T *alloc_string_tv(char_u *string);
814static void init_tv(typval_T *varp);
815static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100816#ifdef FEAT_FLOAT
817static float_T get_tv_float(typval_T *varp);
818#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static linenr_T get_tv_lnum(typval_T *argvars);
820static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
821static char_u *get_tv_string(typval_T *varp);
822static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
823static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
824static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
825static hashtab_T *find_var_ht(char_u *name, char_u **varname);
826static funccall_T *get_funccal(void);
827static void vars_clear_ext(hashtab_T *ht, int free_val);
828static void delete_var(hashtab_T *ht, hashitem_T *hi);
829static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
830static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
831static void set_var(char_u *name, typval_T *varp, int copy);
832static int var_check_ro(int flags, char_u *name, int use_gettext);
833static int var_check_fixed(int flags, char_u *name, int use_gettext);
834static int var_check_func_name(char_u *name, int new_var);
835static int valid_varname(char_u *varname);
836static int tv_check_lock(int lock, char_u *name, int use_gettext);
837static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
838static char_u *find_option_end(char_u **arg, int *opt_flags);
839static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
840static int eval_fname_script(char_u *p);
841static int eval_fname_sid(char_u *p);
842static void list_func_head(ufunc_T *fp, int indent);
843static ufunc_T *find_func(char_u *name);
844static int function_exists(char_u *name);
845static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static void func_do_profile(ufunc_T *fp);
848static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
849static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000850static int
851# ifdef __BORLANDC__
852 _RTLENTRYF
853# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854 prof_total_cmp(const void *s1, const void *s2);
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_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100861static int script_autoload(char_u *name, int reload);
862static char_u *autoload_name(char_u *name);
863static void cat_func_name(char_u *buf, ufunc_T *fp);
864static void func_free(ufunc_T *fp);
865static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
866static int can_free_funccal(funccall_T *fc, int copyID) ;
867static void free_funccal(funccall_T *fc, int free_val);
868static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
869static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
870static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
871static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
872static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
873static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
874static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
875static int write_list(FILE *fd, list_T *list, int binary);
876static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878
879#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static int compare_func_name(const void *s1, const void *s2);
881static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882#endif
883
Bram Moolenaar33570922005-01-25 22:26:29 +0000884/*
885 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000886 */
887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000889{
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 int i;
891 struct vimvar *p;
892
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200893 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
894 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200895 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000896 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000898
899 for (i = 0; i < VV_LEN; ++i)
900 {
901 p = &vimvars[i];
902 STRCPY(p->vv_di.di_key, p->vv_name);
903 if (p->vv_flags & VV_RO)
904 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
905 else if (p->vv_flags & VV_RO_SBX)
906 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
907 else
908 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000909
910 /* add to v: scope dict, unless the value is not always available */
911 if (p->vv_type != VAR_UNKNOWN)
912 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000914 /* add to compat scope dict */
915 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000916 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100917 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
918
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000919 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100920 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200921 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100922 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100923
924 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
925 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
926 set_vim_var_nr(VV_NONE, VVAL_NONE);
927 set_vim_var_nr(VV_NULL, VVAL_NULL);
928
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200929 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200930
931#ifdef EBCDIC
932 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100933 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200934 */
935 sortFunctions();
936#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000937}
938
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939#if defined(EXITFREE) || defined(PROTO)
940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100941eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942{
943 int i;
944 struct vimvar *p;
945
946 for (i = 0; i < VV_LEN; ++i)
947 {
948 p = &vimvars[i];
949 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000951 vim_free(p->vv_str);
952 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000953 }
954 else if (p->vv_di.di_tv.v_type == VAR_LIST)
955 {
956 list_unref(p->vv_list);
957 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000958 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959 }
960 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000961 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962 hash_clear(&compat_hashtab);
963
Bram Moolenaard9fba312005-06-26 22:34:35 +0000964 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100965# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200966 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100967# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968
969 /* global variables */
970 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000971
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000972 /* autoloaded script names */
973 ga_clear_strings(&ga_loaded);
974
Bram Moolenaarcca74132013-09-25 21:00:28 +0200975 /* Script-local variables. First clear all the variables and in a second
976 * loop free the scriptvar_T, because a variable in one script might hold
977 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200978 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200979 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200980 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200981 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 ga_clear(&ga_scripts);
983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000984 /* unreferenced lists and dicts */
985 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000986
987 /* functions */
988 free_all_functions();
989 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990}
991#endif
992
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * Return the name of the executed function.
995 */
996 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100997func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000}
1001
1002/*
1003 * Return the address holding the next breakpoint line for a funccall cookie.
1004 */
1005 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
Bram Moolenaar33570922005-01-25 22:26:29 +00001008 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/*
1012 * Return the address holding the debug tick for a funccall cookie.
1013 */
1014 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001015func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar33570922005-01-25 22:26:29 +00001017 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018}
1019
1020/*
1021 * Return the nesting level for a funccall cookie.
1022 */
1023 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar33570922005-01-25 22:26:29 +00001026 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001030funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001032/* pointer to list of previously used funccal, still around because some
1033 * item in it is still being used. */
1034funccall_T *previous_funccal = NULL;
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036/*
1037 * Return TRUE when a function was ended by a ":return" command.
1038 */
1039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 return current_funccal->returned;
1043}
1044
1045
1046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Set an internal variable to a string value. Creates the variable if it does
1048 * not already exist.
1049 */
1050 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001053 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001054 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 val = vim_strsave(value);
1057 if (val != NULL)
1058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001060 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001062 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065 }
1066}
1067
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001069static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070static char_u *redir_endp = NULL;
1071static char_u *redir_varname = NULL;
1072
1073/*
1074 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 * Returns OK if successfully completed the setup. FAIL otherwise.
1077 */
1078 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080{
1081 int save_emsg;
1082 int err;
1083 typval_T tv;
1084
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 {
1088 EMSG(_(e_invarg));
1089 return FAIL;
1090 }
1091
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001092 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 redir_varname = vim_strsave(name);
1094 if (redir_varname == NULL)
1095 return FAIL;
1096
1097 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1098 if (redir_lval == NULL)
1099 {
1100 var_redir_stop();
1101 return FAIL;
1102 }
1103
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104 /* The output is stored in growarray "redir_ga" until redirection ends. */
1105 ga_init2(&redir_ga, (int)sizeof(char), 500);
1106
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001108 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001109 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1111 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001112 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (redir_endp != NULL && *redir_endp != NUL)
1114 /* Trailing characters are present after the variable name */
1115 EMSG(_(e_trailing));
1116 else
1117 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 var_redir_stop();
1120 return FAIL;
1121 }
1122
1123 /* check if we can write to the variable: set it to or append an empty
1124 * string */
1125 save_emsg = did_emsg;
1126 did_emsg = FALSE;
1127 tv.v_type = VAR_STRING;
1128 tv.vval.v_string = (char_u *)"";
1129 if (append)
1130 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1131 else
1132 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001133 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001135 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (err)
1137 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
1140 return FAIL;
1141 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001147 * Append "value[value_len]" to the variable set by var_redir_start().
1148 * The actual appending is postponed until redirection ends, because the value
1149 * appended may in fact be the string we write to, changing it may cause freed
1150 * memory to be used:
1151 * :redir => foo
1152 * :let foo
1153 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 */
1155 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001156var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159
1160 if (redir_lval == NULL)
1161 return;
1162
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001164 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001166 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001168 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 {
1170 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 }
1173 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175}
1176
1177/*
1178 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001179 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 */
1181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001182var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001184 typval_T tv;
1185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (redir_lval != NULL)
1187 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 /* If there was no error: assign the text to the variable. */
1189 if (redir_endp != NULL)
1190 {
1191 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1192 tv.v_type = VAR_STRING;
1193 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001194 /* Call get_lval() again, if it's inside a Dict or List it may
1195 * have changed. */
1196 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001197 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001198 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1199 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1200 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 /* free the collected output */
1204 vim_free(redir_ga.ga_data);
1205 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 vim_free(redir_lval);
1208 redir_lval = NULL;
1209 }
1210 vim_free(redir_varname);
1211 redir_varname = NULL;
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# if defined(FEAT_MBYTE) || defined(PROTO)
1215 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001216eval_charconvert(
1217 char_u *enc_from,
1218 char_u *enc_to,
1219 char_u *fname_from,
1220 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
1222 int err = FALSE;
1223
1224 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1225 set_vim_var_string(VV_CC_TO, enc_to, -1);
1226 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1227 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1228 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1229 err = TRUE;
1230 set_vim_var_string(VV_CC_FROM, NULL, -1);
1231 set_vim_var_string(VV_CC_TO, NULL, -1);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1234
1235 if (err)
1236 return FAIL;
1237 return OK;
1238}
1239# endif
1240
1241# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001243eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 int err = FALSE;
1246
1247 set_vim_var_string(VV_FNAME_IN, fname, -1);
1248 set_vim_var_string(VV_CMDARG, args, -1);
1249 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1250 err = TRUE;
1251 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1252 set_vim_var_string(VV_CMDARG, NULL, -1);
1253
1254 if (err)
1255 {
1256 mch_remove(fname);
1257 return FAIL;
1258 }
1259 return OK;
1260}
1261# endif
1262
1263# if defined(FEAT_DIFF) || defined(PROTO)
1264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265eval_diff(
1266 char_u *origfile,
1267 char_u *newfile,
1268 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 int err = FALSE;
1271
1272 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1273 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1274 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1275 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1276 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1277 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1278 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1279}
1280
1281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282eval_patch(
1283 char_u *origfile,
1284 char_u *difffile,
1285 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err;
1288
1289 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1290 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1291 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1292 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1293 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1294 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296}
1297# endif
1298
1299/*
1300 * Top level evaluation function, returning a boolean.
1301 * Sets "error" to TRUE if there was an error.
1302 * Return TRUE or FALSE.
1303 */
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_to_bool(
1306 char_u *arg,
1307 int *error,
1308 char_u **nextcmd,
1309 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 int retval = FALSE;
1313
1314 if (skip)
1315 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 else
1319 {
1320 *error = FALSE;
1321 if (!skip)
1322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 }
1327 if (skip)
1328 --emsg_skip;
1329
1330 return retval;
1331}
1332
1333/*
1334 * Top level evaluation function, returning a string. If "skip" is TRUE,
1335 * only parsing to "nextcmd" is done, without reporting errors. Return
1336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1337 */
1338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001339eval_to_string_skip(
1340 char_u *arg,
1341 char_u **nextcmd,
1342 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
1346
1347 if (skip)
1348 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 retval = NULL;
1351 else
1352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 retval = vim_strsave(get_tv_string(&tv));
1354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 if (skip)
1357 --emsg_skip;
1358
1359 return retval;
1360}
1361
1362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363 * Skip over an expression at "*pp".
1364 * Return FAIL for an error, OK otherwise.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001370
1371 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373}
1374
1375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377 * When "convert" is TRUE convert a List into a sequence of lines and convert
1378 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * Return pointer to allocated memory, or NULL for failure.
1380 */
1381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001382eval_to_string(
1383 char_u *arg,
1384 char_u **nextcmd,
1385 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001390#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001391 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 retval = NULL;
1396 else
1397 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 {
1400 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001401 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001402 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001403 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001404 if (tv.vval.v_list->lv_len > 0)
1405 ga_append(&ga, NL);
1406 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 ga_append(&ga, NUL);
1408 retval = (char_u *)ga.ga_data;
1409 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001410#ifdef FEAT_FLOAT
1411 else if (convert && tv.v_type == VAR_FLOAT)
1412 {
1413 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1414 retval = vim_strsave(numbuf);
1415 }
1416#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 else
1418 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421
1422 return retval;
1423}
1424
1425/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 * Call eval_to_string() without using current local variables and using
1427 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 */
1429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430eval_to_string_safe(
1431 char_u *arg,
1432 char_u **nextcmd,
1433 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 char_u *retval;
1436 void *save_funccalp;
1437
1438 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001439 if (use_sandbox)
1440 ++sandbox;
1441 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001443 if (use_sandbox)
1444 --sandbox;
1445 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 restore_funccal(save_funccalp);
1447 return retval;
1448}
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450/*
1451 * Top level evaluation function, returning a number.
1452 * Evaluates "expr" silently.
1453 * Returns -1 for an error.
1454 */
1455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaar33570922005-01-25 22:26:29 +00001458 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001460 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 ++emsg_off;
1463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001464 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 retval = -1;
1466 else
1467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001468 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471 --emsg_off;
1472
1473 return retval;
1474}
1475
Bram Moolenaara40058a2005-07-11 22:42:07 +00001476/*
1477 * Prepare v: variable "idx" to be used.
1478 * Save the current typeval in "save_tv".
1479 * When not used yet add the variable to the v: hashtable.
1480 */
1481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001483{
1484 *save_tv = vimvars[idx].vv_tv;
1485 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1486 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1487}
1488
1489/*
1490 * Restore v: variable "idx" to typeval "save_tv".
1491 * When no longer defined, remove the variable from the v: hashtable.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001495{
1496 hashitem_T *hi;
1497
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498 vimvars[idx].vv_tv = *save_tv;
1499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1500 {
1501 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1502 if (HASHITEM_EMPTY(hi))
1503 EMSG2(_(e_intern2), "restore_vimvar()");
1504 else
1505 hash_remove(&vimvarht, hi);
1506 }
1507}
1508
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001509#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001510/*
1511 * Evaluate an expression to a list with suggestions.
1512 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001513 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 */
1515 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517{
1518 typval_T save_val;
1519 typval_T rettv;
1520 list_T *list = NULL;
1521 char_u *p = skipwhite(expr);
1522
1523 /* Set "v:val" to the bad word. */
1524 prepare_vimvar(VV_VAL, &save_val);
1525 vimvars[VV_VAL].vv_type = VAR_STRING;
1526 vimvars[VV_VAL].vv_str = badword;
1527 if (p_verbose == 0)
1528 ++emsg_off;
1529
1530 if (eval1(&p, &rettv, TRUE) == OK)
1531 {
1532 if (rettv.v_type != VAR_LIST)
1533 clear_tv(&rettv);
1534 else
1535 list = rettv.vval.v_list;
1536 }
1537
1538 if (p_verbose == 0)
1539 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540 restore_vimvar(VV_VAL, &save_val);
1541
1542 return list;
1543}
1544
1545/*
1546 * "list" is supposed to contain two items: a word and a number. Return the
1547 * word in "pp" and the number as the return value.
1548 * Return -1 if anything isn't right.
1549 * Used to get the good word and score from the eval_spell_expr() result.
1550 */
1551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001553{
1554 listitem_T *li;
1555
1556 li = list->lv_first;
1557 if (li == NULL)
1558 return -1;
1559 *pp = get_tv_string(&li->li_tv);
1560
1561 li = li->li_next;
1562 if (li == NULL)
1563 return -1;
1564 return get_tv_number(&li->li_tv);
1565}
1566#endif
1567
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001569 * Top level evaluation function.
1570 * Returns an allocated typval_T with the result.
1571 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001572 */
1573 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575{
1576 typval_T *tv;
1577
1578 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 {
1581 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583 }
1584
1585 return tv;
1586}
1587
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 * Uses argv[argc] for the function arguments. Only Number and String
1592 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596call_vim_function(
1597 char_u *func,
1598 int argc,
1599 char_u **argv,
1600 int safe, /* use the sandbox */
1601 int str_arg_only, /* all arguments are strings */
1602 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 long n;
1606 int len;
1607 int i;
1608 int doesrange;
1609 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001612 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 for (i = 0; i < argc; i++)
1617 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001618 /* Pass a NULL or empty argument as an empty string */
1619 if (argv[i] == NULL || *argv[i] == NUL)
1620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001621 argvars[i].v_type = VAR_STRING;
1622 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001623 continue;
1624 }
1625
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001626 if (str_arg_only)
1627 len = 0;
1628 else
1629 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001630 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (len != 0 && len == (int)STRLEN(argv[i]))
1632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001633 argvars[i].v_type = VAR_NUMBER;
1634 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 else
1637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001638 argvars[i].v_type = VAR_STRING;
1639 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 }
1642
1643 if (safe)
1644 {
1645 save_funccalp = save_funccal();
1646 ++sandbox;
1647 }
1648
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1650 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (safe)
1654 {
1655 --sandbox;
1656 restore_funccal(save_funccalp);
1657 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 vim_free(argvars);
1659
1660 if (ret == FAIL)
1661 clear_tv(rettv);
1662
1663 return ret;
1664}
1665
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001666/*
1667 * Call vimL function "func" and return the result as a number.
1668 * Returns -1 when calling the function fails.
1669 * Uses argv[argc] for the function arguments.
1670 */
1671 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672call_func_retnr(
1673 char_u *func,
1674 int argc,
1675 char_u **argv,
1676 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001677{
1678 typval_T rettv;
1679 long retval;
1680
1681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1683 return -1;
1684
1685 retval = get_tv_number_chk(&rettv, NULL);
1686 clear_tv(&rettv);
1687 return retval;
1688}
1689
1690#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1691 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1692
Bram Moolenaar4f688582007-07-24 12:34:30 +00001693# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001695 * Call vimL function "func" and return the result as a string.
1696 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 * Uses argv[argc] for the function arguments.
1698 */
1699 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700call_func_retstr(
1701 char_u *func,
1702 int argc,
1703 char_u **argv,
1704 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705{
1706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 return NULL;
1712
1713 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return retval;
1716}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001720 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001722 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 */
1724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retlist(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730{
1731 typval_T rettv;
1732
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001733 /* All arguments are passed as strings, no conversion to number. */
1734 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 return NULL;
1736
1737 if (rettv.v_type != VAR_LIST)
1738 {
1739 clear_tv(&rettv);
1740 return NULL;
1741 }
1742
1743 return rettv.vval.v_list;
1744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746
1747/*
1748 * Save the current function call pointer, and set it to NULL.
1749 * Used when executing autocommands and for ":source".
1750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 current_funccal = NULL;
1757 return (void *)fc;
1758}
1759
1760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001761restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = (funccall_T *)vfc;
1764
1765 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768#if defined(FEAT_PROFILE) || defined(PROTO)
1769/*
1770 * Prepare profiling for entering a child or something else that is not
1771 * counted for the script/function itself.
1772 * Should always be called in pair with prof_child_exit().
1773 */
1774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775prof_child_enter(
1776 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 profile_start(&fc->prof_child);
1782 script_prof_save(tm);
1783}
1784
1785/*
1786 * Take care of time spent in a child.
1787 * Should always be called after prof_child_enter().
1788 */
1789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790prof_child_exit(
1791 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792{
1793 funccall_T *fc = current_funccal;
1794
1795 if (fc != NULL && fc->func->uf_profiling)
1796 {
1797 profile_end(&fc->prof_child);
1798 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1799 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1800 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1801 }
1802 script_prof_restore(tm);
1803}
1804#endif
1805
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_FOLDING
1808/*
1809 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1810 * it in "*cp". Doesn't give error messages.
1811 */
1812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 int retval;
1817 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001818 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1819 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 ++sandbox;
1824 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 retval = 0;
1828 else
1829 {
1830 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (tv.v_type == VAR_NUMBER)
1832 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001833 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 retval = 0;
1835 else
1836 {
1837 /* If the result is a string, check if there is a non-digit before
1838 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (!VIM_ISDIGIT(*s) && *s != '-')
1841 *cp = *s++;
1842 retval = atol((char *)s);
1843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 --sandbox;
1849 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 return retval;
1852}
1853#endif
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let" list all variable values
1857 * ":let var1 var2" list variable values
1858 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 * ":let var += expr" assignment command.
1860 * ":let var -= expr" assignment command.
1861 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001869 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 int var_count = 0;
1872 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 argend = skip_var_list(arg, &var_count, &semicolon);
1878 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001880 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1881 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001882 expr = skipwhite(argend);
1883 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1884 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001886 /*
1887 * ":let" without "=": list variables
1888 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 if (*arg == '[')
1890 EMSG(_(e_invarg));
1891 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001893 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 list_glob_vars(&first);
1898 list_buf_vars(&first);
1899 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001900#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001902#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 list_script_vars(&first);
1904 list_func_vars(&first);
1905 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 eap->nextcmd = check_nextcmd(arg);
1908 }
1909 else
1910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 op[0] = '=';
1912 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001913 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1916 op[0] = *expr; /* +=, -= or .= */
1917 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001919 else
1920 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 {
1927 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 --emsg_skip;
1930 }
1931 else if (i != FAIL)
1932 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 }
1938}
1939
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940/*
1941 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1942 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 * When "nextchars" is not NULL it points to a string with characters that
1944 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1945 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 * Returns OK or FAIL;
1947 */
1948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001949ex_let_vars(
1950 char_u *arg_start,
1951 typval_T *tv,
1952 int copy, /* copy values from "tv", don't move */
1953 int semicolon, /* from skip_var_list() */
1954 int var_count, /* from skip_var_list() */
1955 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956{
1957 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 listitem_T *item;
1961 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962
1963 if (*arg != '[')
1964 {
1965 /*
1966 * ":let var = expr" or ":for var in list"
1967 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 return FAIL;
1970 return OK;
1971 }
1972
1973 /*
1974 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1975 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001976 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 {
1978 EMSG(_(e_listreq));
1979 return FAIL;
1980 }
1981
1982 i = list_len(l);
1983 if (semicolon == 0 && var_count < i)
1984 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001985 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 return FAIL;
1987 }
1988 if (var_count - semicolon > i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993
1994 item = l->lv_first;
1995 while (*arg != ']')
1996 {
1997 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 item = item->li_next;
2000 if (arg == NULL)
2001 return FAIL;
2002
2003 arg = skipwhite(arg);
2004 if (*arg == ';')
2005 {
2006 /* Put the rest of the list (may be empty) in the var after ';'.
2007 * Create a new list for this. */
2008 l = list_alloc();
2009 if (l == NULL)
2010 return FAIL;
2011 while (item != NULL)
2012 {
2013 list_append_tv(l, &item->li_tv);
2014 item = item->li_next;
2015 }
2016
2017 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002018 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 ltv.vval.v_list = l;
2020 l->lv_refcount = 1;
2021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2023 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 clear_tv(&ltv);
2025 if (arg == NULL)
2026 return FAIL;
2027 break;
2028 }
2029 else if (*arg != ',' && *arg != ']')
2030 {
2031 EMSG2(_(e_intern2), "ex_let_vars()");
2032 return FAIL;
2033 }
2034 }
2035
2036 return OK;
2037}
2038
2039/*
2040 * Skip over assignable variable "var" or list of variables "[var, var]".
2041 * Used for ":let varvar = expr" and ":for varvar in expr".
2042 * For "[var, var]" increment "*var_count" for each variable.
2043 * for "[var, var; var]" set "semicolon".
2044 * Return NULL for an error.
2045 */
2046 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002047skip_var_list(
2048 char_u *arg,
2049 int *var_count,
2050 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051{
2052 char_u *p, *s;
2053
2054 if (*arg == '[')
2055 {
2056 /* "[var, var]": find the matching ']'. */
2057 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002058 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 {
2060 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2061 s = skip_var_one(p);
2062 if (s == p)
2063 {
2064 EMSG2(_(e_invarg2), p);
2065 return NULL;
2066 }
2067 ++*var_count;
2068
2069 p = skipwhite(s);
2070 if (*p == ']')
2071 break;
2072 else if (*p == ';')
2073 {
2074 if (*semicolon == 1)
2075 {
2076 EMSG(_("Double ; in list of variables"));
2077 return NULL;
2078 }
2079 *semicolon = 1;
2080 }
2081 else if (*p != ',')
2082 {
2083 EMSG2(_(e_invarg2), p);
2084 return NULL;
2085 }
2086 }
2087 return p + 1;
2088 }
2089 else
2090 return skip_var_one(arg);
2091}
2092
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002094 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002095 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002097 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 if (*arg == '@' && arg[1] != NUL)
2101 return arg + 2;
2102 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2103 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104}
2105
Bram Moolenaara7043832005-01-21 11:56:39 +00002106/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 * List variables for hashtab "ht" with prefix "prefix".
2108 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111list_hashtable_vars(
2112 hashtab_T *ht,
2113 char_u *prefix,
2114 int empty,
2115 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002116{
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 hashitem_T *hi;
2118 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 int todo;
2120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002121 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2123 {
2124 if (!HASHITEM_EMPTY(hi))
2125 {
2126 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 di = HI2DI(hi);
2128 if (empty || di->di_tv.v_type != VAR_STRING
2129 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 }
2132 }
2133}
2134
2135/*
2136 * List global variables.
2137 */
2138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142}
2143
2144/*
2145 * List buffer variables.
2146 */
2147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002148list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002166 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002168}
2169
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170#ifdef FEAT_WINDOWS
2171/*
2172 * List tab page variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179}
2180#endif
2181
Bram Moolenaara7043832005-01-21 11:56:39 +00002182/*
2183 * List Vim variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189}
2190
2191/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192 * List script-local variables, if there is a script.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196{
2197 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2199 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200}
2201
2202/*
2203 * List function variables, if there is a function.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207{
2208 if (current_funccal != NULL)
2209 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211}
2212
2213/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 * List variables in "arg".
2215 */
2216 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002260 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002325ex_let_one(
2326 char_u *arg, /* points to variable name */
2327 typval_T *tv, /* value to assign to variable */
2328 int copy, /* copy value from "tv" */
2329 char_u *endchars, /* valid chars after variable name or NULL */
2330 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002519check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520{
2521 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2522 {
2523 EMSG2(_(e_readonlyvar), arg);
2524 return TRUE;
2525 }
2526 return FALSE;
2527}
2528
2529/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Get an lval: variable, Dict item or List item that can be assigned a value
2531 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2532 * "name.key", "name.key[expr]" etc.
2533 * Indexing only works if "name" is an existing List or Dictionary.
2534 * "name" points to the start of the name.
2535 * If "rettv" is not NULL it points to the value to be assigned.
2536 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2537 * wrong; must end in space or cmd separator.
2538 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 * flags:
2540 * GLV_QUIET: do not give error messages
2541 * GLV_NO_AUTOLOAD: do not use script autoloading
2542 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002544 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 */
2547 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002548get_lval(
2549 char_u *name,
2550 typval_T *rettv,
2551 lval_T *lp,
2552 int unlet,
2553 int skip,
2554 int flags, /* GLV_ values */
2555 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 char_u *expr_start, *expr_end;
2559 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 dictitem_T *v;
2561 typval_T var1;
2562 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572
2573 if (skip)
2574 {
2575 /* When skipping just find the end of the name. */
2576 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002577 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 }
2579
2580 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (expr_start != NULL)
2583 {
2584 /* Don't expand the name when we already know there is an error. */
2585 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2586 && *p != '[' && *p != '.')
2587 {
2588 EMSG(_(e_trailing));
2589 return NULL;
2590 }
2591
2592 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2593 if (lp->ll_exp_name == NULL)
2594 {
2595 /* Report an invalid expression in braces, unless the
2596 * expression evaluation has been cancelled due to an
2597 * aborting error, an interrupt, or an exception. */
2598 if (!aborting() && !quiet)
2599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002600 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 EMSG2(_(e_invarg2), name);
2602 return NULL;
2603 }
2604 }
2605 lp->ll_name = lp->ll_exp_name;
2606 }
2607 else
2608 lp->ll_name = name;
2609
2610 /* Without [idx] or .key we are done. */
2611 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2612 return p;
2613
2614 cc = *p;
2615 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002616 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (v == NULL && !quiet)
2618 EMSG2(_(e_undefvar), lp->ll_name);
2619 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 if (v == NULL)
2621 return NULL;
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /*
2624 * Loop until no more [idx] or .key is following.
2625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2630 && !(lp->ll_tv->v_type == VAR_DICT
2631 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E689: Can only index a List or Dictionary"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
2640 EMSG(_("E708: [:] must come last"));
2641 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 len = -1;
2645 if (*p == '.')
2646 {
2647 key = p + 1;
2648 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2649 ;
2650 if (len == 0)
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_(e_emptykey));
2654 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 p = key + len;
2657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (*p == ':')
2663 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 else
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 empty1 = FALSE;
2667 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 if (get_tv_string_chk(&var1) == NULL)
2670 {
2671 /* not a number or string */
2672 clear_tv(&var1);
2673 return NULL;
2674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676
2677 /* Optionally get the second index [ :expr]. */
2678 if (*p == ':')
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (rettv != NULL && (rettv->v_type != VAR_LIST
2689 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697 p = skipwhite(p + 1);
2698 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 if (get_tv_string_chk(&var2) == NULL)
2710 {
2711 /* not a number or string */
2712 if (!empty1)
2713 clear_tv(&var1);
2714 clear_tv(&var2);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733
2734 /* Skip to past ']'. */
2735 ++p;
2736 }
2737
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 {
2740 if (len == -1)
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (*key == NUL)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
2747 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 lp->ll_list = NULL;
2753 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 /* When assigning to a scope dictionary check that a function and
2757 * variable name is valid (only variable name unless it is l: or
2758 * g: dictionary). Disallow overwriting a builtin function. */
2759 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 int prevval;
2762 int wrong;
2763
2764 if (len != -1)
2765 {
2766 prevval = key[len];
2767 key[len] = NUL;
2768 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002769 else
2770 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2772 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 || !valid_varname(key);
2775 if (len != -1)
2776 key[len] = prevval;
2777 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 return NULL;
2779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* Can't add "v:" variable. */
2784 if (lp->ll_dict == &vimvardict)
2785 {
2786 EMSG2(_(e_illvar), name);
2787 return NULL;
2788 }
2789
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002790 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 if (len == -1)
2796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 p = NULL;
2807 break;
2808 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002810 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 return NULL;
2812
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 else
2818 {
2819 /*
2820 * Get the number and item for the only or first index of the List.
2821 */
2822 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 else
2825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var1);
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_list = lp->ll_tv->vval.v_list;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 if (lp->ll_n1 < 0)
2835 {
2836 lp->ll_n1 = 0;
2837 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2838 }
2839 }
2840 if (lp->ll_li == NULL)
2841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
2849 /*
2850 * May need to find the item or absolute index for the second
2851 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 * When no index given: "lp->ll_empty2" is TRUE.
2853 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 {
2864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2872 if (lp->ll_n1 < 0)
2873 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2874 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 {
2876 if (!quiet)
2877 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
2881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return p;
2887}
2888
2889/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 */
2892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894{
2895 vim_free(lp->ll_exp_name);
2896 vim_free(lp->ll_newkey);
2897}
2898
2899/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 */
2904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905set_var_lval(
2906 lval_T *lp,
2907 char_u *endp,
2908 typval_T *rettv,
2909 int copy,
2910 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911{
2912 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 listitem_T *ri;
2914 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915
2916 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 cc = *endp;
2921 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002927 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002928 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002929 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002931 if ((di == NULL
2932 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2933 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2934 FALSE)))
2935 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 set_var(lp->ll_name, &tv, FALSE);
2937 clear_tv(&tv);
2938 }
2939 }
2940 else
2941 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 else if (tv_check_lock(lp->ll_newkey == NULL
2946 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002947 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 else if (lp->ll_range)
2950 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002951 listitem_T *ll_li = lp->ll_li;
2952 int ll_n1 = lp->ll_n1;
2953
2954 /*
2955 * Check whether any of the list items is locked
2956 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002957 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002958 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002959 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002960 return;
2961 ri = ri->li_next;
2962 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2963 break;
2964 ll_li = ll_li->li_next;
2965 ++ll_n1;
2966 }
2967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 /*
2969 * Assign the List values to the list items.
2970 */
2971 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2975 else
2976 {
2977 clear_tv(&lp->ll_li->li_tv);
2978 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 ri = ri->li_next;
2981 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2982 break;
2983 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002986 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 ri = NULL;
2989 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002991 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 lp->ll_li = lp->ll_li->li_next;
2993 ++lp->ll_n1;
2994 }
2995 if (ri != NULL)
2996 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 else if (lp->ll_empty2
2998 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 : lp->ll_n1 != lp->ll_n2)
3000 EMSG(_("E711: List value has not enough items"));
3001 }
3002 else
3003 {
3004 /*
3005 * Assign to a List or Dictionary item.
3006 */
3007 if (lp->ll_newkey != NULL)
3008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 if (op != NULL && *op != '=')
3010 {
3011 EMSG2(_(e_letwrong), op);
3012 return;
3013 }
3014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 if (di == NULL)
3018 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3020 {
3021 vim_free(di);
3022 return;
3023 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003025 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 else if (op != NULL && *op != '=')
3027 {
3028 tv_op(lp->ll_tv, rettv, op);
3029 return;
3030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003031 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 /*
3035 * Assign the value to the variable or list item.
3036 */
3037 if (copy)
3038 copy_tv(rettv, lp->ll_tv);
3039 else
3040 {
3041 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003042 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 }
3045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046}
3047
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3050 * Returns OK or FAIL.
3051 */
3052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003059 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3061 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 {
3063 switch (tv1->v_type)
3064 {
3065 case VAR_DICT:
3066 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003067 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 break;
3069
3070 case VAR_LIST:
3071 if (*op != '+' || tv2->v_type != VAR_LIST)
3072 break;
3073 /* List += List */
3074 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3075 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3076 return OK;
3077
3078 case VAR_NUMBER:
3079 case VAR_STRING:
3080 if (tv2->v_type == VAR_LIST)
3081 break;
3082 if (*op == '+' || *op == '-')
3083 {
3084 /* nr += nr or nr -= nr*/
3085 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086#ifdef FEAT_FLOAT
3087 if (tv2->v_type == VAR_FLOAT)
3088 {
3089 float_T f = n;
3090
3091 if (*op == '+')
3092 f += tv2->vval.v_float;
3093 else
3094 f -= tv2->vval.v_float;
3095 clear_tv(tv1);
3096 tv1->v_type = VAR_FLOAT;
3097 tv1->vval.v_float = f;
3098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003100#endif
3101 {
3102 if (*op == '+')
3103 n += get_tv_number(tv2);
3104 else
3105 n -= get_tv_number(tv2);
3106 clear_tv(tv1);
3107 tv1->v_type = VAR_NUMBER;
3108 tv1->vval.v_number = n;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 else
3112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113 if (tv2->v_type == VAR_FLOAT)
3114 break;
3115
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 /* str .= str */
3117 s = get_tv_string(tv1);
3118 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3119 clear_tv(tv1);
3120 tv1->v_type = VAR_STRING;
3121 tv1->vval.v_string = s;
3122 }
3123 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124
3125#ifdef FEAT_FLOAT
3126 case VAR_FLOAT:
3127 {
3128 float_T f;
3129
3130 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3131 && tv2->v_type != VAR_NUMBER
3132 && tv2->v_type != VAR_STRING))
3133 break;
3134 if (tv2->v_type == VAR_FLOAT)
3135 f = tv2->vval.v_float;
3136 else
3137 f = get_tv_number(tv2);
3138 if (*op == '+')
3139 tv1->vval.v_float += f;
3140 else
3141 tv1->vval.v_float -= f;
3142 }
3143 return OK;
3144#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 }
3146 }
3147
3148 EMSG2(_(e_letwrong), op);
3149 return FAIL;
3150}
3151
3152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153 * Add a watcher to a list.
3154 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003156list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157{
3158 lw->lw_next = l->lv_watch;
3159 l->lv_watch = lw;
3160}
3161
3162/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003163 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * No warning when it isn't found...
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003167list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170
3171 lwp = &l->lv_watch;
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 {
3174 if (lw == lwrem)
3175 {
3176 *lwp = lw->lw_next;
3177 break;
3178 }
3179 lwp = &lw->lw_next;
3180 }
3181}
3182
3183/*
3184 * Just before removing an item from a list: advance watchers to the next
3185 * item.
3186 */
3187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003188list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3193 if (lw->lw_item == item)
3194 lw->lw_item = item->li_next;
3195}
3196
3197/*
3198 * Evaluate the expression used in a ":for var in expr" command.
3199 * "arg" points to "var".
3200 * Set "*errp" to TRUE for an error, FALSE otherwise;
3201 * Return a pointer that holds the info. Null when there is an error.
3202 */
3203 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204eval_for_line(
3205 char_u *arg,
3206 int *errp,
3207 char_u **nextcmdp,
3208 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209{
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 typval_T tv;
3213 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 *errp = TRUE; /* default: there is an error */
3216
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 if (fi == NULL)
3219 return NULL;
3220
3221 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3222 if (expr == NULL)
3223 return fi;
3224
3225 expr = skipwhite(expr);
3226 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3227 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003228 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 return fi;
3230 }
3231
3232 if (skip)
3233 ++emsg_skip;
3234 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3235 {
3236 *errp = FALSE;
3237 if (!skip)
3238 {
3239 l = tv.vval.v_list;
3240 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003241 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003243 clear_tv(&tv);
3244 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 else
3246 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003247 /* No need to increment the refcount, it's already set for the
3248 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 fi->fi_list = l;
3250 list_add_watch(l, &fi->fi_lw);
3251 fi->fi_lw.lw_item = l->lv_first;
3252 }
3253 }
3254 }
3255 if (skip)
3256 --emsg_skip;
3257
3258 return fi;
3259}
3260
3261/*
3262 * Use the first item in a ":for" list. Advance to the next.
3263 * Assign the values to the variable (list). "arg" points to the first one.
3264 * Return TRUE when a valid item was found, FALSE when at end of list or
3265 * something wrong.
3266 */
3267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003270 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273
3274 item = fi->fi_lw.lw_item;
3275 if (item == NULL)
3276 result = FALSE;
3277 else
3278 {
3279 fi->fi_lw.lw_item = item->li_next;
3280 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3281 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3282 }
3283 return result;
3284}
3285
3286/*
3287 * Free the structure used to store info used by ":for".
3288 */
3289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291{
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003294 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003295 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003297 list_unref(fi->fi_list);
3298 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 vim_free(fi);
3300}
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3303
3304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305set_context_for_expression(
3306 expand_T *xp,
3307 char_u *arg,
3308 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 int got_eq = FALSE;
3311 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 if (cmdidx == CMD_let)
3315 {
3316 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003317 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 {
3319 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003320 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 {
3322 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 if (vim_iswhite(*p))
3325 break;
3326 }
3327 return;
3328 }
3329 }
3330 else
3331 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3332 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 while ((xp->xp_pattern = vim_strpbrk(arg,
3334 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3335 {
3336 c = *xp->xp_pattern;
3337 if (c == '&')
3338 {
3339 c = xp->xp_pattern[1];
3340 if (c == '&')
3341 {
3342 ++xp->xp_pattern;
3343 xp->xp_context = cmdidx != CMD_let || got_eq
3344 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3345 }
3346 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003349 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3350 xp->xp_pattern += 2;
3351
3352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354 else if (c == '$')
3355 {
3356 /* environment variable */
3357 xp->xp_context = EXPAND_ENV_VARS;
3358 }
3359 else if (c == '=')
3360 {
3361 got_eq = TRUE;
3362 xp->xp_context = EXPAND_EXPRESSION;
3363 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003364 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 && xp->xp_context == EXPAND_FUNCTIONS
3366 && vim_strchr(xp->xp_pattern, '(') == NULL)
3367 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003368 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 break;
3370 }
3371 else if (cmdidx != CMD_let || got_eq)
3372 {
3373 if (c == '"') /* string */
3374 {
3375 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3376 if (c == '\\' && xp->xp_pattern[1] != NUL)
3377 ++xp->xp_pattern;
3378 xp->xp_context = EXPAND_NOTHING;
3379 }
3380 else if (c == '\'') /* literal string */
3381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003382 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3384 /* skip */ ;
3385 xp->xp_context = EXPAND_NOTHING;
3386 }
3387 else if (c == '|')
3388 {
3389 if (xp->xp_pattern[1] == '|')
3390 {
3391 ++xp->xp_pattern;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
3394 else
3395 xp->xp_context = EXPAND_COMMANDS;
3396 }
3397 else
3398 xp->xp_context = EXPAND_EXPRESSION;
3399 }
3400 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003401 /* Doesn't look like something valid, expand as an expression
3402 * anyway. */
3403 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 arg = xp->xp_pattern;
3405 if (*arg != NUL)
3406 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3407 /* skip */ ;
3408 }
3409 xp->xp_pattern = arg;
3410}
3411
3412#endif /* FEAT_CMDL_COMPL */
3413
3414/*
3415 * ":1,25call func(arg1, arg2)" function call.
3416 */
3417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003418ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 char_u *arg = eap->arg;
3421 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003425 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 linenr_T lnum;
3427 int doesrange;
3428 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003429 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003431 if (eap->skip)
3432 {
3433 /* trans_function_name() doesn't work well when skipping, use eval0()
3434 * instead to skip to any following command, e.g. for:
3435 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003436 ++emsg_skip;
3437 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3438 clear_tv(&rettv);
3439 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003440 return;
3441 }
3442
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003443 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003444 if (fudi.fd_newkey != NULL)
3445 {
3446 /* Still need to give an error message for missing key. */
3447 EMSG2(_(e_dictkey), fudi.fd_newkey);
3448 vim_free(fudi.fd_newkey);
3449 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 if (tofree == NULL)
3451 return;
3452
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003453 /* Increase refcount on dictionary, it could get deleted when evaluating
3454 * the arguments. */
3455 if (fudi.fd_dict != NULL)
3456 ++fudi.fd_dict->dv_refcount;
3457
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003459 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003460 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar532c7802005-01-27 14:44:31 +00003462 /* Skip white space to allow ":call func ()". Not good, but required for
3463 * backward compatibility. */
3464 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 if (*startarg != '(')
3468 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003469 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 goto end;
3471 }
3472
3473 /*
3474 * When skipping, evaluate the function once, to find the end of the
3475 * arguments.
3476 * When the function takes a range, this is discovered after the first
3477 * call, and the loop is broken.
3478 */
3479 if (eap->skip)
3480 {
3481 ++emsg_skip;
3482 lnum = eap->line2; /* do it once, also with an invalid range */
3483 }
3484 else
3485 lnum = eap->line1;
3486 for ( ; lnum <= eap->line2; ++lnum)
3487 {
3488 if (!eap->skip && eap->addr_count > 0)
3489 {
3490 curwin->w_cursor.lnum = lnum;
3491 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003492#ifdef FEAT_VIRTUALEDIT
3493 curwin->w_cursor.coladd = 0;
3494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003497 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003498 eap->line1, eap->line2, &doesrange,
3499 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 failed = TRUE;
3502 break;
3503 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003504
3505 /* Handle a function returning a Funcref, Dictionary or List. */
3506 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3507 {
3508 failed = TRUE;
3509 break;
3510 }
3511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (doesrange || eap->skip)
3514 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003517 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003519 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (aborting())
3521 break;
3522 }
3523 if (eap->skip)
3524 --emsg_skip;
3525
3526 if (!failed)
3527 {
3528 /* Check for trailing illegal characters and a following command. */
3529 if (!ends_excmd(*arg))
3530 {
3531 emsg_severe = TRUE;
3532 EMSG(_(e_trailing));
3533 }
3534 else
3535 eap->nextcmd = check_nextcmd(arg);
3536 }
3537
3538end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003539 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003540 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541}
3542
3543/*
3544 * ":unlet[!] var1 ... " command.
3545 */
3546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003547ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 ex_unletlock(eap, eap->arg, 0);
3550}
3551
3552/*
3553 * ":lockvar" and ":unlockvar" commands
3554 */
3555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003556ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003559 int deep = 2;
3560
3561 if (eap->forceit)
3562 deep = -1;
3563 else if (vim_isdigit(*arg))
3564 {
3565 deep = getdigits(&arg);
3566 arg = skipwhite(arg);
3567 }
3568
3569 ex_unletlock(eap, arg, deep);
3570}
3571
3572/*
3573 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3574 */
3575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003576ex_unletlock(
3577 exarg_T *eap,
3578 char_u *argstart,
3579 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580{
3581 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 do
3587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003589 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003590 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 if (lv.ll_name == NULL)
3592 error = TRUE; /* error but continue parsing */
3593 if (name_end == NULL || (!vim_iswhite(*name_end)
3594 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 if (name_end != NULL)
3597 {
3598 emsg_severe = TRUE;
3599 EMSG(_(e_trailing));
3600 }
3601 if (!(eap->skip || error))
3602 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 break;
3604 }
3605
3606 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 {
3608 if (eap->cmdidx == CMD_unlet)
3609 {
3610 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3611 error = TRUE;
3612 }
3613 else
3614 {
3615 if (do_lock_var(&lv, name_end, deep,
3616 eap->cmdidx == CMD_lockvar) == FAIL)
3617 error = TRUE;
3618 }
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (!eap->skip)
3622 clear_lval(&lv);
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 arg = skipwhite(name_end);
3625 } while (!ends_excmd(*arg));
3626
3627 eap->nextcmd = check_nextcmd(arg);
3628}
3629
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631do_unlet_var(
3632 lval_T *lp,
3633 char_u *name_end,
3634 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 int ret = OK;
3637 int cc;
3638
3639 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 cc = *name_end;
3642 *name_end = NUL;
3643
3644 /* Normal name or expanded name. */
3645 if (check_changedtick(lp->ll_name))
3646 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003651 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003652 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003653 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003654 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 else if (lp->ll_range)
3657 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003659 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003660 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003661
3662 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3663 {
3664 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003665 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003666 return FAIL;
3667 ll_li = li;
3668 ++ll_n1;
3669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670
3671 /* Delete a range of List items. */
3672 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3673 {
3674 li = lp->ll_li->li_next;
3675 listitem_remove(lp->ll_list, lp->ll_li);
3676 lp->ll_li = li;
3677 ++lp->ll_n1;
3678 }
3679 }
3680 else
3681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 /* unlet a List item. */
3684 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003687 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 }
3689
3690 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691}
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693/*
3694 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
3697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003698do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
Bram Moolenaar33570922005-01-25 22:26:29 +00003700 hashtab_T *ht;
3701 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003702 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003703 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003704 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 ht = find_var_ht(name, &varname);
3707 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003709 if (ht == &globvarht)
3710 d = &globvardict;
3711 else if (current_funccal != NULL
3712 && ht == &current_funccal->l_vars.dv_hashtab)
3713 d = &current_funccal->l_vars;
3714 else if (ht == &compat_hashtab)
3715 d = &vimvardict;
3716 else
3717 {
3718 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3719 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3720 }
3721 if (d == NULL)
3722 {
3723 EMSG2(_(e_intern2), "do_unlet()");
3724 return FAIL;
3725 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003726 hi = hash_find(ht, varname);
3727 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003728 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003730 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003731 || var_check_ro(di->di_flags, name, FALSE)
3732 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003733 return FAIL;
3734
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 delete_var(ht, hi);
3736 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739 if (forceit)
3740 return OK;
3741 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743}
3744
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745/*
3746 * Lock or unlock variable indicated by "lp".
3747 * "deep" is the levels to go (-1 for unlimited);
3748 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3749 */
3750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003751do_lock_var(
3752 lval_T *lp,
3753 char_u *name_end,
3754 int deep,
3755 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756{
3757 int ret = OK;
3758 int cc;
3759 dictitem_T *di;
3760
3761 if (deep == 0) /* nothing to do */
3762 return OK;
3763
3764 if (lp->ll_tv == NULL)
3765 {
3766 cc = *name_end;
3767 *name_end = NUL;
3768
3769 /* Normal name or expanded name. */
3770 if (check_changedtick(lp->ll_name))
3771 ret = FAIL;
3772 else
3773 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003774 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 if (di == NULL)
3776 ret = FAIL;
3777 else
3778 {
3779 if (lock)
3780 di->di_flags |= DI_FLAGS_LOCK;
3781 else
3782 di->di_flags &= ~DI_FLAGS_LOCK;
3783 item_lock(&di->di_tv, deep, lock);
3784 }
3785 }
3786 *name_end = cc;
3787 }
3788 else if (lp->ll_range)
3789 {
3790 listitem_T *li = lp->ll_li;
3791
3792 /* (un)lock a range of List items. */
3793 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3794 {
3795 item_lock(&li->li_tv, deep, lock);
3796 li = li->li_next;
3797 ++lp->ll_n1;
3798 }
3799 }
3800 else if (lp->ll_list != NULL)
3801 /* (un)lock a List item. */
3802 item_lock(&lp->ll_li->li_tv, deep, lock);
3803 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003804 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 item_lock(&lp->ll_di->di_tv, deep, lock);
3806
3807 return ret;
3808}
3809
3810/*
3811 * Lock or unlock an item. "deep" is nr of levels to go.
3812 */
3813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815{
3816 static int recurse = 0;
3817 list_T *l;
3818 listitem_T *li;
3819 dict_T *d;
3820 hashitem_T *hi;
3821 int todo;
3822
3823 if (recurse >= DICT_MAXNEST)
3824 {
3825 EMSG(_("E743: variable nested too deep for (un)lock"));
3826 return;
3827 }
3828 if (deep == 0)
3829 return;
3830 ++recurse;
3831
3832 /* lock/unlock the item itself */
3833 if (lock)
3834 tv->v_lock |= VAR_LOCKED;
3835 else
3836 tv->v_lock &= ~VAR_LOCKED;
3837
3838 switch (tv->v_type)
3839 {
3840 case VAR_LIST:
3841 if ((l = tv->vval.v_list) != NULL)
3842 {
3843 if (lock)
3844 l->lv_lock |= VAR_LOCKED;
3845 else
3846 l->lv_lock &= ~VAR_LOCKED;
3847 if (deep < 0 || deep > 1)
3848 /* recursive: lock/unlock the items the List contains */
3849 for (li = l->lv_first; li != NULL; li = li->li_next)
3850 item_lock(&li->li_tv, deep - 1, lock);
3851 }
3852 break;
3853 case VAR_DICT:
3854 if ((d = tv->vval.v_dict) != NULL)
3855 {
3856 if (lock)
3857 d->dv_lock |= VAR_LOCKED;
3858 else
3859 d->dv_lock &= ~VAR_LOCKED;
3860 if (deep < 0 || deep > 1)
3861 {
3862 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003864 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
3869 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3870 }
3871 }
3872 }
3873 }
3874 }
3875 --recurse;
3876}
3877
Bram Moolenaara40058a2005-07-11 22:42:07 +00003878/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003879 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3880 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003881 */
3882 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003883tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003884{
3885 return (tv->v_lock & VAR_LOCKED)
3886 || (tv->v_type == VAR_LIST
3887 && tv->vval.v_list != NULL
3888 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3889 || (tv->v_type == VAR_DICT
3890 && tv->vval.v_dict != NULL
3891 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3892}
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3895/*
3896 * Delete all "menutrans_" variables.
3897 */
3898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003899del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
Bram Moolenaar33570922005-01-25 22:26:29 +00003901 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003902 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003906 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003907 {
3908 if (!HASHITEM_EMPTY(hi))
3909 {
3910 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3912 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003913 }
3914 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916}
3917#endif
3918
3919#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3920
3921/*
3922 * Local string buffer for the next two functions to store a variable name
3923 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3924 * get_user_var_name().
3925 */
3926
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003927static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929static char_u *varnamebuf = NULL;
3930static int varnamebuflen = 0;
3931
3932/*
3933 * Function to concatenate a prefix and a variable name.
3934 */
3935 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 int len;
3939
3940 len = (int)STRLEN(name) + 3;
3941 if (len > varnamebuflen)
3942 {
3943 vim_free(varnamebuf);
3944 len += 10; /* some additional space */
3945 varnamebuf = alloc(len);
3946 if (varnamebuf == NULL)
3947 {
3948 varnamebuflen = 0;
3949 return NULL;
3950 }
3951 varnamebuflen = len;
3952 }
3953 *varnamebuf = prefix;
3954 varnamebuf[1] = ':';
3955 STRCPY(varnamebuf + 2, name);
3956 return varnamebuf;
3957}
3958
3959/*
3960 * Function given to ExpandGeneric() to obtain the list of user defined
3961 * (global/buffer/window/built-in) variable names.
3962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 static long_u gdone;
3967 static long_u bdone;
3968 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003969#ifdef FEAT_WINDOWS
3970 static long_u tdone;
3971#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003972 static int vidx;
3973 static hashitem_T *hi;
3974 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003979#ifdef FEAT_WINDOWS
3980 tdone = 0;
3981#endif
3982 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003983
3984 /* Global variables */
3985 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003987 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 else
3990 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3994 return cat_prefix_varname('g', hi->hi_key);
3995 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003997
3998 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003999 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004002 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004004 else
4005 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004006 while (HASHITEM_EMPTY(hi))
4007 ++hi;
4008 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return (char_u *)"b:changedtick";
4014 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004015
4016 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004017 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004020 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 else
4023 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 while (HASHITEM_EMPTY(hi))
4025 ++hi;
4026 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004028
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029#ifdef FEAT_WINDOWS
4030 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004031 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 if (tdone < ht->ht_used)
4033 {
4034 if (tdone++ == 0)
4035 hi = ht->ht_array;
4036 else
4037 ++hi;
4038 while (HASHITEM_EMPTY(hi))
4039 ++hi;
4040 return cat_prefix_varname('t', hi->hi_key);
4041 }
4042#endif
4043
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 /* v: variables */
4045 if (vidx < VV_LEN)
4046 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 vim_free(varnamebuf);
4049 varnamebuf = NULL;
4050 varnamebuflen = 0;
4051 return NULL;
4052}
4053
4054#endif /* FEAT_CMDL_COMPL */
4055
4056/*
4057 * types for expressions.
4058 */
4059typedef enum
4060{
4061 TYPE_UNKNOWN = 0
4062 , TYPE_EQUAL /* == */
4063 , TYPE_NEQUAL /* != */
4064 , TYPE_GREATER /* > */
4065 , TYPE_GEQUAL /* >= */
4066 , TYPE_SMALLER /* < */
4067 , TYPE_SEQUAL /* <= */
4068 , TYPE_MATCH /* =~ */
4069 , TYPE_NOMATCH /* !~ */
4070} exptype_T;
4071
4072/*
4073 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4076 */
4077
4078/*
4079 * Handle zero level expression.
4080 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004082 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * Return OK or FAIL.
4084 */
4085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004086eval0(
4087 char_u *arg,
4088 typval_T *rettv,
4089 char_u **nextcmd,
4090 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 int ret;
4093 char_u *p;
4094
4095 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (ret == FAIL || !ends_excmd(*p))
4098 {
4099 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 /*
4102 * Report the invalid expression unless the expression evaluation has
4103 * been cancelled due to an aborting error, an interrupt, or an
4104 * exception.
4105 */
4106 if (!aborting())
4107 EMSG2(_(e_invexpr2), arg);
4108 ret = FAIL;
4109 }
4110 if (nextcmd != NULL)
4111 *nextcmd = check_nextcmd(p);
4112
4113 return ret;
4114}
4115
4116/*
4117 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004118 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * "arg" must point to the first non-white of the expression.
4121 * "arg" is advanced to the next non-white after the recognized expression.
4122 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
4124 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * Return OK or FAIL.
4126 */
4127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004128eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004131 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 /*
4134 * Get the first variable.
4135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 return FAIL;
4138
4139 if ((*arg)[0] == '?')
4140 {
4141 result = FALSE;
4142 if (evaluate)
4143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 int error = FALSE;
4145
4146 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 if (error)
4150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
4153 /*
4154 * Get the second variable.
4155 */
4156 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Check for the ":".
4162 */
4163 if ((*arg)[0] != ':')
4164 {
4165 EMSG(_("E109: Missing ':' after '?'"));
4166 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169 }
4170
4171 /*
4172 * Get the third variable.
4173 */
4174 *arg = skipwhite(*arg + 1);
4175 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4176 {
4177 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180 }
4181 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Handle first level expression:
4190 * expr2 || expr2 || expr2 logical OR
4191 *
4192 * "arg" must point to the first non-white of the expression.
4193 * "arg" is advanced to the next non-white after the recognized expression.
4194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 long result;
4202 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 /*
4206 * Get the first variable.
4207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210
4211 /*
4212 * Repeat until there is no following "||".
4213 */
4214 first = TRUE;
4215 result = FALSE;
4216 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4217 {
4218 if (evaluate && first)
4219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (error)
4224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 first = FALSE;
4226 }
4227
4228 /*
4229 * Get the second variable.
4230 */
4231 *arg = skipwhite(*arg + 2);
4232 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4233 return FAIL;
4234
4235 /*
4236 * Compute the result.
4237 */
4238 if (evaluate && !result)
4239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (error)
4244 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 if (evaluate)
4247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252
4253 return OK;
4254}
4255
4256/*
4257 * Handle second level expression:
4258 * expr3 && expr3 && expr3 logical AND
4259 *
4260 * "arg" must point to the first non-white of the expression.
4261 * "arg" is advanced to the next non-white after the recognized expression.
4262 *
4263 * Return OK or FAIL.
4264 */
4265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004266eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
Bram Moolenaar33570922005-01-25 22:26:29 +00004268 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 long result;
4270 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 /*
4274 * Get the first variable.
4275 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FAIL;
4278
4279 /*
4280 * Repeat until there is no following "&&".
4281 */
4282 first = TRUE;
4283 result = TRUE;
4284 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4285 {
4286 if (evaluate && first)
4287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (error)
4292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 first = FALSE;
4294 }
4295
4296 /*
4297 * Get the second variable.
4298 */
4299 *arg = skipwhite(*arg + 2);
4300 if (eval4(arg, &var2, evaluate && result) == FAIL)
4301 return FAIL;
4302
4303 /*
4304 * Compute the result.
4305 */
4306 if (evaluate && result)
4307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 if (error)
4312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314 if (evaluate)
4315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 rettv->v_type = VAR_NUMBER;
4317 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 }
4320
4321 return OK;
4322}
4323
4324/*
4325 * Handle third level expression:
4326 * var1 == var2
4327 * var1 =~ var2
4328 * var1 != var2
4329 * var1 !~ var2
4330 * var1 > var2
4331 * var1 >= var2
4332 * var1 < var2
4333 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004334 * var1 is var2
4335 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 *
4337 * "arg" must point to the first non-white of the expression.
4338 * "arg" is advanced to the next non-white after the recognized expression.
4339 *
4340 * Return OK or FAIL.
4341 */
4342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004343eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
Bram Moolenaar33570922005-01-25 22:26:29 +00004345 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 char_u *p;
4347 int i;
4348 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004349 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int len = 2;
4351 long n1, n2;
4352 char_u *s1, *s2;
4353 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4354 regmatch_T regmatch;
4355 int ic;
4356 char_u *save_cpo;
4357
4358 /*
4359 * Get the first variable.
4360 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return FAIL;
4363
4364 p = *arg;
4365 switch (p[0])
4366 {
4367 case '=': if (p[1] == '=')
4368 type = TYPE_EQUAL;
4369 else if (p[1] == '~')
4370 type = TYPE_MATCH;
4371 break;
4372 case '!': if (p[1] == '=')
4373 type = TYPE_NEQUAL;
4374 else if (p[1] == '~')
4375 type = TYPE_NOMATCH;
4376 break;
4377 case '>': if (p[1] != '=')
4378 {
4379 type = TYPE_GREATER;
4380 len = 1;
4381 }
4382 else
4383 type = TYPE_GEQUAL;
4384 break;
4385 case '<': if (p[1] != '=')
4386 {
4387 type = TYPE_SMALLER;
4388 len = 1;
4389 }
4390 else
4391 type = TYPE_SEQUAL;
4392 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 case 'i': if (p[1] == 's')
4394 {
4395 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4396 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004397 i = p[len];
4398 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 {
4400 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4401 type_is = TRUE;
4402 }
4403 }
4404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406
4407 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
4410 if (type != TYPE_UNKNOWN)
4411 {
4412 /* extra question mark appended: ignore case */
4413 if (p[len] == '?')
4414 {
4415 ic = TRUE;
4416 ++len;
4417 }
4418 /* extra '#' appended: match case */
4419 else if (p[len] == '#')
4420 {
4421 ic = FALSE;
4422 ++len;
4423 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 else
4426 ic = p_ic;
4427
4428 /*
4429 * Get the second variable.
4430 */
4431 *arg = skipwhite(p + len);
4432 if (eval5(arg, &var2, evaluate) == FAIL)
4433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 return FAIL;
4436 }
4437
4438 if (evaluate)
4439 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 if (type_is && rettv->v_type != var2.v_type)
4441 {
4442 /* For "is" a different type always means FALSE, for "notis"
4443 * it means TRUE. */
4444 n1 = (type == TYPE_NEQUAL);
4445 }
4446 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4447 {
4448 if (type_is)
4449 {
4450 n1 = (rettv->v_type == var2.v_type
4451 && rettv->vval.v_list == var2.vval.v_list);
4452 if (type == TYPE_NEQUAL)
4453 n1 = !n1;
4454 }
4455 else if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004459 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004460 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004461 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004469 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4470 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 if (type == TYPE_NEQUAL)
4472 n1 = !n1;
4473 }
4474 }
4475
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004476 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4477 {
4478 if (type_is)
4479 {
4480 n1 = (rettv->v_type == var2.v_type
4481 && rettv->vval.v_dict == var2.vval.v_dict);
4482 if (type == TYPE_NEQUAL)
4483 n1 = !n1;
4484 }
4485 else if (rettv->v_type != var2.v_type
4486 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4487 {
4488 if (rettv->v_type != var2.v_type)
4489 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4490 else
4491 EMSG(_("E736: Invalid operation for Dictionary"));
4492 clear_tv(rettv);
4493 clear_tv(&var2);
4494 return FAIL;
4495 }
4496 else
4497 {
4498 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004499 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4500 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4507 {
4508 if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004512 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004514 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Funcrefs for being equal or unequal. */
4522 if (rettv->vval.v_string == NULL
4523 || var2.vval.v_string == NULL)
4524 n1 = FALSE;
4525 else
4526 n1 = STRCMP(rettv->vval.v_string,
4527 var2.vval.v_string) == 0;
4528 if (type == TYPE_NEQUAL)
4529 n1 = !n1;
4530 }
4531 }
4532
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004533#ifdef FEAT_FLOAT
4534 /*
4535 * If one of the two variables is a float, compare as a float.
4536 * When using "=~" or "!~", always compare as string.
4537 */
4538 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4539 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4540 {
4541 float_T f1, f2;
4542
4543 if (rettv->v_type == VAR_FLOAT)
4544 f1 = rettv->vval.v_float;
4545 else
4546 f1 = get_tv_number(rettv);
4547 if (var2.v_type == VAR_FLOAT)
4548 f2 = var2.vval.v_float;
4549 else
4550 f2 = get_tv_number(&var2);
4551 n1 = FALSE;
4552 switch (type)
4553 {
4554 case TYPE_EQUAL: n1 = (f1 == f2); break;
4555 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4556 case TYPE_GREATER: n1 = (f1 > f2); break;
4557 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4558 case TYPE_SMALLER: n1 = (f1 < f2); break;
4559 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4560 case TYPE_UNKNOWN:
4561 case TYPE_MATCH:
4562 case TYPE_NOMATCH: break; /* avoid gcc warning */
4563 }
4564 }
4565#endif
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /*
4568 * If one of the two variables is a number, compare as a number.
4569 * When using "=~" or "!~", always compare as string.
4570 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 n1 = get_tv_number(rettv);
4575 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 switch (type)
4577 {
4578 case TYPE_EQUAL: n1 = (n1 == n2); break;
4579 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4580 case TYPE_GREATER: n1 = (n1 > n2); break;
4581 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4582 case TYPE_SMALLER: n1 = (n1 < n2); break;
4583 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4584 case TYPE_UNKNOWN:
4585 case TYPE_MATCH:
4586 case TYPE_NOMATCH: break; /* avoid gcc warning */
4587 }
4588 }
4589 else
4590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 s1 = get_tv_string_buf(rettv, buf1);
4592 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4594 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4595 else
4596 i = 0;
4597 n1 = FALSE;
4598 switch (type)
4599 {
4600 case TYPE_EQUAL: n1 = (i == 0); break;
4601 case TYPE_NEQUAL: n1 = (i != 0); break;
4602 case TYPE_GREATER: n1 = (i > 0); break;
4603 case TYPE_GEQUAL: n1 = (i >= 0); break;
4604 case TYPE_SMALLER: n1 = (i < 0); break;
4605 case TYPE_SEQUAL: n1 = (i <= 0); break;
4606
4607 case TYPE_MATCH:
4608 case TYPE_NOMATCH:
4609 /* avoid 'l' flag in 'cpoptions' */
4610 save_cpo = p_cpo;
4611 p_cpo = (char_u *)"";
4612 regmatch.regprog = vim_regcomp(s2,
4613 RE_MAGIC + RE_STRING);
4614 regmatch.rm_ic = ic;
4615 if (regmatch.regprog != NULL)
4616 {
4617 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004618 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (type == TYPE_NOMATCH)
4620 n1 = !n1;
4621 }
4622 p_cpo = save_cpo;
4623 break;
4624
4625 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4626 }
4627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 clear_tv(rettv);
4629 clear_tv(&var2);
4630 rettv->v_type = VAR_NUMBER;
4631 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 }
4634
4635 return OK;
4636}
4637
4638/*
4639 * Handle fourth level expression:
4640 * + number addition
4641 * - number subtraction
4642 * . string concatenation
4643 *
4644 * "arg" must point to the first non-white of the expression.
4645 * "arg" is advanced to the next non-white after the recognized expression.
4646 *
4647 * Return OK or FAIL.
4648 */
4649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
Bram Moolenaar33570922005-01-25 22:26:29 +00004652 typval_T var2;
4653 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 int op;
4655 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004656#ifdef FEAT_FLOAT
4657 float_T f1 = 0, f2 = 0;
4658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u *s1, *s2;
4660 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4661 char_u *p;
4662
4663 /*
4664 * Get the first variable.
4665 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004666 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return FAIL;
4668
4669 /*
4670 * Repeat computing, until no '+', '-' or '.' is following.
4671 */
4672 for (;;)
4673 {
4674 op = **arg;
4675 if (op != '+' && op != '-' && op != '.')
4676 break;
4677
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004678 if ((op != '+' || rettv->v_type != VAR_LIST)
4679#ifdef FEAT_FLOAT
4680 && (op == '.' || rettv->v_type != VAR_FLOAT)
4681#endif
4682 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 {
4684 /* For "list + ...", an illegal use of the first operand as
4685 * a number cannot be determined before evaluating the 2nd
4686 * operand: if this is also a list, all is ok.
4687 * For "something . ...", "something - ..." or "non-list + ...",
4688 * we know that the first operand needs to be a string or number
4689 * without evaluating the 2nd operand. So check before to avoid
4690 * side effects after an error. */
4691 if (evaluate && get_tv_string_chk(rettv) == NULL)
4692 {
4693 clear_tv(rettv);
4694 return FAIL;
4695 }
4696 }
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /*
4699 * Get the second variable.
4700 */
4701 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004702 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FAIL;
4706 }
4707
4708 if (evaluate)
4709 {
4710 /*
4711 * Compute the result.
4712 */
4713 if (op == '.')
4714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4716 s2 = get_tv_string_buf_chk(&var2, buf2);
4717 if (s2 == NULL) /* type error ? */
4718 {
4719 clear_tv(rettv);
4720 clear_tv(&var2);
4721 return FAIL;
4722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004723 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004724 clear_tv(rettv);
4725 rettv->v_type = VAR_STRING;
4726 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004728 else if (op == '+' && rettv->v_type == VAR_LIST
4729 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004730 {
4731 /* concatenate Lists */
4732 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4733 &var3) == FAIL)
4734 {
4735 clear_tv(rettv);
4736 clear_tv(&var2);
4737 return FAIL;
4738 }
4739 clear_tv(rettv);
4740 *rettv = var3;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 else
4743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 int error = FALSE;
4745
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749 f1 = rettv->vval.v_float;
4750 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752 else
4753#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 n1 = get_tv_number_chk(rettv, &error);
4756 if (error)
4757 {
4758 /* This can only happen for "list + non-list". For
4759 * "non-list + ..." or "something - ...", we returned
4760 * before evaluating the 2nd operand. */
4761 clear_tv(rettv);
4762 return FAIL;
4763 }
4764#ifdef FEAT_FLOAT
4765 if (var2.v_type == VAR_FLOAT)
4766 f1 = n1;
4767#endif
4768 }
4769#ifdef FEAT_FLOAT
4770 if (var2.v_type == VAR_FLOAT)
4771 {
4772 f2 = var2.vval.v_float;
4773 n2 = 0;
4774 }
4775 else
4776#endif
4777 {
4778 n2 = get_tv_number_chk(&var2, &error);
4779 if (error)
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
4785#ifdef FEAT_FLOAT
4786 if (rettv->v_type == VAR_FLOAT)
4787 f2 = n2;
4788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791
4792#ifdef FEAT_FLOAT
4793 /* If there is a float on either side the result is a float. */
4794 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4795 {
4796 if (op == '+')
4797 f1 = f1 + f2;
4798 else
4799 f1 = f1 - f2;
4800 rettv->v_type = VAR_FLOAT;
4801 rettv->vval.v_float = f1;
4802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804#endif
4805 {
4806 if (op == '+')
4807 n1 = n1 + n2;
4808 else
4809 n1 = n1 - n2;
4810 rettv->v_type = VAR_NUMBER;
4811 rettv->vval.v_number = n1;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 }
4817 return OK;
4818}
4819
4820/*
4821 * Handle fifth level expression:
4822 * * number multiplication
4823 * / number division
4824 * % number modulo
4825 *
4826 * "arg" must point to the first non-white of the expression.
4827 * "arg" is advanced to the next non-white after the recognized expression.
4828 *
4829 * Return OK or FAIL.
4830 */
4831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004832eval6(
4833 char_u **arg,
4834 typval_T *rettv,
4835 int evaluate,
4836 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaar33570922005-01-25 22:26:29 +00004838 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 int op;
4840 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#ifdef FEAT_FLOAT
4842 int use_float = FALSE;
4843 float_T f1 = 0, f2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 /*
4848 * Get the first variable.
4849 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004850 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 return FAIL;
4852
4853 /*
4854 * Repeat computing, until no '*', '/' or '%' is following.
4855 */
4856 for (;;)
4857 {
4858 op = **arg;
4859 if (op != '*' && op != '/' && op != '%')
4860 break;
4861
4862 if (evaluate)
4863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864#ifdef FEAT_FLOAT
4865 if (rettv->v_type == VAR_FLOAT)
4866 {
4867 f1 = rettv->vval.v_float;
4868 use_float = TRUE;
4869 n1 = 0;
4870 }
4871 else
4872#endif
4873 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 if (error)
4876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 else
4879 n1 = 0;
4880
4881 /*
4882 * Get the second variable.
4883 */
4884 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
4887
4888 if (evaluate)
4889 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 if (var2.v_type == VAR_FLOAT)
4892 {
4893 if (!use_float)
4894 {
4895 f1 = n1;
4896 use_float = TRUE;
4897 }
4898 f2 = var2.vval.v_float;
4899 n2 = 0;
4900 }
4901 else
4902#endif
4903 {
4904 n2 = get_tv_number_chk(&var2, &error);
4905 clear_tv(&var2);
4906 if (error)
4907 return FAIL;
4908#ifdef FEAT_FLOAT
4909 if (use_float)
4910 f2 = n2;
4911#endif
4912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 /*
4915 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918#ifdef FEAT_FLOAT
4919 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 if (op == '*')
4922 f1 = f1 * f2;
4923 else if (op == '/')
4924 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004925# ifdef VMS
4926 /* VMS crashes on divide by zero, work around it */
4927 if (f2 == 0.0)
4928 {
4929 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004930 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004931 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004932 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004933 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004934 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004935 }
4936 else
4937 f1 = f1 / f2;
4938# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 /* We rely on the floating point library to handle divide
4940 * by zero to result in "inf" and not a crash. */
4941 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004942# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004946 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 return FAIL;
4948 }
4949 rettv->v_type = VAR_FLOAT;
4950 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 if (op == '*')
4956 n1 = n1 * n2;
4957 else if (op == '/')
4958 {
4959 if (n2 == 0) /* give an error message? */
4960 {
4961 if (n1 == 0)
4962 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4963 else if (n1 < 0)
4964 n1 = -0x7fffffffL;
4965 else
4966 n1 = 0x7fffffffL;
4967 }
4968 else
4969 n1 = n1 / n2;
4970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 {
4973 if (n2 == 0) /* give an error message? */
4974 n1 = 0;
4975 else
4976 n1 = n1 % n2;
4977 }
4978 rettv->v_type = VAR_NUMBER;
4979 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 }
4983
4984 return OK;
4985}
4986
4987/*
4988 * Handle sixth level expression:
4989 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004990 * "string" string constant
4991 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 * &option-name option value
4993 * @r register contents
4994 * identifier variable value
4995 * function() function call
4996 * $VAR environment variable
4997 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004998 * [expr, expr] List
4999 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 *
5001 * Also handle:
5002 * ! in front logical NOT
5003 * - in front unary minus
5004 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 * trailing [] subscript in String or List
5006 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 *
5008 * "arg" must point to the first non-white of the expression.
5009 * "arg" is advanced to the next non-white after the recognized expression.
5010 *
5011 * Return OK or FAIL.
5012 */
5013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005014eval7(
5015 char_u **arg,
5016 typval_T *rettv,
5017 int evaluate,
5018 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 long n;
5021 int len;
5022 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_u *start_leader, *end_leader;
5024 int ret = OK;
5025 char_u *alias;
5026
5027 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005029 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 /*
5034 * Skip '!' and '-' characters. They are handled later.
5035 */
5036 start_leader = *arg;
5037 while (**arg == '!' || **arg == '-' || **arg == '+')
5038 *arg = skipwhite(*arg + 1);
5039 end_leader = *arg;
5040
5041 switch (**arg)
5042 {
5043 /*
5044 * Number constant.
5045 */
5046 case '0':
5047 case '1':
5048 case '2':
5049 case '3':
5050 case '4':
5051 case '5':
5052 case '6':
5053 case '7':
5054 case '8':
5055 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056 {
5057#ifdef FEAT_FLOAT
5058 char_u *p = skipdigits(*arg + 1);
5059 int get_float = FALSE;
5060
5061 /* We accept a float when the format matches
5062 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005063 * strict to avoid backwards compatibility problems.
5064 * Don't look for a float after the "." operator, so that
5065 * ":let vers = 1.2.3" doesn't fail. */
5066 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068 get_float = TRUE;
5069 p = skipdigits(p + 2);
5070 if (*p == 'e' || *p == 'E')
5071 {
5072 ++p;
5073 if (*p == '-' || *p == '+')
5074 ++p;
5075 if (!vim_isdigit(*p))
5076 get_float = FALSE;
5077 else
5078 p = skipdigits(p + 1);
5079 }
5080 if (ASCII_ISALPHA(*p) || *p == '.')
5081 get_float = FALSE;
5082 }
5083 if (get_float)
5084 {
5085 float_T f;
5086
5087 *arg += string2float(*arg, &f);
5088 if (evaluate)
5089 {
5090 rettv->v_type = VAR_FLOAT;
5091 rettv->vval.v_float = f;
5092 }
5093 }
5094 else
5095#endif
5096 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005097 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005098 *arg += len;
5099 if (evaluate)
5100 {
5101 rettv->v_type = VAR_NUMBER;
5102 rettv->vval.v_number = n;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 /*
5109 * String constant: "string".
5110 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 break;
5119
5120 /*
5121 * List: [expr, expr]
5122 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125
5126 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 * Dictionary: {key: val, key: val}
5128 */
5129 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5130 break;
5131
5132 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005133 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005135 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 break;
5137
5138 /*
5139 * Environment variable: $VAR.
5140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 break;
5143
5144 /*
5145 * Register contents: @r.
5146 */
5147 case '@': ++*arg;
5148 if (evaluate)
5149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005151 rettv->vval.v_string = get_reg_contents(**arg,
5152 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 if (**arg != NUL)
5155 ++*arg;
5156 break;
5157
5158 /*
5159 * nested expression: (expression).
5160 */
5161 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (**arg == ')')
5164 ++*arg;
5165 else if (ret == OK)
5166 {
5167 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 ret = FAIL;
5170 }
5171 break;
5172
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176
5177 if (ret == NOTDONE)
5178 {
5179 /*
5180 * Must be a variable or function name.
5181 * Can also be a curly-braces kind of name: {expr}.
5182 */
5183 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 if (alias != NULL)
5186 s = alias;
5187
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 ret = FAIL;
5190 else
5191 {
5192 if (**arg == '(') /* recursive! */
5193 {
5194 /* If "s" is the name of a variable of type VAR_FUNC
5195 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005196 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197
5198 /* Invoke the function. */
5199 ret = get_func_tv(s, len, rettv, arg,
5200 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005201 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005202
5203 /* If evaluate is FALSE rettv->v_type was not set in
5204 * get_func_tv, but it's needed in handle_subscript() to parse
5205 * what follows. So set it here. */
5206 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5207 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005208 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005209 rettv->v_type = VAR_FUNC;
5210 }
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 /* Stop the expression evaluation when immediately
5213 * aborting on error, or when an interrupt occurred or
5214 * an exception was thrown but not caught. */
5215 if (aborting())
5216 {
5217 if (ret == OK)
5218 clear_tv(rettv);
5219 ret = FAIL;
5220 }
5221 }
5222 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005223 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005224 else
5225 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005227 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 }
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 *arg = skipwhite(*arg);
5231
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005232 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5233 * expr(expr). */
5234 if (ret == OK)
5235 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 /*
5238 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5239 */
5240 if (ret == OK && evaluate && end_leader > start_leader)
5241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005242 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005243 int val = 0;
5244#ifdef FEAT_FLOAT
5245 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247 if (rettv->v_type == VAR_FLOAT)
5248 f = rettv->vval.v_float;
5249 else
5250#endif
5251 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005254 clear_tv(rettv);
5255 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 else
5258 {
5259 while (end_leader > start_leader)
5260 {
5261 --end_leader;
5262 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005263 {
5264#ifdef FEAT_FLOAT
5265 if (rettv->v_type == VAR_FLOAT)
5266 f = !f;
5267 else
5268#endif
5269 val = !val;
5270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005272 {
5273#ifdef FEAT_FLOAT
5274 if (rettv->v_type == VAR_FLOAT)
5275 f = -f;
5276 else
5277#endif
5278 val = -val;
5279 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281#ifdef FEAT_FLOAT
5282 if (rettv->v_type == VAR_FLOAT)
5283 {
5284 clear_tv(rettv);
5285 rettv->vval.v_float = f;
5286 }
5287 else
5288#endif
5289 {
5290 clear_tv(rettv);
5291 rettv->v_type = VAR_NUMBER;
5292 rettv->vval.v_number = val;
5293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296
5297 return ret;
5298}
5299
5300/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005301 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5302 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5304 */
5305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005306eval_index(
5307 char_u **arg,
5308 typval_T *rettv,
5309 int evaluate,
5310 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311{
5312 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 long len = -1;
5316 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005320 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005322 if (verbose)
5323 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 return FAIL;
5325 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005326#ifdef FEAT_FLOAT
5327 else if (rettv->v_type == VAR_FLOAT)
5328 {
5329 if (verbose)
5330 EMSG(_(e_float_as_string));
5331 return FAIL;
5332 }
5333#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005334 else if (rettv->v_type == VAR_SPECIAL)
5335 {
5336 return FAIL;
5337 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005339 init_tv(&var1);
5340 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 /*
5344 * dict.name
5345 */
5346 key = *arg + 1;
5347 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5348 ;
5349 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 }
5353 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 /*
5356 * something[idx]
5357 *
5358 * Get the (first) variable from inside the [].
5359 */
5360 *arg = skipwhite(*arg + 1);
5361 if (**arg == ':')
5362 empty1 = TRUE;
5363 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5364 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005365 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5366 {
5367 /* not a number or string */
5368 clear_tv(&var1);
5369 return FAIL;
5370 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371
5372 /*
5373 * Get the second variable from inside the [:].
5374 */
5375 if (**arg == ':')
5376 {
5377 range = TRUE;
5378 *arg = skipwhite(*arg + 1);
5379 if (**arg == ']')
5380 empty2 = TRUE;
5381 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005383 if (!empty1)
5384 clear_tv(&var1);
5385 return FAIL;
5386 }
5387 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5388 {
5389 /* not a number or string */
5390 if (!empty1)
5391 clear_tv(&var1);
5392 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 return FAIL;
5394 }
5395 }
5396
5397 /* Check for the ']'. */
5398 if (**arg != ']')
5399 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005400 if (verbose)
5401 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 clear_tv(&var1);
5403 if (range)
5404 clear_tv(&var2);
5405 return FAIL;
5406 }
5407 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 }
5409
5410 if (evaluate)
5411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 n1 = 0;
5413 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 n1 = get_tv_number(&var1);
5416 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 }
5418 if (range)
5419 {
5420 if (empty2)
5421 n2 = -1;
5422 else
5423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 n2 = get_tv_number(&var2);
5425 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 }
5427 }
5428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 {
5431 case VAR_NUMBER:
5432 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 len = (long)STRLEN(s);
5435 if (range)
5436 {
5437 /* The resulting variable is a substring. If the indexes
5438 * are out of range the result is empty. */
5439 if (n1 < 0)
5440 {
5441 n1 = len + n1;
5442 if (n1 < 0)
5443 n1 = 0;
5444 }
5445 if (n2 < 0)
5446 n2 = len + n2;
5447 else if (n2 >= len)
5448 n2 = len;
5449 if (n1 >= len || n2 < 0 || n1 > n2)
5450 s = NULL;
5451 else
5452 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5453 }
5454 else
5455 {
5456 /* The resulting variable is a string of a single
5457 * character. If the index is too big or negative the
5458 * result is empty. */
5459 if (n1 >= len || n1 < 0)
5460 s = NULL;
5461 else
5462 s = vim_strnsave(s + n1, 1);
5463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(rettv);
5465 rettv->v_type = VAR_STRING;
5466 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 break;
5468
5469 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 if (n1 < 0)
5472 n1 = len + n1;
5473 if (!empty1 && (n1 < 0 || n1 >= len))
5474 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005475 /* For a range we allow invalid values and return an empty
5476 * list. A list index out of range is an error. */
5477 if (!range)
5478 {
5479 if (verbose)
5480 EMSGN(_(e_listidx), n1);
5481 return FAIL;
5482 }
5483 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485 if (range)
5486 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 list_T *l;
5488 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489
5490 if (n2 < 0)
5491 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005492 else if (n2 >= len)
5493 n2 = len - 1;
5494 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005495 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 l = list_alloc();
5497 if (l == NULL)
5498 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 n1 <= n2; ++n1)
5501 {
5502 if (list_append_tv(l, &item->li_tv) == FAIL)
5503 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005504 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 return FAIL;
5506 }
5507 item = item->li_next;
5508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 clear_tv(rettv);
5510 rettv->v_type = VAR_LIST;
5511 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005512 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 }
5514 else
5515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005516 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 clear_tv(rettv);
5518 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 }
5520 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521
5522 case VAR_DICT:
5523 if (range)
5524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 if (verbose)
5526 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 if (len == -1)
5528 clear_tv(&var1);
5529 return FAIL;
5530 }
5531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005533
5534 if (len == -1)
5535 {
5536 key = get_tv_string(&var1);
5537 if (*key == NUL)
5538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005539 if (verbose)
5540 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541 clear_tv(&var1);
5542 return FAIL;
5543 }
5544 }
5545
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005546 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005548 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005549 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 if (len == -1)
5551 clear_tv(&var1);
5552 if (item == NULL)
5553 return FAIL;
5554
5555 copy_tv(&item->di_tv, &var1);
5556 clear_tv(rettv);
5557 *rettv = var1;
5558 }
5559 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 }
5561 }
5562
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 return OK;
5564}
5565
5566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 * Get an option value.
5568 * "arg" points to the '&' or '+' before the option name.
5569 * "arg" is advanced to character after the option name.
5570 * Return OK or FAIL.
5571 */
5572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005573get_option_tv(
5574 char_u **arg,
5575 typval_T *rettv, /* when NULL, only check if option exists */
5576 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 char_u *option_end;
5579 long numval;
5580 char_u *stringval;
5581 int opt_type;
5582 int c;
5583 int working = (**arg == '+'); /* has("+option") */
5584 int ret = OK;
5585 int opt_flags;
5586
5587 /*
5588 * Isolate the option name and find its value.
5589 */
5590 option_end = find_option_end(arg, &opt_flags);
5591 if (option_end == NULL)
5592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 EMSG2(_("E112: Option name missing: %s"), *arg);
5595 return FAIL;
5596 }
5597
5598 if (!evaluate)
5599 {
5600 *arg = option_end;
5601 return OK;
5602 }
5603
5604 c = *option_end;
5605 *option_end = NUL;
5606 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 if (opt_type == -3) /* invalid name */
5610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 EMSG2(_("E113: Unknown option: %s"), *arg);
5613 ret = FAIL;
5614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 if (opt_type == -2) /* hidden string option */
5618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 rettv->v_type = VAR_STRING;
5620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
5622 else if (opt_type == -1) /* hidden number option */
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 rettv->v_type = VAR_NUMBER;
5625 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (opt_type == 1) /* number option */
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 rettv->v_type = VAR_NUMBER;
5630 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
5632 else /* string option */
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 rettv->v_type = VAR_STRING;
5635 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 }
5638 else if (working && (opt_type == -2 || opt_type == -1))
5639 ret = FAIL;
5640
5641 *option_end = c; /* put back for error messages */
5642 *arg = option_end;
5643
5644 return ret;
5645}
5646
5647/*
5648 * Allocate a variable for a string constant.
5649 * Return OK or FAIL.
5650 */
5651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005652get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 char_u *p;
5655 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 int extra = 0;
5657
5658 /*
5659 * Find the end of the string, skipping backslashed characters.
5660 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 if (*p == '\\' && p[1] != NUL)
5664 {
5665 ++p;
5666 /* A "\<x>" form occupies at least 4 characters, and produces up
5667 * to 6 characters: reserve space for 2 extra */
5668 if (*p == '<')
5669 extra += 2;
5670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672
5673 if (*p != '"')
5674 {
5675 EMSG2(_("E114: Missing quote: %s"), *arg);
5676 return FAIL;
5677 }
5678
5679 /* If only parsing, set *arg and return here */
5680 if (!evaluate)
5681 {
5682 *arg = p + 1;
5683 return OK;
5684 }
5685
5686 /*
5687 * Copy the string into allocated memory, handling backslashed
5688 * characters.
5689 */
5690 name = alloc((unsigned)(p - *arg + extra));
5691 if (name == NULL)
5692 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 rettv->v_type = VAR_STRING;
5694 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 if (*p == '\\')
5699 {
5700 switch (*++p)
5701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 case 'b': *name++ = BS; ++p; break;
5703 case 'e': *name++ = ESC; ++p; break;
5704 case 'f': *name++ = FF; ++p; break;
5705 case 'n': *name++ = NL; ++p; break;
5706 case 'r': *name++ = CAR; ++p; break;
5707 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
5709 case 'X': /* hex: "\x1", "\x12" */
5710 case 'x':
5711 case 'u': /* Unicode: "\u0023" */
5712 case 'U':
5713 if (vim_isxdigit(p[1]))
5714 {
5715 int n, nr;
5716 int c = toupper(*p);
5717
5718 if (c == 'X')
5719 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005720 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005722 else
5723 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 nr = 0;
5725 while (--n >= 0 && vim_isxdigit(p[1]))
5726 {
5727 ++p;
5728 nr = (nr << 4) + hex2nr(*p);
5729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731#ifdef FEAT_MBYTE
5732 /* For "\u" store the number according to
5733 * 'encoding'. */
5734 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 else
5737#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 break;
5741
5742 /* octal: "\1", "\12", "\123" */
5743 case '0':
5744 case '1':
5745 case '2':
5746 case '3':
5747 case '4':
5748 case '5':
5749 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 case '7': *name = *p++ - '0';
5751 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 *name = (*name << 3) + *p++ - '0';
5754 if (*p >= '0' && *p <= '7')
5755 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 break;
5759
5760 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 if (extra != 0)
5763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 break;
5766 }
5767 /* FALLTHROUGH */
5768
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 break;
5771 }
5772 }
5773 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 *arg = p + 1;
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 return OK;
5781}
5782
5783/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 * Return OK or FAIL.
5786 */
5787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 int reduce = 0;
5793
5794 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 */
5797 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 break;
5803 ++reduce;
5804 ++p;
5805 }
5806 }
5807
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 return FAIL;
5812 }
5813
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 if (!evaluate)
5816 {
5817 *arg = p + 1;
5818 return OK;
5819 }
5820
5821 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005823 */
5824 str = alloc((unsigned)((p - *arg) - reduce));
5825 if (str == NULL)
5826 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 rettv->v_type = VAR_STRING;
5828 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 break;
5836 ++p;
5837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 *arg = p + 1;
5842
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 return OK;
5844}
5845
5846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 * Allocate a variable for a List and fill it from "*arg".
5848 * Return OK or FAIL.
5849 */
5850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005851get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852{
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 list_T *l = NULL;
5854 typval_T tv;
5855 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856
5857 if (evaluate)
5858 {
5859 l = list_alloc();
5860 if (l == NULL)
5861 return FAIL;
5862 }
5863
5864 *arg = skipwhite(*arg + 1);
5865 while (**arg != ']' && **arg != NUL)
5866 {
5867 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5868 goto failret;
5869 if (evaluate)
5870 {
5871 item = listitem_alloc();
5872 if (item != NULL)
5873 {
5874 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005875 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 list_append(l, item);
5877 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005878 else
5879 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 }
5881
5882 if (**arg == ']')
5883 break;
5884 if (**arg != ',')
5885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 goto failret;
5888 }
5889 *arg = skipwhite(*arg + 1);
5890 }
5891
5892 if (**arg != ']')
5893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895failret:
5896 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005897 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 return FAIL;
5899 }
5900
5901 *arg = skipwhite(*arg + 1);
5902 if (evaluate)
5903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005904 rettv->v_type = VAR_LIST;
5905 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 ++l->lv_refcount;
5907 }
5908
5909 return OK;
5910}
5911
5912/*
5913 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005914 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005916 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005917list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005919 list_T *l;
5920
5921 l = (list_T *)alloc_clear(sizeof(list_T));
5922 if (l != NULL)
5923 {
5924 /* Prepend the list to the list of lists for garbage collection. */
5925 if (first_list != NULL)
5926 first_list->lv_used_prev = l;
5927 l->lv_used_prev = NULL;
5928 l->lv_used_next = first_list;
5929 first_list = l;
5930 }
5931 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932}
5933
5934/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005935 * Allocate an empty list for a return value.
5936 * Returns OK or FAIL.
5937 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005939rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005940{
5941 list_T *l = list_alloc();
5942
5943 if (l == NULL)
5944 return FAIL;
5945
5946 rettv->vval.v_list = l;
5947 rettv->v_type = VAR_LIST;
5948 ++l->lv_refcount;
5949 return OK;
5950}
5951
5952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 * Unreference a list: decrement the reference count and free it when it
5954 * becomes zero.
5955 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005959 if (l != NULL && --l->lv_refcount <= 0)
5960 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961}
5962
5963/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005964 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 * Ignores the reference count.
5966 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005968list_free(
5969 list_T *l,
5970 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005974 /* Remove the list from the list of lists for garbage collection. */
5975 if (l->lv_used_prev == NULL)
5976 first_list = l->lv_used_next;
5977 else
5978 l->lv_used_prev->lv_used_next = l->lv_used_next;
5979 if (l->lv_used_next != NULL)
5980 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5981
Bram Moolenaard9fba312005-06-26 22:34:35 +00005982 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005984 /* Remove the item before deleting it. */
5985 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005986 if (recurse || (item->li_tv.v_type != VAR_LIST
5987 && item->li_tv.v_type != VAR_DICT))
5988 clear_tv(&item->li_tv);
5989 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 }
5991 vim_free(l);
5992}
5993
5994/*
5995 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01005996 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005998 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005999listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002}
6003
6004/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006005 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006010 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 vim_free(item);
6012}
6013
6014/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006015 * Remove a list item from a List and free it. Also clears the value.
6016 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006019{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006020 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006021 listitem_free(item);
6022}
6023
6024/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 * Get the number of items in a list.
6026 */
6027 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 if (l == NULL)
6031 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006032 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033}
6034
6035/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 * Return TRUE when two lists have exactly the same values.
6037 */
6038 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039list_equal(
6040 list_T *l1,
6041 list_T *l2,
6042 int ic, /* ignore case for strings */
6043 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006047 if (l1 == NULL || l2 == NULL)
6048 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006049 if (l1 == l2)
6050 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 if (list_len(l1) != list_len(l2))
6052 return FALSE;
6053
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054 for (item1 = l1->lv_first, item2 = l2->lv_first;
6055 item1 != NULL && item2 != NULL;
6056 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058 return FALSE;
6059 return item1 == NULL && item2 == NULL;
6060}
6061
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006062/*
6063 * Return the dictitem that an entry in a hashtable points to.
6064 */
6065 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006067{
6068 return HI2DI(hi);
6069}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006070
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006072 * Return TRUE when two dictionaries have exactly the same key/values.
6073 */
6074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075dict_equal(
6076 dict_T *d1,
6077 dict_T *d2,
6078 int ic, /* ignore case for strings */
6079 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hashitem_T *hi;
6082 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006084
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006085 if (d1 == NULL || d2 == NULL)
6086 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 if (d1 == d2)
6088 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006089 if (dict_len(d1) != dict_len(d2))
6090 return FALSE;
6091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006092 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006095 if (!HASHITEM_EMPTY(hi))
6096 {
6097 item2 = dict_find(d2, hi->hi_key, -1);
6098 if (item2 == NULL)
6099 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 return FALSE;
6102 --todo;
6103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006104 }
6105 return TRUE;
6106}
6107
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108static int tv_equal_recurse_limit;
6109
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006113 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116tv_equal(
6117 typval_T *tv1,
6118 typval_T *tv2,
6119 int ic, /* ignore case */
6120 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121{
6122 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006123 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006125 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006130 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006131 * recursiveness to a limit. We guess they are equal then.
6132 * A fixed limit has the problem of still taking an awful long time.
6133 * Reduce the limit every time running into it. That should work fine for
6134 * deeply linked structures that are not recursively linked and catch
6135 * recursiveness quickly. */
6136 if (!recursive)
6137 tv_equal_recurse_limit = 1000;
6138 if (recursive_cnt >= tv_equal_recurse_limit)
6139 {
6140 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006141 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006146 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006147 ++recursive_cnt;
6148 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6149 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006150 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006151
6152 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153 ++recursive_cnt;
6154 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6155 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006156 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157
6158 case VAR_FUNC:
6159 return (tv1->vval.v_string != NULL
6160 && tv2->vval.v_string != NULL
6161 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6162
6163 case VAR_NUMBER:
6164 return tv1->vval.v_number == tv2->vval.v_number;
6165
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006166#ifdef FEAT_FLOAT
6167 case VAR_FLOAT:
6168 return tv1->vval.v_float == tv2->vval.v_float;
6169#endif
6170
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 case VAR_STRING:
6172 s1 = get_tv_string_buf(tv1, buf1);
6173 s2 = get_tv_string_buf(tv2, buf2);
6174 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006175
6176 case VAR_SPECIAL:
6177 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179
6180 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 return TRUE;
6182}
6183
6184/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 * Locate item with index "n" in list "l" and return it.
6186 * A negative index is counted from the end; -1 is the last item.
6187 * Returns NULL when "n" is out of range.
6188 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 long idx;
6194
6195 if (l == NULL)
6196 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197
6198 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 n = l->lv_len + n;
6201
6202 /* Check for index out of range. */
6203 if (n < 0 || n >= l->lv_len)
6204 return NULL;
6205
6206 /* When there is a cached index may start search from there. */
6207 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006209 if (n < l->lv_idx / 2)
6210 {
6211 /* closest to the start of the list */
6212 item = l->lv_first;
6213 idx = 0;
6214 }
6215 else if (n > (l->lv_idx + l->lv_len) / 2)
6216 {
6217 /* closest to the end of the list */
6218 item = l->lv_last;
6219 idx = l->lv_len - 1;
6220 }
6221 else
6222 {
6223 /* closest to the cached index */
6224 item = l->lv_idx_item;
6225 idx = l->lv_idx;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
6228 else
6229 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006230 if (n < l->lv_len / 2)
6231 {
6232 /* closest to the start of the list */
6233 item = l->lv_first;
6234 idx = 0;
6235 }
6236 else
6237 {
6238 /* closest to the end of the list */
6239 item = l->lv_last;
6240 idx = l->lv_len - 1;
6241 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006243
6244 while (n > idx)
6245 {
6246 /* search forward */
6247 item = item->li_next;
6248 ++idx;
6249 }
6250 while (n < idx)
6251 {
6252 /* search backward */
6253 item = item->li_prev;
6254 --idx;
6255 }
6256
6257 /* cache the used index */
6258 l->lv_idx = idx;
6259 l->lv_idx_item = item;
6260
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 return item;
6262}
6263
6264/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006265 * Get list item "l[idx]" as a number.
6266 */
6267 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006268list_find_nr(
6269 list_T *l,
6270 long idx,
6271 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006272{
6273 listitem_T *li;
6274
6275 li = list_find(l, idx);
6276 if (li == NULL)
6277 {
6278 if (errorp != NULL)
6279 *errorp = TRUE;
6280 return -1L;
6281 }
6282 return get_tv_number_chk(&li->li_tv, errorp);
6283}
6284
6285/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006286 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6287 */
6288 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006289list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006290{
6291 listitem_T *li;
6292
6293 li = list_find(l, idx - 1);
6294 if (li == NULL)
6295 {
6296 EMSGN(_(e_listidx), idx);
6297 return NULL;
6298 }
6299 return get_tv_string(&li->li_tv);
6300}
6301
6302/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006303 * Locate "item" list "l" and return its index.
6304 * Returns -1 when "item" is not in the list.
6305 */
6306 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006307list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006308{
6309 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006311
6312 if (l == NULL)
6313 return -1;
6314 idx = 0;
6315 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6316 ++idx;
6317 if (li == NULL)
6318 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006319 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006320}
6321
6322/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 * Append item "item" to the end of list "l".
6324 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006326list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327{
6328 if (l->lv_last == NULL)
6329 {
6330 /* empty list */
6331 l->lv_first = item;
6332 l->lv_last = item;
6333 item->li_prev = NULL;
6334 }
6335 else
6336 {
6337 l->lv_last->li_next = item;
6338 item->li_prev = l->lv_last;
6339 l->lv_last = item;
6340 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006341 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 item->li_next = NULL;
6343}
6344
6345/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006347 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 copy_tv(tv, &li->li_tv);
6357 list_append(l, li);
6358 return OK;
6359}
6360
6361/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006362 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006363 * Return FAIL when out of memory.
6364 */
6365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006366list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006367{
6368 listitem_T *li = listitem_alloc();
6369
6370 if (li == NULL)
6371 return FAIL;
6372 li->li_tv.v_type = VAR_DICT;
6373 li->li_tv.v_lock = 0;
6374 li->li_tv.vval.v_dict = dict;
6375 list_append(list, li);
6376 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 return OK;
6378}
6379
6380/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006381 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006382 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383 * Returns FAIL when out of memory.
6384 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006386list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387{
6388 listitem_T *li = listitem_alloc();
6389
6390 if (li == NULL)
6391 return FAIL;
6392 list_append(l, li);
6393 li->li_tv.v_type = VAR_STRING;
6394 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006395 if (str == NULL)
6396 li->li_tv.vval.v_string = NULL;
6397 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006398 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006399 return FAIL;
6400 return OK;
6401}
6402
6403/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006404 * Append "n" to list "l".
6405 * Returns FAIL when out of memory.
6406 */
6407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006408list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429{
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431
6432 if (ni == NULL)
6433 return FAIL;
6434 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006435 list_insert(l, ni, item);
6436 return OK;
6437}
6438
6439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006441{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006442 if (item == NULL)
6443 /* Append new item at end of list. */
6444 list_append(l, ni);
6445 else
6446 {
6447 /* Insert new item before existing item. */
6448 ni->li_prev = item->li_prev;
6449 ni->li_next = item;
6450 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006451 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 ++l->lv_idx;
6454 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 l->lv_idx_item = NULL;
6459 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006461 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463}
6464
6465/*
6466 * Extend "l1" with "l2".
6467 * If "bef" is NULL append at the end, otherwise insert before this item.
6468 * Returns FAIL when out of memory.
6469 */
6470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472{
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006474 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006476 /* We also quit the loop when we have inserted the original item count of
6477 * the list, avoid a hang when we extend a list with itself. */
6478 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6480 return FAIL;
6481 return OK;
6482}
6483
6484/*
6485 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6486 * Return FAIL when out of memory.
6487 */
6488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006493 if (l1 == NULL || l2 == NULL)
6494 return FAIL;
6495
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 if (l == NULL)
6499 return FAIL;
6500 tv->v_type = VAR_LIST;
6501 tv->vval.v_list = l;
6502
6503 /* append all items from the second list */
6504 return list_extend(l, l2, NULL);
6505}
6506
6507/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006508 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 * Returns NULL when out of memory.
6512 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006514list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515{
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 list_T *copy;
6517 listitem_T *item;
6518 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519
6520 if (orig == NULL)
6521 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
6523 copy = list_alloc();
6524 if (copy != NULL)
6525 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 if (copyID != 0)
6527 {
6528 /* Do this before adding the items, because one of the items may
6529 * refer back to this list. */
6530 orig->lv_copyID = copyID;
6531 orig->lv_copylist = copy;
6532 }
6533 for (item = orig->lv_first; item != NULL && !got_int;
6534 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 {
6536 ni = listitem_alloc();
6537 if (ni == NULL)
6538 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006540 {
6541 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6542 {
6543 vim_free(ni);
6544 break;
6545 }
6546 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 list_append(copy, ni);
6550 }
6551 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006552 if (item != NULL)
6553 {
6554 list_unref(copy);
6555 copy = NULL;
6556 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 }
6558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 return copy;
6560}
6561
6562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006564 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006565 * This used to be called list_remove, but that conflicts with a Sun header
6566 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006569vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570{
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 /* notify watchers */
6574 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006576 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 list_fix_watch(l, ip);
6578 if (ip == item2)
6579 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581
6582 if (item2->li_next == NULL)
6583 l->lv_last = item->li_prev;
6584 else
6585 item2->li_next->li_prev = item->li_prev;
6586 if (item->li_prev == NULL)
6587 l->lv_first = item2->li_next;
6588 else
6589 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006590 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591}
6592
6593/*
6594 * Return an allocated string with the string representation of a list.
6595 * May return NULL.
6596 */
6597 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006598list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599{
6600 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601
6602 if (tv->vval.v_list == NULL)
6603 return NULL;
6604 ga_init2(&ga, (int)sizeof(char), 80);
6605 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 {
6608 vim_free(ga.ga_data);
6609 return NULL;
6610 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 ga_append(&ga, ']');
6612 ga_append(&ga, NUL);
6613 return (char_u *)ga.ga_data;
6614}
6615
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006616typedef struct join_S {
6617 char_u *s;
6618 char_u *tofree;
6619} join_T;
6620
6621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006622list_join_inner(
6623 garray_T *gap, /* to store the result in */
6624 list_T *l,
6625 char_u *sep,
6626 int echo_style,
6627 int copyID,
6628 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006629{
6630 int i;
6631 join_T *p;
6632 int len;
6633 int sumlen = 0;
6634 int first = TRUE;
6635 char_u *tofree;
6636 char_u numbuf[NUMBUFLEN];
6637 listitem_T *item;
6638 char_u *s;
6639
6640 /* Stringify each item in the list. */
6641 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6642 {
6643 if (echo_style)
6644 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6645 else
6646 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6647 if (s == NULL)
6648 return FAIL;
6649
6650 len = (int)STRLEN(s);
6651 sumlen += len;
6652
Bram Moolenaarcde88542015-08-11 19:14:00 +02006653 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006654 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6655 if (tofree != NULL || s != numbuf)
6656 {
6657 p->s = s;
6658 p->tofree = tofree;
6659 }
6660 else
6661 {
6662 p->s = vim_strnsave(s, len);
6663 p->tofree = p->s;
6664 }
6665
6666 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006667 if (did_echo_string_emsg) /* recursion error, bail out */
6668 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006669 }
6670
6671 /* Allocate result buffer with its total size, avoid re-allocation and
6672 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6673 if (join_gap->ga_len >= 2)
6674 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6675 if (ga_grow(gap, sumlen + 2) == FAIL)
6676 return FAIL;
6677
6678 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6679 {
6680 if (first)
6681 first = FALSE;
6682 else
6683 ga_concat(gap, sep);
6684 p = ((join_T *)join_gap->ga_data) + i;
6685
6686 if (p->s != NULL)
6687 ga_concat(gap, p->s);
6688 line_breakcheck();
6689 }
6690
6691 return OK;
6692}
6693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006696 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006697 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006700list_join(
6701 garray_T *gap,
6702 list_T *l,
6703 char_u *sep,
6704 int echo_style,
6705 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006707 garray_T join_ga;
6708 int retval;
6709 join_T *p;
6710 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711
Bram Moolenaard39a7512015-04-16 22:51:22 +02006712 if (l->lv_len < 1)
6713 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006714 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6715 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6716
6717 /* Dispose each item in join_ga. */
6718 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006720 p = (join_T *)join_ga.ga_data;
6721 for (i = 0; i < join_ga.ga_len; ++i)
6722 {
6723 vim_free(p->tofree);
6724 ++p;
6725 }
6726 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006728
6729 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730}
6731
6732/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006733 * Return the next (unique) copy ID.
6734 * Used for serializing nested structures.
6735 */
6736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006738{
6739 current_copyID += COPYID_INC;
6740 return current_copyID;
6741}
6742
6743/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 * Garbage collection for lists and dictionaries.
6745 *
6746 * We use reference counts to be able to free most items right away when they
6747 * are no longer used. But for composite items it's possible that it becomes
6748 * unused while the reference count is > 0: When there is a recursive
6749 * reference. Example:
6750 * :let l = [1, 2, 3]
6751 * :let d = {9: l}
6752 * :let l[1] = d
6753 *
6754 * Since this is quite unusual we handle this with garbage collection: every
6755 * once in a while find out which lists and dicts are not referenced from any
6756 * variable.
6757 *
6758 * Here is a good reference text about garbage collection (refers to Python
6759 * but it applies to all reference-counting mechanisms):
6760 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762
6763/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 * Do garbage collection for lists and dicts.
6765 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006768garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006770 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006771 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 buf_T *buf;
6773 win_T *wp;
6774 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006775 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006776 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006777 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006778#ifdef FEAT_WINDOWS
6779 tabpage_T *tp;
6780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006782 /* Only do this once. */
6783 want_garbage_collect = FALSE;
6784 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006785 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006787 /* We advance by two because we add one for items referenced through
6788 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006789 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006790
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 /*
6792 * 1. Go through all accessible variables and mark all lists and dicts
6793 * with copyID.
6794 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006795
6796 /* Don't free variables in the previous_funccal list unless they are only
6797 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006798 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006799 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6800 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006801 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6802 NULL);
6803 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6804 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006805 }
6806
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 /* script-local variables */
6808 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006809 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810
6811 /* buffer-local variables */
6812 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006813 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6814 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815
6816 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006817 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006818 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6819 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006820#ifdef FEAT_AUTOCMD
6821 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006822 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6823 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006824#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 /* tabpage-local variables */
6828 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006829 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6830 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831#endif
6832
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006834 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835
6836 /* function-local variables */
6837 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6838 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006839 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6840 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 }
6842
Bram Moolenaard812df62008-11-09 12:46:09 +00006843 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006845
Bram Moolenaar1dced572012-04-05 16:54:08 +02006846#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006847 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006848#endif
6849
Bram Moolenaardb913952012-06-29 12:54:53 +02006850#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006851 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006852#endif
6853
6854#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006855 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006856#endif
6857
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006858#ifdef FEAT_CHANNEL
6859 abort = abort || set_ref_in_channel(copyID);
6860#endif
6861
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006864 /*
6865 * 2. Free lists and dictionaries that are not referenced.
6866 */
6867 did_free = free_unref_items(copyID);
6868
6869 /*
6870 * 3. Check if any funccal can be freed now.
6871 */
6872 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 if (can_free_funccal(*pfc, copyID))
6875 {
6876 fc = *pfc;
6877 *pfc = fc->caller;
6878 free_funccal(fc, TRUE);
6879 did_free = TRUE;
6880 did_free_funccal = TRUE;
6881 }
6882 else
6883 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 if (did_free_funccal)
6886 /* When a funccal was freed some more items might be garbage
6887 * collected, so run again. */
6888 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 else if (p_verbose > 0)
6891 {
6892 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6893 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894
6895 return did_free;
6896}
6897
6898/*
6899 * Free lists and dictionaries that are no longer referenced.
6900 */
6901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006904 dict_T *dd, *dd_next;
6905 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006906 int did_free = FALSE;
6907
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 */
6911 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006912 {
6913 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006916 /* Free the Dictionary and ordinary items it contains, but don't
6917 * recurse into Lists and Dictionaries, they will be in the list
6918 * of dicts or list of lists. */
6919 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006922 dd = dd_next;
6923 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924
6925 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 * Go through the list of lists and free items without the copyID.
6927 * But don't free a list that has a watcher (used in a for loop), these
6928 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 */
6930 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006931 {
6932 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6934 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006936 /* Free the List and ordinary items it contains, but don't recurse
6937 * into Lists and Dictionaries, they will be in the list of dicts
6938 * or list of lists. */
6939 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006942 ll = ll_next;
6943 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 return did_free;
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 * "list_stack" is used to add lists to be marked. Can be NULL.
6950 *
6951 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006954set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955{
6956 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 hashtab_T *cur_ht;
6960 ht_stack_T *ht_stack = NULL;
6961 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 cur_ht = ht;
6964 for (;;)
6965 {
6966 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 /* Mark each item in the hashtab. If the item contains a hashtab
6969 * it is added to ht_stack, if it contains a list it is added to
6970 * list_stack. */
6971 todo = (int)cur_ht->ht_used;
6972 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6973 if (!HASHITEM_EMPTY(hi))
6974 {
6975 --todo;
6976 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6977 &ht_stack, list_stack);
6978 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980
6981 if (ht_stack == NULL)
6982 break;
6983
6984 /* take an item from the stack */
6985 cur_ht = ht_stack->ht;
6986 tempitem = ht_stack;
6987 ht_stack = ht_stack->prev;
6988 free(tempitem);
6989 }
6990
6991 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992}
6993
6994/*
6995 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6997 *
6998 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007001set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 listitem_T *li;
7004 int abort = FALSE;
7005 list_T *cur_l;
7006 list_stack_T *list_stack = NULL;
7007 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 cur_l = l;
7010 for (;;)
7011 {
7012 if (!abort)
7013 /* Mark each item in the list. If the item contains a hashtab
7014 * it is added to ht_stack, if it contains a list it is added to
7015 * list_stack. */
7016 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7017 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7018 ht_stack, &list_stack);
7019 if (list_stack == NULL)
7020 break;
7021
7022 /* take an item from the stack */
7023 cur_l = list_stack->list;
7024 tempitem = list_stack;
7025 list_stack = list_stack->prev;
7026 free(tempitem);
7027 }
7028
7029 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030}
7031
7032/*
7033 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 * "list_stack" is used to add lists to be marked. Can be NULL.
7035 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7036 *
7037 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007040set_ref_in_item(
7041 typval_T *tv,
7042 int copyID,
7043 ht_stack_T **ht_stack,
7044 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045{
7046 dict_T *dd;
7047 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049
7050 switch (tv->v_type)
7051 {
7052 case VAR_DICT:
7053 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007054 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007055 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 /* Didn't see this dict yet. */
7057 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 if (ht_stack == NULL)
7059 {
7060 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7061 }
7062 else
7063 {
7064 ht_stack_T *newitem = (ht_stack_T*)malloc(
7065 sizeof(ht_stack_T));
7066 if (newitem == NULL)
7067 abort = TRUE;
7068 else
7069 {
7070 newitem->ht = &dd->dv_hashtab;
7071 newitem->prev = *ht_stack;
7072 *ht_stack = newitem;
7073 }
7074 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007075 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077
7078 case VAR_LIST:
7079 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007080 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007081 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /* Didn't see this list yet. */
7083 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 if (list_stack == NULL)
7085 {
7086 abort = set_ref_in_list(ll, copyID, ht_stack);
7087 }
7088 else
7089 {
7090 list_stack_T *newitem = (list_stack_T*)malloc(
7091 sizeof(list_stack_T));
7092 if (newitem == NULL)
7093 abort = TRUE;
7094 else
7095 {
7096 newitem->list = ll;
7097 newitem->prev = *list_stack;
7098 *list_stack = newitem;
7099 }
7100 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007101 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007103 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007105}
7106
7107/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 * Allocate an empty header for a dictionary.
7109 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007110 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112{
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007117 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007118 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119 if (first_dict != NULL)
7120 first_dict->dv_used_prev = d;
7121 d->dv_used_next = first_dict;
7122 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007123 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007126 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007127 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007128 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007129 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007130 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132}
7133
7134/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007135 * Allocate an empty dict for a return value.
7136 * Returns OK or FAIL.
7137 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007138 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007139rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007140{
7141 dict_T *d = dict_alloc();
7142
7143 if (d == NULL)
7144 return FAIL;
7145
7146 rettv->vval.v_dict = d;
7147 rettv->v_type = VAR_DICT;
7148 ++d->dv_refcount;
7149 return OK;
7150}
7151
7152
7153/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 * Unreference a Dictionary: decrement the reference count and free it when it
7155 * becomes zero.
7156 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007160 if (d != NULL && --d->dv_refcount <= 0)
7161 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162}
7163
7164/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007165 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 * Ignores the reference count.
7167 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169dict_free(
7170 dict_T *d,
7171 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177 /* Remove the dict from the list of dicts for garbage collection. */
7178 if (d->dv_used_prev == NULL)
7179 first_dict = d->dv_used_next;
7180 else
7181 d->dv_used_prev->dv_used_next = d->dv_used_next;
7182 if (d->dv_used_next != NULL)
7183 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7184
7185 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007186 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007187 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 if (!HASHITEM_EMPTY(hi))
7191 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192 /* Remove the item before deleting it, just in case there is
7193 * something recursive causing trouble. */
7194 di = HI2DI(hi);
7195 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007196 if (recurse || (di->di_tv.v_type != VAR_LIST
7197 && di->di_tv.v_type != VAR_DICT))
7198 clear_tv(&di->di_tv);
7199 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 --todo;
7201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 vim_free(d);
7205}
7206
7207/*
7208 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209 * The "key" is copied to the new item.
7210 * Note that the value of the item "di_tv" still needs to be initialized!
7211 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215{
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007218 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007222 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007224 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225}
7226
7227/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228 * Make a copy of a Dictionary item.
7229 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007231dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232{
Bram Moolenaar33570922005-01-25 22:26:29 +00007233 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007235 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7236 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 if (di != NULL)
7238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007240 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007241 copy_tv(&org->di_tv, &di->di_tv);
7242 }
7243 return di;
7244}
7245
7246/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 * Remove item "item" from Dictionary "dict" and free it.
7248 */
7249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251{
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (HASHITEM_EMPTY(hi))
7256 EMSG2(_(e_intern2), "dictitem_remove()");
7257 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 dictitem_free(item);
7260}
7261
7262/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 * Free a dict item. Also clears the value.
7264 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007266dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007269 if (item->di_flags & DI_FLAGS_ALLOC)
7270 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271}
7272
7273/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7275 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007276 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 * Returns NULL when out of memory.
7278 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281{
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dict_T *copy;
7283 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286
7287 if (orig == NULL)
7288 return NULL;
7289
7290 copy = dict_alloc();
7291 if (copy != NULL)
7292 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007293 if (copyID != 0)
7294 {
7295 orig->dv_copyID = copyID;
7296 orig->dv_copydict = copy;
7297 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007299 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 --todo;
7304
7305 di = dictitem_alloc(hi->hi_key);
7306 if (di == NULL)
7307 break;
7308 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007309 {
7310 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7311 copyID) == FAIL)
7312 {
7313 vim_free(di);
7314 break;
7315 }
7316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 else
7318 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7319 if (dict_add(copy, di) == FAIL)
7320 {
7321 dictitem_free(di);
7322 break;
7323 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007328 if (todo > 0)
7329 {
7330 dict_unref(copy);
7331 copy = NULL;
7332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 }
7334
7335 return copy;
7336}
7337
7338/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007340 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346}
7347
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007349 * Add a number or string entry to dictionary "d".
7350 * When "str" is NULL use number "nr", otherwise use "str".
7351 * Returns FAIL when out of memory and when key already exists.
7352 */
7353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354dict_add_nr_str(
7355 dict_T *d,
7356 char *key,
7357 long nr,
7358 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007359{
7360 dictitem_T *item;
7361
7362 item = dictitem_alloc((char_u *)key);
7363 if (item == NULL)
7364 return FAIL;
7365 item->di_tv.v_lock = 0;
7366 if (str == NULL)
7367 {
7368 item->di_tv.v_type = VAR_NUMBER;
7369 item->di_tv.vval.v_number = nr;
7370 }
7371 else
7372 {
7373 item->di_tv.v_type = VAR_STRING;
7374 item->di_tv.vval.v_string = vim_strsave(str);
7375 }
7376 if (dict_add(d, item) == FAIL)
7377 {
7378 dictitem_free(item);
7379 return FAIL;
7380 }
7381 return OK;
7382}
7383
7384/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007385 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007386 * Returns FAIL when out of memory and when key already exists.
7387 */
7388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007390{
7391 dictitem_T *item;
7392
7393 item = dictitem_alloc((char_u *)key);
7394 if (item == NULL)
7395 return FAIL;
7396 item->di_tv.v_lock = 0;
7397 item->di_tv.v_type = VAR_LIST;
7398 item->di_tv.vval.v_list = list;
7399 if (dict_add(d, item) == FAIL)
7400 {
7401 dictitem_free(item);
7402 return FAIL;
7403 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007404 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007405 return OK;
7406}
7407
7408/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 * Get the number of items in a Dictionary.
7410 */
7411 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 if (d == NULL)
7415 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007416 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417}
7418
7419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 * Find item "key[len]" in Dictionary "d".
7421 * If "len" is negative use strlen(key).
7422 * Returns NULL when not found.
7423 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007424 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007425dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427#define AKEYLEN 200
7428 char_u buf[AKEYLEN];
7429 char_u *akey;
7430 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 if (len < 0)
7434 akey = key;
7435 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 tofree = akey = vim_strnsave(key, len);
7438 if (akey == NULL)
7439 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007440 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 else
7442 {
7443 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007444 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445 akey = buf;
7446 }
7447
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007449 vim_free(tofree);
7450 if (HASHITEM_EMPTY(hi))
7451 return NULL;
7452 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453}
7454
7455/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007456 * Get a string item from a dictionary.
7457 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007458 * Returns NULL if the entry doesn't exist or out of memory.
7459 */
7460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007462{
7463 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007464 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007465
7466 di = dict_find(d, key, -1);
7467 if (di == NULL)
7468 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007469 s = get_tv_string(&di->di_tv);
7470 if (save && s != NULL)
7471 s = vim_strsave(s);
7472 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007473}
7474
7475/*
7476 * Get a number item from a dictionary.
7477 * Returns 0 if the entry doesn't exist or out of memory.
7478 */
7479 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007481{
7482 dictitem_T *di;
7483
7484 di = dict_find(d, key, -1);
7485 if (di == NULL)
7486 return 0;
7487 return get_tv_number(&di->di_tv);
7488}
7489
7490/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 * Return an allocated string with the string representation of a Dictionary.
7492 * May return NULL.
7493 */
7494 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007495dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496{
7497 garray_T ga;
7498 int first = TRUE;
7499 char_u *tofree;
7500 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007503 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 return NULL;
7508 ga_init2(&ga, (int)sizeof(char), 80);
7509 ga_append(&ga, '{');
7510
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007511 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007512 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 --todo;
7517
7518 if (first)
7519 first = FALSE;
7520 else
7521 ga_concat(&ga, (char_u *)", ");
7522
7523 tofree = string_quote(hi->hi_key, FALSE);
7524 if (tofree != NULL)
7525 {
7526 ga_concat(&ga, tofree);
7527 vim_free(tofree);
7528 }
7529 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007530 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (s != NULL)
7532 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007534 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007535 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007536 line_breakcheck();
7537
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007540 if (todo > 0)
7541 {
7542 vim_free(ga.ga_data);
7543 return NULL;
7544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545
7546 ga_append(&ga, '}');
7547 ga_append(&ga, NUL);
7548 return (char_u *)ga.ga_data;
7549}
7550
7551/*
7552 * Allocate a variable for a Dictionary and fill it from "*arg".
7553 * Return OK or FAIL. Returns NOTDONE for {expr}.
7554 */
7555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 dict_T *d = NULL;
7559 typval_T tvkey;
7560 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007561 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565
7566 /*
7567 * First check if it's not a curly-braces thing: {expr}.
7568 * Must do this without evaluating, otherwise a function may be called
7569 * twice. Unfortunately this means we need to call eval1() twice for the
7570 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 if (*start != '}')
7574 {
7575 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7576 return FAIL;
7577 if (*start == '}')
7578 return NOTDONE;
7579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580
7581 if (evaluate)
7582 {
7583 d = dict_alloc();
7584 if (d == NULL)
7585 return FAIL;
7586 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007587 tvkey.v_type = VAR_UNKNOWN;
7588 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589
7590 *arg = skipwhite(*arg + 1);
7591 while (**arg != '}' && **arg != NUL)
7592 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 goto failret;
7595 if (**arg != ':')
7596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007597 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 goto failret;
7600 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007601 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007603 key = get_tv_string_buf_chk(&tvkey, buf);
7604 if (key == NULL || *key == NUL)
7605 {
7606 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7607 if (key != NULL)
7608 EMSG(_(e_emptykey));
7609 clear_tv(&tvkey);
7610 goto failret;
7611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
7614 *arg = skipwhite(*arg + 1);
7615 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7616 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007617 if (evaluate)
7618 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 goto failret;
7620 }
7621 if (evaluate)
7622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 if (item != NULL)
7625 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007626 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 clear_tv(&tv);
7629 goto failret;
7630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 item = dictitem_alloc(key);
7632 clear_tv(&tvkey);
7633 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007636 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 if (dict_add(d, item) == FAIL)
7638 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 }
7640 }
7641
7642 if (**arg == '}')
7643 break;
7644 if (**arg != ',')
7645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007646 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 goto failret;
7648 }
7649 *arg = skipwhite(*arg + 1);
7650 }
7651
7652 if (**arg != '}')
7653 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007654 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655failret:
7656 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007657 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 return FAIL;
7659 }
7660
7661 *arg = skipwhite(*arg + 1);
7662 if (evaluate)
7663 {
7664 rettv->v_type = VAR_DICT;
7665 rettv->vval.v_dict = d;
7666 ++d->dv_refcount;
7667 }
7668
7669 return OK;
7670}
7671
Bram Moolenaar17a13432016-01-24 14:22:10 +01007672 static char *
7673get_var_special_name(int nr)
7674{
7675 switch (nr)
7676 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007677 case VVAL_FALSE: return "v:false";
7678 case VVAL_TRUE: return "v:true";
7679 case VVAL_NONE: return "v:none";
7680 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007681 }
7682 EMSG2(_(e_intern2), "get_var_special_name()");
7683 return "42";
7684}
7685
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 * Return a string with the string representation of a variable.
7688 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007689 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007691 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007692 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 */
7694 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695echo_string(
7696 typval_T *tv,
7697 char_u **tofree,
7698 char_u *numbuf,
7699 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701 static int recurse = 0;
7702 char_u *r = NULL;
7703
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007705 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007706 if (!did_echo_string_emsg)
7707 {
7708 /* Only give this message once for a recursive call to avoid
7709 * flooding the user with errors. And stop iterating over lists
7710 * and dicts. */
7711 did_echo_string_emsg = TRUE;
7712 EMSG(_("E724: variable nested too deep for displaying"));
7713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007715 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 }
7717 ++recurse;
7718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719 switch (tv->v_type)
7720 {
7721 case VAR_FUNC:
7722 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 r = tv->vval.v_string;
7724 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007725
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007727 if (tv->vval.v_list == NULL)
7728 {
7729 *tofree = NULL;
7730 r = NULL;
7731 }
7732 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7733 {
7734 *tofree = NULL;
7735 r = (char_u *)"[...]";
7736 }
7737 else
7738 {
7739 tv->vval.v_list->lv_copyID = copyID;
7740 *tofree = list2string(tv, copyID);
7741 r = *tofree;
7742 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007743 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007744
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007746 if (tv->vval.v_dict == NULL)
7747 {
7748 *tofree = NULL;
7749 r = NULL;
7750 }
7751 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7752 {
7753 *tofree = NULL;
7754 r = (char_u *)"{...}";
7755 }
7756 else
7757 {
7758 tv->vval.v_dict->dv_copyID = copyID;
7759 *tofree = dict2string(tv, copyID);
7760 r = *tofree;
7761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007762 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764 case VAR_STRING:
7765 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007766 *tofree = NULL;
7767 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007770#ifdef FEAT_FLOAT
7771 case VAR_FLOAT:
7772 *tofree = NULL;
7773 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7774 r = numbuf;
7775 break;
7776#endif
7777
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007778 case VAR_SPECIAL:
7779 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007780 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007781 break;
7782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007787
Bram Moolenaar8502c702014-06-17 12:51:16 +02007788 if (--recurse == 0)
7789 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791}
7792
7793/*
7794 * Return a string with the string representation of a variable.
7795 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7796 * "numbuf" is used for a number.
7797 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007798 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007799 */
7800 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801tv2string(
7802 typval_T *tv,
7803 char_u **tofree,
7804 char_u *numbuf,
7805 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007806{
7807 switch (tv->v_type)
7808 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007809 case VAR_FUNC:
7810 *tofree = string_quote(tv->vval.v_string, TRUE);
7811 return *tofree;
7812 case VAR_STRING:
7813 *tofree = string_quote(tv->vval.v_string, FALSE);
7814 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007815#ifdef FEAT_FLOAT
7816 case VAR_FLOAT:
7817 *tofree = NULL;
7818 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7819 return numbuf;
7820#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007822 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007823 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007824 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007826 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007829 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007830}
7831
7832/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 * Return string "str" in ' quotes, doubling ' characters.
7834 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007836 */
7837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007838string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007839{
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007841 char_u *p, *r, *s;
7842
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 len = (function ? 13 : 3);
7844 if (str != NULL)
7845 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007846 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 for (p = str; *p != NUL; mb_ptr_adv(p))
7848 if (*p == '\'')
7849 ++len;
7850 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007851 s = r = alloc(len);
7852 if (r != NULL)
7853 {
7854 if (function)
7855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007857 r += 10;
7858 }
7859 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 if (str != NULL)
7862 for (p = str; *p != NUL; )
7863 {
7864 if (*p == '\'')
7865 *r++ = '\'';
7866 MB_COPY_CHAR(p, r);
7867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869 if (function)
7870 *r++ = ')';
7871 *r++ = NUL;
7872 }
7873 return s;
7874}
7875
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007876#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877/*
7878 * Convert the string "text" to a floating point number.
7879 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7880 * this always uses a decimal point.
7881 * Returns the length of the text that was consumed.
7882 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884string2float(
7885 char_u *text,
7886 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007887{
7888 char *s = (char *)text;
7889 float_T f;
7890
7891 f = strtod(s, &s);
7892 *value = f;
7893 return (int)((char_u *)s - text);
7894}
7895#endif
7896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 * Get the value of an environment variable.
7899 * "arg" is pointing to the '$'. It is advanced to after the name.
7900 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007901 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 */
7903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007904get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 char_u *string = NULL;
7907 int len;
7908 int cc;
7909 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007910 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911
7912 ++*arg;
7913 name = *arg;
7914 len = get_env_len(arg);
7915 if (evaluate)
7916 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007917 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007918 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007919
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007920 cc = name[len];
7921 name[len] = NUL;
7922 /* first try vim_getenv(), fast for normal environment vars */
7923 string = vim_getenv(name, &mustfree);
7924 if (string != NULL && *string != NUL)
7925 {
7926 if (!mustfree)
7927 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007929 else
7930 {
7931 if (mustfree)
7932 vim_free(string);
7933
7934 /* next try expanding things like $VIM and ${HOME} */
7935 string = expand_env_save(name - 1);
7936 if (string != NULL && *string == '$')
7937 {
7938 vim_free(string);
7939 string = NULL;
7940 }
7941 }
7942 name[len] = cc;
7943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944 rettv->v_type = VAR_STRING;
7945 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 }
7947
7948 return OK;
7949}
7950
7951/*
7952 * Array with names and number of arguments of all internal functions
7953 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7954 */
7955static struct fst
7956{
7957 char *f_name; /* function name */
7958 char f_min_argc; /* minimal number of arguments */
7959 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007960 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007961 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962} functions[] =
7963{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964#ifdef FEAT_FLOAT
7965 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007966 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007967#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007968 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007969 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007970 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"append", 2, 2, f_append},
7972 {"argc", 0, 0, f_argc},
7973 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007974 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007975 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007976#ifdef FEAT_FLOAT
7977 {"asin", 1, 1, f_asin}, /* WJMc */
7978#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007979 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007980 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007981 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007982 {"assert_false", 1, 2, f_assert_false},
7983 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007984#ifdef FEAT_FLOAT
7985 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007986 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007989 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"bufexists", 1, 1, f_bufexists},
7991 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7992 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7993 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7994 {"buflisted", 1, 1, f_buflisted},
7995 {"bufloaded", 1, 1, f_bufloaded},
7996 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007997 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {"bufwinnr", 1, 1, f_bufwinnr},
7999 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008000 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008001 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003#ifdef FEAT_FLOAT
8004 {"ceil", 1, 1, f_ceil},
8005#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008006#ifdef FEAT_CHANNEL
8007 {"ch_close", 1, 1, f_ch_close},
8008 {"ch_open", 2, 3, f_ch_open},
8009 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8010 {"ch_sendraw", 2, 3, f_ch_sendraw},
8011#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008012 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008013 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008015 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008017#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008018 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008019 {"complete_add", 1, 1, f_complete_add},
8020 {"complete_check", 0, 0, f_complete_check},
8021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008023 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
8025 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008026 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008027#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008028 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008030 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008031 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008032 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008034 {"diff_filler", 1, 1, f_diff_filler},
8035 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008036 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008038 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"eventhandler", 0, 0, f_eventhandler},
8040 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008041 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008043#ifdef FEAT_FLOAT
8044 {"exp", 1, 1, f_exp},
8045#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008046 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008047 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008048 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8050 {"filereadable", 1, 1, f_filereadable},
8051 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008053 {"finddir", 1, 3, f_finddir},
8054 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#ifdef FEAT_FLOAT
8056 {"float2nr", 1, 1, f_float2nr},
8057 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008058 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008059#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008060 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"fnamemodify", 2, 2, f_fnamemodify},
8062 {"foldclosed", 1, 1, f_foldclosed},
8063 {"foldclosedend", 1, 1, f_foldclosedend},
8064 {"foldlevel", 1, 1, f_foldlevel},
8065 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008066 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008069 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008070 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008071 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008072 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"getchar", 0, 1, f_getchar},
8074 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008075 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"getcmdline", 0, 0, f_getcmdline},
8077 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008078 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008079 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008080 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008081 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008082 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008083 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {"getfsize", 1, 1, f_getfsize},
8085 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008086 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008087 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008088 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008089 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008090 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008091 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008092 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008093 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008095 {"gettabvar", 2, 3, f_gettabvar},
8096 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"getwinposx", 0, 0, f_getwinposx},
8098 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008099 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008100 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008101 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008102 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008105 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008106 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8108 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8109 {"histadd", 2, 2, f_histadd},
8110 {"histdel", 1, 2, f_histdel},
8111 {"histget", 1, 2, f_histget},
8112 {"histnr", 1, 1, f_histnr},
8113 {"hlID", 1, 1, f_hlID},
8114 {"hlexists", 1, 1, f_hlexists},
8115 {"hostname", 0, 0, f_hostname},
8116 {"iconv", 3, 3, f_iconv},
8117 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008118 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008119 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008121 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"inputrestore", 0, 0, f_inputrestore},
8123 {"inputsave", 0, 0, f_inputsave},
8124 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008126 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008128 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008129 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008131 {"jsondecode", 1, 1, f_jsondecode},
8132 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008133 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"libcall", 3, 3, f_libcall},
8137 {"libcallnr", 3, 3, f_libcallnr},
8138 {"line", 1, 1, f_line},
8139 {"line2byte", 1, 1, f_line2byte},
8140 {"lispindent", 1, 1, f_lispindent},
8141 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008142#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008143 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008144 {"log10", 1, 1, f_log10},
8145#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008146#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008147 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008148#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008150 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008151 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008152 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008153 {"matchadd", 2, 5, f_matchadd},
8154 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008155 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008156 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008157 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008158 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008159 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008160 {"max", 1, 1, f_max},
8161 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008162#ifdef vim_mkdir
8163 {"mkdir", 1, 3, f_mkdir},
8164#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008166#ifdef FEAT_MZSCHEME
8167 {"mzeval", 1, 1, f_mzeval},
8168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008170 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008172 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008173#ifdef FEAT_PERL
8174 {"perleval", 1, 1, f_perleval},
8175#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008176#ifdef FEAT_FLOAT
8177 {"pow", 2, 2, f_pow},
8178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008180 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008181 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008182#ifdef FEAT_PYTHON3
8183 {"py3eval", 1, 1, f_py3eval},
8184#endif
8185#ifdef FEAT_PYTHON
8186 {"pyeval", 1, 1, f_pyeval},
8187#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008188 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008189 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008190 {"reltime", 0, 2, f_reltime},
8191 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"remote_expr", 2, 3, f_remote_expr},
8193 {"remote_foreground", 1, 1, f_remote_foreground},
8194 {"remote_peek", 1, 2, f_remote_peek},
8195 {"remote_read", 1, 1, f_remote_read},
8196 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008197 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008199 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008202#ifdef FEAT_FLOAT
8203 {"round", 1, 1, f_round},
8204#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008205 {"screenattr", 2, 2, f_screenattr},
8206 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008207 {"screencol", 0, 0, f_screencol},
8208 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008209 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008210 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008211 {"searchpair", 3, 7, f_searchpair},
8212 {"searchpairpos", 3, 7, f_searchpairpos},
8213 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"server2client", 2, 2, f_server2client},
8215 {"serverlist", 0, 0, f_serverlist},
8216 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008217 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"setcmdpos", 1, 1, f_setcmdpos},
8219 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008220 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008221 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008222 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008223 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008225 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008226 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008228#ifdef FEAT_CRYPT
8229 {"sha256", 1, 1, f_sha256},
8230#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008231 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008232 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234#ifdef FEAT_FLOAT
8235 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008236 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008238 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008239 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008240 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008241 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008242 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008243#ifdef FEAT_FLOAT
8244 {"sqrt", 1, 1, f_sqrt},
8245 {"str2float", 1, 1, f_str2float},
8246#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008247 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008248 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008249 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250#ifdef HAVE_STRFTIME
8251 {"strftime", 1, 2, f_strftime},
8252#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008254 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"strlen", 1, 1, f_strlen},
8256 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008257 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008259 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008260 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"substitute", 4, 4, f_substitute},
8262 {"synID", 3, 3, f_synID},
8263 {"synIDattr", 2, 3, f_synIDattr},
8264 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008265 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008266 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008267 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008268 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008269 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008270 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008271 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008272 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008273 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008274#ifdef FEAT_FLOAT
8275 {"tan", 1, 1, f_tan},
8276 {"tanh", 1, 1, f_tanh},
8277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008279 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"tolower", 1, 1, f_tolower},
8281 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008282 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008283#ifdef FEAT_FLOAT
8284 {"trunc", 1, 1, f_trunc},
8285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008287 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008288 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008289 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008290 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"virtcol", 1, 1, f_virtcol},
8292 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008293 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"winbufnr", 1, 1, f_winbufnr},
8295 {"wincol", 0, 0, f_wincol},
8296 {"winheight", 1, 1, f_winheight},
8297 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008298 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008300 {"winrestview", 1, 1, f_winrestview},
8301 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008303 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008304 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008305 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306};
8307
8308#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8309
8310/*
8311 * Function given to ExpandGeneric() to obtain the list of internal
8312 * or user defined function names.
8313 */
8314 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008315get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
8317 static int intidx = -1;
8318 char_u *name;
8319
8320 if (idx == 0)
8321 intidx = -1;
8322 if (intidx < 0)
8323 {
8324 name = get_user_func_name(xp, idx);
8325 if (name != NULL)
8326 return name;
8327 }
8328 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8329 {
8330 STRCPY(IObuff, functions[intidx].f_name);
8331 STRCAT(IObuff, "(");
8332 if (functions[intidx].f_max_argc == 0)
8333 STRCAT(IObuff, ")");
8334 return IObuff;
8335 }
8336
8337 return NULL;
8338}
8339
8340/*
8341 * Function given to ExpandGeneric() to obtain the list of internal or
8342 * user defined variable or function names.
8343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008345get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 static int intidx = -1;
8348 char_u *name;
8349
8350 if (idx == 0)
8351 intidx = -1;
8352 if (intidx < 0)
8353 {
8354 name = get_function_name(xp, idx);
8355 if (name != NULL)
8356 return name;
8357 }
8358 return get_user_var_name(xp, ++intidx);
8359}
8360
8361#endif /* FEAT_CMDL_COMPL */
8362
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008363#if defined(EBCDIC) || defined(PROTO)
8364/*
8365 * Compare struct fst by function name.
8366 */
8367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008368compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008369{
8370 struct fst *p1 = (struct fst *)s1;
8371 struct fst *p2 = (struct fst *)s2;
8372
8373 return STRCMP(p1->f_name, p2->f_name);
8374}
8375
8376/*
8377 * Sort the function table by function name.
8378 * The sorting of the table above is ASCII dependant.
8379 * On machines using EBCDIC we have to sort it.
8380 */
8381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008382sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008383{
8384 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8385
8386 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8387}
8388#endif
8389
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391/*
8392 * Find internal function in table above.
8393 * Return index, or -1 if not found
8394 */
8395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008396find_internal_func(
8397 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 int first = 0;
8400 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8401 int cmp;
8402 int x;
8403
8404 /*
8405 * Find the function name in the table. Binary search.
8406 */
8407 while (first <= last)
8408 {
8409 x = first + ((unsigned)(last - first) >> 1);
8410 cmp = STRCMP(name, functions[x].f_name);
8411 if (cmp < 0)
8412 last = x - 1;
8413 else if (cmp > 0)
8414 first = x + 1;
8415 else
8416 return x;
8417 }
8418 return -1;
8419}
8420
8421/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008422 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8423 * name it contains, otherwise return "name".
8424 */
8425 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008426deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008427{
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008429 int cc;
8430
8431 cc = name[*lenp];
8432 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008433 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008435 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 {
8439 *lenp = 0;
8440 return (char_u *)""; /* just in case */
8441 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008442 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008443 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008444 }
8445
8446 return name;
8447}
8448
8449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 * Allocate a variable for the result of a function.
8451 * Return OK or FAIL.
8452 */
8453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008454get_func_tv(
8455 char_u *name, /* name of the function */
8456 int len, /* length of "name" */
8457 typval_T *rettv,
8458 char_u **arg, /* argument, pointing to the '(' */
8459 linenr_T firstline, /* first line of range */
8460 linenr_T lastline, /* last line of range */
8461 int *doesrange, /* return: function handled range */
8462 int evaluate,
8463 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 char_u *argp;
8466 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008467 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 int argcount = 0; /* number of arguments found */
8469
8470 /*
8471 * Get the arguments.
8472 */
8473 argp = *arg;
8474 while (argcount < MAX_FUNC_ARGS)
8475 {
8476 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8477 if (*argp == ')' || *argp == ',' || *argp == NUL)
8478 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8480 {
8481 ret = FAIL;
8482 break;
8483 }
8484 ++argcount;
8485 if (*argp != ',')
8486 break;
8487 }
8488 if (*argp == ')')
8489 ++argp;
8490 else
8491 ret = FAIL;
8492
8493 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 {
8498 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008499 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008501 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
8507 *arg = skipwhite(argp);
8508 return ret;
8509}
8510
8511
8512/*
8513 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008514 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008515 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008518call_func(
8519 char_u *funcname, /* name of the function */
8520 int len, /* length of "name" */
8521 typval_T *rettv, /* return value goes here */
8522 int argcount, /* number of "argvars" */
8523 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008524 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008525 linenr_T firstline, /* first line of range */
8526 linenr_T lastline, /* last line of range */
8527 int *doesrange, /* return: function handled range */
8528 int evaluate,
8529 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532#define ERROR_UNKNOWN 0
8533#define ERROR_TOOMANY 1
8534#define ERROR_TOOFEW 2
8535#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536#define ERROR_DICT 4
8537#define ERROR_NONE 5
8538#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 int error = ERROR_NONE;
8540 int i;
8541 int llen;
8542 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543#define FLEN_FIXED 40
8544 char_u fname_buf[FLEN_FIXED + 1];
8545 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008546 char_u *name;
8547
8548 /* Make a copy of the name, if it comes from a funcref variable it could
8549 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008550 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008551 if (name == NULL)
8552 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
8554 /*
8555 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8556 * Change <SNR>123_name() to K_SNR 123_name().
8557 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 llen = eval_fname_script(name);
8560 if (llen > 0)
8561 {
8562 fname_buf[0] = K_SPECIAL;
8563 fname_buf[1] = KS_EXTRA;
8564 fname_buf[2] = (int)KE_SNR;
8565 i = 3;
8566 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8567 {
8568 if (current_SID <= 0)
8569 error = ERROR_SCRIPT;
8570 else
8571 {
8572 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8573 i = (int)STRLEN(fname_buf);
8574 }
8575 }
8576 if (i + STRLEN(name + llen) < FLEN_FIXED)
8577 {
8578 STRCPY(fname_buf + i, name + llen);
8579 fname = fname_buf;
8580 }
8581 else
8582 {
8583 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8584 if (fname == NULL)
8585 error = ERROR_OTHER;
8586 else
8587 {
8588 mch_memmove(fname, fname_buf, (size_t)i);
8589 STRCPY(fname + i, name + llen);
8590 }
8591 }
8592 }
8593 else
8594 fname = name;
8595
8596 *doesrange = FALSE;
8597
8598
8599 /* execute the function if no errors detected and executing */
8600 if (evaluate && error == ERROR_NONE)
8601 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008602 char_u *rfname = fname;
8603
8604 /* Ignore "g:" before a function name. */
8605 if (fname[0] == 'g' && fname[1] == ':')
8606 rfname = fname + 2;
8607
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008608 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8609 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 error = ERROR_UNKNOWN;
8611
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008612 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {
8614 /*
8615 * User defined function.
8616 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008617 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008618
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008620 /* Trigger FuncUndefined event, may load the function. */
8621 if (fp == NULL
8622 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008623 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008624 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008626 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008627 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 }
8629#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008630 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008631 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008632 {
8633 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008634 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008635 }
8636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (fp != NULL)
8638 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008639 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008641 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008643 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008645 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 else
8648 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008649 int did_save_redo = FALSE;
8650
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 /*
8652 * Call the user function.
8653 * Save and restore search patterns, script variables and
8654 * redo buffer.
8655 */
8656 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008657#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008658 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008659#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008660 {
8661 saveRedobuff();
8662 did_save_redo = TRUE;
8663 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008664 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008666 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008667 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8668 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8669 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008670 /* Function was unreferenced while being used, free it
8671 * now. */
8672 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008673 if (did_save_redo)
8674 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 restore_search_patterns();
8676 error = ERROR_NONE;
8677 }
8678 }
8679 }
8680 else
8681 {
8682 /*
8683 * Find the function name in the table, call its implementation.
8684 */
8685 i = find_internal_func(fname);
8686 if (i >= 0)
8687 {
8688 if (argcount < functions[i].f_min_argc)
8689 error = ERROR_TOOFEW;
8690 else if (argcount > functions[i].f_max_argc)
8691 error = ERROR_TOOMANY;
8692 else
8693 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008694 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 error = ERROR_NONE;
8697 }
8698 }
8699 }
8700 /*
8701 * The function call (or "FuncUndefined" autocommand sequence) might
8702 * have been aborted by an error, an interrupt, or an explicitly thrown
8703 * exception that has not been caught so far. This situation can be
8704 * tested for by calling aborting(). For an error in an internal
8705 * function or for the "E132" error in call_user_func(), however, the
8706 * throw point at which the "force_abort" flag (temporarily reset by
8707 * emsg()) is normally updated has not been reached yet. We need to
8708 * update that flag first to make aborting() reliable.
8709 */
8710 update_force_abort();
8711 }
8712 if (error == ERROR_NONE)
8713 ret = OK;
8714
8715 /*
8716 * Report an error unless the argument evaluation or function call has been
8717 * cancelled due to an aborting error, an interrupt, or an exception.
8718 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008719 if (!aborting())
8720 {
8721 switch (error)
8722 {
8723 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008724 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008725 break;
8726 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008727 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008728 break;
8729 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008730 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 name);
8732 break;
8733 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008734 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008735 name);
8736 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008737 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008738 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008739 name);
8740 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008741 }
8742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (fname != name && fname != fname_buf)
8745 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008746 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
8748 return ret;
8749}
8750
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008751/*
8752 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008753 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008754 */
8755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008756emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008757{
8758 char_u *p;
8759
8760 if (*name == K_SPECIAL)
8761 p = concat_str((char_u *)"<SNR>", name + 3);
8762 else
8763 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008764 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008765 if (p != name)
8766 vim_free(p);
8767}
8768
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008769/*
8770 * Return TRUE for a non-zero Number and a non-empty String.
8771 */
8772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008773non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008774{
8775 return ((argvars[0].v_type == VAR_NUMBER
8776 && argvars[0].vval.v_number != 0)
8777 || (argvars[0].v_type == VAR_STRING
8778 && argvars[0].vval.v_string != NULL
8779 && *argvars[0].vval.v_string != NUL));
8780}
8781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782/*********************************************
8783 * Implementation of the built-in functions
8784 */
8785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008786#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008787static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008788
8789/*
8790 * Get the float value of "argvars[0]" into "f".
8791 * Returns FAIL when the argument is not a Number or Float.
8792 */
8793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008794get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008795{
8796 if (argvars[0].v_type == VAR_FLOAT)
8797 {
8798 *f = argvars[0].vval.v_float;
8799 return OK;
8800 }
8801 if (argvars[0].v_type == VAR_NUMBER)
8802 {
8803 *f = (float_T)argvars[0].vval.v_number;
8804 return OK;
8805 }
8806 EMSG(_("E808: Number or Float required"));
8807 return FAIL;
8808}
8809
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008810/*
8811 * "abs(expr)" function
8812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008815{
8816 if (argvars[0].v_type == VAR_FLOAT)
8817 {
8818 rettv->v_type = VAR_FLOAT;
8819 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8820 }
8821 else
8822 {
8823 varnumber_T n;
8824 int error = FALSE;
8825
8826 n = get_tv_number_chk(&argvars[0], &error);
8827 if (error)
8828 rettv->vval.v_number = -1;
8829 else if (n > 0)
8830 rettv->vval.v_number = n;
8831 else
8832 rettv->vval.v_number = -n;
8833 }
8834}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835
8836/*
8837 * "acos()" function
8838 */
8839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008840f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008841{
8842 float_T f;
8843
8844 rettv->v_type = VAR_FLOAT;
8845 if (get_float_arg(argvars, &f) == OK)
8846 rettv->vval.v_float = acos(f);
8847 else
8848 rettv->vval.v_float = 0.0;
8849}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008850#endif
8851
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008853 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 */
8855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008856f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
Bram Moolenaar33570922005-01-25 22:26:29 +00008858 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008861 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008863 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008864 && !tv_check_lock(l->lv_lock,
8865 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008866 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008868 }
8869 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008870 EMSG(_(e_listreq));
8871}
8872
8873/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008874 * "alloc_fail(id, countdown, repeat)" function
8875 */
8876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008878{
8879 if (argvars[0].v_type != VAR_NUMBER
8880 || argvars[0].vval.v_number <= 0
8881 || argvars[1].v_type != VAR_NUMBER
8882 || argvars[1].vval.v_number < 0
8883 || argvars[2].v_type != VAR_NUMBER)
8884 EMSG(_(e_invarg));
8885 else
8886 {
8887 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008888 if (alloc_fail_id >= aid_last)
8889 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008890 alloc_fail_countdown = argvars[1].vval.v_number;
8891 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008892 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008893 }
8894}
8895
8896/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008897 * "and(expr, expr)" function
8898 */
8899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008900f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008901{
8902 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8903 & get_tv_number_chk(&argvars[1], NULL);
8904}
8905
8906/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008907 * "append(lnum, string/list)" function
8908 */
8909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008910f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008911{
8912 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008913 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 list_T *l = NULL;
8915 listitem_T *li = NULL;
8916 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008917 long added = 0;
8918
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008919 /* When coming here from Insert mode, sync undo, so that this can be
8920 * undone separately from what was previously inserted. */
8921 if (u_sync_once == 2)
8922 {
8923 u_sync_once = 1; /* notify that u_sync() was called */
8924 u_sync(TRUE);
8925 }
8926
Bram Moolenaar0d660222005-01-07 21:51:51 +00008927 lnum = get_tv_lnum(argvars);
8928 if (lnum >= 0
8929 && lnum <= curbuf->b_ml.ml_line_count
8930 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008931 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008933 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008934 l = argvars[1].vval.v_list;
8935 if (l == NULL)
8936 return;
8937 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008938 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008939 for (;;)
8940 {
8941 if (l == NULL)
8942 tv = &argvars[1]; /* append a string */
8943 else if (li == NULL)
8944 break; /* end of list */
8945 else
8946 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 line = get_tv_string_chk(tv);
8948 if (line == NULL) /* type error */
8949 {
8950 rettv->vval.v_number = 1; /* Failed */
8951 break;
8952 }
8953 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008954 ++added;
8955 if (l == NULL)
8956 break;
8957 li = li->li_next;
8958 }
8959
8960 appended_lines_mark(lnum, added);
8961 if (curwin->w_cursor.lnum > lnum)
8962 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 else
8965 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966}
8967
8968/*
8969 * "argc()" function
8970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008972f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975}
8976
8977/*
8978 * "argidx()" function
8979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008981f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008987 * "arglistid()" function
8988 */
8989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008990f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008991{
8992 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008993
8994 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01008995 wp = find_tabwin(&argvars[0], &argvars[1]);
8996 if (wp != NULL)
8997 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008998}
8999
9000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 * "argv(nr)" function
9002 */
9003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009004f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006 int idx;
9007
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009008 if (argvars[0].v_type != VAR_UNKNOWN)
9009 {
9010 idx = get_tv_number_chk(&argvars[0], NULL);
9011 if (idx >= 0 && idx < ARGCOUNT)
9012 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9013 else
9014 rettv->vval.v_string = NULL;
9015 rettv->v_type = VAR_STRING;
9016 }
9017 else if (rettv_list_alloc(rettv) == OK)
9018 for (idx = 0; idx < ARGCOUNT; ++idx)
9019 list_append_string(rettv->vval.v_list,
9020 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021}
9022
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009023static void prepare_assert_error(garray_T*gap);
9024static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9025static void assert_error(garray_T *gap);
9026static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009027
9028/*
9029 * Prepare "gap" for an assert error and add the sourcing position.
9030 */
9031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009033{
9034 char buf[NUMBUFLEN];
9035
9036 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009037 if (sourcing_name != NULL)
9038 {
9039 ga_concat(gap, sourcing_name);
9040 if (sourcing_lnum > 0)
9041 ga_concat(gap, (char_u *)" ");
9042 }
9043 if (sourcing_lnum > 0)
9044 {
9045 sprintf(buf, "line %ld", (long)sourcing_lnum);
9046 ga_concat(gap, (char_u *)buf);
9047 }
9048 if (sourcing_name != NULL || sourcing_lnum > 0)
9049 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009050}
9051
9052/*
9053 * Fill "gap" with information about an assert error.
9054 */
9055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009056fill_assert_error(
9057 garray_T *gap,
9058 typval_T *opt_msg_tv,
9059 char_u *exp_str,
9060 typval_T *exp_tv,
9061 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009062{
9063 char_u numbuf[NUMBUFLEN];
9064 char_u *tofree;
9065
9066 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9067 {
9068 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9069 vim_free(tofree);
9070 }
9071 else
9072 {
9073 ga_concat(gap, (char_u *)"Expected ");
9074 if (exp_str == NULL)
9075 {
9076 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9077 vim_free(tofree);
9078 }
9079 else
9080 ga_concat(gap, exp_str);
9081 ga_concat(gap, (char_u *)" but got ");
9082 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9083 vim_free(tofree);
9084 }
9085}
Bram Moolenaar43345542015-11-29 17:35:35 +01009086
9087/*
9088 * Add an assert error to v:errors.
9089 */
9090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009091assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009092{
9093 struct vimvar *vp = &vimvars[VV_ERRORS];
9094
9095 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9096 /* Make sure v:errors is a list. */
9097 set_vim_var_list(VV_ERRORS, list_alloc());
9098 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9099}
9100
Bram Moolenaar43345542015-11-29 17:35:35 +01009101/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009102 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009103 */
9104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009105f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009106{
9107 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009108
9109 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9110 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009111 prepare_assert_error(&ga);
9112 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9113 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009114 ga_clear(&ga);
9115 }
9116}
9117
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009118/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009119 * "assert_exception(string[, msg])" function
9120 */
9121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009122f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009123{
9124 garray_T ga;
9125 char *error;
9126
9127 error = (char *)get_tv_string_chk(&argvars[0]);
9128 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9129 {
9130 prepare_assert_error(&ga);
9131 ga_concat(&ga, (char_u *)"v:exception is not set");
9132 assert_error(&ga);
9133 ga_clear(&ga);
9134 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009135 else if (error != NULL
9136 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009137 {
9138 prepare_assert_error(&ga);
9139 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9140 &vimvars[VV_EXCEPTION].vv_tv);
9141 assert_error(&ga);
9142 ga_clear(&ga);
9143 }
9144}
9145
9146/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009147 * "assert_fails(cmd [, error])" function
9148 */
9149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009150f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009151{
9152 char_u *cmd = get_tv_string_chk(&argvars[0]);
9153 garray_T ga;
9154
9155 called_emsg = FALSE;
9156 suppress_errthrow = TRUE;
9157 emsg_silent = TRUE;
9158 do_cmdline_cmd(cmd);
9159 if (!called_emsg)
9160 {
9161 prepare_assert_error(&ga);
9162 ga_concat(&ga, (char_u *)"command did not fail: ");
9163 ga_concat(&ga, cmd);
9164 assert_error(&ga);
9165 ga_clear(&ga);
9166 }
9167 else if (argvars[1].v_type != VAR_UNKNOWN)
9168 {
9169 char_u buf[NUMBUFLEN];
9170 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9171
9172 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9173 {
9174 prepare_assert_error(&ga);
9175 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9176 &vimvars[VV_ERRMSG].vv_tv);
9177 assert_error(&ga);
9178 ga_clear(&ga);
9179 }
9180 }
9181
9182 called_emsg = FALSE;
9183 suppress_errthrow = FALSE;
9184 emsg_silent = FALSE;
9185 emsg_on_display = FALSE;
9186 set_vim_var_string(VV_ERRMSG, NULL, 0);
9187}
9188
9189/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009190 * Common for assert_true() and assert_false().
9191 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009193assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009194{
9195 int error = FALSE;
9196 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009197
9198 if (argvars[0].v_type != VAR_NUMBER
9199 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9200 || error)
9201 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009202 prepare_assert_error(&ga);
9203 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009204 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009205 NULL, &argvars[0]);
9206 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009207 ga_clear(&ga);
9208 }
9209}
9210
9211/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009212 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009213 */
9214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009215f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009216{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009217 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009218}
9219
9220/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009221 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009222 */
9223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009224f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009225{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009226 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009227}
9228
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009229#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009230/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009231 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009232 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009234f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009235{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009236 float_T f;
9237
9238 rettv->v_type = VAR_FLOAT;
9239 if (get_float_arg(argvars, &f) == OK)
9240 rettv->vval.v_float = asin(f);
9241 else
9242 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009243}
9244
9245/*
9246 * "atan()" function
9247 */
9248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009249f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009250{
9251 float_T f;
9252
9253 rettv->v_type = VAR_FLOAT;
9254 if (get_float_arg(argvars, &f) == OK)
9255 rettv->vval.v_float = atan(f);
9256 else
9257 rettv->vval.v_float = 0.0;
9258}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009259
9260/*
9261 * "atan2()" function
9262 */
9263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009264f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009265{
9266 float_T fx, fy;
9267
9268 rettv->v_type = VAR_FLOAT;
9269 if (get_float_arg(argvars, &fx) == OK
9270 && get_float_arg(&argvars[1], &fy) == OK)
9271 rettv->vval.v_float = atan2(fx, fy);
9272 else
9273 rettv->vval.v_float = 0.0;
9274}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009275#endif
9276
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277/*
9278 * "browse(save, title, initdir, default)" function
9279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009281f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283#ifdef FEAT_BROWSE
9284 int save;
9285 char_u *title;
9286 char_u *initdir;
9287 char_u *defname;
9288 char_u buf[NUMBUFLEN];
9289 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009292 save = get_tv_number_chk(&argvars[0], &error);
9293 title = get_tv_string_chk(&argvars[1]);
9294 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9295 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 if (error || title == NULL || initdir == NULL || defname == NULL)
9298 rettv->vval.v_string = NULL;
9299 else
9300 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009301 do_browse(save ? BROWSE_SAVE : 0,
9302 title, defname, NULL, initdir, NULL, curbuf);
9303#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009305#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009307}
9308
9309/*
9310 * "browsedir(title, initdir)" function
9311 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009313f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009314{
9315#ifdef FEAT_BROWSE
9316 char_u *title;
9317 char_u *initdir;
9318 char_u buf[NUMBUFLEN];
9319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009320 title = get_tv_string_chk(&argvars[0]);
9321 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 if (title == NULL || initdir == NULL)
9324 rettv->vval.v_string = NULL;
9325 else
9326 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009327 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009329 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332}
9333
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009334static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336/*
9337 * Find a buffer by number or exact name.
9338 */
9339 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009340find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
9342 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009344 if (avar->v_type == VAR_NUMBER)
9345 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009346 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009348 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009349 if (buf == NULL)
9350 {
9351 /* No full path name match, try a match with a URL or a "nofile"
9352 * buffer, these don't use the full path. */
9353 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9354 if (buf->b_fname != NULL
9355 && (path_with_url(buf->b_fname)
9356#ifdef FEAT_QUICKFIX
9357 || bt_nofile(buf)
9358#endif
9359 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009360 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009361 break;
9362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 }
9364 return buf;
9365}
9366
9367/*
9368 * "bufexists(expr)" function
9369 */
9370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009371f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374}
9375
9376/*
9377 * "buflisted(expr)" function
9378 */
9379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009380f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 buf_T *buf;
9383
9384 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386}
9387
9388/*
9389 * "bufloaded(expr)" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 buf_T *buf;
9395
9396 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009400static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009401
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402/*
9403 * Get buffer by number or pattern.
9404 */
9405 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009406get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 int save_magic;
9410 char_u *save_cpo;
9411 buf_T *buf;
9412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 if (tv->v_type == VAR_NUMBER)
9414 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009415 if (tv->v_type != VAR_STRING)
9416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (name == NULL || *name == NUL)
9418 return curbuf;
9419 if (name[0] == '$' && name[1] == NUL)
9420 return lastbuf;
9421
9422 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9423 save_magic = p_magic;
9424 p_magic = TRUE;
9425 save_cpo = p_cpo;
9426 p_cpo = (char_u *)"";
9427
9428 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009429 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430
9431 p_magic = save_magic;
9432 p_cpo = save_cpo;
9433
9434 /* If not found, try expanding the name, like done for bufexists(). */
9435 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
9438 return buf;
9439}
9440
9441/*
9442 * "bufname(expr)" function
9443 */
9444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009445f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 buf_T *buf;
9448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009451 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 --emsg_off;
9458}
9459
9460/*
9461 * "bufnr(expr)" function
9462 */
9463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009464f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009467 int error = FALSE;
9468 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009472 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009473 --emsg_off;
9474
9475 /* If the buffer isn't found and the second argument is not zero create a
9476 * new buffer. */
9477 if (buf == NULL
9478 && argvars[1].v_type != VAR_UNKNOWN
9479 && get_tv_number_chk(&argvars[1], &error) != 0
9480 && !error
9481 && (name = get_tv_string_chk(&argvars[0])) != NULL
9482 && !error)
9483 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9484
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489}
9490
9491/*
9492 * "bufwinnr(nr)" function
9493 */
9494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009495f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497#ifdef FEAT_WINDOWS
9498 win_T *wp;
9499 int winnr = 0;
9500#endif
9501 buf_T *buf;
9502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009505 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506#ifdef FEAT_WINDOWS
9507 for (wp = firstwin; wp; wp = wp->w_next)
9508 {
9509 ++winnr;
9510 if (wp->w_buffer == buf)
9511 break;
9512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516#endif
9517 --emsg_off;
9518}
9519
9520/*
9521 * "byte2line(byte)" function
9522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528#else
9529 long boff = 0;
9530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 (linenr_T)0, &boff);
9537#endif
9538}
9539
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009541byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009542{
9543#ifdef FEAT_MBYTE
9544 char_u *t;
9545#endif
9546 char_u *str;
9547 long idx;
9548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 str = get_tv_string_chk(&argvars[0]);
9550 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009553 return;
9554
9555#ifdef FEAT_MBYTE
9556 t = str;
9557 for ( ; idx > 0; idx--)
9558 {
9559 if (*t == NUL) /* EOL reached */
9560 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009561 if (enc_utf8 && comp)
9562 t += utf_ptr2len(t);
9563 else
9564 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009565 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009566 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009567#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009568 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009570#endif
9571}
9572
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009573/*
9574 * "byteidx()" function
9575 */
9576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009577f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009578{
9579 byteidx(argvars, rettv, FALSE);
9580}
9581
9582/*
9583 * "byteidxcomp()" function
9584 */
9585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009587{
9588 byteidx(argvars, rettv, TRUE);
9589}
9590
Bram Moolenaardb913952012-06-29 12:54:53 +02009591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592func_call(
9593 char_u *name,
9594 typval_T *args,
9595 dict_T *selfdict,
9596 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009597{
9598 listitem_T *item;
9599 typval_T argv[MAX_FUNC_ARGS + 1];
9600 int argc = 0;
9601 int dummy;
9602 int r = 0;
9603
9604 for (item = args->vval.v_list->lv_first; item != NULL;
9605 item = item->li_next)
9606 {
9607 if (argc == MAX_FUNC_ARGS)
9608 {
9609 EMSG(_("E699: Too many arguments"));
9610 break;
9611 }
9612 /* Make a copy of each argument. This is needed to be able to set
9613 * v_lock to VAR_FIXED in the copy without changing the original list.
9614 */
9615 copy_tv(&item->li_tv, &argv[argc++]);
9616 }
9617
9618 if (item == NULL)
9619 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9620 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9621 &dummy, TRUE, selfdict);
9622
9623 /* Free the arguments. */
9624 while (argc > 0)
9625 clear_tv(&argv[--argc]);
9626
9627 return r;
9628}
9629
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009630/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009631 * "call(func, arglist)" function
9632 */
9633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009634f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009635{
9636 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009638
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009639 if (argvars[1].v_type != VAR_LIST)
9640 {
9641 EMSG(_(e_listreq));
9642 return;
9643 }
9644 if (argvars[1].vval.v_list == NULL)
9645 return;
9646
9647 if (argvars[0].v_type == VAR_FUNC)
9648 func = argvars[0].vval.v_string;
9649 else
9650 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 if (*func == NUL)
9652 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009653
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 if (argvars[2].v_type != VAR_UNKNOWN)
9655 {
9656 if (argvars[2].v_type != VAR_DICT)
9657 {
9658 EMSG(_(e_dictreq));
9659 return;
9660 }
9661 selfdict = argvars[2].vval.v_dict;
9662 }
9663
Bram Moolenaardb913952012-06-29 12:54:53 +02009664 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009665}
9666
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009667#ifdef FEAT_FLOAT
9668/*
9669 * "ceil({float})" function
9670 */
9671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009672f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009673{
9674 float_T f;
9675
9676 rettv->v_type = VAR_FLOAT;
9677 if (get_float_arg(argvars, &f) == OK)
9678 rettv->vval.v_float = ceil(f);
9679 else
9680 rettv->vval.v_float = 0.0;
9681}
9682#endif
9683
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009684#ifdef FEAT_CHANNEL
9685/*
9686 * Get the channel index from the handle argument.
9687 * Returns -1 if the handle is invalid or the channel is closed.
9688 */
9689 static int
9690get_channel_arg(typval_T *tv)
9691{
9692 int ch_idx;
9693
9694 if (tv->v_type != VAR_NUMBER)
9695 {
9696 EMSG2(_(e_invarg2), get_tv_string(tv));
9697 return -1;
9698 }
9699 ch_idx = tv->vval.v_number;
9700
9701 if (!channel_is_open(ch_idx))
9702 {
9703 EMSGN(_("E906: not an open channel"), ch_idx);
9704 return -1;
9705 }
9706 return ch_idx;
9707}
9708
9709/*
9710 * "ch_close()" function
9711 */
9712 static void
9713f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9714{
9715 int ch_idx = get_channel_arg(&argvars[0]);
9716
9717 if (ch_idx >= 0)
9718 channel_close(ch_idx);
9719}
9720
9721/*
9722 * Get a callback from "arg". It can be a Funcref or a function name.
9723 * When "arg" is zero return an empty string.
9724 * Return NULL for an invalid argument.
9725 */
9726 static char_u *
9727get_callback(typval_T *arg)
9728{
9729 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9730 return arg->vval.v_string;
9731 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9732 return (char_u *)"";
9733 EMSG(_("E999: Invalid callback argument"));
9734 return NULL;
9735}
9736
9737/*
9738 * "ch_open()" function
9739 */
9740 static void
9741f_ch_open(typval_T *argvars, typval_T *rettv)
9742{
9743 char_u *address;
9744 char_u *mode;
9745 char_u *callback = NULL;
9746 char_u buf1[NUMBUFLEN];
9747 char_u *p;
9748 int port;
9749 int json_mode = FALSE;
9750
9751 /* default: fail */
9752 rettv->vval.v_number = -1;
9753
9754 address = get_tv_string(&argvars[0]);
9755 mode = get_tv_string_buf(&argvars[1], buf1);
9756 if (argvars[2].v_type != VAR_UNKNOWN)
9757 {
9758 callback = get_callback(&argvars[2]);
9759 if (callback == NULL)
9760 return;
9761 }
9762
9763 /* parse address */
9764 p = vim_strchr(address, ':');
9765 if (p == NULL)
9766 {
9767 EMSG2(_(e_invarg2), address);
9768 return;
9769 }
9770 *p++ = NUL;
9771 port = atoi((char *)p);
9772 if (*address == NUL || port <= 0)
9773 {
9774 p[-1] = ':';
9775 EMSG2(_(e_invarg2), address);
9776 return;
9777 }
9778
9779 /* parse mode */
9780 if (STRCMP(mode, "json") == 0)
9781 json_mode = TRUE;
9782 else if (STRCMP(mode, "raw") != 0)
9783 {
9784 EMSG2(_(e_invarg2), mode);
9785 return;
9786 }
9787
9788 rettv->vval.v_number = channel_open((char *)address, port, NULL);
9789 if (rettv->vval.v_number >= 0)
9790 {
9791 channel_set_json_mode(rettv->vval.v_number, json_mode);
9792 if (callback != NULL && *callback != NUL)
9793 channel_set_callback(rettv->vval.v_number, callback);
9794 }
9795}
9796
9797/*
9798 * common for "sendexpr()" and "sendraw()"
9799 * Returns the channel index if the caller should read the response.
9800 * Otherwise returns -1.
9801 */
9802 static int
9803send_common(typval_T *argvars, char_u *text, char *fun)
9804{
9805 int ch_idx;
9806 char_u *callback = NULL;
9807
9808 ch_idx = get_channel_arg(&argvars[0]);
9809 if (ch_idx < 0)
9810 return -1;
9811
9812 if (argvars[2].v_type != VAR_UNKNOWN)
9813 {
9814 callback = get_callback(&argvars[2]);
9815 if (callback == NULL)
9816 return -1;
9817 }
9818 /* Set the callback or clear it. An empty callback means no callback and
9819 * not reading the response. */
9820 channel_set_req_callback(ch_idx,
9821 callback != NULL && *callback == NUL ? NULL : callback);
9822
9823 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9824 return ch_idx;
9825 return -1;
9826}
9827
9828/*
9829 * "ch_sendexpr()" function
9830 */
9831 static void
9832f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9833{
9834 char_u *text;
9835 typval_T *listtv;
9836 int ch_idx;
9837 int id;
9838
9839 /* return an empty string by default */
9840 rettv->v_type = VAR_STRING;
9841 rettv->vval.v_string = NULL;
9842
9843 id = channel_get_id();
9844 text = json_encode_nr_expr(id, &argvars[1]);
9845 if (text == NULL)
9846 return;
9847
9848 ch_idx = send_common(argvars, text, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009849 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009850 if (ch_idx >= 0)
9851 {
9852 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9853 {
9854 if (listtv->v_type == VAR_LIST)
9855 {
9856 list_T *list = listtv->vval.v_list;
9857
9858 if (list->lv_len == 2)
9859 {
9860 /* Move the item from the list and then change the type to
9861 * avoid the value being freed. */
9862 *rettv = list->lv_last->li_tv;
9863 list->lv_last->li_tv.v_type = VAR_NUMBER;
9864 }
9865 }
9866 clear_tv(listtv);
9867 }
9868 }
9869}
9870
9871/*
9872 * "ch_sendraw()" function
9873 */
9874 static void
9875f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9876{
9877 char_u buf[NUMBUFLEN];
9878 char_u *text;
9879 int ch_idx;
9880
9881 /* return an empty string by default */
9882 rettv->v_type = VAR_STRING;
9883 rettv->vval.v_string = NULL;
9884
9885 text = get_tv_string_buf(&argvars[1], buf);
9886 ch_idx = send_common(argvars, text, "sendraw");
9887 if (ch_idx >= 0)
9888 rettv->vval.v_string = channel_read_block(ch_idx);
9889}
9890#endif
9891
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009893 * "changenr()" function
9894 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009896f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009897{
9898 rettv->vval.v_number = curbuf->b_u_seq_cur;
9899}
9900
9901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 * "char2nr(string)" function
9903 */
9904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009905f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
9907#ifdef FEAT_MBYTE
9908 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009909 {
9910 int utf8 = 0;
9911
9912 if (argvars[1].v_type != VAR_UNKNOWN)
9913 utf8 = get_tv_number_chk(&argvars[1], NULL);
9914
9915 if (utf8)
9916 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9917 else
9918 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 else
9921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009922 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923}
9924
9925/*
9926 * "cindent(lnum)" function
9927 */
9928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009929f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931#ifdef FEAT_CINDENT
9932 pos_T pos;
9933 linenr_T lnum;
9934
9935 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9938 {
9939 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 curwin->w_cursor = pos;
9942 }
9943 else
9944#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946}
9947
9948/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009949 * "clearmatches()" function
9950 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009953{
9954#ifdef FEAT_SEARCH_EXTRA
9955 clear_matches(curwin);
9956#endif
9957}
9958
9959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 * "col(string)" function
9961 */
9962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009963f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965 colnr_T col = 0;
9966 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009967 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009969 fp = var2fpos(&argvars[0], FALSE, &fnum);
9970 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 {
9972 if (fp->col == MAXCOL)
9973 {
9974 /* '> can be MAXCOL, get the length of the line then */
9975 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009976 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 else
9978 col = MAXCOL;
9979 }
9980 else
9981 {
9982 col = fp->col + 1;
9983#ifdef FEAT_VIRTUALEDIT
9984 /* col(".") when the cursor is on the NUL at the end of the line
9985 * because of "coladd" can be seen as an extra column. */
9986 if (virtual_active() && fp == &curwin->w_cursor)
9987 {
9988 char_u *p = ml_get_cursor();
9989
9990 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9991 curwin->w_virtcol - curwin->w_cursor.coladd))
9992 {
9993# ifdef FEAT_MBYTE
9994 int l;
9995
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009996 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 col += l;
9998# else
9999 if (*p != NUL && p[1] == NUL)
10000 ++col;
10001# endif
10002 }
10003 }
10004#endif
10005 }
10006 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008}
10009
Bram Moolenaar572cb562005-08-05 21:35:02 +000010010#if defined(FEAT_INS_EXPAND)
10011/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010012 * "complete()" function
10013 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010015f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010016{
10017 int startcol;
10018
10019 if ((State & INSERT) == 0)
10020 {
10021 EMSG(_("E785: complete() can only be used in Insert mode"));
10022 return;
10023 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010024
10025 /* Check for undo allowed here, because if something was already inserted
10026 * the line was already saved for undo and this check isn't done. */
10027 if (!undo_allowed())
10028 return;
10029
Bram Moolenaarade00832006-03-10 21:46:58 +000010030 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10031 {
10032 EMSG(_(e_invarg));
10033 return;
10034 }
10035
10036 startcol = get_tv_number_chk(&argvars[0], NULL);
10037 if (startcol <= 0)
10038 return;
10039
10040 set_completion(startcol - 1, argvars[1].vval.v_list);
10041}
10042
10043/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010044 * "complete_add()" function
10045 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010047f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010048{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010049 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010050}
10051
10052/*
10053 * "complete_check()" function
10054 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010056f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010057{
10058 int saved = RedrawingDisabled;
10059
10060 RedrawingDisabled = 0;
10061 ins_compl_check_keys(0);
10062 rettv->vval.v_number = compl_interrupted;
10063 RedrawingDisabled = saved;
10064}
10065#endif
10066
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067/*
10068 * "confirm(message, buttons[, default [, type]])" function
10069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010071f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072{
10073#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10074 char_u *message;
10075 char_u *buttons = NULL;
10076 char_u buf[NUMBUFLEN];
10077 char_u buf2[NUMBUFLEN];
10078 int def = 1;
10079 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010080 char_u *typestr;
10081 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010083 message = get_tv_string_chk(&argvars[0]);
10084 if (message == NULL)
10085 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10089 if (buttons == NULL)
10090 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010091 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010094 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10097 if (typestr == NULL)
10098 error = TRUE;
10099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 switch (TOUPPER_ASC(*typestr))
10102 {
10103 case 'E': type = VIM_ERROR; break;
10104 case 'Q': type = VIM_QUESTION; break;
10105 case 'I': type = VIM_INFO; break;
10106 case 'W': type = VIM_WARNING; break;
10107 case 'G': type = VIM_GENERIC; break;
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110 }
10111 }
10112 }
10113
10114 if (buttons == NULL || *buttons == NUL)
10115 buttons = (char_u *)_("&Ok");
10116
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010117 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010119 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#endif
10121}
10122
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010123/*
10124 * "copy()" function
10125 */
10126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010127f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010128{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010129 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010130}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010132#ifdef FEAT_FLOAT
10133/*
10134 * "cos()" function
10135 */
10136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010137f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010138{
10139 float_T f;
10140
10141 rettv->v_type = VAR_FLOAT;
10142 if (get_float_arg(argvars, &f) == OK)
10143 rettv->vval.v_float = cos(f);
10144 else
10145 rettv->vval.v_float = 0.0;
10146}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010147
10148/*
10149 * "cosh()" function
10150 */
10151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010152f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010153{
10154 float_T f;
10155
10156 rettv->v_type = VAR_FLOAT;
10157 if (get_float_arg(argvars, &f) == OK)
10158 rettv->vval.v_float = cosh(f);
10159 else
10160 rettv->vval.v_float = 0.0;
10161}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010162#endif
10163
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010165 * "count()" function
10166 */
10167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010168f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010169{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010170 long n = 0;
10171 int ic = FALSE;
10172
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010174 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 listitem_T *li;
10176 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010177 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010178
Bram Moolenaare9a41262005-01-15 22:18:47 +000010179 if ((l = argvars[0].vval.v_list) != NULL)
10180 {
10181 li = l->lv_first;
10182 if (argvars[2].v_type != VAR_UNKNOWN)
10183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 int error = FALSE;
10185
10186 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187 if (argvars[3].v_type != VAR_UNKNOWN)
10188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 idx = get_tv_number_chk(&argvars[3], &error);
10190 if (!error)
10191 {
10192 li = list_find(l, idx);
10193 if (li == NULL)
10194 EMSGN(_(e_listidx), idx);
10195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 if (error)
10198 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 }
10200
10201 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010202 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 ++n;
10204 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010206 else if (argvars[0].v_type == VAR_DICT)
10207 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 int todo;
10209 dict_T *d;
10210 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010211
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010212 if ((d = argvars[0].vval.v_dict) != NULL)
10213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010214 int error = FALSE;
10215
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 if (argvars[2].v_type != VAR_UNKNOWN)
10217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 if (argvars[3].v_type != VAR_UNKNOWN)
10220 EMSG(_(e_invarg));
10221 }
10222
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010223 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010225 {
10226 if (!HASHITEM_EMPTY(hi))
10227 {
10228 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010229 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010230 ++n;
10231 }
10232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 }
10234 }
10235 else
10236 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010237 rettv->vval.v_number = n;
10238}
10239
10240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10242 *
10243 * Checks the existence of a cscope connection.
10244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010246f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247{
10248#ifdef FEAT_CSCOPE
10249 int num = 0;
10250 char_u *dbpath = NULL;
10251 char_u *prepend = NULL;
10252 char_u buf[NUMBUFLEN];
10253
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010254 if (argvars[0].v_type != VAR_UNKNOWN
10255 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 num = (int)get_tv_number(&argvars[0]);
10258 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010259 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 }
10262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#endif
10265}
10266
10267/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010268 * "cursor(lnum, col)" function, or
10269 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010271 * Moves the cursor to the specified line and column.
10272 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010275f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276{
10277 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010278#ifdef FEAT_VIRTUALEDIT
10279 long coladd = 0;
10280#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010281 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010283 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010284 if (argvars[1].v_type == VAR_UNKNOWN)
10285 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010286 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010287 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010288
Bram Moolenaar493c1782014-05-28 14:34:46 +020010289 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010290 {
10291 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010292 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010293 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010294 line = pos.lnum;
10295 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010296#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010297 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010298#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010299 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010300 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010301 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010302 set_curswant = FALSE;
10303 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010304 }
10305 else
10306 {
10307 line = get_tv_lnum(argvars);
10308 col = get_tv_number_chk(&argvars[1], NULL);
10309#ifdef FEAT_VIRTUALEDIT
10310 if (argvars[2].v_type != VAR_UNKNOWN)
10311 coladd = get_tv_number_chk(&argvars[2], NULL);
10312#endif
10313 }
10314 if (line < 0 || col < 0
10315#ifdef FEAT_VIRTUALEDIT
10316 || coladd < 0
10317#endif
10318 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 if (line > 0)
10321 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 if (col > 0)
10323 curwin->w_cursor.col = col - 1;
10324#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010325 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326#endif
10327
10328 /* Make sure the cursor is in a valid position. */
10329 check_cursor();
10330#ifdef FEAT_MBYTE
10331 /* Correct cursor for multi-byte character. */
10332 if (has_mbyte)
10333 mb_adjust_cursor();
10334#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010335
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010336 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010337 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338}
10339
10340/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010341 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 */
10343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010344f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010346 int noref = 0;
10347
10348 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010349 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010350 if (noref < 0 || noref > 1)
10351 EMSG(_(e_invarg));
10352 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010353 {
10354 current_copyID += COPYID_INC;
10355 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357}
10358
10359/*
10360 * "delete()" function
10361 */
10362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010363f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010365 char_u nbuf[NUMBUFLEN];
10366 char_u *name;
10367 char_u *flags;
10368
10369 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010371 return;
10372
10373 name = get_tv_string(&argvars[0]);
10374 if (name == NULL || *name == NUL)
10375 {
10376 EMSG(_(e_invarg));
10377 return;
10378 }
10379
10380 if (argvars[1].v_type != VAR_UNKNOWN)
10381 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010383 flags = (char_u *)"";
10384
10385 if (*flags == NUL)
10386 /* delete a file */
10387 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10388 else if (STRCMP(flags, "d") == 0)
10389 /* delete an empty directory */
10390 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10391 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010392 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010393 rettv->vval.v_number = delete_recursive(name);
10394 else
10395 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396}
10397
10398/*
10399 * "did_filetype()" function
10400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010402f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403{
10404#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406#endif
10407}
10408
10409/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010410 * "diff_filler()" function
10411 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010413f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010414{
10415#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010417#endif
10418}
10419
10420/*
10421 * "diff_hlID()" function
10422 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010424f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010425{
10426#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010428 static linenr_T prev_lnum = 0;
10429 static int changedtick = 0;
10430 static int fnum = 0;
10431 static int change_start = 0;
10432 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010433 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010434 int filler_lines;
10435 int col;
10436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 if (lnum < 0) /* ignore type error in {lnum} arg */
10438 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010439 if (lnum != prev_lnum
10440 || changedtick != curbuf->b_changedtick
10441 || fnum != curbuf->b_fnum)
10442 {
10443 /* New line, buffer, change: need to get the values. */
10444 filler_lines = diff_check(curwin, lnum);
10445 if (filler_lines < 0)
10446 {
10447 if (filler_lines == -1)
10448 {
10449 change_start = MAXCOL;
10450 change_end = -1;
10451 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10452 hlID = HLF_ADD; /* added line */
10453 else
10454 hlID = HLF_CHD; /* changed line */
10455 }
10456 else
10457 hlID = HLF_ADD; /* added line */
10458 }
10459 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010460 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010461 prev_lnum = lnum;
10462 changedtick = curbuf->b_changedtick;
10463 fnum = curbuf->b_fnum;
10464 }
10465
10466 if (hlID == HLF_CHD || hlID == HLF_TXD)
10467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010469 if (col >= change_start && col <= change_end)
10470 hlID = HLF_TXD; /* changed text */
10471 else
10472 hlID = HLF_CHD; /* changed line */
10473 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010474 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010475#endif
10476}
10477
10478/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010479 * "empty({expr})" function
10480 */
10481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010482f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010483{
10484 int n;
10485
10486 switch (argvars[0].v_type)
10487 {
10488 case VAR_STRING:
10489 case VAR_FUNC:
10490 n = argvars[0].vval.v_string == NULL
10491 || *argvars[0].vval.v_string == NUL;
10492 break;
10493 case VAR_NUMBER:
10494 n = argvars[0].vval.v_number == 0;
10495 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010496#ifdef FEAT_FLOAT
10497 case VAR_FLOAT:
10498 n = argvars[0].vval.v_float == 0.0;
10499 break;
10500#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010501 case VAR_LIST:
10502 n = argvars[0].vval.v_list == NULL
10503 || argvars[0].vval.v_list->lv_first == NULL;
10504 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010505 case VAR_DICT:
10506 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010508 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010509 case VAR_SPECIAL:
10510 n = argvars[0].vval.v_number != VVAL_TRUE;
10511 break;
10512
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010513 default:
10514 EMSG2(_(e_intern2), "f_empty()");
10515 n = 0;
10516 }
10517
10518 rettv->vval.v_number = n;
10519}
10520
10521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 * "escape({string}, {chars})" function
10523 */
10524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010525f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526{
10527 char_u buf[NUMBUFLEN];
10528
Bram Moolenaar758711c2005-02-02 23:11:38 +000010529 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10530 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532}
10533
10534/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010535 * "eval()" function
10536 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010538f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010539{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010540 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 s = get_tv_string_chk(&argvars[0]);
10543 if (s != NULL)
10544 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010545
Bram Moolenaar615b9972015-01-14 17:15:05 +010010546 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10548 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010549 if (p != NULL && !aborting())
10550 EMSG2(_(e_invexpr2), p);
10551 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010555 else if (*s != NUL)
10556 EMSG(_(e_trailing));
10557}
10558
10559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 * "eventhandler()" function
10561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010563f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566}
10567
10568/*
10569 * "executable()" function
10570 */
10571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010572f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010574 char_u *name = get_tv_string(&argvars[0]);
10575
10576 /* Check in $PATH and also check directly if there is a directory name. */
10577 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10578 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010579}
10580
10581/*
10582 * "exepath()" function
10583 */
10584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010585f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010586{
10587 char_u *p = NULL;
10588
Bram Moolenaarb5971142015-03-21 17:32:19 +010010589 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010590 rettv->v_type = VAR_STRING;
10591 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592}
10593
10594/*
10595 * "exists()" function
10596 */
10597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010598f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599{
10600 char_u *p;
10601 char_u *name;
10602 int n = FALSE;
10603 int len = 0;
10604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 if (*p == '$') /* environment variable */
10607 {
10608 /* first try "normal" environment variables (fast) */
10609 if (mch_getenv(p + 1) != NULL)
10610 n = TRUE;
10611 else
10612 {
10613 /* try expanding things like $VIM and ${HOME} */
10614 p = expand_env_save(p);
10615 if (p != NULL && *p != '$')
10616 n = TRUE;
10617 vim_free(p);
10618 }
10619 }
10620 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010623 if (*skipwhite(p) != NUL)
10624 n = FALSE; /* trailing garbage */
10625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 else if (*p == '*') /* internal or user defined function */
10627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010628 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 }
10630 else if (*p == ':')
10631 {
10632 n = cmd_exists(p + 1);
10633 }
10634 else if (*p == '#')
10635 {
10636#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010637 if (p[1] == '#')
10638 n = autocmd_supported(p + 2);
10639 else
10640 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641#endif
10642 }
10643 else /* internal variable */
10644 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010645 char_u *tofree;
10646 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010648 /* get_name_len() takes care of expanding curly braces */
10649 name = p;
10650 len = get_name_len(&p, &tofree, TRUE, FALSE);
10651 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010653 if (tofree != NULL)
10654 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010655 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010656 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010658 /* handle d.key, l[idx], f(expr) */
10659 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10660 if (n)
10661 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
10663 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010664 if (*p != NUL)
10665 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010667 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 }
10669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671}
10672
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010673#ifdef FEAT_FLOAT
10674/*
10675 * "exp()" function
10676 */
10677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010679{
10680 float_T f;
10681
10682 rettv->v_type = VAR_FLOAT;
10683 if (get_float_arg(argvars, &f) == OK)
10684 rettv->vval.v_float = exp(f);
10685 else
10686 rettv->vval.v_float = 0.0;
10687}
10688#endif
10689
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690/*
10691 * "expand()" function
10692 */
10693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010694f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695{
10696 char_u *s;
10697 int len;
10698 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010699 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010702 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010705 if (argvars[1].v_type != VAR_UNKNOWN
10706 && argvars[2].v_type != VAR_UNKNOWN
10707 && get_tv_number_chk(&argvars[2], &error)
10708 && !error)
10709 {
10710 rettv->v_type = VAR_LIST;
10711 rettv->vval.v_list = NULL;
10712 }
10713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 if (*s == '%' || *s == '#' || *s == '<')
10716 {
10717 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010718 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010720 if (rettv->v_type == VAR_LIST)
10721 {
10722 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10723 list_append_string(rettv->vval.v_list, result, -1);
10724 else
10725 vim_free(result);
10726 }
10727 else
10728 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 }
10730 else
10731 {
10732 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010733 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 if (argvars[1].v_type != VAR_UNKNOWN
10735 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010736 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010737 if (!error)
10738 {
10739 ExpandInit(&xpc);
10740 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010741 if (p_wic)
10742 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010743 if (rettv->v_type == VAR_STRING)
10744 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10745 options, WILD_ALL);
10746 else if (rettv_list_alloc(rettv) != FAIL)
10747 {
10748 int i;
10749
10750 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10751 for (i = 0; i < xpc.xp_numfiles; i++)
10752 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10753 ExpandCleanup(&xpc);
10754 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 }
10756 else
10757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 }
10759}
10760
10761/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010762 * Go over all entries in "d2" and add them to "d1".
10763 * When "action" is "error" then a duplicate key is an error.
10764 * When "action" is "force" then a duplicate key is overwritten.
10765 * Otherwise duplicate keys are ignored ("action" is "keep").
10766 */
10767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010768dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010769{
10770 dictitem_T *di1;
10771 hashitem_T *hi2;
10772 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010773 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010774
10775 todo = (int)d2->dv_hashtab.ht_used;
10776 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10777 {
10778 if (!HASHITEM_EMPTY(hi2))
10779 {
10780 --todo;
10781 di1 = dict_find(d1, hi2->hi_key, -1);
10782 if (d1->dv_scope != 0)
10783 {
10784 /* Disallow replacing a builtin function in l: and g:.
10785 * Check the key to be valid when adding to any
10786 * scope. */
10787 if (d1->dv_scope == VAR_DEF_SCOPE
10788 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10789 && var_check_func_name(hi2->hi_key,
10790 di1 == NULL))
10791 break;
10792 if (!valid_varname(hi2->hi_key))
10793 break;
10794 }
10795 if (di1 == NULL)
10796 {
10797 di1 = dictitem_copy(HI2DI(hi2));
10798 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10799 dictitem_free(di1);
10800 }
10801 else if (*action == 'e')
10802 {
10803 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10804 break;
10805 }
10806 else if (*action == 'f' && HI2DI(hi2) != di1)
10807 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010808 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10809 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010810 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010811 clear_tv(&di1->di_tv);
10812 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10813 }
10814 }
10815 }
10816}
10817
10818/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010819 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010821 */
10822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010823f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010824{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010825 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010826
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010828 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 list_T *l1, *l2;
10830 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010833
Bram Moolenaare9a41262005-01-15 22:18:47 +000010834 l1 = argvars[0].vval.v_list;
10835 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010836 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010837 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 {
10839 if (argvars[2].v_type != VAR_UNKNOWN)
10840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010841 before = get_tv_number_chk(&argvars[2], &error);
10842 if (error)
10843 return; /* type error; errmsg already given */
10844
Bram Moolenaar758711c2005-02-02 23:11:38 +000010845 if (before == l1->lv_len)
10846 item = NULL;
10847 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010849 item = list_find(l1, before);
10850 if (item == NULL)
10851 {
10852 EMSGN(_(e_listidx), before);
10853 return;
10854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 }
10856 }
10857 else
10858 item = NULL;
10859 list_extend(l1, l2, item);
10860
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 copy_tv(&argvars[0], rettv);
10862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10865 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010866 dict_T *d1, *d2;
10867 char_u *action;
10868 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869
10870 d1 = argvars[0].vval.v_dict;
10871 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010872 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010873 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 {
10875 /* Check the third argument. */
10876 if (argvars[2].v_type != VAR_UNKNOWN)
10877 {
10878 static char *(av[]) = {"keep", "force", "error"};
10879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010880 action = get_tv_string_chk(&argvars[2]);
10881 if (action == NULL)
10882 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010883 for (i = 0; i < 3; ++i)
10884 if (STRCMP(action, av[i]) == 0)
10885 break;
10886 if (i == 3)
10887 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010888 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010889 return;
10890 }
10891 }
10892 else
10893 action = (char_u *)"force";
10894
Bram Moolenaara9922d62013-05-30 13:01:18 +020010895 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010896
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 copy_tv(&argvars[0], rettv);
10898 }
10899 }
10900 else
10901 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010902}
10903
10904/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010905 * "feedkeys()" function
10906 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010908f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010909{
10910 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010911 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010912 char_u *keys, *flags;
10913 char_u nbuf[NUMBUFLEN];
10914 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010915 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010916 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010917
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010918 /* This is not allowed in the sandbox. If the commands would still be
10919 * executed in the sandbox it would be OK, but it probably happens later,
10920 * when "sandbox" is no longer set. */
10921 if (check_secure())
10922 return;
10923
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010924 keys = get_tv_string(&argvars[0]);
10925 if (*keys != NUL)
10926 {
10927 if (argvars[1].v_type != VAR_UNKNOWN)
10928 {
10929 flags = get_tv_string_buf(&argvars[1], nbuf);
10930 for ( ; *flags != NUL; ++flags)
10931 {
10932 switch (*flags)
10933 {
10934 case 'n': remap = FALSE; break;
10935 case 'm': remap = TRUE; break;
10936 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010937 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010938 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010939 }
10940 }
10941 }
10942
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010943 /* Need to escape K_SPECIAL and CSI before putting the string in the
10944 * typeahead buffer. */
10945 keys_esc = vim_strsave_escape_csi(keys);
10946 if (keys_esc != NULL)
10947 {
10948 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010949 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010950 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010951 if (vgetc_busy)
10952 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010953 if (execute)
10954 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010955 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010956 }
10957}
10958
10959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 * "filereadable()" function
10961 */
10962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010963f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010965 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 char_u *p;
10967 int n;
10968
Bram Moolenaarc236c162008-07-13 17:41:49 +000010969#ifndef O_NONBLOCK
10970# define O_NONBLOCK 0
10971#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010973 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10974 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 {
10976 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010977 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 }
10979 else
10980 n = FALSE;
10981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983}
10984
10985/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010986 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 * rights to write into.
10988 */
10989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010990f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010992 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993}
10994
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010995 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010010996findfilendir(
10997 typval_T *argvars UNUSED,
10998 typval_T *rettv,
10999 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011000{
11001#ifdef FEAT_SEARCHPATH
11002 char_u *fname;
11003 char_u *fresult = NULL;
11004 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11005 char_u *p;
11006 char_u pathbuf[NUMBUFLEN];
11007 int count = 1;
11008 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011009 int error = FALSE;
11010#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011011
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011012 rettv->vval.v_string = NULL;
11013 rettv->v_type = VAR_STRING;
11014
11015#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011018 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11021 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011022 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 else
11024 {
11025 if (*p != NUL)
11026 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011029 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011030 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011031 }
11032
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011033 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11034 error = TRUE;
11035
11036 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 do
11039 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011040 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011041 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 fresult = find_file_in_path_option(first ? fname : NULL,
11043 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011044 0, first, path,
11045 find_what,
11046 curbuf->b_ffname,
11047 find_what == FINDFILE_DIR
11048 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011049 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011050
11051 if (fresult != NULL && rettv->v_type == VAR_LIST)
11052 list_append_string(rettv->vval.v_list, fresult, -1);
11053
11054 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011056
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011057 if (rettv->v_type == VAR_STRING)
11058 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011059#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011060}
11061
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011062static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11063static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011064
11065/*
11066 * Implementation of map() and filter().
11067 */
11068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011069filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011070{
11071 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011072 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011073 listitem_T *li, *nli;
11074 list_T *l = NULL;
11075 dictitem_T *di;
11076 hashtab_T *ht;
11077 hashitem_T *hi;
11078 dict_T *d = NULL;
11079 typval_T save_val;
11080 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011081 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011082 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011083 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011084 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011085 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011086 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011087 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011088
Bram Moolenaare9a41262005-01-15 22:18:47 +000011089 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011090 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011091 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011092 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 return;
11094 }
11095 else if (argvars[0].v_type == VAR_DICT)
11096 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011097 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011098 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 return;
11100 }
11101 else
11102 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011103 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011104 return;
11105 }
11106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 expr = get_tv_string_buf_chk(&argvars[1], buf);
11108 /* On type errors, the preceding call has already displayed an error
11109 * message. Avoid a misleading error message for an empty string that
11110 * was not passed as argument. */
11111 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 prepare_vimvar(VV_VAL, &save_val);
11114 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011115
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011116 /* We reset "did_emsg" to be able to detect whether an error
11117 * occurred during evaluation of the expression. */
11118 save_did_emsg = did_emsg;
11119 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011120
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011121 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 vimvars[VV_KEY].vv_type = VAR_STRING;
11125
11126 ht = &d->dv_hashtab;
11127 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011128 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 if (!HASHITEM_EMPTY(hi))
11132 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011133 int r;
11134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 --todo;
11136 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011137 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011138 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11139 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 break;
11141 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011142 r = filter_map_one(&di->di_tv, expr, map, &rem);
11143 clear_tv(&vimvars[VV_KEY].vv_tv);
11144 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 break;
11146 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011147 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011148 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11149 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011150 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011151 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011153 }
11154 }
11155 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 }
11157 else
11158 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011159 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11160
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011161 for (li = l->lv_first; li != NULL; li = nli)
11162 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011163 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011164 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011166 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011167 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011168 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011169 break;
11170 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011172 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011173 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011174 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011175
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011176 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011178
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011179 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181
11182 copy_tv(&argvars[0], rettv);
11183}
11184
11185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011186filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187{
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011189 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011190 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191
Bram Moolenaar33570922005-01-25 22:26:29 +000011192 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011193 s = expr;
11194 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011195 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011196 if (*s != NUL) /* check for trailing chars after expr */
11197 {
11198 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011199 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011200 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011201 }
11202 if (map)
11203 {
11204 /* map(): replace the list item value */
11205 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011206 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011207 *tv = rettv;
11208 }
11209 else
11210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 int error = FALSE;
11212
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011215 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 /* On type error, nothing has been removed; return FAIL to stop the
11217 * loop. The error message was given by get_tv_number_chk(). */
11218 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011219 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011221 retval = OK;
11222theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011224 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011225}
11226
11227/*
11228 * "filter()" function
11229 */
11230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011231f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011232{
11233 filter_map(argvars, rettv, FALSE);
11234}
11235
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011236/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237 * "finddir({fname}[, {path}[, {count}]])" function
11238 */
11239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011240f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011241{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011242 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243}
11244
11245/*
11246 * "findfile({fname}[, {path}[, {count}]])" function
11247 */
11248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011249f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011250{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011251 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011252}
11253
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011254#ifdef FEAT_FLOAT
11255/*
11256 * "float2nr({float})" function
11257 */
11258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011259f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011260{
11261 float_T f;
11262
11263 if (get_float_arg(argvars, &f) == OK)
11264 {
11265 if (f < -0x7fffffff)
11266 rettv->vval.v_number = -0x7fffffff;
11267 else if (f > 0x7fffffff)
11268 rettv->vval.v_number = 0x7fffffff;
11269 else
11270 rettv->vval.v_number = (varnumber_T)f;
11271 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011272}
11273
11274/*
11275 * "floor({float})" function
11276 */
11277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011278f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011279{
11280 float_T f;
11281
11282 rettv->v_type = VAR_FLOAT;
11283 if (get_float_arg(argvars, &f) == OK)
11284 rettv->vval.v_float = floor(f);
11285 else
11286 rettv->vval.v_float = 0.0;
11287}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011288
11289/*
11290 * "fmod()" function
11291 */
11292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011293f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011294{
11295 float_T fx, fy;
11296
11297 rettv->v_type = VAR_FLOAT;
11298 if (get_float_arg(argvars, &fx) == OK
11299 && get_float_arg(&argvars[1], &fy) == OK)
11300 rettv->vval.v_float = fmod(fx, fy);
11301 else
11302 rettv->vval.v_float = 0.0;
11303}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011304#endif
11305
Bram Moolenaar0d660222005-01-07 21:51:51 +000011306/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011307 * "fnameescape({string})" function
11308 */
11309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011310f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011311{
11312 rettv->vval.v_string = vim_strsave_fnameescape(
11313 get_tv_string(&argvars[0]), FALSE);
11314 rettv->v_type = VAR_STRING;
11315}
11316
11317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 * "fnamemodify({fname}, {mods})" function
11319 */
11320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011321f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
11323 char_u *fname;
11324 char_u *mods;
11325 int usedlen = 0;
11326 int len;
11327 char_u *fbuf = NULL;
11328 char_u buf[NUMBUFLEN];
11329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011330 fname = get_tv_string_chk(&argvars[0]);
11331 mods = get_tv_string_buf_chk(&argvars[1], buf);
11332 if (fname == NULL || mods == NULL)
11333 fname = NULL;
11334 else
11335 {
11336 len = (int)STRLEN(fname);
11337 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 vim_free(fbuf);
11346}
11347
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011348static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349
11350/*
11351 * "foldclosed()" function
11352 */
11353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011354foldclosed_both(
11355 typval_T *argvars UNUSED,
11356 typval_T *rettv,
11357 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358{
11359#ifdef FEAT_FOLDING
11360 linenr_T lnum;
11361 linenr_T first, last;
11362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11365 {
11366 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11367 {
11368 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 return;
11373 }
11374 }
11375#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377}
11378
11379/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011380 * "foldclosed()" function
11381 */
11382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011383f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011384{
11385 foldclosed_both(argvars, rettv, FALSE);
11386}
11387
11388/*
11389 * "foldclosedend()" function
11390 */
11391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011392f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011393{
11394 foldclosed_both(argvars, rettv, TRUE);
11395}
11396
11397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 * "foldlevel()" function
11399 */
11400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011401f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402{
11403#ifdef FEAT_FOLDING
11404 linenr_T lnum;
11405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410}
11411
11412/*
11413 * "foldtext()" function
11414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011416f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
11418#ifdef FEAT_FOLDING
11419 linenr_T lnum;
11420 char_u *s;
11421 char_u *r;
11422 int len;
11423 char *txt;
11424#endif
11425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->v_type = VAR_STRING;
11427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011429 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11430 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11431 <= curbuf->b_ml.ml_line_count
11432 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 {
11434 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011435 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11436 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 {
11438 if (!linewhite(lnum))
11439 break;
11440 ++lnum;
11441 }
11442
11443 /* Find interesting text in this line. */
11444 s = skipwhite(ml_get(lnum));
11445 /* skip C comment-start */
11446 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011449 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011450 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011451 {
11452 s = skipwhite(ml_get(lnum + 1));
11453 if (*s == '*')
11454 s = skipwhite(s + 1);
11455 }
11456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 txt = _("+-%s%3ld lines: ");
11458 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 + 20 /* for %3ld */
11461 + STRLEN(s))); /* concatenated */
11462 if (r != NULL)
11463 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011464 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11465 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11466 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 len = (int)STRLEN(r);
11468 STRCAT(r, s);
11469 /* remove 'foldmarker' and 'commentstring' */
11470 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 }
11473 }
11474#endif
11475}
11476
11477/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011478 * "foldtextresult(lnum)" function
11479 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011481f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011482{
11483#ifdef FEAT_FOLDING
11484 linenr_T lnum;
11485 char_u *text;
11486 char_u buf[51];
11487 foldinfo_T foldinfo;
11488 int fold_count;
11489#endif
11490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->v_type = VAR_STRING;
11492 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011493#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011495 /* treat illegal types and illegal string values for {lnum} the same */
11496 if (lnum < 0)
11497 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011498 fold_count = foldedCount(curwin, lnum, &foldinfo);
11499 if (fold_count > 0)
11500 {
11501 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11502 &foldinfo, buf);
11503 if (text == buf)
11504 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011506 }
11507#endif
11508}
11509
11510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 * "foreground()" function
11512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011514f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516#ifdef FEAT_GUI
11517 if (gui.in_use)
11518 gui_mch_set_foreground();
11519#else
11520# ifdef WIN32
11521 win32_set_foreground();
11522# endif
11523#endif
11524}
11525
11526/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011527 * "function()" function
11528 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011530f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011531{
11532 char_u *s;
11533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011535 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011536 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011537 /* Don't check an autoload name for existence here. */
11538 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011539 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011540 else
11541 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011542 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011543 {
11544 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011545 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011546
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011547 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11548 * also be called from another script. Using trans_function_name()
11549 * would also work, but some plugins depend on the name being
11550 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011551 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011552 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011553 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011554 if (rettv->vval.v_string != NULL)
11555 {
11556 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011557 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011558 }
11559 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011560 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011561 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011563 }
11564}
11565
11566/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011567 * "garbagecollect()" function
11568 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011570f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011571{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011572 /* This is postponed until we are back at the toplevel, because we may be
11573 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11574 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011575
11576 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11577 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011578}
11579
11580/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011581 * "get()" function
11582 */
11583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011584f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585{
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 listitem_T *li;
11587 list_T *l;
11588 dictitem_T *di;
11589 dict_T *d;
11590 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591
Bram Moolenaare9a41262005-01-15 22:18:47 +000011592 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011593 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011594 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011596 int error = FALSE;
11597
11598 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11599 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011601 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011602 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011603 else if (argvars[0].v_type == VAR_DICT)
11604 {
11605 if ((d = argvars[0].vval.v_dict) != NULL)
11606 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011607 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011608 if (di != NULL)
11609 tv = &di->di_tv;
11610 }
11611 }
11612 else
11613 EMSG2(_(e_listdictarg), "get()");
11614
11615 if (tv == NULL)
11616 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011617 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011618 copy_tv(&argvars[2], rettv);
11619 }
11620 else
11621 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011622}
11623
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011624static 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 +000011625
11626/*
11627 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011628 * Return a range (from start to end) of lines in rettv from the specified
11629 * buffer.
11630 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011631 */
11632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011633get_buffer_lines(
11634 buf_T *buf,
11635 linenr_T start,
11636 linenr_T end,
11637 int retlist,
11638 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011639{
11640 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011641
Bram Moolenaar959a1432013-12-14 12:17:38 +010011642 rettv->v_type = VAR_STRING;
11643 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011644 if (retlist && rettv_list_alloc(rettv) == FAIL)
11645 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011646
11647 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11648 return;
11649
11650 if (!retlist)
11651 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011652 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11653 p = ml_get_buf(buf, start, FALSE);
11654 else
11655 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011656 rettv->vval.v_string = vim_strsave(p);
11657 }
11658 else
11659 {
11660 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011661 return;
11662
11663 if (start < 1)
11664 start = 1;
11665 if (end > buf->b_ml.ml_line_count)
11666 end = buf->b_ml.ml_line_count;
11667 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011668 if (list_append_string(rettv->vval.v_list,
11669 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011670 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011671 }
11672}
11673
11674/*
11675 * "getbufline()" function
11676 */
11677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011678f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011679{
11680 linenr_T lnum;
11681 linenr_T end;
11682 buf_T *buf;
11683
11684 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11685 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011686 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011687 --emsg_off;
11688
Bram Moolenaar661b1822005-07-28 22:36:45 +000011689 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011690 if (argvars[2].v_type == VAR_UNKNOWN)
11691 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011692 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011693 end = get_tv_lnum_buf(&argvars[2], buf);
11694
Bram Moolenaar342337a2005-07-21 21:11:17 +000011695 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011696}
11697
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698/*
11699 * "getbufvar()" function
11700 */
11701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011702f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703{
11704 buf_T *buf;
11705 buf_T *save_curbuf;
11706 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011707 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011708 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11711 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011712 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011713 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011715 rettv->v_type = VAR_STRING;
11716 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011717
11718 if (buf != NULL && varname != NULL)
11719 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011720 /* set curbuf to be our buf, temporarily */
11721 save_curbuf = curbuf;
11722 curbuf = buf;
11723
Bram Moolenaar0d660222005-01-07 21:51:51 +000011724 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011725 {
11726 if (get_option_tv(&varname, rettv, TRUE) == OK)
11727 done = TRUE;
11728 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011729 else if (STRCMP(varname, "changedtick") == 0)
11730 {
11731 rettv->v_type = VAR_NUMBER;
11732 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011733 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011734 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011735 else
11736 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011737 /* Look up the variable. */
11738 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11739 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11740 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011742 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011743 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011744 done = TRUE;
11745 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011746 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011747
11748 /* restore previous notion of curbuf */
11749 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011750 }
11751
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011752 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11753 /* use the default value */
11754 copy_tv(&argvars[2], rettv);
11755
Bram Moolenaar0d660222005-01-07 21:51:51 +000011756 --emsg_off;
11757}
11758
11759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 * "getchar()" function
11761 */
11762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011763f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764{
11765 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011768 /* Position the cursor. Needed after a message that ends in a space. */
11769 windgoto(msg_row, msg_col);
11770
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 ++no_mapping;
11772 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011773 for (;;)
11774 {
11775 if (argvars[0].v_type == VAR_UNKNOWN)
11776 /* getchar(): blocking wait. */
11777 n = safe_vgetc();
11778 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11779 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011780 n = vpeekc_any();
11781 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011782 /* illegal argument or getchar(0) and no char avail: return zero */
11783 n = 0;
11784 else
11785 /* getchar(0) and char avail: return char */
11786 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011787
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011788 if (n == K_IGNORE)
11789 continue;
11790 break;
11791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 --no_mapping;
11793 --allow_keys;
11794
Bram Moolenaar219b8702006-11-01 14:32:36 +000011795 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11796 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11797 vimvars[VV_MOUSE_COL].vv_nr = 0;
11798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 if (IS_SPECIAL(n) || mod_mask != 0)
11801 {
11802 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11803 int i = 0;
11804
11805 /* Turn a special key into three bytes, plus modifier. */
11806 if (mod_mask != 0)
11807 {
11808 temp[i++] = K_SPECIAL;
11809 temp[i++] = KS_MODIFIER;
11810 temp[i++] = mod_mask;
11811 }
11812 if (IS_SPECIAL(n))
11813 {
11814 temp[i++] = K_SPECIAL;
11815 temp[i++] = K_SECOND(n);
11816 temp[i++] = K_THIRD(n);
11817 }
11818#ifdef FEAT_MBYTE
11819 else if (has_mbyte)
11820 i += (*mb_char2bytes)(n, temp + i);
11821#endif
11822 else
11823 temp[i++] = n;
11824 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825 rettv->v_type = VAR_STRING;
11826 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011827
11828#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011829 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011830 {
11831 int row = mouse_row;
11832 int col = mouse_col;
11833 win_T *win;
11834 linenr_T lnum;
11835# ifdef FEAT_WINDOWS
11836 win_T *wp;
11837# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011838 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011839
11840 if (row >= 0 && col >= 0)
11841 {
11842 /* Find the window at the mouse coordinates and compute the
11843 * text position. */
11844 win = mouse_find_win(&row, &col);
11845 (void)mouse_comp_pos(win, &row, &col, &lnum);
11846# ifdef FEAT_WINDOWS
11847 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011848 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011849# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011850 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011851 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11852 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11853 }
11854 }
11855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 }
11857}
11858
11859/*
11860 * "getcharmod()" function
11861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011863f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
11868/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011869 * "getcharsearch()" function
11870 */
11871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011872f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011873{
11874 if (rettv_dict_alloc(rettv) != FAIL)
11875 {
11876 dict_T *dict = rettv->vval.v_dict;
11877
11878 dict_add_nr_str(dict, "char", 0L, last_csearch());
11879 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11880 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11881 }
11882}
11883
11884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 * "getcmdline()" function
11886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011888f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->v_type = VAR_STRING;
11891 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892}
11893
11894/*
11895 * "getcmdpos()" function
11896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011898f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901}
11902
11903/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011904 * "getcmdtype()" function
11905 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011907f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011908{
11909 rettv->v_type = VAR_STRING;
11910 rettv->vval.v_string = alloc(2);
11911 if (rettv->vval.v_string != NULL)
11912 {
11913 rettv->vval.v_string[0] = get_cmdline_type();
11914 rettv->vval.v_string[1] = NUL;
11915 }
11916}
11917
11918/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011919 * "getcmdwintype()" function
11920 */
11921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011922f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011923{
11924 rettv->v_type = VAR_STRING;
11925 rettv->vval.v_string = NULL;
11926#ifdef FEAT_CMDWIN
11927 rettv->vval.v_string = alloc(2);
11928 if (rettv->vval.v_string != NULL)
11929 {
11930 rettv->vval.v_string[0] = cmdwin_type;
11931 rettv->vval.v_string[1] = NUL;
11932 }
11933#endif
11934}
11935
11936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 * "getcwd()" function
11938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011940f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011942 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011943 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011946 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011947
11948 wp = find_tabwin(&argvars[0], &argvars[1]);
11949 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011951 if (wp->w_localdir != NULL)
11952 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11953 else if(globaldir != NULL)
11954 rettv->vval.v_string = vim_strsave(globaldir);
11955 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011956 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011957 cwd = alloc(MAXPATHL);
11958 if (cwd != NULL)
11959 {
11960 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11961 rettv->vval.v_string = vim_strsave(cwd);
11962 vim_free(cwd);
11963 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011964 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011965#ifdef BACKSLASH_IN_FILENAME
11966 if (rettv->vval.v_string != NULL)
11967 slash_adjust(rettv->vval.v_string);
11968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 }
11970}
11971
11972/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011973 * "getfontname()" function
11974 */
11975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011976f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->v_type = VAR_STRING;
11979 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011980#ifdef FEAT_GUI
11981 if (gui.in_use)
11982 {
11983 GuiFont font;
11984 char_u *name = NULL;
11985
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011986 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011987 {
11988 /* Get the "Normal" font. Either the name saved by
11989 * hl_set_font_name() or from the font ID. */
11990 font = gui.norm_font;
11991 name = hl_get_font_name();
11992 }
11993 else
11994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011996 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11997 return;
11998 font = gui_mch_get_font(name, FALSE);
11999 if (font == NOFONT)
12000 return; /* Invalid font name, return empty string. */
12001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012003 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012004 gui_mch_free_font(font);
12005 }
12006#endif
12007}
12008
12009/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012010 * "getfperm({fname})" function
12011 */
12012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012013f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012014{
12015 char_u *fname;
12016 struct stat st;
12017 char_u *perm = NULL;
12018 char_u flags[] = "rwx";
12019 int i;
12020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012024 if (mch_stat((char *)fname, &st) >= 0)
12025 {
12026 perm = vim_strsave((char_u *)"---------");
12027 if (perm != NULL)
12028 {
12029 for (i = 0; i < 9; i++)
12030 {
12031 if (st.st_mode & (1 << (8 - i)))
12032 perm[i] = flags[i % 3];
12033 }
12034 }
12035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012037}
12038
12039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 * "getfsize({fname})" function
12041 */
12042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012043f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044{
12045 char_u *fname;
12046 struct stat st;
12047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
12052 if (mch_stat((char *)fname, &st) >= 0)
12053 {
12054 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012059
12060 /* non-perfect check for overflow */
12061 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12062 rettv->vval.v_number = -2;
12063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 }
12065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067}
12068
12069/*
12070 * "getftime({fname})" function
12071 */
12072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012073f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074{
12075 char_u *fname;
12076 struct stat st;
12077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079
12080 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084}
12085
12086/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012087 * "getftype({fname})" function
12088 */
12089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012090f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012091{
12092 char_u *fname;
12093 struct stat st;
12094 char_u *type = NULL;
12095 char *t;
12096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012100 if (mch_lstat((char *)fname, &st) >= 0)
12101 {
12102#ifdef S_ISREG
12103 if (S_ISREG(st.st_mode))
12104 t = "file";
12105 else if (S_ISDIR(st.st_mode))
12106 t = "dir";
12107# ifdef S_ISLNK
12108 else if (S_ISLNK(st.st_mode))
12109 t = "link";
12110# endif
12111# ifdef S_ISBLK
12112 else if (S_ISBLK(st.st_mode))
12113 t = "bdev";
12114# endif
12115# ifdef S_ISCHR
12116 else if (S_ISCHR(st.st_mode))
12117 t = "cdev";
12118# endif
12119# ifdef S_ISFIFO
12120 else if (S_ISFIFO(st.st_mode))
12121 t = "fifo";
12122# endif
12123# ifdef S_ISSOCK
12124 else if (S_ISSOCK(st.st_mode))
12125 t = "fifo";
12126# endif
12127 else
12128 t = "other";
12129#else
12130# ifdef S_IFMT
12131 switch (st.st_mode & S_IFMT)
12132 {
12133 case S_IFREG: t = "file"; break;
12134 case S_IFDIR: t = "dir"; break;
12135# ifdef S_IFLNK
12136 case S_IFLNK: t = "link"; break;
12137# endif
12138# ifdef S_IFBLK
12139 case S_IFBLK: t = "bdev"; break;
12140# endif
12141# ifdef S_IFCHR
12142 case S_IFCHR: t = "cdev"; break;
12143# endif
12144# ifdef S_IFIFO
12145 case S_IFIFO: t = "fifo"; break;
12146# endif
12147# ifdef S_IFSOCK
12148 case S_IFSOCK: t = "socket"; break;
12149# endif
12150 default: t = "other";
12151 }
12152# else
12153 if (mch_isdir(fname))
12154 t = "dir";
12155 else
12156 t = "file";
12157# endif
12158#endif
12159 type = vim_strsave((char_u *)t);
12160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012162}
12163
12164/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012165 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012166 */
12167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012168f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012169{
12170 linenr_T lnum;
12171 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012172 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012173
12174 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012175 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012176 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012177 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012178 retlist = FALSE;
12179 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012180 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012181 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012183 retlist = TRUE;
12184 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012185
Bram Moolenaar342337a2005-07-21 21:11:17 +000012186 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012187}
12188
12189/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012190 * "getmatches()" function
12191 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012193f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012194{
12195#ifdef FEAT_SEARCH_EXTRA
12196 dict_T *dict;
12197 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012198 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012199
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012200 if (rettv_list_alloc(rettv) == OK)
12201 {
12202 while (cur != NULL)
12203 {
12204 dict = dict_alloc();
12205 if (dict == NULL)
12206 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012207 if (cur->match.regprog == NULL)
12208 {
12209 /* match added with matchaddpos() */
12210 for (i = 0; i < MAXPOSMATCH; ++i)
12211 {
12212 llpos_T *llpos;
12213 char buf[6];
12214 list_T *l;
12215
12216 llpos = &cur->pos.pos[i];
12217 if (llpos->lnum == 0)
12218 break;
12219 l = list_alloc();
12220 if (l == NULL)
12221 break;
12222 list_append_number(l, (varnumber_T)llpos->lnum);
12223 if (llpos->col > 0)
12224 {
12225 list_append_number(l, (varnumber_T)llpos->col);
12226 list_append_number(l, (varnumber_T)llpos->len);
12227 }
12228 sprintf(buf, "pos%d", i + 1);
12229 dict_add_list(dict, buf, l);
12230 }
12231 }
12232 else
12233 {
12234 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12235 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012236 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012237 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12238 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012239# ifdef FEAT_CONCEAL
12240 if (cur->conceal_char)
12241 {
12242 char_u buf[MB_MAXBYTES + 1];
12243
12244 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12245 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12246 }
12247# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012248 list_append_dict(rettv->vval.v_list, dict);
12249 cur = cur->next;
12250 }
12251 }
12252#endif
12253}
12254
12255/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012256 * "getpid()" function
12257 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012259f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012260{
12261 rettv->vval.v_number = mch_get_pid();
12262}
12263
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012264static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012265
12266/*
12267 * "getcurpos()" function
12268 */
12269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012270f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012271{
12272 getpos_both(argvars, rettv, TRUE);
12273}
12274
Bram Moolenaar18081e32008-02-20 19:11:07 +000012275/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012276 * "getpos(string)" function
12277 */
12278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012279f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012280{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012281 getpos_both(argvars, rettv, FALSE);
12282}
12283
12284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012285getpos_both(
12286 typval_T *argvars,
12287 typval_T *rettv,
12288 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012289{
Bram Moolenaara5525202006-03-02 22:52:09 +000012290 pos_T *fp;
12291 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012292 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012293
12294 if (rettv_list_alloc(rettv) == OK)
12295 {
12296 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012297 if (getcurpos)
12298 fp = &curwin->w_cursor;
12299 else
12300 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012301 if (fnum != -1)
12302 list_append_number(l, (varnumber_T)fnum);
12303 else
12304 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012305 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12306 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012307 list_append_number(l, (fp != NULL)
12308 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012309 : (varnumber_T)0);
12310 list_append_number(l,
12311#ifdef FEAT_VIRTUALEDIT
12312 (fp != NULL) ? (varnumber_T)fp->coladd :
12313#endif
12314 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012315 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012316 list_append_number(l, curwin->w_curswant == MAXCOL ?
12317 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012318 }
12319 else
12320 rettv->vval.v_number = FALSE;
12321}
12322
12323/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012324 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012325 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012327f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012328{
12329#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012330 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012331#endif
12332
Bram Moolenaar2641f772005-03-25 21:58:17 +000012333#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012334 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012335 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012336 wp = NULL;
12337 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12338 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012339 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012340 if (wp == NULL)
12341 return;
12342 }
12343
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012344 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012345 }
12346#endif
12347}
12348
12349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 * "getreg()" function
12351 */
12352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012353f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354{
12355 char_u *strregname;
12356 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012357 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012358 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012359 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012361 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012363 strregname = get_tv_string_chk(&argvars[0]);
12364 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012365 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012367 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012368 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12369 return_list = get_tv_number_chk(&argvars[2], &error);
12370 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012373 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012374
12375 if (error)
12376 return;
12377
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 regname = (strregname == NULL ? '"' : *strregname);
12379 if (regname == 0)
12380 regname = '"';
12381
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012382 if (return_list)
12383 {
12384 rettv->v_type = VAR_LIST;
12385 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12386 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012387 if (rettv->vval.v_list != NULL)
12388 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012389 }
12390 else
12391 {
12392 rettv->v_type = VAR_STRING;
12393 rettv->vval.v_string = get_reg_contents(regname,
12394 arg2 ? GREG_EXPR_SRC : 0);
12395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396}
12397
12398/*
12399 * "getregtype()" function
12400 */
12401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012402f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403{
12404 char_u *strregname;
12405 int regname;
12406 char_u buf[NUMBUFLEN + 2];
12407 long reglen = 0;
12408
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012409 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 {
12411 strregname = get_tv_string_chk(&argvars[0]);
12412 if (strregname == NULL) /* type error; errmsg already given */
12413 {
12414 rettv->v_type = VAR_STRING;
12415 rettv->vval.v_string = NULL;
12416 return;
12417 }
12418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 else
12420 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012421 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422
12423 regname = (strregname == NULL ? '"' : *strregname);
12424 if (regname == 0)
12425 regname = '"';
12426
12427 buf[0] = NUL;
12428 buf[1] = NUL;
12429 switch (get_reg_type(regname, &reglen))
12430 {
12431 case MLINE: buf[0] = 'V'; break;
12432 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 case MBLOCK:
12434 buf[0] = Ctrl_V;
12435 sprintf((char *)buf + 1, "%ld", reglen + 1);
12436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012438 rettv->v_type = VAR_STRING;
12439 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440}
12441
12442/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012443 * "gettabvar()" function
12444 */
12445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012446f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012447{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012448 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012449 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012450 dictitem_T *v;
12451 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012452 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012453
12454 rettv->v_type = VAR_STRING;
12455 rettv->vval.v_string = NULL;
12456
12457 varname = get_tv_string_chk(&argvars[1]);
12458 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12459 if (tp != NULL && varname != NULL)
12460 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012461 /* Set tp to be our tabpage, temporarily. Also set the window to the
12462 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012463 if (switch_win(&oldcurwin, &oldtabpage,
12464 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012465 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012466 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012467 /* look up the variable */
12468 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12469 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12470 if (v != NULL)
12471 {
12472 copy_tv(&v->di_tv, rettv);
12473 done = TRUE;
12474 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012475 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012476
12477 /* restore previous notion of curwin */
12478 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012479 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012480
12481 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012482 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012483}
12484
12485/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012486 * "gettabwinvar()" function
12487 */
12488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012489f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012490{
12491 getwinvar(argvars, rettv, 1);
12492}
12493
12494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 * "getwinposx()" function
12496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012498f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501#ifdef FEAT_GUI
12502 if (gui.in_use)
12503 {
12504 int x, y;
12505
12506 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 }
12509#endif
12510}
12511
12512/*
12513 * "getwinposy()" function
12514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012516f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519#ifdef FEAT_GUI
12520 if (gui.in_use)
12521 {
12522 int x, y;
12523
12524 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 }
12527#endif
12528}
12529
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012530/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012531 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012532 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012533 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012534find_win_by_nr(
12535 typval_T *vp,
12536 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012537{
12538#ifdef FEAT_WINDOWS
12539 win_T *wp;
12540#endif
12541 int nr;
12542
12543 nr = get_tv_number_chk(vp, NULL);
12544
12545#ifdef FEAT_WINDOWS
12546 if (nr < 0)
12547 return NULL;
12548 if (nr == 0)
12549 return curwin;
12550
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012551 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12552 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012553 if (--nr <= 0)
12554 break;
12555 return wp;
12556#else
12557 if (nr == 0 || nr == 1)
12558 return curwin;
12559 return NULL;
12560#endif
12561}
12562
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012564 * Find window specified by "wvp" in tabpage "tvp".
12565 */
12566 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012567find_tabwin(
12568 typval_T *wvp, /* VAR_UNKNOWN for current window */
12569 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012570{
12571 win_T *wp = NULL;
12572 tabpage_T *tp = NULL;
12573 long n;
12574
12575 if (wvp->v_type != VAR_UNKNOWN)
12576 {
12577 if (tvp->v_type != VAR_UNKNOWN)
12578 {
12579 n = get_tv_number(tvp);
12580 if (n >= 0)
12581 tp = find_tabpage(n);
12582 }
12583 else
12584 tp = curtab;
12585
12586 if (tp != NULL)
12587 wp = find_win_by_nr(wvp, tp);
12588 }
12589 else
12590 wp = curwin;
12591
12592 return wp;
12593}
12594
12595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 * "getwinvar()" function
12597 */
12598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012599f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012601 getwinvar(argvars, rettv, 0);
12602}
12603
12604/*
12605 * getwinvar() and gettabwinvar()
12606 */
12607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012608getwinvar(
12609 typval_T *argvars,
12610 typval_T *rettv,
12611 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012612{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012613 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012615 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012616 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012617 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012618#ifdef FEAT_WINDOWS
12619 win_T *oldcurwin;
12620 tabpage_T *oldtabpage;
12621 int need_switch_win;
12622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012624#ifdef FEAT_WINDOWS
12625 if (off == 1)
12626 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12627 else
12628 tp = curtab;
12629#endif
12630 win = find_win_by_nr(&argvars[off], tp);
12631 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012634 rettv->v_type = VAR_STRING;
12635 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636
12637 if (win != NULL && varname != NULL)
12638 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012639#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012640 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012641 * otherwise the window is not valid. Only do this when needed,
12642 * autocommands get blocked. */
12643 need_switch_win = !(tp == curtab && win == curwin);
12644 if (!need_switch_win
12645 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12646#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012647 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012648 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012649 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012650 if (get_option_tv(&varname, rettv, 1) == OK)
12651 done = TRUE;
12652 }
12653 else
12654 {
12655 /* Look up the variable. */
12656 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12657 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12658 varname, FALSE);
12659 if (v != NULL)
12660 {
12661 copy_tv(&v->di_tv, rettv);
12662 done = TRUE;
12663 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012666
Bram Moolenaarba117c22015-09-29 16:53:22 +020012667#ifdef FEAT_WINDOWS
12668 if (need_switch_win)
12669 /* restore previous notion of curwin */
12670 restore_win(oldcurwin, oldtabpage, TRUE);
12671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 }
12673
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012674 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12675 /* use the default return value */
12676 copy_tv(&argvars[off + 2], rettv);
12677
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 --emsg_off;
12679}
12680
12681/*
12682 * "glob()" function
12683 */
12684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012685f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012687 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012689 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012691 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012692 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012694 if (argvars[1].v_type != VAR_UNKNOWN)
12695 {
12696 if (get_tv_number_chk(&argvars[1], &error))
12697 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012698 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012699 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012700 if (get_tv_number_chk(&argvars[2], &error))
12701 {
12702 rettv->v_type = VAR_LIST;
12703 rettv->vval.v_list = NULL;
12704 }
12705 if (argvars[3].v_type != VAR_UNKNOWN
12706 && get_tv_number_chk(&argvars[3], &error))
12707 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012708 }
12709 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012710 if (!error)
12711 {
12712 ExpandInit(&xpc);
12713 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012714 if (p_wic)
12715 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012716 if (rettv->v_type == VAR_STRING)
12717 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012718 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012719 else if (rettv_list_alloc(rettv) != FAIL)
12720 {
12721 int i;
12722
12723 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12724 NULL, options, WILD_ALL_KEEP);
12725 for (i = 0; i < xpc.xp_numfiles; i++)
12726 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12727
12728 ExpandCleanup(&xpc);
12729 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012730 }
12731 else
12732 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733}
12734
12735/*
12736 * "globpath()" function
12737 */
12738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012739f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012741 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012744 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012745 garray_T ga;
12746 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012748 /* When the optional second argument is non-zero, don't remove matches
12749 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012751 if (argvars[2].v_type != VAR_UNKNOWN)
12752 {
12753 if (get_tv_number_chk(&argvars[2], &error))
12754 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012755 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012756 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012757 if (get_tv_number_chk(&argvars[3], &error))
12758 {
12759 rettv->v_type = VAR_LIST;
12760 rettv->vval.v_list = NULL;
12761 }
12762 if (argvars[4].v_type != VAR_UNKNOWN
12763 && get_tv_number_chk(&argvars[4], &error))
12764 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012765 }
12766 }
12767 if (file != NULL && !error)
12768 {
12769 ga_init2(&ga, (int)sizeof(char_u *), 10);
12770 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12771 if (rettv->v_type == VAR_STRING)
12772 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12773 else if (rettv_list_alloc(rettv) != FAIL)
12774 for (i = 0; i < ga.ga_len; ++i)
12775 list_append_string(rettv->vval.v_list,
12776 ((char_u **)(ga.ga_data))[i], -1);
12777 ga_clear_strings(&ga);
12778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012779 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012780 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781}
12782
12783/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012784 * "glob2regpat()" function
12785 */
12786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012787f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012788{
12789 char_u *pat = get_tv_string_chk(&argvars[0]);
12790
12791 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012792 rettv->vval.v_string = (pat == NULL)
12793 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012794}
12795
12796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 * "has()" function
12798 */
12799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012800f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801{
12802 int i;
12803 char_u *name;
12804 int n = FALSE;
12805 static char *(has_list[]) =
12806 {
12807#ifdef AMIGA
12808 "amiga",
12809# ifdef FEAT_ARP
12810 "arp",
12811# endif
12812#endif
12813#ifdef __BEOS__
12814 "beos",
12815#endif
12816#ifdef MSDOS
12817# ifdef DJGPP
12818 "dos32",
12819# else
12820 "dos16",
12821# endif
12822#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012823#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 "mac",
12825#endif
12826#if defined(MACOS_X_UNIX)
12827 "macunix",
12828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829#ifdef __QNX__
12830 "qnx",
12831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832#ifdef UNIX
12833 "unix",
12834#endif
12835#ifdef VMS
12836 "vms",
12837#endif
12838#ifdef WIN16
12839 "win16",
12840#endif
12841#ifdef WIN32
12842 "win32",
12843#endif
12844#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12845 "win32unix",
12846#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012847#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 "win64",
12849#endif
12850#ifdef EBCDIC
12851 "ebcdic",
12852#endif
12853#ifndef CASE_INSENSITIVE_FILENAME
12854 "fname_case",
12855#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012856#ifdef HAVE_ACL
12857 "acl",
12858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859#ifdef FEAT_ARABIC
12860 "arabic",
12861#endif
12862#ifdef FEAT_AUTOCMD
12863 "autocmd",
12864#endif
12865#ifdef FEAT_BEVAL
12866 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012867# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12868 "balloon_multiline",
12869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870#endif
12871#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12872 "builtin_terms",
12873# ifdef ALL_BUILTIN_TCAPS
12874 "all_builtin_terms",
12875# endif
12876#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012877#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12878 || defined(FEAT_GUI_W32) \
12879 || defined(FEAT_GUI_MOTIF))
12880 "browsefilter",
12881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882#ifdef FEAT_BYTEOFF
12883 "byte_offset",
12884#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012885#ifdef FEAT_CHANNEL
12886 "channel",
12887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888#ifdef FEAT_CINDENT
12889 "cindent",
12890#endif
12891#ifdef FEAT_CLIENTSERVER
12892 "clientserver",
12893#endif
12894#ifdef FEAT_CLIPBOARD
12895 "clipboard",
12896#endif
12897#ifdef FEAT_CMDL_COMPL
12898 "cmdline_compl",
12899#endif
12900#ifdef FEAT_CMDHIST
12901 "cmdline_hist",
12902#endif
12903#ifdef FEAT_COMMENTS
12904 "comments",
12905#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012906#ifdef FEAT_CONCEAL
12907 "conceal",
12908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909#ifdef FEAT_CRYPT
12910 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012911 "crypt-blowfish",
12912 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913#endif
12914#ifdef FEAT_CSCOPE
12915 "cscope",
12916#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012917#ifdef FEAT_CURSORBIND
12918 "cursorbind",
12919#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012920#ifdef CURSOR_SHAPE
12921 "cursorshape",
12922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923#ifdef DEBUG
12924 "debug",
12925#endif
12926#ifdef FEAT_CON_DIALOG
12927 "dialog_con",
12928#endif
12929#ifdef FEAT_GUI_DIALOG
12930 "dialog_gui",
12931#endif
12932#ifdef FEAT_DIFF
12933 "diff",
12934#endif
12935#ifdef FEAT_DIGRAPHS
12936 "digraphs",
12937#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012938#ifdef FEAT_DIRECTX
12939 "directx",
12940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941#ifdef FEAT_DND
12942 "dnd",
12943#endif
12944#ifdef FEAT_EMACS_TAGS
12945 "emacs_tags",
12946#endif
12947 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012948 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949#ifdef FEAT_SEARCH_EXTRA
12950 "extra_search",
12951#endif
12952#ifdef FEAT_FKMAP
12953 "farsi",
12954#endif
12955#ifdef FEAT_SEARCHPATH
12956 "file_in_path",
12957#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012958#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012959 "filterpipe",
12960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961#ifdef FEAT_FIND_ID
12962 "find_in_path",
12963#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012964#ifdef FEAT_FLOAT
12965 "float",
12966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967#ifdef FEAT_FOLDING
12968 "folding",
12969#endif
12970#ifdef FEAT_FOOTER
12971 "footer",
12972#endif
12973#if !defined(USE_SYSTEM) && defined(UNIX)
12974 "fork",
12975#endif
12976#ifdef FEAT_GETTEXT
12977 "gettext",
12978#endif
12979#ifdef FEAT_GUI
12980 "gui",
12981#endif
12982#ifdef FEAT_GUI_ATHENA
12983# ifdef FEAT_GUI_NEXTAW
12984 "gui_neXtaw",
12985# else
12986 "gui_athena",
12987# endif
12988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989#ifdef FEAT_GUI_GTK
12990 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012993#ifdef FEAT_GUI_GNOME
12994 "gui_gnome",
12995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996#ifdef FEAT_GUI_MAC
12997 "gui_mac",
12998#endif
12999#ifdef FEAT_GUI_MOTIF
13000 "gui_motif",
13001#endif
13002#ifdef FEAT_GUI_PHOTON
13003 "gui_photon",
13004#endif
13005#ifdef FEAT_GUI_W16
13006 "gui_win16",
13007#endif
13008#ifdef FEAT_GUI_W32
13009 "gui_win32",
13010#endif
13011#ifdef FEAT_HANGULIN
13012 "hangul_input",
13013#endif
13014#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13015 "iconv",
13016#endif
13017#ifdef FEAT_INS_EXPAND
13018 "insert_expand",
13019#endif
13020#ifdef FEAT_JUMPLIST
13021 "jumplist",
13022#endif
13023#ifdef FEAT_KEYMAP
13024 "keymap",
13025#endif
13026#ifdef FEAT_LANGMAP
13027 "langmap",
13028#endif
13029#ifdef FEAT_LIBCALL
13030 "libcall",
13031#endif
13032#ifdef FEAT_LINEBREAK
13033 "linebreak",
13034#endif
13035#ifdef FEAT_LISP
13036 "lispindent",
13037#endif
13038#ifdef FEAT_LISTCMDS
13039 "listcmds",
13040#endif
13041#ifdef FEAT_LOCALMAP
13042 "localmap",
13043#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013044#ifdef FEAT_LUA
13045# ifndef DYNAMIC_LUA
13046 "lua",
13047# endif
13048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049#ifdef FEAT_MENU
13050 "menu",
13051#endif
13052#ifdef FEAT_SESSION
13053 "mksession",
13054#endif
13055#ifdef FEAT_MODIFY_FNAME
13056 "modify_fname",
13057#endif
13058#ifdef FEAT_MOUSE
13059 "mouse",
13060#endif
13061#ifdef FEAT_MOUSESHAPE
13062 "mouseshape",
13063#endif
13064#if defined(UNIX) || defined(VMS)
13065# ifdef FEAT_MOUSE_DEC
13066 "mouse_dec",
13067# endif
13068# ifdef FEAT_MOUSE_GPM
13069 "mouse_gpm",
13070# endif
13071# ifdef FEAT_MOUSE_JSB
13072 "mouse_jsbterm",
13073# endif
13074# ifdef FEAT_MOUSE_NET
13075 "mouse_netterm",
13076# endif
13077# ifdef FEAT_MOUSE_PTERM
13078 "mouse_pterm",
13079# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013080# ifdef FEAT_MOUSE_SGR
13081 "mouse_sgr",
13082# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013083# ifdef FEAT_SYSMOUSE
13084 "mouse_sysmouse",
13085# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013086# ifdef FEAT_MOUSE_URXVT
13087 "mouse_urxvt",
13088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089# ifdef FEAT_MOUSE_XTERM
13090 "mouse_xterm",
13091# endif
13092#endif
13093#ifdef FEAT_MBYTE
13094 "multi_byte",
13095#endif
13096#ifdef FEAT_MBYTE_IME
13097 "multi_byte_ime",
13098#endif
13099#ifdef FEAT_MULTI_LANG
13100 "multi_lang",
13101#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013102#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013103#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013104 "mzscheme",
13105#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#ifdef FEAT_OLE
13108 "ole",
13109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110#ifdef FEAT_PATH_EXTRA
13111 "path_extra",
13112#endif
13113#ifdef FEAT_PERL
13114#ifndef DYNAMIC_PERL
13115 "perl",
13116#endif
13117#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013118#ifdef FEAT_PERSISTENT_UNDO
13119 "persistent_undo",
13120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121#ifdef FEAT_PYTHON
13122#ifndef DYNAMIC_PYTHON
13123 "python",
13124#endif
13125#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013126#ifdef FEAT_PYTHON3
13127#ifndef DYNAMIC_PYTHON3
13128 "python3",
13129#endif
13130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131#ifdef FEAT_POSTSCRIPT
13132 "postscript",
13133#endif
13134#ifdef FEAT_PRINTER
13135 "printer",
13136#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013137#ifdef FEAT_PROFILE
13138 "profile",
13139#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013140#ifdef FEAT_RELTIME
13141 "reltime",
13142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143#ifdef FEAT_QUICKFIX
13144 "quickfix",
13145#endif
13146#ifdef FEAT_RIGHTLEFT
13147 "rightleft",
13148#endif
13149#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13150 "ruby",
13151#endif
13152#ifdef FEAT_SCROLLBIND
13153 "scrollbind",
13154#endif
13155#ifdef FEAT_CMDL_INFO
13156 "showcmd",
13157 "cmdline_info",
13158#endif
13159#ifdef FEAT_SIGNS
13160 "signs",
13161#endif
13162#ifdef FEAT_SMARTINDENT
13163 "smartindent",
13164#endif
13165#ifdef FEAT_SNIFF
13166 "sniff",
13167#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013168#ifdef STARTUPTIME
13169 "startuptime",
13170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171#ifdef FEAT_STL_OPT
13172 "statusline",
13173#endif
13174#ifdef FEAT_SUN_WORKSHOP
13175 "sun_workshop",
13176#endif
13177#ifdef FEAT_NETBEANS_INTG
13178 "netbeans_intg",
13179#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013180#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013181 "spell",
13182#endif
13183#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 "syntax",
13185#endif
13186#if defined(USE_SYSTEM) || !defined(UNIX)
13187 "system",
13188#endif
13189#ifdef FEAT_TAG_BINS
13190 "tag_binary",
13191#endif
13192#ifdef FEAT_TAG_OLDSTATIC
13193 "tag_old_static",
13194#endif
13195#ifdef FEAT_TAG_ANYWHITE
13196 "tag_any_white",
13197#endif
13198#ifdef FEAT_TCL
13199# ifndef DYNAMIC_TCL
13200 "tcl",
13201# endif
13202#endif
13203#ifdef TERMINFO
13204 "terminfo",
13205#endif
13206#ifdef FEAT_TERMRESPONSE
13207 "termresponse",
13208#endif
13209#ifdef FEAT_TEXTOBJ
13210 "textobjects",
13211#endif
13212#ifdef HAVE_TGETENT
13213 "tgetent",
13214#endif
13215#ifdef FEAT_TITLE
13216 "title",
13217#endif
13218#ifdef FEAT_TOOLBAR
13219 "toolbar",
13220#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013221#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13222 "unnamedplus",
13223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224#ifdef FEAT_USR_CMDS
13225 "user-commands", /* was accidentally included in 5.4 */
13226 "user_commands",
13227#endif
13228#ifdef FEAT_VIMINFO
13229 "viminfo",
13230#endif
13231#ifdef FEAT_VERTSPLIT
13232 "vertsplit",
13233#endif
13234#ifdef FEAT_VIRTUALEDIT
13235 "virtualedit",
13236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238#ifdef FEAT_VISUALEXTRA
13239 "visualextra",
13240#endif
13241#ifdef FEAT_VREPLACE
13242 "vreplace",
13243#endif
13244#ifdef FEAT_WILDIGN
13245 "wildignore",
13246#endif
13247#ifdef FEAT_WILDMENU
13248 "wildmenu",
13249#endif
13250#ifdef FEAT_WINDOWS
13251 "windows",
13252#endif
13253#ifdef FEAT_WAK
13254 "winaltkeys",
13255#endif
13256#ifdef FEAT_WRITEBACKUP
13257 "writebackup",
13258#endif
13259#ifdef FEAT_XIM
13260 "xim",
13261#endif
13262#ifdef FEAT_XFONTSET
13263 "xfontset",
13264#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013265#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013266 "xpm",
13267 "xpm_w32", /* for backward compatibility */
13268#else
13269# if defined(HAVE_XPM)
13270 "xpm",
13271# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273#ifdef USE_XSMP
13274 "xsmp",
13275#endif
13276#ifdef USE_XSMP_INTERACT
13277 "xsmp_interact",
13278#endif
13279#ifdef FEAT_XCLIPBOARD
13280 "xterm_clipboard",
13281#endif
13282#ifdef FEAT_XTERM_SAVE
13283 "xterm_save",
13284#endif
13285#if defined(UNIX) && defined(FEAT_X11)
13286 "X11",
13287#endif
13288 NULL
13289 };
13290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 for (i = 0; has_list[i] != NULL; ++i)
13293 if (STRICMP(name, has_list[i]) == 0)
13294 {
13295 n = TRUE;
13296 break;
13297 }
13298
13299 if (n == FALSE)
13300 {
13301 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013302 {
13303 if (name[5] == '-'
13304 && STRLEN(name) > 11
13305 && vim_isdigit(name[6])
13306 && vim_isdigit(name[8])
13307 && vim_isdigit(name[10]))
13308 {
13309 int major = atoi((char *)name + 6);
13310 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013311
13312 /* Expect "patch-9.9.01234". */
13313 n = (major < VIM_VERSION_MAJOR
13314 || (major == VIM_VERSION_MAJOR
13315 && (minor < VIM_VERSION_MINOR
13316 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013317 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013318 }
13319 else
13320 n = has_patch(atoi((char *)name + 5));
13321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 else if (STRICMP(name, "vim_starting") == 0)
13323 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013324#ifdef FEAT_MBYTE
13325 else if (STRICMP(name, "multi_byte_encoding") == 0)
13326 n = has_mbyte;
13327#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013328#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13329 else if (STRICMP(name, "balloon_multiline") == 0)
13330 n = multiline_balloon_available();
13331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332#ifdef DYNAMIC_TCL
13333 else if (STRICMP(name, "tcl") == 0)
13334 n = tcl_enabled(FALSE);
13335#endif
13336#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13337 else if (STRICMP(name, "iconv") == 0)
13338 n = iconv_enabled(FALSE);
13339#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013340#ifdef DYNAMIC_LUA
13341 else if (STRICMP(name, "lua") == 0)
13342 n = lua_enabled(FALSE);
13343#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013344#ifdef DYNAMIC_MZSCHEME
13345 else if (STRICMP(name, "mzscheme") == 0)
13346 n = mzscheme_enabled(FALSE);
13347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348#ifdef DYNAMIC_RUBY
13349 else if (STRICMP(name, "ruby") == 0)
13350 n = ruby_enabled(FALSE);
13351#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013352#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353#ifdef DYNAMIC_PYTHON
13354 else if (STRICMP(name, "python") == 0)
13355 n = python_enabled(FALSE);
13356#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013357#endif
13358#ifdef FEAT_PYTHON3
13359#ifdef DYNAMIC_PYTHON3
13360 else if (STRICMP(name, "python3") == 0)
13361 n = python3_enabled(FALSE);
13362#endif
13363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364#ifdef DYNAMIC_PERL
13365 else if (STRICMP(name, "perl") == 0)
13366 n = perl_enabled(FALSE);
13367#endif
13368#ifdef FEAT_GUI
13369 else if (STRICMP(name, "gui_running") == 0)
13370 n = (gui.in_use || gui.starting);
13371# ifdef FEAT_GUI_W32
13372 else if (STRICMP(name, "gui_win32s") == 0)
13373 n = gui_is_win32s();
13374# endif
13375# ifdef FEAT_BROWSE
13376 else if (STRICMP(name, "browse") == 0)
13377 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13378# endif
13379#endif
13380#ifdef FEAT_SYN_HL
13381 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013382 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383#endif
13384#if defined(WIN3264)
13385 else if (STRICMP(name, "win95") == 0)
13386 n = mch_windows95();
13387#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013388#ifdef FEAT_NETBEANS_INTG
13389 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013390 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 }
13393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013394 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395}
13396
13397/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013398 * "has_key()" function
13399 */
13400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013401f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013402{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013403 if (argvars[0].v_type != VAR_DICT)
13404 {
13405 EMSG(_(e_dictreq));
13406 return;
13407 }
13408 if (argvars[0].vval.v_dict == NULL)
13409 return;
13410
13411 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013412 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013413}
13414
13415/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013416 * "haslocaldir()" function
13417 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013419f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013420{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013421 win_T *wp = NULL;
13422
13423 wp = find_tabwin(&argvars[0], &argvars[1]);
13424 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013425}
13426
13427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 * "hasmapto()" function
13429 */
13430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013431f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432{
13433 char_u *name;
13434 char_u *mode;
13435 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013436 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013439 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 mode = (char_u *)"nvo";
13441 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013443 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013444 if (argvars[2].v_type != VAR_UNKNOWN)
13445 abbr = get_tv_number(&argvars[2]);
13446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447
Bram Moolenaar2c932302006-03-18 21:42:09 +000013448 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013451 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452}
13453
13454/*
13455 * "histadd()" function
13456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013458f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459{
13460#ifdef FEAT_CMDHIST
13461 int histype;
13462 char_u *str;
13463 char_u buf[NUMBUFLEN];
13464#endif
13465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 if (check_restricted() || check_secure())
13468 return;
13469#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013470 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13471 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 if (histype >= 0)
13473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 if (*str != NUL)
13476 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013477 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 return;
13481 }
13482 }
13483#endif
13484}
13485
13486/*
13487 * "histdel()" function
13488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013490f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491{
13492#ifdef FEAT_CMDHIST
13493 int n;
13494 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013495 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13498 if (str == NULL)
13499 n = 0;
13500 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013503 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013506 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 else
13508 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510 get_tv_string_buf(&argvars[1], buf));
13511 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512#endif
13513}
13514
13515/*
13516 * "histget()" function
13517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013519f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520{
13521#ifdef FEAT_CMDHIST
13522 int type;
13523 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13527 if (str == NULL)
13528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 {
13531 type = get_histtype(str);
13532 if (argvars[1].v_type == VAR_UNKNOWN)
13533 idx = get_history_idx(type);
13534 else
13535 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13536 /* -1 on type error */
13537 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543}
13544
13545/*
13546 * "histnr()" function
13547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013549f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550{
13551 int i;
13552
13553#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013554 char_u *history = get_tv_string_chk(&argvars[0]);
13555
13556 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 if (i >= HIST_CMD && i < HIST_COUNT)
13558 i = get_history_idx(i);
13559 else
13560#endif
13561 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563}
13564
13565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 * "highlightID(name)" function
13567 */
13568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013569f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572}
13573
13574/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013575 * "highlight_exists()" function
13576 */
13577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013578f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579{
13580 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13581}
13582
13583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 * "hostname()" function
13585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013587f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588{
13589 char_u hostname[256];
13590
13591 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->v_type = VAR_STRING;
13593 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594}
13595
13596/*
13597 * iconv() function
13598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013600f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601{
13602#ifdef FEAT_MBYTE
13603 char_u buf1[NUMBUFLEN];
13604 char_u buf2[NUMBUFLEN];
13605 char_u *from, *to, *str;
13606 vimconv_T vimconv;
13607#endif
13608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->v_type = VAR_STRING;
13610 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611
13612#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 str = get_tv_string(&argvars[0]);
13614 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13615 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 vimconv.vc_type = CONV_NONE;
13617 convert_setup(&vimconv, from, to);
13618
13619 /* If the encodings are equal, no conversion needed. */
13620 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624
13625 convert_setup(&vimconv, NULL, NULL);
13626 vim_free(from);
13627 vim_free(to);
13628#endif
13629}
13630
13631/*
13632 * "indent()" function
13633 */
13634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013635f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636{
13637 linenr_T lnum;
13638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644}
13645
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013646/*
13647 * "index()" function
13648 */
13649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013650f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013651{
Bram Moolenaar33570922005-01-25 22:26:29 +000013652 list_T *l;
13653 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013654 long idx = 0;
13655 int ic = FALSE;
13656
13657 rettv->vval.v_number = -1;
13658 if (argvars[0].v_type != VAR_LIST)
13659 {
13660 EMSG(_(e_listreq));
13661 return;
13662 }
13663 l = argvars[0].vval.v_list;
13664 if (l != NULL)
13665 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013666 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013667 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 int error = FALSE;
13670
Bram Moolenaar758711c2005-02-02 23:11:38 +000013671 /* Start at specified item. Use the cached index that list_find()
13672 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013673 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013674 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013675 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 ic = get_tv_number_chk(&argvars[3], &error);
13677 if (error)
13678 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013679 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013680
Bram Moolenaar758711c2005-02-02 23:11:38 +000013681 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013682 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013683 {
13684 rettv->vval.v_number = idx;
13685 break;
13686 }
13687 }
13688}
13689
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690static int inputsecret_flag = 0;
13691
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013692static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013693
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013695 * This function is used by f_input() and f_inputdialog() functions. The third
13696 * argument to f_input() specifies the type of completion to use at the
13697 * prompt. The third argument to f_inputdialog() specifies the value to return
13698 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 */
13700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013701get_user_input(
13702 typval_T *argvars,
13703 typval_T *rettv,
13704 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 char_u *p = NULL;
13708 int c;
13709 char_u buf[NUMBUFLEN];
13710 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013711 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013712 int xp_type = EXPAND_NOTHING;
13713 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717
13718#ifdef NO_CONSOLE_INPUT
13719 /* While starting up, there is no place to enter text. */
13720 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722#endif
13723
13724 cmd_silent = FALSE; /* Want to see the prompt. */
13725 if (prompt != NULL)
13726 {
13727 /* Only the part of the message after the last NL is considered as
13728 * prompt for the command line */
13729 p = vim_strrchr(prompt, '\n');
13730 if (p == NULL)
13731 p = prompt;
13732 else
13733 {
13734 ++p;
13735 c = *p;
13736 *p = NUL;
13737 msg_start();
13738 msg_clr_eos();
13739 msg_puts_attr(prompt, echo_attr);
13740 msg_didout = FALSE;
13741 msg_starthere();
13742 *p = c;
13743 }
13744 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 if (argvars[1].v_type != VAR_UNKNOWN)
13747 {
13748 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13749 if (defstr != NULL)
13750 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013752 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013753 {
13754 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013755 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013756 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013757
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013758 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013759 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013760
Bram Moolenaar4463f292005-09-25 22:20:24 +000013761 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13762 if (xp_name == NULL)
13763 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013764
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013765 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013766
Bram Moolenaar4463f292005-09-25 22:20:24 +000013767 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13768 &xp_arg) == FAIL)
13769 return;
13770 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013771 }
13772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013774 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013775 int save_ex_normal_busy = ex_normal_busy;
13776 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013778 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13779 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013780 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013781 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013782 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013783 && argvars[1].v_type != VAR_UNKNOWN
13784 && argvars[2].v_type != VAR_UNKNOWN)
13785 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13786 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013787
13788 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013790 /* since the user typed this, no need to wait for return */
13791 need_wait_return = FALSE;
13792 msg_didout = FALSE;
13793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 cmd_silent = cmd_silent_save;
13795}
13796
13797/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013798 * "input()" function
13799 * Also handles inputsecret() when inputsecret is set.
13800 */
13801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013802f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013803{
13804 get_user_input(argvars, rettv, FALSE);
13805}
13806
13807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 * "inputdialog()" function
13809 */
13810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013811f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812{
13813#if defined(FEAT_GUI_TEXTDIALOG)
13814 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13815 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13816 {
13817 char_u *message;
13818 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 message = get_tv_string_chk(&argvars[0]);
13822 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013823 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013824 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 else
13826 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 if (message != NULL && defstr != NULL
13828 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013829 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 else
13832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013833 if (message != NULL && defstr != NULL
13834 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013835 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836 rettv->vval.v_string = vim_strsave(
13837 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 }
13843 else
13844#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013845 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846}
13847
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013848/*
13849 * "inputlist()" function
13850 */
13851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013852f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013853{
13854 listitem_T *li;
13855 int selected;
13856 int mouse_used;
13857
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013858#ifdef NO_CONSOLE_INPUT
13859 /* While starting up, there is no place to enter text. */
13860 if (no_console_input())
13861 return;
13862#endif
13863 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13864 {
13865 EMSG2(_(e_listarg), "inputlist()");
13866 return;
13867 }
13868
13869 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013870 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013871 lines_left = Rows; /* avoid more prompt */
13872 msg_scroll = TRUE;
13873 msg_clr_eos();
13874
13875 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13876 {
13877 msg_puts(get_tv_string(&li->li_tv));
13878 msg_putchar('\n');
13879 }
13880
13881 /* Ask for choice. */
13882 selected = prompt_for_number(&mouse_used);
13883 if (mouse_used)
13884 selected -= lines_left;
13885
13886 rettv->vval.v_number = selected;
13887}
13888
13889
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13891
13892/*
13893 * "inputrestore()" function
13894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013896f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897{
13898 if (ga_userinput.ga_len > 0)
13899 {
13900 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13902 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013903 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 }
13905 else if (p_verbose > 1)
13906 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013907 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 }
13910}
13911
13912/*
13913 * "inputsave()" function
13914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013916f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013918 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 if (ga_grow(&ga_userinput, 1) == OK)
13920 {
13921 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13922 + ga_userinput.ga_len);
13923 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013924 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 }
13926 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928}
13929
13930/*
13931 * "inputsecret()" function
13932 */
13933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013934f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935{
13936 ++cmdline_star;
13937 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 --cmdline_star;
13940 --inputsecret_flag;
13941}
13942
13943/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013944 * "insert()" function
13945 */
13946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013947f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013948{
13949 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013950 listitem_T *item;
13951 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013953
13954 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013955 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013956 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013957 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013958 {
13959 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 before = get_tv_number_chk(&argvars[2], &error);
13961 if (error)
13962 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013963
Bram Moolenaar758711c2005-02-02 23:11:38 +000013964 if (before == l->lv_len)
13965 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013966 else
13967 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013968 item = list_find(l, before);
13969 if (item == NULL)
13970 {
13971 EMSGN(_(e_listidx), before);
13972 l = NULL;
13973 }
13974 }
13975 if (l != NULL)
13976 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013977 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013978 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013979 }
13980 }
13981}
13982
13983/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013984 * "invert(expr)" function
13985 */
13986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013987f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013988{
13989 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13990}
13991
13992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 * "isdirectory()" function
13994 */
13995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013996f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999}
14000
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014001/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014002 * "islocked()" function
14003 */
14004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014005f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014006{
14007 lval_T lv;
14008 char_u *end;
14009 dictitem_T *di;
14010
14011 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014012 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14013 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014014 if (end != NULL && lv.ll_name != NULL)
14015 {
14016 if (*end != NUL)
14017 EMSG(_(e_trailing));
14018 else
14019 {
14020 if (lv.ll_tv == NULL)
14021 {
14022 if (check_changedtick(lv.ll_name))
14023 rettv->vval.v_number = 1; /* always locked */
14024 else
14025 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014026 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014027 if (di != NULL)
14028 {
14029 /* Consider a variable locked when:
14030 * 1. the variable itself is locked
14031 * 2. the value of the variable is locked.
14032 * 3. the List or Dict value is locked.
14033 */
14034 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14035 || tv_islocked(&di->di_tv));
14036 }
14037 }
14038 }
14039 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014040 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014041 else if (lv.ll_newkey != NULL)
14042 EMSG2(_(e_dictkey), lv.ll_newkey);
14043 else if (lv.ll_list != NULL)
14044 /* List item. */
14045 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14046 else
14047 /* Dictionary item. */
14048 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14049 }
14050 }
14051
14052 clear_lval(&lv);
14053}
14054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014055static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014056
14057/*
14058 * Turn a dict into a list:
14059 * "what" == 0: list of keys
14060 * "what" == 1: list of values
14061 * "what" == 2: list of items
14062 */
14063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014064dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014065{
Bram Moolenaar33570922005-01-25 22:26:29 +000014066 list_T *l2;
14067 dictitem_T *di;
14068 hashitem_T *hi;
14069 listitem_T *li;
14070 listitem_T *li2;
14071 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014072 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014073
Bram Moolenaar8c711452005-01-14 21:53:12 +000014074 if (argvars[0].v_type != VAR_DICT)
14075 {
14076 EMSG(_(e_dictreq));
14077 return;
14078 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014079 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014080 return;
14081
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014082 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014083 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014085 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014087 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014088 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014089 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014090 --todo;
14091 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014092
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014093 li = listitem_alloc();
14094 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014095 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014096 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014097
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014098 if (what == 0)
14099 {
14100 /* keys() */
14101 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014102 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014103 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14104 }
14105 else if (what == 1)
14106 {
14107 /* values() */
14108 copy_tv(&di->di_tv, &li->li_tv);
14109 }
14110 else
14111 {
14112 /* items() */
14113 l2 = list_alloc();
14114 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014115 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014116 li->li_tv.vval.v_list = l2;
14117 if (l2 == NULL)
14118 break;
14119 ++l2->lv_refcount;
14120
14121 li2 = listitem_alloc();
14122 if (li2 == NULL)
14123 break;
14124 list_append(l2, li2);
14125 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014126 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014127 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14128
14129 li2 = listitem_alloc();
14130 if (li2 == NULL)
14131 break;
14132 list_append(l2, li2);
14133 copy_tv(&di->di_tv, &li2->li_tv);
14134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014135 }
14136 }
14137}
14138
14139/*
14140 * "items(dict)" function
14141 */
14142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014143f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014144{
14145 dict_list(argvars, rettv, 2);
14146}
14147
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014149 * "join()" function
14150 */
14151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014152f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014153{
14154 garray_T ga;
14155 char_u *sep;
14156
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014157 if (argvars[0].v_type != VAR_LIST)
14158 {
14159 EMSG(_(e_listreq));
14160 return;
14161 }
14162 if (argvars[0].vval.v_list == NULL)
14163 return;
14164 if (argvars[1].v_type == VAR_UNKNOWN)
14165 sep = (char_u *)" ";
14166 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014168
14169 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170
14171 if (sep != NULL)
14172 {
14173 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014174 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 ga_append(&ga, NUL);
14176 rettv->vval.v_string = (char_u *)ga.ga_data;
14177 }
14178 else
14179 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014180}
14181
14182/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014183 * "jsondecode()" function
14184 */
14185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014186f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014187{
14188 js_read_T reader;
14189
14190 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014191 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014192 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +010014193 if (json_decode_all(&reader, rettv) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014194 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014195}
14196
14197/*
14198 * "jsonencode()" function
14199 */
14200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014201f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014202{
14203 rettv->v_type = VAR_STRING;
14204 rettv->vval.v_string = json_encode(&argvars[0]);
14205}
14206
14207/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014208 * "keys()" function
14209 */
14210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014211f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014212{
14213 dict_list(argvars, rettv, 0);
14214}
14215
14216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 * "last_buffer_nr()" function.
14218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014220f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221{
14222 int n = 0;
14223 buf_T *buf;
14224
14225 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14226 if (n < buf->b_fnum)
14227 n = buf->b_fnum;
14228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014230}
14231
14232/*
14233 * "len()" function
14234 */
14235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014236f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014237{
14238 switch (argvars[0].v_type)
14239 {
14240 case VAR_STRING:
14241 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_number = (varnumber_T)STRLEN(
14243 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014244 break;
14245 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014246 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014247 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014248 case VAR_DICT:
14249 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14250 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014251 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014252 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014253 break;
14254 }
14255}
14256
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014257static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014258
14259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014260libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014261{
14262#ifdef FEAT_LIBCALL
14263 char_u *string_in;
14264 char_u **string_result;
14265 int nr_result;
14266#endif
14267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014268 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014269 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014271
14272 if (check_restricted() || check_secure())
14273 return;
14274
14275#ifdef FEAT_LIBCALL
14276 /* The first two args must be strings, otherwise its meaningless */
14277 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014279 string_in = NULL;
14280 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014281 string_in = argvars[2].vval.v_string;
14282 if (type == VAR_NUMBER)
14283 string_result = NULL;
14284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014286 if (mch_libcall(argvars[0].vval.v_string,
14287 argvars[1].vval.v_string,
14288 string_in,
14289 argvars[2].vval.v_number,
14290 string_result,
14291 &nr_result) == OK
14292 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014293 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014294 }
14295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296}
14297
14298/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014299 * "libcall()" function
14300 */
14301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014302f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014303{
14304 libcall_common(argvars, rettv, VAR_STRING);
14305}
14306
14307/*
14308 * "libcallnr()" function
14309 */
14310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014311f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014312{
14313 libcall_common(argvars, rettv, VAR_NUMBER);
14314}
14315
14316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 * "line(string)" function
14318 */
14319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014320f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321{
14322 linenr_T lnum = 0;
14323 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014324 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014326 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 if (fp != NULL)
14328 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014329 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330}
14331
14332/*
14333 * "line2byte(lnum)" function
14334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014336f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337{
14338#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340#else
14341 linenr_T lnum;
14342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14348 if (rettv->vval.v_number >= 0)
14349 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350#endif
14351}
14352
14353/*
14354 * "lispindent(lnum)" function
14355 */
14356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014357f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358{
14359#ifdef FEAT_LISP
14360 pos_T pos;
14361 linenr_T lnum;
14362
14363 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14366 {
14367 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 curwin->w_cursor = pos;
14370 }
14371 else
14372#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374}
14375
14376/*
14377 * "localtime()" function
14378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014380f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383}
14384
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014385static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386
14387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014388get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389{
14390 char_u *keys;
14391 char_u *which;
14392 char_u buf[NUMBUFLEN];
14393 char_u *keys_buf = NULL;
14394 char_u *rhs;
14395 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014396 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014397 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014398 mapblock_T *mp;
14399 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400
14401 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402 rettv->v_type = VAR_STRING;
14403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 if (*keys == NUL)
14407 return;
14408
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014409 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014412 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014413 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014414 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014415 if (argvars[3].v_type != VAR_UNKNOWN)
14416 get_dict = get_tv_number(&argvars[3]);
14417 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 else
14420 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 if (which == NULL)
14422 return;
14423
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 mode = get_map_mode(&which, 0);
14425
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014426 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014427 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014429
14430 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014432 /* Return a string. */
14433 if (rhs != NULL)
14434 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435
Bram Moolenaarbd743252010-10-20 21:23:33 +020014436 }
14437 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14438 {
14439 /* Return a dictionary. */
14440 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14441 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14442 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443
Bram Moolenaarbd743252010-10-20 21:23:33 +020014444 dict_add_nr_str(dict, "lhs", 0L, lhs);
14445 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14446 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14447 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14448 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14449 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14450 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014451 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014452 dict_add_nr_str(dict, "mode", 0L, mapmode);
14453
14454 vim_free(lhs);
14455 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 }
14457}
14458
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014459#ifdef FEAT_FLOAT
14460/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014461 * "log()" function
14462 */
14463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014464f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014465{
14466 float_T f;
14467
14468 rettv->v_type = VAR_FLOAT;
14469 if (get_float_arg(argvars, &f) == OK)
14470 rettv->vval.v_float = log(f);
14471 else
14472 rettv->vval.v_float = 0.0;
14473}
14474
14475/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014476 * "log10()" function
14477 */
14478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014479f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014480{
14481 float_T f;
14482
14483 rettv->v_type = VAR_FLOAT;
14484 if (get_float_arg(argvars, &f) == OK)
14485 rettv->vval.v_float = log10(f);
14486 else
14487 rettv->vval.v_float = 0.0;
14488}
14489#endif
14490
Bram Moolenaar1dced572012-04-05 16:54:08 +020014491#ifdef FEAT_LUA
14492/*
14493 * "luaeval()" function
14494 */
14495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014496f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014497{
14498 char_u *str;
14499 char_u buf[NUMBUFLEN];
14500
14501 str = get_tv_string_buf(&argvars[0], buf);
14502 do_luaeval(str, argvars + 1, rettv);
14503}
14504#endif
14505
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014507 * "map()" function
14508 */
14509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014510f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014511{
14512 filter_map(argvars, rettv, TRUE);
14513}
14514
14515/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014516 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 */
14518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014519f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014521 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522}
14523
14524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014525 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 */
14527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014528f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014530 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531}
14532
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014533static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534
14535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014536find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014538 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014539 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014540 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 char_u *pat;
14542 regmatch_T regmatch;
14543 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014544 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 char_u *save_cpo;
14546 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014547 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014548 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014549 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014550 list_T *l = NULL;
14551 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014552 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014553 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554
14555 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14556 save_cpo = p_cpo;
14557 p_cpo = (char_u *)"";
14558
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014559 rettv->vval.v_number = -1;
14560 if (type == 3)
14561 {
14562 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014563 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014564 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014565 }
14566 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 rettv->v_type = VAR_STRING;
14569 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014572 if (argvars[0].v_type == VAR_LIST)
14573 {
14574 if ((l = argvars[0].vval.v_list) == NULL)
14575 goto theend;
14576 li = l->lv_first;
14577 }
14578 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014579 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014580 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014581 len = (long)STRLEN(str);
14582 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14585 if (pat == NULL)
14586 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014587
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014588 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014590 int error = FALSE;
14591
14592 start = get_tv_number_chk(&argvars[2], &error);
14593 if (error)
14594 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014595 if (l != NULL)
14596 {
14597 li = list_find(l, start);
14598 if (li == NULL)
14599 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014600 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014601 }
14602 else
14603 {
14604 if (start < 0)
14605 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014606 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014607 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014608 /* When "count" argument is there ignore matches before "start",
14609 * otherwise skip part of the string. Differs when pattern is "^"
14610 * or "\<". */
14611 if (argvars[3].v_type != VAR_UNKNOWN)
14612 startcol = start;
14613 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014614 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014615 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014616 len -= start;
14617 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014618 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014619
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014620 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 nth = get_tv_number_chk(&argvars[3], &error);
14622 if (error)
14623 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 }
14625
14626 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14627 if (regmatch.regprog != NULL)
14628 {
14629 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014630
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014631 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014632 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014633 if (l != NULL)
14634 {
14635 if (li == NULL)
14636 {
14637 match = FALSE;
14638 break;
14639 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014640 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014641 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014642 if (str == NULL)
14643 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014644 }
14645
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014646 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014647
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014648 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014649 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014650 if (l == NULL && !match)
14651 break;
14652
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014653 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014654 if (l != NULL)
14655 {
14656 li = li->li_next;
14657 ++idx;
14658 }
14659 else
14660 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014661#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014662 startcol = (colnr_T)(regmatch.startp[0]
14663 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014664#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014665 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014666#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014667 if (startcol > (colnr_T)len
14668 || str + startcol <= regmatch.startp[0])
14669 {
14670 match = FALSE;
14671 break;
14672 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014673 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014674 }
14675
14676 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014679 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014680 int i;
14681
14682 /* return list with matched string and submatches */
14683 for (i = 0; i < NSUBEXP; ++i)
14684 {
14685 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014686 {
14687 if (list_append_string(rettv->vval.v_list,
14688 (char_u *)"", 0) == FAIL)
14689 break;
14690 }
14691 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014692 regmatch.startp[i],
14693 (int)(regmatch.endp[i] - regmatch.startp[i]))
14694 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014695 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014696 }
14697 }
14698 else if (type == 2)
14699 {
14700 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014701 if (l != NULL)
14702 copy_tv(&li->li_tv, rettv);
14703 else
14704 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014706 }
14707 else if (l != NULL)
14708 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 else
14710 {
14711 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014712 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 (varnumber_T)(regmatch.startp[0] - str);
14714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014715 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014717 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 }
14719 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014720 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 }
14722
14723theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 p_cpo = save_cpo;
14726}
14727
14728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 * "match()" function
14730 */
14731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014732f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733{
14734 find_some_match(argvars, rettv, 1);
14735}
14736
14737/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014738 * "matchadd()" function
14739 */
14740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014741f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014742{
14743#ifdef FEAT_SEARCH_EXTRA
14744 char_u buf[NUMBUFLEN];
14745 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14746 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14747 int prio = 10; /* default priority */
14748 int id = -1;
14749 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014750 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014751
14752 rettv->vval.v_number = -1;
14753
14754 if (grp == NULL || pat == NULL)
14755 return;
14756 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014757 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014758 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014759 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014760 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014761 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014762 if (argvars[4].v_type != VAR_UNKNOWN)
14763 {
14764 if (argvars[4].v_type != VAR_DICT)
14765 {
14766 EMSG(_(e_dictreq));
14767 return;
14768 }
14769 if (dict_find(argvars[4].vval.v_dict,
14770 (char_u *)"conceal", -1) != NULL)
14771 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14772 (char_u *)"conceal", FALSE);
14773 }
14774 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014775 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014776 if (error == TRUE)
14777 return;
14778 if (id >= 1 && id <= 3)
14779 {
14780 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14781 return;
14782 }
14783
Bram Moolenaar6561d522015-07-21 15:48:27 +020014784 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14785 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014786#endif
14787}
14788
14789/*
14790 * "matchaddpos()" function
14791 */
14792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014793f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014794{
14795#ifdef FEAT_SEARCH_EXTRA
14796 char_u buf[NUMBUFLEN];
14797 char_u *group;
14798 int prio = 10;
14799 int id = -1;
14800 int error = FALSE;
14801 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014802 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014803
14804 rettv->vval.v_number = -1;
14805
14806 group = get_tv_string_buf_chk(&argvars[0], buf);
14807 if (group == NULL)
14808 return;
14809
14810 if (argvars[1].v_type != VAR_LIST)
14811 {
14812 EMSG2(_(e_listarg), "matchaddpos()");
14813 return;
14814 }
14815 l = argvars[1].vval.v_list;
14816 if (l == NULL)
14817 return;
14818
14819 if (argvars[2].v_type != VAR_UNKNOWN)
14820 {
14821 prio = get_tv_number_chk(&argvars[2], &error);
14822 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014823 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014824 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014825 if (argvars[4].v_type != VAR_UNKNOWN)
14826 {
14827 if (argvars[4].v_type != VAR_DICT)
14828 {
14829 EMSG(_(e_dictreq));
14830 return;
14831 }
14832 if (dict_find(argvars[4].vval.v_dict,
14833 (char_u *)"conceal", -1) != NULL)
14834 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14835 (char_u *)"conceal", FALSE);
14836 }
14837 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014838 }
14839 if (error == TRUE)
14840 return;
14841
14842 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14843 if (id == 1 || id == 2)
14844 {
14845 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14846 return;
14847 }
14848
Bram Moolenaar6561d522015-07-21 15:48:27 +020014849 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14850 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014851#endif
14852}
14853
14854/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014855 * "matcharg()" function
14856 */
14857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014858f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014859{
14860 if (rettv_list_alloc(rettv) == OK)
14861 {
14862#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014863 int id = get_tv_number(&argvars[0]);
14864 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014865
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014866 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014867 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014868 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14869 {
14870 list_append_string(rettv->vval.v_list,
14871 syn_id2name(m->hlg_id), -1);
14872 list_append_string(rettv->vval.v_list, m->pattern, -1);
14873 }
14874 else
14875 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014876 list_append_string(rettv->vval.v_list, NULL, -1);
14877 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014878 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014879 }
14880#endif
14881 }
14882}
14883
14884/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014885 * "matchdelete()" function
14886 */
14887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014888f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014889{
14890#ifdef FEAT_SEARCH_EXTRA
14891 rettv->vval.v_number = match_delete(curwin,
14892 (int)get_tv_number(&argvars[0]), TRUE);
14893#endif
14894}
14895
14896/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014897 * "matchend()" function
14898 */
14899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014900f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014901{
14902 find_some_match(argvars, rettv, 0);
14903}
14904
14905/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014906 * "matchlist()" function
14907 */
14908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014909f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014910{
14911 find_some_match(argvars, rettv, 3);
14912}
14913
14914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014915 * "matchstr()" function
14916 */
14917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014918f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014919{
14920 find_some_match(argvars, rettv, 2);
14921}
14922
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014923static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014924
14925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014926max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014927{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014928 long n = 0;
14929 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014930 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014931
14932 if (argvars[0].v_type == VAR_LIST)
14933 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014934 list_T *l;
14935 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014936
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014937 l = argvars[0].vval.v_list;
14938 if (l != NULL)
14939 {
14940 li = l->lv_first;
14941 if (li != NULL)
14942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014943 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014944 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014945 {
14946 li = li->li_next;
14947 if (li == NULL)
14948 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014950 if (domax ? i > n : i < n)
14951 n = i;
14952 }
14953 }
14954 }
14955 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014956 else if (argvars[0].v_type == VAR_DICT)
14957 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014958 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014959 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014960 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014961 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014962
14963 d = argvars[0].vval.v_dict;
14964 if (d != NULL)
14965 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014966 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014968 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014969 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014970 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014971 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014972 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014973 if (first)
14974 {
14975 n = i;
14976 first = FALSE;
14977 }
14978 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014979 n = i;
14980 }
14981 }
14982 }
14983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014984 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014985 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014986 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014987}
14988
14989/*
14990 * "max()" function
14991 */
14992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014993f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014994{
14995 max_min(argvars, rettv, TRUE);
14996}
14997
14998/*
14999 * "min()" function
15000 */
15001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015002f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015003{
15004 max_min(argvars, rettv, FALSE);
15005}
15006
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015007static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015008
15009/*
15010 * Create the directory in which "dir" is located, and higher levels when
15011 * needed.
15012 */
15013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015014mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015015{
15016 char_u *p;
15017 char_u *updir;
15018 int r = FAIL;
15019
15020 /* Get end of directory name in "dir".
15021 * We're done when it's "/" or "c:/". */
15022 p = gettail_sep(dir);
15023 if (p <= get_past_head(dir))
15024 return OK;
15025
15026 /* If the directory exists we're done. Otherwise: create it.*/
15027 updir = vim_strnsave(dir, (int)(p - dir));
15028 if (updir == NULL)
15029 return FAIL;
15030 if (mch_isdir(updir))
15031 r = OK;
15032 else if (mkdir_recurse(updir, prot) == OK)
15033 r = vim_mkdir_emsg(updir, prot);
15034 vim_free(updir);
15035 return r;
15036}
15037
15038#ifdef vim_mkdir
15039/*
15040 * "mkdir()" function
15041 */
15042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015043f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015044{
15045 char_u *dir;
15046 char_u buf[NUMBUFLEN];
15047 int prot = 0755;
15048
15049 rettv->vval.v_number = FAIL;
15050 if (check_restricted() || check_secure())
15051 return;
15052
15053 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015054 if (*dir == NUL)
15055 rettv->vval.v_number = FAIL;
15056 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015057 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015058 if (*gettail(dir) == NUL)
15059 /* remove trailing slashes */
15060 *gettail_sep(dir) = NUL;
15061
15062 if (argvars[1].v_type != VAR_UNKNOWN)
15063 {
15064 if (argvars[2].v_type != VAR_UNKNOWN)
15065 prot = get_tv_number_chk(&argvars[2], NULL);
15066 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15067 mkdir_recurse(dir, prot);
15068 }
15069 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015070 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015071}
15072#endif
15073
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 * "mode()" function
15076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015078f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015080 char_u buf[3];
15081
15082 buf[1] = NUL;
15083 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 if (VIsual_active)
15086 {
15087 if (VIsual_select)
15088 buf[0] = VIsual_mode + 's' - 'v';
15089 else
15090 buf[0] = VIsual_mode;
15091 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015092 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015093 || State == CONFIRM)
15094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015096 if (State == ASKMORE)
15097 buf[1] = 'm';
15098 else if (State == CONFIRM)
15099 buf[1] = '?';
15100 }
15101 else if (State == EXTERNCMD)
15102 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 else if (State & INSERT)
15104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015105#ifdef FEAT_VREPLACE
15106 if (State & VREPLACE_FLAG)
15107 {
15108 buf[0] = 'R';
15109 buf[1] = 'v';
15110 }
15111 else
15112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 if (State & REPLACE_FLAG)
15114 buf[0] = 'R';
15115 else
15116 buf[0] = 'i';
15117 }
15118 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015119 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015121 if (exmode_active)
15122 buf[1] = 'v';
15123 }
15124 else if (exmode_active)
15125 {
15126 buf[0] = 'c';
15127 buf[1] = 'e';
15128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015132 if (finish_op)
15133 buf[1] = 'o';
15134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015136 /* Clear out the minor mode when the argument is not a non-zero number or
15137 * non-empty string. */
15138 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015139 buf[1] = NUL;
15140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 rettv->vval.v_string = vim_strsave(buf);
15142 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143}
15144
Bram Moolenaar429fa852013-04-15 12:27:36 +020015145#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015146/*
15147 * "mzeval()" function
15148 */
15149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015150f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015151{
15152 char_u *str;
15153 char_u buf[NUMBUFLEN];
15154
15155 str = get_tv_string_buf(&argvars[0], buf);
15156 do_mzeval(str, rettv);
15157}
Bram Moolenaar75676462013-01-30 14:55:42 +010015158
15159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015160mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015161{
15162 typval_T argvars[3];
15163
15164 argvars[0].v_type = VAR_STRING;
15165 argvars[0].vval.v_string = name;
15166 copy_tv(args, &argvars[1]);
15167 argvars[2].v_type = VAR_UNKNOWN;
15168 f_call(argvars, rettv);
15169 clear_tv(&argvars[1]);
15170}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015171#endif
15172
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174 * "nextnonblank()" function
15175 */
15176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015177f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178{
15179 linenr_T lnum;
15180
15181 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015183 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184 {
15185 lnum = 0;
15186 break;
15187 }
15188 if (*skipwhite(ml_get(lnum)) != NUL)
15189 break;
15190 }
15191 rettv->vval.v_number = lnum;
15192}
15193
15194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 * "nr2char()" function
15196 */
15197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015198f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199{
15200 char_u buf[NUMBUFLEN];
15201
15202#ifdef FEAT_MBYTE
15203 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015204 {
15205 int utf8 = 0;
15206
15207 if (argvars[1].v_type != VAR_UNKNOWN)
15208 utf8 = get_tv_number_chk(&argvars[1], NULL);
15209 if (utf8)
15210 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15211 else
15212 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 else
15215#endif
15216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015217 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 buf[1] = NUL;
15219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015220 rettv->v_type = VAR_STRING;
15221 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015222}
15223
15224/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015225 * "or(expr, expr)" function
15226 */
15227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015228f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015229{
15230 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15231 | get_tv_number_chk(&argvars[1], NULL);
15232}
15233
15234/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015235 * "pathshorten()" function
15236 */
15237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015238f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015239{
15240 char_u *p;
15241
15242 rettv->v_type = VAR_STRING;
15243 p = get_tv_string_chk(&argvars[0]);
15244 if (p == NULL)
15245 rettv->vval.v_string = NULL;
15246 else
15247 {
15248 p = vim_strsave(p);
15249 rettv->vval.v_string = p;
15250 if (p != NULL)
15251 shorten_dir(p);
15252 }
15253}
15254
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015255#ifdef FEAT_PERL
15256/*
15257 * "perleval()" function
15258 */
15259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015260f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015261{
15262 char_u *str;
15263 char_u buf[NUMBUFLEN];
15264
15265 str = get_tv_string_buf(&argvars[0], buf);
15266 do_perleval(str, rettv);
15267}
15268#endif
15269
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015270#ifdef FEAT_FLOAT
15271/*
15272 * "pow()" function
15273 */
15274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015275f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015276{
15277 float_T fx, fy;
15278
15279 rettv->v_type = VAR_FLOAT;
15280 if (get_float_arg(argvars, &fx) == OK
15281 && get_float_arg(&argvars[1], &fy) == OK)
15282 rettv->vval.v_float = pow(fx, fy);
15283 else
15284 rettv->vval.v_float = 0.0;
15285}
15286#endif
15287
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015288/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015289 * "prevnonblank()" function
15290 */
15291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015292f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293{
15294 linenr_T lnum;
15295
15296 lnum = get_tv_lnum(argvars);
15297 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15298 lnum = 0;
15299 else
15300 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15301 --lnum;
15302 rettv->vval.v_number = lnum;
15303}
15304
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015305/* This dummy va_list is here because:
15306 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15307 * - locally in the function results in a "used before set" warning
15308 * - using va_start() to initialize it gives "function with fixed args" error */
15309static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015310
Bram Moolenaar8c711452005-01-14 21:53:12 +000015311/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015312 * "printf()" function
15313 */
15314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015315f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015316{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015317 char_u buf[NUMBUFLEN];
15318 int len;
15319 char_u *s;
15320 int saved_did_emsg = did_emsg;
15321 char *fmt;
15322
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015323 rettv->v_type = VAR_STRING;
15324 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015325
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015326 /* Get the required length, allocate the buffer and do it for real. */
15327 did_emsg = FALSE;
15328 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15329 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15330 if (!did_emsg)
15331 {
15332 s = alloc(len + 1);
15333 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015334 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015335 rettv->vval.v_string = s;
15336 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015337 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015338 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015339 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015340}
15341
15342/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015343 * "pumvisible()" function
15344 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015346f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015347{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015348#ifdef FEAT_INS_EXPAND
15349 if (pum_visible())
15350 rettv->vval.v_number = 1;
15351#endif
15352}
15353
Bram Moolenaardb913952012-06-29 12:54:53 +020015354#ifdef FEAT_PYTHON3
15355/*
15356 * "py3eval()" function
15357 */
15358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015359f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015360{
15361 char_u *str;
15362 char_u buf[NUMBUFLEN];
15363
15364 str = get_tv_string_buf(&argvars[0], buf);
15365 do_py3eval(str, rettv);
15366}
15367#endif
15368
15369#ifdef FEAT_PYTHON
15370/*
15371 * "pyeval()" function
15372 */
15373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015374f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015375{
15376 char_u *str;
15377 char_u buf[NUMBUFLEN];
15378
15379 str = get_tv_string_buf(&argvars[0], buf);
15380 do_pyeval(str, rettv);
15381}
15382#endif
15383
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015384/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015385 * "range()" function
15386 */
15387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015388f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015389{
15390 long start;
15391 long end;
15392 long stride = 1;
15393 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015394 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015396 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015397 if (argvars[1].v_type == VAR_UNKNOWN)
15398 {
15399 end = start - 1;
15400 start = 0;
15401 }
15402 else
15403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015404 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015406 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015407 }
15408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015409 if (error)
15410 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015411 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015412 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015413 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015414 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015415 else
15416 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015417 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015418 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015419 if (list_append_number(rettv->vval.v_list,
15420 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015421 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015422 }
15423}
15424
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425/*
15426 * "readfile()" function
15427 */
15428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015429f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015430{
15431 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015432 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433 char_u *fname;
15434 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015435 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15436 int io_size = sizeof(buf);
15437 int readlen; /* size of last fread() */
15438 char_u *prev = NULL; /* previously read bytes, if any */
15439 long prevlen = 0; /* length of data in prev */
15440 long prevsize = 0; /* size of prev buffer */
15441 long maxline = MAXLNUM;
15442 long cnt = 0;
15443 char_u *p; /* position in buf */
15444 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015445
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015446 if (argvars[1].v_type != VAR_UNKNOWN)
15447 {
15448 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15449 binary = TRUE;
15450 if (argvars[2].v_type != VAR_UNKNOWN)
15451 maxline = get_tv_number(&argvars[2]);
15452 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015454 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015455 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015456
15457 /* Always open the file in binary mode, library functions have a mind of
15458 * their own about CR-LF conversion. */
15459 fname = get_tv_string(&argvars[0]);
15460 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15461 {
15462 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15463 return;
15464 }
15465
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015466 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015467 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015468 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015469
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015470 /* This for loop processes what was read, but is also entered at end
15471 * of file so that either:
15472 * - an incomplete line gets written
15473 * - a "binary" file gets an empty line at the end if it ends in a
15474 * newline. */
15475 for (p = buf, start = buf;
15476 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15477 ++p)
15478 {
15479 if (*p == '\n' || readlen <= 0)
15480 {
15481 listitem_T *li;
15482 char_u *s = NULL;
15483 long_u len = p - start;
15484
15485 /* Finished a line. Remove CRs before NL. */
15486 if (readlen > 0 && !binary)
15487 {
15488 while (len > 0 && start[len - 1] == '\r')
15489 --len;
15490 /* removal may cross back to the "prev" string */
15491 if (len == 0)
15492 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15493 --prevlen;
15494 }
15495 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015496 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015497 else
15498 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015499 /* Change "prev" buffer to be the right size. This way
15500 * the bytes are only copied once, and very long lines are
15501 * allocated only once. */
15502 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015503 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015504 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015505 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015506 prev = NULL; /* the list will own the string */
15507 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015508 }
15509 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015510 if (s == NULL)
15511 {
15512 do_outofmem_msg((long_u) prevlen + len + 1);
15513 failed = TRUE;
15514 break;
15515 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015516
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015517 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015518 {
15519 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015520 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015521 break;
15522 }
15523 li->li_tv.v_type = VAR_STRING;
15524 li->li_tv.v_lock = 0;
15525 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015526 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015527
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015528 start = p + 1; /* step over newline */
15529 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015530 break;
15531 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015532 else if (*p == NUL)
15533 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015534#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015535 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15536 * when finding the BF and check the previous two bytes. */
15537 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015538 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015539 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15540 * + 1, these may be in the "prev" string. */
15541 char_u back1 = p >= buf + 1 ? p[-1]
15542 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15543 char_u back2 = p >= buf + 2 ? p[-2]
15544 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15545 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015547 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015549 char_u *dest = p - 2;
15550
15551 /* Usually a BOM is at the beginning of a file, and so at
15552 * the beginning of a line; then we can just step over it.
15553 */
15554 if (start == dest)
15555 start = p + 1;
15556 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015557 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015558 /* have to shuffle buf to close gap */
15559 int adjust_prevlen = 0;
15560
15561 if (dest < buf)
15562 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015563 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015564 dest = buf;
15565 }
15566 if (readlen > p - buf + 1)
15567 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15568 readlen -= 3 - adjust_prevlen;
15569 prevlen -= adjust_prevlen;
15570 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015571 }
15572 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015573 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015574#endif
15575 } /* for */
15576
15577 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15578 break;
15579 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015580 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015581 /* There's part of a line in buf, store it in "prev". */
15582 if (p - start + prevlen >= prevsize)
15583 {
15584 /* need bigger "prev" buffer */
15585 char_u *newprev;
15586
15587 /* A common use case is ordinary text files and "prev" gets a
15588 * fragment of a line, so the first allocation is made
15589 * small, to avoid repeatedly 'allocing' large and
15590 * 'reallocing' small. */
15591 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015592 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015593 else
15594 {
15595 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015596 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015597 prevsize = grow50pc > growmin ? grow50pc : growmin;
15598 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015599 newprev = prev == NULL ? alloc(prevsize)
15600 : vim_realloc(prev, prevsize);
15601 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015602 {
15603 do_outofmem_msg((long_u)prevsize);
15604 failed = TRUE;
15605 break;
15606 }
15607 prev = newprev;
15608 }
15609 /* Add the line part to end of "prev". */
15610 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015611 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015612 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015613 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015614
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015615 /*
15616 * For a negative line count use only the lines at the end of the file,
15617 * free the rest.
15618 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015619 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015620 while (cnt > -maxline)
15621 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015622 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015623 --cnt;
15624 }
15625
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015626 if (failed)
15627 {
15628 list_free(rettv->vval.v_list, TRUE);
15629 /* readfile doc says an empty list is returned on error */
15630 rettv->vval.v_list = list_alloc();
15631 }
15632
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015633 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015634 fclose(fd);
15635}
15636
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015637#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015638static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015639
15640/*
15641 * Convert a List to proftime_T.
15642 * Return FAIL when there is something wrong.
15643 */
15644 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015646{
15647 long n1, n2;
15648 int error = FALSE;
15649
15650 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15651 || arg->vval.v_list->lv_len != 2)
15652 return FAIL;
15653 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15654 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15655# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015656 tm->HighPart = n1;
15657 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015658# else
15659 tm->tv_sec = n1;
15660 tm->tv_usec = n2;
15661# endif
15662 return error ? FAIL : OK;
15663}
15664#endif /* FEAT_RELTIME */
15665
15666/*
15667 * "reltime()" function
15668 */
15669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015670f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015671{
15672#ifdef FEAT_RELTIME
15673 proftime_T res;
15674 proftime_T start;
15675
15676 if (argvars[0].v_type == VAR_UNKNOWN)
15677 {
15678 /* No arguments: get current time. */
15679 profile_start(&res);
15680 }
15681 else if (argvars[1].v_type == VAR_UNKNOWN)
15682 {
15683 if (list2proftime(&argvars[0], &res) == FAIL)
15684 return;
15685 profile_end(&res);
15686 }
15687 else
15688 {
15689 /* Two arguments: compute the difference. */
15690 if (list2proftime(&argvars[0], &start) == FAIL
15691 || list2proftime(&argvars[1], &res) == FAIL)
15692 return;
15693 profile_sub(&res, &start);
15694 }
15695
15696 if (rettv_list_alloc(rettv) == OK)
15697 {
15698 long n1, n2;
15699
15700# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015701 n1 = res.HighPart;
15702 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015703# else
15704 n1 = res.tv_sec;
15705 n2 = res.tv_usec;
15706# endif
15707 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15708 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15709 }
15710#endif
15711}
15712
15713/*
15714 * "reltimestr()" function
15715 */
15716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015717f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015718{
15719#ifdef FEAT_RELTIME
15720 proftime_T tm;
15721#endif
15722
15723 rettv->v_type = VAR_STRING;
15724 rettv->vval.v_string = NULL;
15725#ifdef FEAT_RELTIME
15726 if (list2proftime(&argvars[0], &tm) == OK)
15727 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15728#endif
15729}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015732static void make_connection(void);
15733static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734
15735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015736make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015737{
15738 if (X_DISPLAY == NULL
15739# ifdef FEAT_GUI
15740 && !gui.in_use
15741# endif
15742 )
15743 {
15744 x_force_connect = TRUE;
15745 setup_term_clip();
15746 x_force_connect = FALSE;
15747 }
15748}
15749
15750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015751check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752{
15753 make_connection();
15754 if (X_DISPLAY == NULL)
15755 {
15756 EMSG(_("E240: No connection to Vim server"));
15757 return FAIL;
15758 }
15759 return OK;
15760}
15761#endif
15762
15763#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015764static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015765
15766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015767remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768{
15769 char_u *server_name;
15770 char_u *keys;
15771 char_u *r = NULL;
15772 char_u buf[NUMBUFLEN];
15773# ifdef WIN32
15774 HWND w;
15775# else
15776 Window w;
15777# endif
15778
15779 if (check_restricted() || check_secure())
15780 return;
15781
15782# ifdef FEAT_X11
15783 if (check_connection() == FAIL)
15784 return;
15785# endif
15786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015787 server_name = get_tv_string_chk(&argvars[0]);
15788 if (server_name == NULL)
15789 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015790 keys = get_tv_string_buf(&argvars[1], buf);
15791# ifdef WIN32
15792 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15793# else
15794 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15795 < 0)
15796# endif
15797 {
15798 if (r != NULL)
15799 EMSG(r); /* sending worked but evaluation failed */
15800 else
15801 EMSG2(_("E241: Unable to send to %s"), server_name);
15802 return;
15803 }
15804
15805 rettv->vval.v_string = r;
15806
15807 if (argvars[2].v_type != VAR_UNKNOWN)
15808 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015809 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015810 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015813 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015814 v.di_tv.v_type = VAR_STRING;
15815 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015816 idvar = get_tv_string_chk(&argvars[2]);
15817 if (idvar != NULL)
15818 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015820 }
15821}
15822#endif
15823
15824/*
15825 * "remote_expr()" function
15826 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015828f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829{
15830 rettv->v_type = VAR_STRING;
15831 rettv->vval.v_string = NULL;
15832#ifdef FEAT_CLIENTSERVER
15833 remote_common(argvars, rettv, TRUE);
15834#endif
15835}
15836
15837/*
15838 * "remote_foreground()" function
15839 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015841f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015842{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015843#ifdef FEAT_CLIENTSERVER
15844# ifdef WIN32
15845 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015846 {
15847 char_u *server_name = get_tv_string_chk(&argvars[0]);
15848
15849 if (server_name != NULL)
15850 serverForeground(server_name);
15851 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015852# else
15853 /* Send a foreground() expression to the server. */
15854 argvars[1].v_type = VAR_STRING;
15855 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15856 argvars[2].v_type = VAR_UNKNOWN;
15857 remote_common(argvars, rettv, TRUE);
15858 vim_free(argvars[1].vval.v_string);
15859# endif
15860#endif
15861}
15862
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015864f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015865{
15866#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015867 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015868 char_u *s = NULL;
15869# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015870 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015871# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015873
15874 if (check_restricted() || check_secure())
15875 {
15876 rettv->vval.v_number = -1;
15877 return;
15878 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 serverid = get_tv_string_chk(&argvars[0]);
15880 if (serverid == NULL)
15881 {
15882 rettv->vval.v_number = -1;
15883 return; /* type error; errmsg already given */
15884 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015885# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015886 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015887 if (n == 0)
15888 rettv->vval.v_number = -1;
15889 else
15890 {
15891 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15892 rettv->vval.v_number = (s != NULL);
15893 }
15894# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895 if (check_connection() == FAIL)
15896 return;
15897
15898 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015899 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015900# endif
15901
15902 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015904 char_u *retvar;
15905
Bram Moolenaar33570922005-01-25 22:26:29 +000015906 v.di_tv.v_type = VAR_STRING;
15907 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015908 retvar = get_tv_string_chk(&argvars[1]);
15909 if (retvar != NULL)
15910 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015911 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912 }
15913#else
15914 rettv->vval.v_number = -1;
15915#endif
15916}
15917
Bram Moolenaar0d660222005-01-07 21:51:51 +000015918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015919f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015920{
15921 char_u *r = NULL;
15922
15923#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 char_u *serverid = get_tv_string_chk(&argvars[0]);
15925
15926 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015927 {
15928# ifdef WIN32
15929 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015930 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015932 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933 if (n != 0)
15934 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15935 if (r == NULL)
15936# else
15937 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015938 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939# endif
15940 EMSG(_("E277: Unable to read a server reply"));
15941 }
15942#endif
15943 rettv->v_type = VAR_STRING;
15944 rettv->vval.v_string = r;
15945}
15946
15947/*
15948 * "remote_send()" function
15949 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015951f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015952{
15953 rettv->v_type = VAR_STRING;
15954 rettv->vval.v_string = NULL;
15955#ifdef FEAT_CLIENTSERVER
15956 remote_common(argvars, rettv, FALSE);
15957#endif
15958}
15959
15960/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015961 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015962 */
15963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015964f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015965{
Bram Moolenaar33570922005-01-25 22:26:29 +000015966 list_T *l;
15967 listitem_T *item, *item2;
15968 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015969 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015970 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015971 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 dict_T *d;
15973 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015974 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015975
Bram Moolenaar8c711452005-01-14 21:53:12 +000015976 if (argvars[0].v_type == VAR_DICT)
15977 {
15978 if (argvars[2].v_type != VAR_UNKNOWN)
15979 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015980 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015981 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 key = get_tv_string_chk(&argvars[1]);
15984 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015986 di = dict_find(d, key, -1);
15987 if (di == NULL)
15988 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015989 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15990 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015991 {
15992 *rettv = di->di_tv;
15993 init_tv(&di->di_tv);
15994 dictitem_remove(d, di);
15995 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015997 }
15998 }
15999 else if (argvars[0].v_type != VAR_LIST)
16000 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016001 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016002 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016004 int error = FALSE;
16005
16006 idx = get_tv_number_chk(&argvars[1], &error);
16007 if (error)
16008 ; /* type error: do nothing, errmsg already given */
16009 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016010 EMSGN(_(e_listidx), idx);
16011 else
16012 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016013 if (argvars[2].v_type == VAR_UNKNOWN)
16014 {
16015 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016016 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016017 *rettv = item->li_tv;
16018 vim_free(item);
16019 }
16020 else
16021 {
16022 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016023 end = get_tv_number_chk(&argvars[2], &error);
16024 if (error)
16025 ; /* type error: do nothing */
16026 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016027 EMSGN(_(e_listidx), end);
16028 else
16029 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016030 int cnt = 0;
16031
16032 for (li = item; li != NULL; li = li->li_next)
16033 {
16034 ++cnt;
16035 if (li == item2)
16036 break;
16037 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016038 if (li == NULL) /* didn't find "item2" after "item" */
16039 EMSG(_(e_invrange));
16040 else
16041 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016042 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016043 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016044 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016045 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016046 l->lv_first = item;
16047 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016048 item->li_prev = NULL;
16049 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016050 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016051 }
16052 }
16053 }
16054 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016055 }
16056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057}
16058
16059/*
16060 * "rename({from}, {to})" function
16061 */
16062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016063f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064{
16065 char_u buf[NUMBUFLEN];
16066
16067 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016068 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16071 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072}
16073
16074/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016075 * "repeat()" function
16076 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016078f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016079{
16080 char_u *p;
16081 int n;
16082 int slen;
16083 int len;
16084 char_u *r;
16085 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016086
16087 n = get_tv_number(&argvars[1]);
16088 if (argvars[0].v_type == VAR_LIST)
16089 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016090 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016091 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016092 if (list_extend(rettv->vval.v_list,
16093 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016094 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016095 }
16096 else
16097 {
16098 p = get_tv_string(&argvars[0]);
16099 rettv->v_type = VAR_STRING;
16100 rettv->vval.v_string = NULL;
16101
16102 slen = (int)STRLEN(p);
16103 len = slen * n;
16104 if (len <= 0)
16105 return;
16106
16107 r = alloc(len + 1);
16108 if (r != NULL)
16109 {
16110 for (i = 0; i < n; i++)
16111 mch_memmove(r + i * slen, p, (size_t)slen);
16112 r[len] = NUL;
16113 }
16114
16115 rettv->vval.v_string = r;
16116 }
16117}
16118
16119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 * "resolve()" function
16121 */
16122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016123f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124{
16125 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016126#ifdef HAVE_READLINK
16127 char_u *buf = NULL;
16128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016130 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131#ifdef FEAT_SHORTCUT
16132 {
16133 char_u *v = NULL;
16134
16135 v = mch_resolve_shortcut(p);
16136 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016137 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 }
16141#else
16142# ifdef HAVE_READLINK
16143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 char_u *cpy;
16145 int len;
16146 char_u *remain = NULL;
16147 char_u *q;
16148 int is_relative_to_current = FALSE;
16149 int has_trailing_pathsep = FALSE;
16150 int limit = 100;
16151
16152 p = vim_strsave(p);
16153
16154 if (p[0] == '.' && (vim_ispathsep(p[1])
16155 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16156 is_relative_to_current = TRUE;
16157
16158 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016159 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016162 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164
16165 q = getnextcomp(p);
16166 if (*q != NUL)
16167 {
16168 /* Separate the first path component in "p", and keep the
16169 * remainder (beginning with the path separator). */
16170 remain = vim_strsave(q - 1);
16171 q[-1] = NUL;
16172 }
16173
Bram Moolenaard9462e32011-04-11 21:35:11 +020016174 buf = alloc(MAXPATHL + 1);
16175 if (buf == NULL)
16176 goto fail;
16177
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 for (;;)
16179 {
16180 for (;;)
16181 {
16182 len = readlink((char *)p, (char *)buf, MAXPATHL);
16183 if (len <= 0)
16184 break;
16185 buf[len] = NUL;
16186
16187 if (limit-- == 0)
16188 {
16189 vim_free(p);
16190 vim_free(remain);
16191 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016192 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 goto fail;
16194 }
16195
16196 /* Ensure that the result will have a trailing path separator
16197 * if the argument has one. */
16198 if (remain == NULL && has_trailing_pathsep)
16199 add_pathsep(buf);
16200
16201 /* Separate the first path component in the link value and
16202 * concatenate the remainders. */
16203 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16204 if (*q != NUL)
16205 {
16206 if (remain == NULL)
16207 remain = vim_strsave(q - 1);
16208 else
16209 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016210 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 if (cpy != NULL)
16212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 vim_free(remain);
16214 remain = cpy;
16215 }
16216 }
16217 q[-1] = NUL;
16218 }
16219
16220 q = gettail(p);
16221 if (q > p && *q == NUL)
16222 {
16223 /* Ignore trailing path separator. */
16224 q[-1] = NUL;
16225 q = gettail(p);
16226 }
16227 if (q > p && !mch_isFullName(buf))
16228 {
16229 /* symlink is relative to directory of argument */
16230 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16231 if (cpy != NULL)
16232 {
16233 STRCPY(cpy, p);
16234 STRCPY(gettail(cpy), buf);
16235 vim_free(p);
16236 p = cpy;
16237 }
16238 }
16239 else
16240 {
16241 vim_free(p);
16242 p = vim_strsave(buf);
16243 }
16244 }
16245
16246 if (remain == NULL)
16247 break;
16248
16249 /* Append the first path component of "remain" to "p". */
16250 q = getnextcomp(remain + 1);
16251 len = q - remain - (*q != NUL);
16252 cpy = vim_strnsave(p, STRLEN(p) + len);
16253 if (cpy != NULL)
16254 {
16255 STRNCAT(cpy, remain, len);
16256 vim_free(p);
16257 p = cpy;
16258 }
16259 /* Shorten "remain". */
16260 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016261 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 else
16263 {
16264 vim_free(remain);
16265 remain = NULL;
16266 }
16267 }
16268
16269 /* If the result is a relative path name, make it explicitly relative to
16270 * the current directory if and only if the argument had this form. */
16271 if (!vim_ispathsep(*p))
16272 {
16273 if (is_relative_to_current
16274 && *p != NUL
16275 && !(p[0] == '.'
16276 && (p[1] == NUL
16277 || vim_ispathsep(p[1])
16278 || (p[1] == '.'
16279 && (p[2] == NUL
16280 || vim_ispathsep(p[2]))))))
16281 {
16282 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016283 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 if (cpy != NULL)
16285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 vim_free(p);
16287 p = cpy;
16288 }
16289 }
16290 else if (!is_relative_to_current)
16291 {
16292 /* Strip leading "./". */
16293 q = p;
16294 while (q[0] == '.' && vim_ispathsep(q[1]))
16295 q += 2;
16296 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016297 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 }
16299 }
16300
16301 /* Ensure that the result will have no trailing path separator
16302 * if the argument had none. But keep "/" or "//". */
16303 if (!has_trailing_pathsep)
16304 {
16305 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016306 if (after_pathsep(p, q))
16307 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 }
16309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 }
16312# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016313 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314# endif
16315#endif
16316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016317 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318
16319#ifdef HAVE_READLINK
16320fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016321 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016323 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324}
16325
16326/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016327 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328 */
16329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016330f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331{
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 list_T *l;
16333 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334
Bram Moolenaar0d660222005-01-07 21:51:51 +000016335 if (argvars[0].v_type != VAR_LIST)
16336 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016337 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016338 && !tv_check_lock(l->lv_lock,
16339 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 {
16341 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016342 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016343 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 while (li != NULL)
16345 {
16346 ni = li->li_prev;
16347 list_append(l, li);
16348 li = ni;
16349 }
16350 rettv->vval.v_list = l;
16351 rettv->v_type = VAR_LIST;
16352 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016353 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355}
16356
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016357#define SP_NOMOVE 0x01 /* don't move cursor */
16358#define SP_REPEAT 0x02 /* repeat to find outer pair */
16359#define SP_RETCOUNT 0x04 /* return matchcount */
16360#define SP_SETPCMARK 0x08 /* set previous context mark */
16361#define SP_START 0x10 /* accept match at start position */
16362#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16363#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016364#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016365
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016366static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367
16368/*
16369 * Get flags for a search function.
16370 * Possibly sets "p_ws".
16371 * Returns BACKWARD, FORWARD or zero (for an error).
16372 */
16373 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016374get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375{
16376 int dir = FORWARD;
16377 char_u *flags;
16378 char_u nbuf[NUMBUFLEN];
16379 int mask;
16380
16381 if (varp->v_type != VAR_UNKNOWN)
16382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016383 flags = get_tv_string_buf_chk(varp, nbuf);
16384 if (flags == NULL)
16385 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 while (*flags != NUL)
16387 {
16388 switch (*flags)
16389 {
16390 case 'b': dir = BACKWARD; break;
16391 case 'w': p_ws = TRUE; break;
16392 case 'W': p_ws = FALSE; break;
16393 default: mask = 0;
16394 if (flagsp != NULL)
16395 switch (*flags)
16396 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016397 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016398 case 'e': mask = SP_END; break;
16399 case 'm': mask = SP_RETCOUNT; break;
16400 case 'n': mask = SP_NOMOVE; break;
16401 case 'p': mask = SP_SUBPAT; break;
16402 case 'r': mask = SP_REPEAT; break;
16403 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016404 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405 }
16406 if (mask == 0)
16407 {
16408 EMSG2(_(e_invarg2), flags);
16409 dir = 0;
16410 }
16411 else
16412 *flagsp |= mask;
16413 }
16414 if (dir == 0)
16415 break;
16416 ++flags;
16417 }
16418 }
16419 return dir;
16420}
16421
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016423 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016428 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 char_u *pat;
16430 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016431 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 int save_p_ws = p_ws;
16433 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016434 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016435 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016436 proftime_T tm;
16437#ifdef FEAT_RELTIME
16438 long time_limit = 0;
16439#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016440 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016441 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016443 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016444 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016445 if (dir == 0)
16446 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016447 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016448 if (flags & SP_START)
16449 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016450 if (flags & SP_END)
16451 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016452 if (flags & SP_COLUMN)
16453 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016454
Bram Moolenaar76929292008-01-06 19:07:36 +000016455 /* Optional arguments: line number to stop searching and timeout. */
16456 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016457 {
16458 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16459 if (lnum_stop < 0)
16460 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016461#ifdef FEAT_RELTIME
16462 if (argvars[3].v_type != VAR_UNKNOWN)
16463 {
16464 time_limit = get_tv_number_chk(&argvars[3], NULL);
16465 if (time_limit < 0)
16466 goto theend;
16467 }
16468#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016469 }
16470
Bram Moolenaar76929292008-01-06 19:07:36 +000016471#ifdef FEAT_RELTIME
16472 /* Set the time limit, if there is one. */
16473 profile_setlimit(time_limit, &tm);
16474#endif
16475
Bram Moolenaar231334e2005-07-25 20:46:57 +000016476 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016477 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016478 * Check to make sure only those flags are set.
16479 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16480 * flags cannot be set. Check for that condition also.
16481 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016482 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016483 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016485 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016486 goto theend;
16487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016489 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016490 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016491 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016492 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016494 if (flags & SP_SUBPAT)
16495 retval = subpatnum;
16496 else
16497 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016498 if (flags & SP_SETPCMARK)
16499 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016501 if (match_pos != NULL)
16502 {
16503 /* Store the match cursor position */
16504 match_pos->lnum = pos.lnum;
16505 match_pos->col = pos.col + 1;
16506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 /* "/$" will put the cursor after the end of the line, may need to
16508 * correct that here */
16509 check_cursor();
16510 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016511
16512 /* If 'n' flag is used: restore cursor position. */
16513 if (flags & SP_NOMOVE)
16514 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016515 else
16516 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016517theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016519
16520 return retval;
16521}
16522
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016523#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016524
16525/*
16526 * round() is not in C90, use ceil() or floor() instead.
16527 */
16528 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016529vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016530{
16531 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16532}
16533
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016534/*
16535 * "round({float})" function
16536 */
16537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016538f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016539{
16540 float_T f;
16541
16542 rettv->v_type = VAR_FLOAT;
16543 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016544 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016545 else
16546 rettv->vval.v_float = 0.0;
16547}
16548#endif
16549
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016550/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016551 * "screenattr()" function
16552 */
16553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016554f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016555{
16556 int row;
16557 int col;
16558 int c;
16559
16560 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16561 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16562 if (row < 0 || row >= screen_Rows
16563 || col < 0 || col >= screen_Columns)
16564 c = -1;
16565 else
16566 c = ScreenAttrs[LineOffset[row] + col];
16567 rettv->vval.v_number = c;
16568}
16569
16570/*
16571 * "screenchar()" function
16572 */
16573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016574f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016575{
16576 int row;
16577 int col;
16578 int off;
16579 int c;
16580
16581 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16582 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16583 if (row < 0 || row >= screen_Rows
16584 || col < 0 || col >= screen_Columns)
16585 c = -1;
16586 else
16587 {
16588 off = LineOffset[row] + col;
16589#ifdef FEAT_MBYTE
16590 if (enc_utf8 && ScreenLinesUC[off] != 0)
16591 c = ScreenLinesUC[off];
16592 else
16593#endif
16594 c = ScreenLines[off];
16595 }
16596 rettv->vval.v_number = c;
16597}
16598
16599/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016600 * "screencol()" function
16601 *
16602 * First column is 1 to be consistent with virtcol().
16603 */
16604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016605f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016606{
16607 rettv->vval.v_number = screen_screencol() + 1;
16608}
16609
16610/*
16611 * "screenrow()" function
16612 */
16613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016614f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016615{
16616 rettv->vval.v_number = screen_screenrow() + 1;
16617}
16618
16619/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016620 * "search()" function
16621 */
16622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016623f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016624{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016625 int flags = 0;
16626
16627 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628}
16629
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016631 * "searchdecl()" function
16632 */
16633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016634f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016635{
16636 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016637 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016638 int error = FALSE;
16639 char_u *name;
16640
16641 rettv->vval.v_number = 1; /* default: FAIL */
16642
16643 name = get_tv_string_chk(&argvars[0]);
16644 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016645 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016646 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016647 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16648 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16649 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016650 if (!error && name != NULL)
16651 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016652 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016653}
16654
16655/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016656 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016659searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660{
16661 char_u *spat, *mpat, *epat;
16662 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 int dir;
16665 int flags = 0;
16666 char_u nbuf1[NUMBUFLEN];
16667 char_u nbuf2[NUMBUFLEN];
16668 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016669 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016670 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016671 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016674 spat = get_tv_string_chk(&argvars[0]);
16675 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16676 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16677 if (spat == NULL || mpat == NULL || epat == NULL)
16678 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 /* Handle the optional fourth argument: flags */
16681 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016682 if (dir == 0)
16683 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016684
16685 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016686 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16687 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016688 if ((flags & (SP_END | SP_SUBPAT)) != 0
16689 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016690 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016691 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016692 goto theend;
16693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016695 /* Using 'r' implies 'W', otherwise it doesn't work. */
16696 if (flags & SP_REPEAT)
16697 p_ws = FALSE;
16698
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016699 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016700 if (argvars[3].v_type == VAR_UNKNOWN
16701 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 skip = (char_u *)"";
16703 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016705 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016706 if (argvars[5].v_type != VAR_UNKNOWN)
16707 {
16708 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16709 if (lnum_stop < 0)
16710 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016711#ifdef FEAT_RELTIME
16712 if (argvars[6].v_type != VAR_UNKNOWN)
16713 {
16714 time_limit = get_tv_number_chk(&argvars[6], NULL);
16715 if (time_limit < 0)
16716 goto theend;
16717 }
16718#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016719 }
16720 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 if (skip == NULL)
16722 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016724 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016725 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016726
16727theend:
16728 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016729
16730 return retval;
16731}
16732
16733/*
16734 * "searchpair()" function
16735 */
16736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016737f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016738{
16739 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16740}
16741
16742/*
16743 * "searchpairpos()" function
16744 */
16745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016746f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016747{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016748 pos_T match_pos;
16749 int lnum = 0;
16750 int col = 0;
16751
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016752 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016753 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016754
16755 if (searchpair_cmn(argvars, &match_pos) > 0)
16756 {
16757 lnum = match_pos.lnum;
16758 col = match_pos.col;
16759 }
16760
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016761 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16762 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016763}
16764
16765/*
16766 * Search for a start/middle/end thing.
16767 * Used by searchpair(), see its documentation for the details.
16768 * Returns 0 or -1 for no match,
16769 */
16770 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016771do_searchpair(
16772 char_u *spat, /* start pattern */
16773 char_u *mpat, /* middle pattern */
16774 char_u *epat, /* end pattern */
16775 int dir, /* BACKWARD or FORWARD */
16776 char_u *skip, /* skip expression */
16777 int flags, /* SP_SETPCMARK and other SP_ values */
16778 pos_T *match_pos,
16779 linenr_T lnum_stop, /* stop at this line if not zero */
16780 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016781{
16782 char_u *save_cpo;
16783 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16784 long retval = 0;
16785 pos_T pos;
16786 pos_T firstpos;
16787 pos_T foundpos;
16788 pos_T save_cursor;
16789 pos_T save_pos;
16790 int n;
16791 int r;
16792 int nest = 1;
16793 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016794 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016795 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016796
16797 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16798 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016799 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016800
Bram Moolenaar76929292008-01-06 19:07:36 +000016801#ifdef FEAT_RELTIME
16802 /* Set the time limit, if there is one. */
16803 profile_setlimit(time_limit, &tm);
16804#endif
16805
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016806 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16807 * start/middle/end (pat3, for the top pair). */
16808 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16809 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16810 if (pat2 == NULL || pat3 == NULL)
16811 goto theend;
16812 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16813 if (*mpat == NUL)
16814 STRCPY(pat3, pat2);
16815 else
16816 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16817 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016818 if (flags & SP_START)
16819 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016820
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 save_cursor = curwin->w_cursor;
16822 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016823 clearpos(&firstpos);
16824 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 pat = pat3;
16826 for (;;)
16827 {
16828 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016829 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16831 /* didn't find it or found the first match again: FAIL */
16832 break;
16833
16834 if (firstpos.lnum == 0)
16835 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016836 if (equalpos(pos, foundpos))
16837 {
16838 /* Found the same position again. Can happen with a pattern that
16839 * has "\zs" at the end and searching backwards. Advance one
16840 * character and try again. */
16841 if (dir == BACKWARD)
16842 decl(&pos);
16843 else
16844 incl(&pos);
16845 }
16846 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016848 /* clear the start flag to avoid getting stuck here */
16849 options &= ~SEARCH_START;
16850
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 /* If the skip pattern matches, ignore this match. */
16852 if (*skip != NUL)
16853 {
16854 save_pos = curwin->w_cursor;
16855 curwin->w_cursor = pos;
16856 r = eval_to_bool(skip, &err, NULL, FALSE);
16857 curwin->w_cursor = save_pos;
16858 if (err)
16859 {
16860 /* Evaluating {skip} caused an error, break here. */
16861 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016862 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 break;
16864 }
16865 if (r)
16866 continue;
16867 }
16868
16869 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16870 {
16871 /* Found end when searching backwards or start when searching
16872 * forward: nested pair. */
16873 ++nest;
16874 pat = pat2; /* nested, don't search for middle */
16875 }
16876 else
16877 {
16878 /* Found end when searching forward or start when searching
16879 * backward: end of (nested) pair; or found middle in outer pair. */
16880 if (--nest == 1)
16881 pat = pat3; /* outer level, search for middle */
16882 }
16883
16884 if (nest == 0)
16885 {
16886 /* Found the match: return matchcount or line number. */
16887 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016888 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016890 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016891 if (flags & SP_SETPCMARK)
16892 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 curwin->w_cursor = pos;
16894 if (!(flags & SP_REPEAT))
16895 break;
16896 nest = 1; /* search for next unmatched */
16897 }
16898 }
16899
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016900 if (match_pos != NULL)
16901 {
16902 /* Store the match cursor position */
16903 match_pos->lnum = curwin->w_cursor.lnum;
16904 match_pos->col = curwin->w_cursor.col + 1;
16905 }
16906
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016908 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 curwin->w_cursor = save_cursor;
16910
16911theend:
16912 vim_free(pat2);
16913 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016914 if (p_cpo == empty_option)
16915 p_cpo = save_cpo;
16916 else
16917 /* Darn, evaluating the {skip} expression changed the value. */
16918 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016919
16920 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921}
16922
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016923/*
16924 * "searchpos()" function
16925 */
16926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016927f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016928{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016929 pos_T match_pos;
16930 int lnum = 0;
16931 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016932 int n;
16933 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016934
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016935 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016936 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016937
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016938 n = search_cmn(argvars, &match_pos, &flags);
16939 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016940 {
16941 lnum = match_pos.lnum;
16942 col = match_pos.col;
16943 }
16944
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016945 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16946 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016947 if (flags & SP_SUBPAT)
16948 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016949}
16950
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016952f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954#ifdef FEAT_CLIENTSERVER
16955 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 char_u *server = get_tv_string_chk(&argvars[0]);
16957 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016960 if (server == NULL || reply == NULL)
16961 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 if (check_restricted() || check_secure())
16963 return;
16964# ifdef FEAT_X11
16965 if (check_connection() == FAIL)
16966 return;
16967# endif
16968
16969 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 EMSG(_("E258: Unable to send to client"));
16972 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 rettv->vval.v_number = 0;
16975#else
16976 rettv->vval.v_number = -1;
16977#endif
16978}
16979
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016981f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982{
16983 char_u *r = NULL;
16984
16985#ifdef FEAT_CLIENTSERVER
16986# ifdef WIN32
16987 r = serverGetVimNames();
16988# else
16989 make_connection();
16990 if (X_DISPLAY != NULL)
16991 r = serverGetVimNames(X_DISPLAY);
16992# endif
16993#endif
16994 rettv->v_type = VAR_STRING;
16995 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996}
16997
16998/*
16999 * "setbufvar()" function
17000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017002f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003{
17004 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 char_u nbuf[NUMBUFLEN];
17009
17010 if (check_restricted() || check_secure())
17011 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17013 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017014 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 varp = &argvars[2];
17016
17017 if (buf != NULL && varname != NULL && varp != NULL)
17018 {
17019 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021
17022 if (*varname == '&')
17023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017024 long numval;
17025 char_u *strval;
17026 int error = FALSE;
17027
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 numval = get_tv_number_chk(varp, &error);
17030 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017031 if (!error && strval != NULL)
17032 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 }
17034 else
17035 {
17036 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17037 if (bufvarname != NULL)
17038 {
17039 STRCPY(bufvarname, "b:");
17040 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017041 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 vim_free(bufvarname);
17043 }
17044 }
17045
17046 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049}
17050
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017052f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017053{
17054 dict_T *d;
17055 dictitem_T *di;
17056 char_u *csearch;
17057
17058 if (argvars[0].v_type != VAR_DICT)
17059 {
17060 EMSG(_(e_dictreq));
17061 return;
17062 }
17063
17064 if ((d = argvars[0].vval.v_dict) != NULL)
17065 {
17066 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17067 if (csearch != NULL)
17068 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017069#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017070 if (enc_utf8)
17071 {
17072 int pcc[MAX_MCO];
17073 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017074
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017075 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17076 }
17077 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017078#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017079 set_last_csearch(PTR2CHAR(csearch),
17080 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017081 }
17082
17083 di = dict_find(d, (char_u *)"forward", -1);
17084 if (di != NULL)
17085 set_csearch_direction(get_tv_number(&di->di_tv)
17086 ? FORWARD : BACKWARD);
17087
17088 di = dict_find(d, (char_u *)"until", -1);
17089 if (di != NULL)
17090 set_csearch_until(!!get_tv_number(&di->di_tv));
17091 }
17092}
17093
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094/*
17095 * "setcmdpos()" function
17096 */
17097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017098f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017100 int pos = (int)get_tv_number(&argvars[0]) - 1;
17101
17102 if (pos >= 0)
17103 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104}
17105
17106/*
17107 * "setline()" function
17108 */
17109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017110f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111{
17112 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017113 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017114 list_T *l = NULL;
17115 listitem_T *li = NULL;
17116 long added = 0;
17117 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017119 lnum = get_tv_lnum(&argvars[0]);
17120 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017122 l = argvars[1].vval.v_list;
17123 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017125 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017127
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017128 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017129 for (;;)
17130 {
17131 if (l != NULL)
17132 {
17133 /* list argument, get next string */
17134 if (li == NULL)
17135 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017137 li = li->li_next;
17138 }
17139
17140 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017141 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017142 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017143
17144 /* When coming here from Insert mode, sync undo, so that this can be
17145 * undone separately from what was previously inserted. */
17146 if (u_sync_once == 2)
17147 {
17148 u_sync_once = 1; /* notify that u_sync() was called */
17149 u_sync(TRUE);
17150 }
17151
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017152 if (lnum <= curbuf->b_ml.ml_line_count)
17153 {
17154 /* existing line, replace it */
17155 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17156 {
17157 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017158 if (lnum == curwin->w_cursor.lnum)
17159 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017160 rettv->vval.v_number = 0; /* OK */
17161 }
17162 }
17163 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17164 {
17165 /* lnum is one past the last line, append the line */
17166 ++added;
17167 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17168 rettv->vval.v_number = 0; /* OK */
17169 }
17170
17171 if (l == NULL) /* only one string argument */
17172 break;
17173 ++lnum;
17174 }
17175
17176 if (added > 0)
17177 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178}
17179
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017180static 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 +000017181
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017183 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017184 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186set_qf_ll_list(
17187 win_T *wp UNUSED,
17188 typval_T *list_arg UNUSED,
17189 typval_T *action_arg UNUSED,
17190 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017191{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017192#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017193 char_u *act;
17194 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017195#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017196
Bram Moolenaar2641f772005-03-25 21:58:17 +000017197 rettv->vval.v_number = -1;
17198
17199#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017200 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017201 EMSG(_(e_listreq));
17202 else
17203 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017204 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017205
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017206 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017207 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017208 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017209 if (act == NULL)
17210 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017211 if (*act == 'a' || *act == 'r')
17212 action = *act;
17213 }
17214
Bram Moolenaar81484f42012-12-05 15:16:47 +010017215 if (l != NULL && set_errorlist(wp, l, action,
17216 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017217 rettv->vval.v_number = 0;
17218 }
17219#endif
17220}
17221
17222/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017223 * "setloclist()" function
17224 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017226f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017227{
17228 win_T *win;
17229
17230 rettv->vval.v_number = -1;
17231
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017232 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017233 if (win != NULL)
17234 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17235}
17236
17237/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017238 * "setmatches()" function
17239 */
17240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017241f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017242{
17243#ifdef FEAT_SEARCH_EXTRA
17244 list_T *l;
17245 listitem_T *li;
17246 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017247 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017248
17249 rettv->vval.v_number = -1;
17250 if (argvars[0].v_type != VAR_LIST)
17251 {
17252 EMSG(_(e_listreq));
17253 return;
17254 }
17255 if ((l = argvars[0].vval.v_list) != NULL)
17256 {
17257
17258 /* To some extent make sure that we are dealing with a list from
17259 * "getmatches()". */
17260 li = l->lv_first;
17261 while (li != NULL)
17262 {
17263 if (li->li_tv.v_type != VAR_DICT
17264 || (d = li->li_tv.vval.v_dict) == NULL)
17265 {
17266 EMSG(_(e_invarg));
17267 return;
17268 }
17269 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017270 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17271 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017272 && dict_find(d, (char_u *)"priority", -1) != NULL
17273 && dict_find(d, (char_u *)"id", -1) != NULL))
17274 {
17275 EMSG(_(e_invarg));
17276 return;
17277 }
17278 li = li->li_next;
17279 }
17280
17281 clear_matches(curwin);
17282 li = l->lv_first;
17283 while (li != NULL)
17284 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017285 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017286 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017287 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017288 char_u *group;
17289 int priority;
17290 int id;
17291 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017292
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017293 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017294 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17295 {
17296 if (s == NULL)
17297 {
17298 s = list_alloc();
17299 if (s == NULL)
17300 return;
17301 }
17302
17303 /* match from matchaddpos() */
17304 for (i = 1; i < 9; i++)
17305 {
17306 sprintf((char *)buf, (char *)"pos%d", i);
17307 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17308 {
17309 if (di->di_tv.v_type != VAR_LIST)
17310 return;
17311
17312 list_append_tv(s, &di->di_tv);
17313 s->lv_refcount++;
17314 }
17315 else
17316 break;
17317 }
17318 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017319
17320 group = get_dict_string(d, (char_u *)"group", FALSE);
17321 priority = (int)get_dict_number(d, (char_u *)"priority");
17322 id = (int)get_dict_number(d, (char_u *)"id");
17323 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17324 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17325 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017326 if (i == 0)
17327 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017328 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017329 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017330 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017331 }
17332 else
17333 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017334 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017335 list_unref(s);
17336 s = NULL;
17337 }
17338
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017339 li = li->li_next;
17340 }
17341 rettv->vval.v_number = 0;
17342 }
17343#endif
17344}
17345
17346/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017347 * "setpos()" function
17348 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017350f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017351{
17352 pos_T pos;
17353 int fnum;
17354 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017355 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017356
Bram Moolenaar08250432008-02-13 11:42:46 +000017357 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017358 name = get_tv_string_chk(argvars);
17359 if (name != NULL)
17360 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017361 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017362 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017363 if (--pos.col < 0)
17364 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017365 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017366 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017367 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017368 if (fnum == curbuf->b_fnum)
17369 {
17370 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017371 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017372 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017373 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017374 curwin->w_set_curswant = FALSE;
17375 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017376 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017377 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017378 }
17379 else
17380 EMSG(_(e_invarg));
17381 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017382 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17383 {
17384 /* set mark */
17385 if (setmark_pos(name[1], &pos, fnum) == OK)
17386 rettv->vval.v_number = 0;
17387 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017388 else
17389 EMSG(_(e_invarg));
17390 }
17391 }
17392}
17393
17394/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017395 * "setqflist()" function
17396 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017398f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017399{
17400 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17401}
17402
17403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 * "setreg()" function
17405 */
17406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017407f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408{
17409 int regname;
17410 char_u *strregname;
17411 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017412 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 int append;
17414 char_u yank_type;
17415 long block_len;
17416
17417 block_len = -1;
17418 yank_type = MAUTO;
17419 append = FALSE;
17420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017422 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017424 if (strregname == NULL)
17425 return; /* type error; errmsg already given */
17426 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 if (regname == 0 || regname == '@')
17428 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017432 stropt = get_tv_string_chk(&argvars[2]);
17433 if (stropt == NULL)
17434 return; /* type error */
17435 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 switch (*stropt)
17437 {
17438 case 'a': case 'A': /* append */
17439 append = TRUE;
17440 break;
17441 case 'v': case 'c': /* character-wise selection */
17442 yank_type = MCHAR;
17443 break;
17444 case 'V': case 'l': /* line-wise selection */
17445 yank_type = MLINE;
17446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 case 'b': case Ctrl_V: /* block-wise selection */
17448 yank_type = MBLOCK;
17449 if (VIM_ISDIGIT(stropt[1]))
17450 {
17451 ++stropt;
17452 block_len = getdigits(&stropt) - 1;
17453 --stropt;
17454 }
17455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 }
17457 }
17458
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017459 if (argvars[1].v_type == VAR_LIST)
17460 {
17461 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017462 char_u **allocval;
17463 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017464 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017465 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017466 int len = argvars[1].vval.v_list->lv_len;
17467 listitem_T *li;
17468
Bram Moolenaar7d647822014-04-05 21:28:56 +020017469 /* First half: use for pointers to result lines; second half: use for
17470 * pointers to allocated copies. */
17471 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017472 if (lstval == NULL)
17473 return;
17474 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017475 allocval = lstval + len + 2;
17476 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017477
17478 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17479 li = li->li_next)
17480 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017481 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017482 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017483 goto free_lstval;
17484 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017485 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017486 /* Need to make a copy, next get_tv_string_buf_chk() will
17487 * overwrite the string. */
17488 strval = vim_strsave(buf);
17489 if (strval == NULL)
17490 goto free_lstval;
17491 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017492 }
17493 *curval++ = strval;
17494 }
17495 *curval++ = NULL;
17496
17497 write_reg_contents_lst(regname, lstval, -1,
17498 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017499free_lstval:
17500 while (curallocval > allocval)
17501 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017502 vim_free(lstval);
17503 }
17504 else
17505 {
17506 strval = get_tv_string_chk(&argvars[1]);
17507 if (strval == NULL)
17508 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017509 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513}
17514
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017515/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017516 * "settabvar()" function
17517 */
17518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017519f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017520{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017521#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017522 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017523 tabpage_T *tp;
17524#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017525 char_u *varname, *tabvarname;
17526 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017527
17528 rettv->vval.v_number = 0;
17529
17530 if (check_restricted() || check_secure())
17531 return;
17532
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017533#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017534 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017535#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017536 varname = get_tv_string_chk(&argvars[1]);
17537 varp = &argvars[2];
17538
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017539 if (varname != NULL && varp != NULL
17540#ifdef FEAT_WINDOWS
17541 && tp != NULL
17542#endif
17543 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017544 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017545#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017546 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017547 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017548#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017549
17550 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17551 if (tabvarname != NULL)
17552 {
17553 STRCPY(tabvarname, "t:");
17554 STRCPY(tabvarname + 2, varname);
17555 set_var(tabvarname, varp, TRUE);
17556 vim_free(tabvarname);
17557 }
17558
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017559#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017560 /* Restore current tabpage */
17561 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017562 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017563#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017564 }
17565}
17566
17567/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017568 * "settabwinvar()" function
17569 */
17570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017571f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017572{
17573 setwinvar(argvars, rettv, 1);
17574}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575
17576/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017577 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017580f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017582 setwinvar(argvars, rettv, 0);
17583}
17584
17585/*
17586 * "setwinvar()" and "settabwinvar()" functions
17587 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017588
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017590setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017591{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 win_T *win;
17593#ifdef FEAT_WINDOWS
17594 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017595 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017596 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597#endif
17598 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017601 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602
17603 if (check_restricted() || check_secure())
17604 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017605
17606#ifdef FEAT_WINDOWS
17607 if (off == 1)
17608 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17609 else
17610 tp = curtab;
17611#endif
17612 win = find_win_by_nr(&argvars[off], tp);
17613 varname = get_tv_string_chk(&argvars[off + 1]);
17614 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615
17616 if (win != NULL && varname != NULL && varp != NULL)
17617 {
17618#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017619 need_switch_win = !(tp == curtab && win == curwin);
17620 if (!need_switch_win
17621 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017624 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017626 long numval;
17627 char_u *strval;
17628 int error = FALSE;
17629
17630 ++varname;
17631 numval = get_tv_number_chk(varp, &error);
17632 strval = get_tv_string_buf_chk(varp, nbuf);
17633 if (!error && strval != NULL)
17634 set_option_value(varname, numval, strval, OPT_LOCAL);
17635 }
17636 else
17637 {
17638 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17639 if (winvarname != NULL)
17640 {
17641 STRCPY(winvarname, "w:");
17642 STRCPY(winvarname + 2, varname);
17643 set_var(winvarname, varp, TRUE);
17644 vim_free(winvarname);
17645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 }
17647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017649 if (need_switch_win)
17650 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651#endif
17652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653}
17654
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017655#ifdef FEAT_CRYPT
17656/*
17657 * "sha256({string})" function
17658 */
17659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017660f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017661{
17662 char_u *p;
17663
17664 p = get_tv_string(&argvars[0]);
17665 rettv->vval.v_string = vim_strsave(
17666 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17667 rettv->v_type = VAR_STRING;
17668}
17669#endif /* FEAT_CRYPT */
17670
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017672 * "shellescape({string})" function
17673 */
17674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017675f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017676{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017677 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017678 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017679 rettv->v_type = VAR_STRING;
17680}
17681
17682/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017683 * shiftwidth() function
17684 */
17685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017686f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017687{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017688 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017689}
17690
17691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017692 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 */
17694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017695f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698
Bram Moolenaar0d660222005-01-07 21:51:51 +000017699 p = get_tv_string(&argvars[0]);
17700 rettv->vval.v_string = vim_strsave(p);
17701 simplify_filename(rettv->vval.v_string); /* simplify in place */
17702 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703}
17704
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017705#ifdef FEAT_FLOAT
17706/*
17707 * "sin()" function
17708 */
17709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017710f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017711{
17712 float_T f;
17713
17714 rettv->v_type = VAR_FLOAT;
17715 if (get_float_arg(argvars, &f) == OK)
17716 rettv->vval.v_float = sin(f);
17717 else
17718 rettv->vval.v_float = 0.0;
17719}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017720
17721/*
17722 * "sinh()" function
17723 */
17724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017725f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017726{
17727 float_T f;
17728
17729 rettv->v_type = VAR_FLOAT;
17730 if (get_float_arg(argvars, &f) == OK)
17731 rettv->vval.v_float = sinh(f);
17732 else
17733 rettv->vval.v_float = 0.0;
17734}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017735#endif
17736
Bram Moolenaar0d660222005-01-07 21:51:51 +000017737static int
17738#ifdef __BORLANDC__
17739 _RTLENTRYF
17740#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017741 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742static int
17743#ifdef __BORLANDC__
17744 _RTLENTRYF
17745#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017746 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017747
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017748/* struct used in the array that's given to qsort() */
17749typedef struct
17750{
17751 listitem_T *item;
17752 int idx;
17753} sortItem_T;
17754
Bram Moolenaar0d660222005-01-07 21:51:51 +000017755static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017756static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017757static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017758#ifdef FEAT_FLOAT
17759static int item_compare_float;
17760#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017761static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017762static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017763static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017764static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017765static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766#define ITEM_COMPARE_FAIL 999
17767
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017769 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017771 static int
17772#ifdef __BORLANDC__
17773_RTLENTRYF
17774#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017775item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017777 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017778 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017779 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017780 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017781 int res;
17782 char_u numbuf1[NUMBUFLEN];
17783 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017785 si1 = (sortItem_T *)s1;
17786 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017787 tv1 = &si1->item->li_tv;
17788 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017789
17790 if (item_compare_numbers)
17791 {
17792 long v1 = get_tv_number(tv1);
17793 long v2 = get_tv_number(tv2);
17794
17795 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17796 }
17797
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017798#ifdef FEAT_FLOAT
17799 if (item_compare_float)
17800 {
17801 float_T v1 = get_tv_float(tv1);
17802 float_T v2 = get_tv_float(tv2);
17803
17804 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17805 }
17806#endif
17807
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017808 /* tv2string() puts quotes around a string and allocates memory. Don't do
17809 * that for string variables. Use a single quote when comparing with a
17810 * non-string to do what the docs promise. */
17811 if (tv1->v_type == VAR_STRING)
17812 {
17813 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17814 p1 = (char_u *)"'";
17815 else
17816 p1 = tv1->vval.v_string;
17817 }
17818 else
17819 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17820 if (tv2->v_type == VAR_STRING)
17821 {
17822 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17823 p2 = (char_u *)"'";
17824 else
17825 p2 = tv2->vval.v_string;
17826 }
17827 else
17828 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017829 if (p1 == NULL)
17830 p1 = (char_u *)"";
17831 if (p2 == NULL)
17832 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017833 if (!item_compare_numeric)
17834 {
17835 if (item_compare_ic)
17836 res = STRICMP(p1, p2);
17837 else
17838 res = STRCMP(p1, p2);
17839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017841 {
17842 double n1, n2;
17843 n1 = strtod((char *)p1, (char **)&p1);
17844 n2 = strtod((char *)p2, (char **)&p2);
17845 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17846 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017847
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017848 /* When the result would be zero, compare the item indexes. Makes the
17849 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017850 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017851 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017852
Bram Moolenaar0d660222005-01-07 21:51:51 +000017853 vim_free(tofree1);
17854 vim_free(tofree2);
17855 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856}
17857
17858 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017859#ifdef __BORLANDC__
17860_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017862item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017864 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017865 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017866 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017867 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017868 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017870 /* shortcut after failure in previous call; compare all items equal */
17871 if (item_compare_func_err)
17872 return 0;
17873
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017874 si1 = (sortItem_T *)s1;
17875 si2 = (sortItem_T *)s2;
17876
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017877 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017878 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017879 copy_tv(&si1->item->li_tv, &argv[0]);
17880 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881
17882 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017883 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017884 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17885 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017886 clear_tv(&argv[0]);
17887 clear_tv(&argv[1]);
17888
17889 if (res == FAIL)
17890 res = ITEM_COMPARE_FAIL;
17891 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017892 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17893 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017894 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017896
17897 /* When the result would be zero, compare the pointers themselves. Makes
17898 * the sort stable. */
17899 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017900 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017901
Bram Moolenaar0d660222005-01-07 21:51:51 +000017902 return res;
17903}
17904
17905/*
17906 * "sort({list})" function
17907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017909do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910{
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 list_T *l;
17912 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017913 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017914 long len;
17915 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017918 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 else
17920 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017921 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017922 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017923 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17924 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017925 return;
17926 rettv->vval.v_list = l;
17927 rettv->v_type = VAR_LIST;
17928 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930 len = list_len(l);
17931 if (len <= 1)
17932 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933
Bram Moolenaar0d660222005-01-07 21:51:51 +000017934 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017935 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017936 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017937#ifdef FEAT_FLOAT
17938 item_compare_float = FALSE;
17939#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017940 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017941 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017942 if (argvars[1].v_type != VAR_UNKNOWN)
17943 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017944 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017945 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017946 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017947 else
17948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017949 int error = FALSE;
17950
17951 i = get_tv_number_chk(&argvars[1], &error);
17952 if (error)
17953 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017954 if (i == 1)
17955 item_compare_ic = TRUE;
17956 else
17957 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017958 if (item_compare_func != NULL)
17959 {
17960 if (STRCMP(item_compare_func, "n") == 0)
17961 {
17962 item_compare_func = NULL;
17963 item_compare_numeric = TRUE;
17964 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017965 else if (STRCMP(item_compare_func, "N") == 0)
17966 {
17967 item_compare_func = NULL;
17968 item_compare_numbers = TRUE;
17969 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017970#ifdef FEAT_FLOAT
17971 else if (STRCMP(item_compare_func, "f") == 0)
17972 {
17973 item_compare_func = NULL;
17974 item_compare_float = TRUE;
17975 }
17976#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017977 else if (STRCMP(item_compare_func, "i") == 0)
17978 {
17979 item_compare_func = NULL;
17980 item_compare_ic = TRUE;
17981 }
17982 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017983 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017984
17985 if (argvars[2].v_type != VAR_UNKNOWN)
17986 {
17987 /* optional third argument: {dict} */
17988 if (argvars[2].v_type != VAR_DICT)
17989 {
17990 EMSG(_(e_dictreq));
17991 return;
17992 }
17993 item_compare_selfdict = argvars[2].vval.v_dict;
17994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017998 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017999 if (ptrs == NULL)
18000 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001
Bram Moolenaar327aa022014-03-25 18:24:23 +010018002 i = 0;
18003 if (sort)
18004 {
18005 /* sort(): ptrs will be the list to sort */
18006 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018007 {
18008 ptrs[i].item = li;
18009 ptrs[i].idx = i;
18010 ++i;
18011 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018012
18013 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018014 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018015 /* test the compare function */
18016 if (item_compare_func != NULL
18017 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018018 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018019 EMSG(_("E702: Sort compare function failed"));
18020 else
18021 {
18022 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018023 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018024 item_compare_func == NULL ? item_compare : item_compare2);
18025
18026 if (!item_compare_func_err)
18027 {
18028 /* Clear the List and append the items in sorted order. */
18029 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18030 l->lv_len = 0;
18031 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018032 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018033 }
18034 }
18035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018037 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018038 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018039
18040 /* f_uniq(): ptrs will be a stack of items to remove */
18041 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018042 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018043 item_compare_func_ptr = item_compare_func
18044 ? item_compare2 : item_compare;
18045
18046 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18047 li = li->li_next)
18048 {
18049 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18050 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018051 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018052 if (item_compare_func_err)
18053 {
18054 EMSG(_("E882: Uniq compare function failed"));
18055 break;
18056 }
18057 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018059 if (!item_compare_func_err)
18060 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018061 while (--i >= 0)
18062 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018063 li = ptrs[i].item->li_next;
18064 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018065 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018066 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018067 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018068 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018069 list_fix_watch(l, li);
18070 listitem_free(li);
18071 l->lv_len--;
18072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018073 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018074 }
18075
18076 vim_free(ptrs);
18077 }
18078}
18079
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018080/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018081 * "sort({list})" function
18082 */
18083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018084f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018085{
18086 do_sort_uniq(argvars, rettv, TRUE);
18087}
18088
18089/*
18090 * "uniq({list})" function
18091 */
18092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018093f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018094{
18095 do_sort_uniq(argvars, rettv, FALSE);
18096}
18097
18098/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018099 * "soundfold({word})" function
18100 */
18101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018102f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018103{
18104 char_u *s;
18105
18106 rettv->v_type = VAR_STRING;
18107 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018108#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018109 rettv->vval.v_string = eval_soundfold(s);
18110#else
18111 rettv->vval.v_string = vim_strsave(s);
18112#endif
18113}
18114
18115/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018116 * "spellbadword()" function
18117 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018119f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018120{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018121 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018122 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018123 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018124
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018125 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018126 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018127
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018128#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018129 if (argvars[0].v_type == VAR_UNKNOWN)
18130 {
18131 /* Find the start and length of the badly spelled word. */
18132 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18133 if (len != 0)
18134 word = ml_get_cursor();
18135 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018136 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018137 {
18138 char_u *str = get_tv_string_chk(&argvars[0]);
18139 int capcol = -1;
18140
18141 if (str != NULL)
18142 {
18143 /* Check the argument for spelling. */
18144 while (*str != NUL)
18145 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018146 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018147 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018148 {
18149 word = str;
18150 break;
18151 }
18152 str += len;
18153 }
18154 }
18155 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018156#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018157
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018158 list_append_string(rettv->vval.v_list, word, len);
18159 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018160 attr == HLF_SPB ? "bad" :
18161 attr == HLF_SPR ? "rare" :
18162 attr == HLF_SPL ? "local" :
18163 attr == HLF_SPC ? "caps" :
18164 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018165}
18166
18167/*
18168 * "spellsuggest()" function
18169 */
18170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018171f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018172{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018173#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018174 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018175 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018176 int maxcount;
18177 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018178 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018179 listitem_T *li;
18180 int need_capital = FALSE;
18181#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018182
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018183 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018184 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018185
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018186#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018187 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018188 {
18189 str = get_tv_string(&argvars[0]);
18190 if (argvars[1].v_type != VAR_UNKNOWN)
18191 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018192 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018193 if (maxcount <= 0)
18194 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018195 if (argvars[2].v_type != VAR_UNKNOWN)
18196 {
18197 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18198 if (typeerr)
18199 return;
18200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018201 }
18202 else
18203 maxcount = 25;
18204
Bram Moolenaar4770d092006-01-12 23:22:24 +000018205 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018206
18207 for (i = 0; i < ga.ga_len; ++i)
18208 {
18209 str = ((char_u **)ga.ga_data)[i];
18210
18211 li = listitem_alloc();
18212 if (li == NULL)
18213 vim_free(str);
18214 else
18215 {
18216 li->li_tv.v_type = VAR_STRING;
18217 li->li_tv.v_lock = 0;
18218 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018219 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018220 }
18221 }
18222 ga_clear(&ga);
18223 }
18224#endif
18225}
18226
Bram Moolenaar0d660222005-01-07 21:51:51 +000018227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018228f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229{
18230 char_u *str;
18231 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018232 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018233 regmatch_T regmatch;
18234 char_u patbuf[NUMBUFLEN];
18235 char_u *save_cpo;
18236 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018238 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018239 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240
18241 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18242 save_cpo = p_cpo;
18243 p_cpo = (char_u *)"";
18244
18245 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018246 if (argvars[1].v_type != VAR_UNKNOWN)
18247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018248 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18249 if (pat == NULL)
18250 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018251 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018252 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018253 }
18254 if (pat == NULL || *pat == NUL)
18255 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018256
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018257 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018259 if (typeerr)
18260 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261
Bram Moolenaar0d660222005-01-07 21:51:51 +000018262 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18263 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018265 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018266 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018268 if (*str == NUL)
18269 match = FALSE; /* empty item at the end */
18270 else
18271 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018272 if (match)
18273 end = regmatch.startp[0];
18274 else
18275 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018276 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18277 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018278 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018279 if (list_append_string(rettv->vval.v_list, str,
18280 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282 }
18283 if (!match)
18284 break;
18285 /* Advance to just after the match. */
18286 if (regmatch.endp[0] > str)
18287 col = 0;
18288 else
18289 {
18290 /* Don't get stuck at the same match. */
18291#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018292 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018293#else
18294 col = 1;
18295#endif
18296 }
18297 str = regmatch.endp[0];
18298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299
Bram Moolenaar473de612013-06-08 18:19:48 +020018300 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302
Bram Moolenaar0d660222005-01-07 21:51:51 +000018303 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304}
18305
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018306#ifdef FEAT_FLOAT
18307/*
18308 * "sqrt()" function
18309 */
18310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018311f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018312{
18313 float_T f;
18314
18315 rettv->v_type = VAR_FLOAT;
18316 if (get_float_arg(argvars, &f) == OK)
18317 rettv->vval.v_float = sqrt(f);
18318 else
18319 rettv->vval.v_float = 0.0;
18320}
18321
18322/*
18323 * "str2float()" function
18324 */
18325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018326f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018327{
18328 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18329
18330 if (*p == '+')
18331 p = skipwhite(p + 1);
18332 (void)string2float(p, &rettv->vval.v_float);
18333 rettv->v_type = VAR_FLOAT;
18334}
18335#endif
18336
Bram Moolenaar2c932302006-03-18 21:42:09 +000018337/*
18338 * "str2nr()" function
18339 */
18340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018341f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018342{
18343 int base = 10;
18344 char_u *p;
18345 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018346 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018347
18348 if (argvars[1].v_type != VAR_UNKNOWN)
18349 {
18350 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018351 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018352 {
18353 EMSG(_(e_invarg));
18354 return;
18355 }
18356 }
18357
18358 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018359 if (*p == '+')
18360 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018361 switch (base)
18362 {
18363 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18364 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18365 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18366 default: what = 0;
18367 }
18368 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018369 rettv->vval.v_number = n;
18370}
18371
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372#ifdef HAVE_STRFTIME
18373/*
18374 * "strftime({format}[, {time}])" function
18375 */
18376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018377f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378{
18379 char_u result_buf[256];
18380 struct tm *curtime;
18381 time_t seconds;
18382 char_u *p;
18383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018384 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018386 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018387 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 seconds = time(NULL);
18389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018390 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 curtime = localtime(&seconds);
18392 /* MSVC returns NULL for an invalid value of seconds. */
18393 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018394 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 else
18396 {
18397# ifdef FEAT_MBYTE
18398 vimconv_T conv;
18399 char_u *enc;
18400
18401 conv.vc_type = CONV_NONE;
18402 enc = enc_locale();
18403 convert_setup(&conv, p_enc, enc);
18404 if (conv.vc_type != CONV_NONE)
18405 p = string_convert(&conv, p, NULL);
18406# endif
18407 if (p != NULL)
18408 (void)strftime((char *)result_buf, sizeof(result_buf),
18409 (char *)p, curtime);
18410 else
18411 result_buf[0] = NUL;
18412
18413# ifdef FEAT_MBYTE
18414 if (conv.vc_type != CONV_NONE)
18415 vim_free(p);
18416 convert_setup(&conv, enc, p_enc);
18417 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 else
18420# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422
18423# ifdef FEAT_MBYTE
18424 /* Release conversion descriptors */
18425 convert_setup(&conv, NULL, NULL);
18426 vim_free(enc);
18427# endif
18428 }
18429}
18430#endif
18431
18432/*
18433 * "stridx()" function
18434 */
18435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018436f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437{
18438 char_u buf[NUMBUFLEN];
18439 char_u *needle;
18440 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018443 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018445 needle = get_tv_string_chk(&argvars[1]);
18446 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018447 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018448 if (needle == NULL || haystack == NULL)
18449 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 if (argvars[2].v_type != VAR_UNKNOWN)
18452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018453 int error = FALSE;
18454
18455 start_idx = get_tv_number_chk(&argvars[2], &error);
18456 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018457 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018458 if (start_idx >= 0)
18459 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018460 }
18461
18462 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18463 if (pos != NULL)
18464 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465}
18466
18467/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018468 * "string()" function
18469 */
18470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018471f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018472{
18473 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018474 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018477 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018478 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018479 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018480 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481}
18482
18483/*
18484 * "strlen()" function
18485 */
18486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018487f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018489 rettv->vval.v_number = (varnumber_T)(STRLEN(
18490 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491}
18492
18493/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018494 * "strchars()" function
18495 */
18496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018497f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018498{
18499 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018500 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018501#ifdef FEAT_MBYTE
18502 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018503 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018504#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018505
18506 if (argvars[1].v_type != VAR_UNKNOWN)
18507 skipcc = get_tv_number_chk(&argvars[1], NULL);
18508 if (skipcc < 0 || skipcc > 1)
18509 EMSG(_(e_invarg));
18510 else
18511 {
18512#ifdef FEAT_MBYTE
18513 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18514 while (*s != NUL)
18515 {
18516 func_mb_ptr2char_adv(&s);
18517 ++len;
18518 }
18519 rettv->vval.v_number = len;
18520#else
18521 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18522#endif
18523 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018524}
18525
18526/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018527 * "strdisplaywidth()" function
18528 */
18529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018530f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018531{
18532 char_u *s = get_tv_string(&argvars[0]);
18533 int col = 0;
18534
18535 if (argvars[1].v_type != VAR_UNKNOWN)
18536 col = get_tv_number(&argvars[1]);
18537
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018538 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018539}
18540
18541/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018542 * "strwidth()" function
18543 */
18544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018545f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018546{
18547 char_u *s = get_tv_string(&argvars[0]);
18548
18549 rettv->vval.v_number = (varnumber_T)(
18550#ifdef FEAT_MBYTE
18551 mb_string2cells(s, -1)
18552#else
18553 STRLEN(s)
18554#endif
18555 );
18556}
18557
18558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 * "strpart()" function
18560 */
18561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018562f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563{
18564 char_u *p;
18565 int n;
18566 int len;
18567 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018568 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 slen = (int)STRLEN(p);
18572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018573 n = get_tv_number_chk(&argvars[1], &error);
18574 if (error)
18575 len = 0;
18576 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 else
18579 len = slen - n; /* default len: all bytes that are available. */
18580
18581 /*
18582 * Only return the overlap between the specified part and the actual
18583 * string.
18584 */
18585 if (n < 0)
18586 {
18587 len += n;
18588 n = 0;
18589 }
18590 else if (n > slen)
18591 n = slen;
18592 if (len < 0)
18593 len = 0;
18594 else if (n + len > slen)
18595 len = slen - n;
18596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 rettv->v_type = VAR_STRING;
18598 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599}
18600
18601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018602 * "strridx()" function
18603 */
18604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018605f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018606{
18607 char_u buf[NUMBUFLEN];
18608 char_u *needle;
18609 char_u *haystack;
18610 char_u *rest;
18611 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018612 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018614 needle = get_tv_string_chk(&argvars[1]);
18615 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018616
18617 rettv->vval.v_number = -1;
18618 if (needle == NULL || haystack == NULL)
18619 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018620
18621 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018622 if (argvars[2].v_type != VAR_UNKNOWN)
18623 {
18624 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018625 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018626 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018627 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018628 }
18629 else
18630 end_idx = haystack_len;
18631
Bram Moolenaar0d660222005-01-07 21:51:51 +000018632 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018633 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018635 lastmatch = haystack + end_idx;
18636 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018638 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018639 for (rest = haystack; *rest != '\0'; ++rest)
18640 {
18641 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018642 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643 break;
18644 lastmatch = rest;
18645 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018646 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647
18648 if (lastmatch == NULL)
18649 rettv->vval.v_number = -1;
18650 else
18651 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18652}
18653
18654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 * "strtrans()" function
18656 */
18657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018658f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->v_type = VAR_STRING;
18661 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662}
18663
18664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018665 * "submatch()" function
18666 */
18667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018668f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018669{
Bram Moolenaar41571762014-04-02 19:00:58 +020018670 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018671 int no;
18672 int retList = 0;
18673
18674 no = (int)get_tv_number_chk(&argvars[0], &error);
18675 if (error)
18676 return;
18677 error = FALSE;
18678 if (argvars[1].v_type != VAR_UNKNOWN)
18679 retList = get_tv_number_chk(&argvars[1], &error);
18680 if (error)
18681 return;
18682
18683 if (retList == 0)
18684 {
18685 rettv->v_type = VAR_STRING;
18686 rettv->vval.v_string = reg_submatch(no);
18687 }
18688 else
18689 {
18690 rettv->v_type = VAR_LIST;
18691 rettv->vval.v_list = reg_submatch_list(no);
18692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018693}
18694
18695/*
18696 * "substitute()" function
18697 */
18698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018699f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018700{
18701 char_u patbuf[NUMBUFLEN];
18702 char_u subbuf[NUMBUFLEN];
18703 char_u flagsbuf[NUMBUFLEN];
18704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018705 char_u *str = get_tv_string_chk(&argvars[0]);
18706 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18707 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18708 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18709
Bram Moolenaar0d660222005-01-07 21:51:51 +000018710 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018711 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18712 rettv->vval.v_string = NULL;
18713 else
18714 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018715}
18716
18717/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018718 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018721f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722{
18723 int id = 0;
18724#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018725 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 long col;
18727 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018728 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018730 lnum = get_tv_lnum(argvars); /* -1 on type error */
18731 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18732 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018734 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018735 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018736 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737#endif
18738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740}
18741
18742/*
18743 * "synIDattr(id, what [, mode])" function
18744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018746f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747{
18748 char_u *p = NULL;
18749#ifdef FEAT_SYN_HL
18750 int id;
18751 char_u *what;
18752 char_u *mode;
18753 char_u modebuf[NUMBUFLEN];
18754 int modec;
18755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018756 id = get_tv_number(&argvars[0]);
18757 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018758 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018760 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018762 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 modec = 0; /* replace invalid with current */
18764 }
18765 else
18766 {
18767#ifdef FEAT_GUI
18768 if (gui.in_use)
18769 modec = 'g';
18770 else
18771#endif
18772 if (t_colors > 1)
18773 modec = 'c';
18774 else
18775 modec = 't';
18776 }
18777
18778
18779 switch (TOLOWER_ASC(what[0]))
18780 {
18781 case 'b':
18782 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18783 p = highlight_color(id, what, modec);
18784 else /* bold */
18785 p = highlight_has_attr(id, HL_BOLD, modec);
18786 break;
18787
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018788 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 p = highlight_color(id, what, modec);
18790 break;
18791
18792 case 'i':
18793 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18794 p = highlight_has_attr(id, HL_INVERSE, modec);
18795 else /* italic */
18796 p = highlight_has_attr(id, HL_ITALIC, modec);
18797 break;
18798
18799 case 'n': /* name */
18800 p = get_highlight_name(NULL, id - 1);
18801 break;
18802
18803 case 'r': /* reverse */
18804 p = highlight_has_attr(id, HL_INVERSE, modec);
18805 break;
18806
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018807 case 's':
18808 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18809 p = highlight_color(id, what, modec);
18810 else /* standout */
18811 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 break;
18813
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018814 case 'u':
18815 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18816 /* underline */
18817 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18818 else
18819 /* undercurl */
18820 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 break;
18822 }
18823
18824 if (p != NULL)
18825 p = vim_strsave(p);
18826#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827 rettv->v_type = VAR_STRING;
18828 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829}
18830
18831/*
18832 * "synIDtrans(id)" function
18833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018835f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836{
18837 int id;
18838
18839#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018840 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841
18842 if (id > 0)
18843 id = syn_get_final_id(id);
18844 else
18845#endif
18846 id = 0;
18847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849}
18850
18851/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018852 * "synconcealed(lnum, col)" function
18853 */
18854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018855f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018856{
18857#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18858 long lnum;
18859 long col;
18860 int syntax_flags = 0;
18861 int cchar;
18862 int matchid = 0;
18863 char_u str[NUMBUFLEN];
18864#endif
18865
18866 rettv->v_type = VAR_LIST;
18867 rettv->vval.v_list = NULL;
18868
18869#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18870 lnum = get_tv_lnum(argvars); /* -1 on type error */
18871 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18872
18873 vim_memset(str, NUL, sizeof(str));
18874
18875 if (rettv_list_alloc(rettv) != FAIL)
18876 {
18877 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18878 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18879 && curwin->w_p_cole > 0)
18880 {
18881 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18882 syntax_flags = get_syntax_info(&matchid);
18883
18884 /* get the conceal character */
18885 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18886 {
18887 cchar = syn_get_sub_char();
18888 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18889 cchar = lcs_conceal;
18890 if (cchar != NUL)
18891 {
18892# ifdef FEAT_MBYTE
18893 if (has_mbyte)
18894 (*mb_char2bytes)(cchar, str);
18895 else
18896# endif
18897 str[0] = cchar;
18898 }
18899 }
18900 }
18901
18902 list_append_number(rettv->vval.v_list,
18903 (syntax_flags & HL_CONCEAL) != 0);
18904 /* -1 to auto-determine strlen */
18905 list_append_string(rettv->vval.v_list, str, -1);
18906 list_append_number(rettv->vval.v_list, matchid);
18907 }
18908#endif
18909}
18910
18911/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018912 * "synstack(lnum, col)" function
18913 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018915f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018916{
18917#ifdef FEAT_SYN_HL
18918 long lnum;
18919 long col;
18920 int i;
18921 int id;
18922#endif
18923
18924 rettv->v_type = VAR_LIST;
18925 rettv->vval.v_list = NULL;
18926
18927#ifdef FEAT_SYN_HL
18928 lnum = get_tv_lnum(argvars); /* -1 on type error */
18929 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18930
18931 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018932 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018933 && rettv_list_alloc(rettv) != FAIL)
18934 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018935 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018936 for (i = 0; ; ++i)
18937 {
18938 id = syn_get_stack_item(i);
18939 if (id < 0)
18940 break;
18941 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18942 break;
18943 }
18944 }
18945#endif
18946}
18947
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018949get_cmd_output_as_rettv(
18950 typval_T *argvars,
18951 typval_T *rettv,
18952 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018954 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018956 char_u *infile = NULL;
18957 char_u buf[NUMBUFLEN];
18958 int err = FALSE;
18959 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018960 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018961 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018963 rettv->v_type = VAR_STRING;
18964 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018965 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018966 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018969 {
18970 /*
18971 * Write the string to a temp file, to be used for input of the shell
18972 * command.
18973 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018974 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018975 {
18976 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018977 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018978 }
18979
18980 fd = mch_fopen((char *)infile, WRITEBIN);
18981 if (fd == NULL)
18982 {
18983 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018984 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018985 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018986 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018987 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018988 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18989 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018990 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018991 else
18992 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018993 size_t len;
18994
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018995 p = get_tv_string_buf_chk(&argvars[1], buf);
18996 if (p == NULL)
18997 {
18998 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018999 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019000 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019001 len = STRLEN(p);
19002 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019003 err = TRUE;
19004 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019005 if (fclose(fd) != 0)
19006 err = TRUE;
19007 if (err)
19008 {
19009 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019010 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019011 }
19012 }
19013
Bram Moolenaar52a72462014-08-29 15:53:52 +020019014 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19015 * echoes typeahead, that messes up the display. */
19016 if (!msg_silent)
19017 flags += SHELL_COOKED;
19018
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019019 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019021 int len;
19022 listitem_T *li;
19023 char_u *s = NULL;
19024 char_u *start;
19025 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019026 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027
Bram Moolenaar52a72462014-08-29 15:53:52 +020019028 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019029 if (res == NULL)
19030 goto errret;
19031
19032 list = list_alloc();
19033 if (list == NULL)
19034 goto errret;
19035
19036 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019038 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019039 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019040 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019041 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019042
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019043 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019044 if (s == NULL)
19045 goto errret;
19046
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019047 for (p = s; start < end; ++p, ++start)
19048 *p = *start == NUL ? NL : *start;
19049 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019050
19051 li = listitem_alloc();
19052 if (li == NULL)
19053 {
19054 vim_free(s);
19055 goto errret;
19056 }
19057 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019058 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059 li->li_tv.vval.v_string = s;
19060 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019062
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019063 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019064 rettv->v_type = VAR_LIST;
19065 rettv->vval.v_list = list;
19066 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019068 else
19069 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019070 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019071#ifdef USE_CR
19072 /* translate <CR> into <NL> */
19073 if (res != NULL)
19074 {
19075 char_u *s;
19076
19077 for (s = res; *s; ++s)
19078 {
19079 if (*s == CAR)
19080 *s = NL;
19081 }
19082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083#else
19084# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019085 /* translate <CR><NL> into <NL> */
19086 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019088 char_u *s, *d;
19089
19090 d = res;
19091 for (s = res; *s; ++s)
19092 {
19093 if (s[0] == CAR && s[1] == NL)
19094 ++s;
19095 *d++ = *s;
19096 }
19097 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099# endif
19100#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019101 rettv->vval.v_string = res;
19102 res = NULL;
19103 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019104
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019105errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019106 if (infile != NULL)
19107 {
19108 mch_remove(infile);
19109 vim_free(infile);
19110 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019111 if (res != NULL)
19112 vim_free(res);
19113 if (list != NULL)
19114 list_free(list, TRUE);
19115}
19116
19117/*
19118 * "system()" function
19119 */
19120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019121f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019122{
19123 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19124}
19125
19126/*
19127 * "systemlist()" function
19128 */
19129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019130f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019131{
19132 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133}
19134
19135/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019136 * "tabpagebuflist()" function
19137 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019139f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019140{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019141#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019142 tabpage_T *tp;
19143 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019144
19145 if (argvars[0].v_type == VAR_UNKNOWN)
19146 wp = firstwin;
19147 else
19148 {
19149 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19150 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019151 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019152 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019153 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019154 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019155 for (; wp != NULL; wp = wp->w_next)
19156 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019157 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019158 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019159 }
19160#endif
19161}
19162
19163
19164/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019165 * "tabpagenr()" function
19166 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019168f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019169{
19170 int nr = 1;
19171#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019172 char_u *arg;
19173
19174 if (argvars[0].v_type != VAR_UNKNOWN)
19175 {
19176 arg = get_tv_string_chk(&argvars[0]);
19177 nr = 0;
19178 if (arg != NULL)
19179 {
19180 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019181 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019182 else
19183 EMSG2(_(e_invexpr2), arg);
19184 }
19185 }
19186 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019187 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019188#endif
19189 rettv->vval.v_number = nr;
19190}
19191
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019192
19193#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019194static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019195
19196/*
19197 * Common code for tabpagewinnr() and winnr().
19198 */
19199 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019200get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019201{
19202 win_T *twin;
19203 int nr = 1;
19204 win_T *wp;
19205 char_u *arg;
19206
19207 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19208 if (argvar->v_type != VAR_UNKNOWN)
19209 {
19210 arg = get_tv_string_chk(argvar);
19211 if (arg == NULL)
19212 nr = 0; /* type error; errmsg already given */
19213 else if (STRCMP(arg, "$") == 0)
19214 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19215 else if (STRCMP(arg, "#") == 0)
19216 {
19217 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19218 if (twin == NULL)
19219 nr = 0;
19220 }
19221 else
19222 {
19223 EMSG2(_(e_invexpr2), arg);
19224 nr = 0;
19225 }
19226 }
19227
19228 if (nr > 0)
19229 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19230 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019231 {
19232 if (wp == NULL)
19233 {
19234 /* didn't find it in this tabpage */
19235 nr = 0;
19236 break;
19237 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019238 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019239 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019240 return nr;
19241}
19242#endif
19243
19244/*
19245 * "tabpagewinnr()" function
19246 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019249{
19250 int nr = 1;
19251#ifdef FEAT_WINDOWS
19252 tabpage_T *tp;
19253
19254 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19255 if (tp == NULL)
19256 nr = 0;
19257 else
19258 nr = get_winnr(tp, &argvars[1]);
19259#endif
19260 rettv->vval.v_number = nr;
19261}
19262
19263
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019264/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019265 * "tagfiles()" function
19266 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019268f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019269{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019270 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019271 tagname_T tn;
19272 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019273
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019274 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019275 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019276 fname = alloc(MAXPATHL);
19277 if (fname == NULL)
19278 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019279
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019280 for (first = TRUE; ; first = FALSE)
19281 if (get_tagfname(&tn, first, fname) == FAIL
19282 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019283 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019284 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019285 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019286}
19287
19288/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019289 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019290 */
19291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019292f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019293{
19294 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019295
19296 tag_pattern = get_tv_string(&argvars[0]);
19297
19298 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019299 if (*tag_pattern == NUL)
19300 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019302 if (rettv_list_alloc(rettv) == OK)
19303 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019304}
19305
19306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 * "tempname()" function
19308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019310f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311{
19312 static int x = 'A';
19313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019314 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019315 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316
19317 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19318 * names. Skip 'I' and 'O', they are used for shell redirection. */
19319 do
19320 {
19321 if (x == 'Z')
19322 x = '0';
19323 else if (x == '9')
19324 x = 'A';
19325 else
19326 {
19327#ifdef EBCDIC
19328 if (x == 'I')
19329 x = 'J';
19330 else if (x == 'R')
19331 x = 'S';
19332 else
19333#endif
19334 ++x;
19335 }
19336 } while (x == 'I' || x == 'O');
19337}
19338
19339/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019340 * "test(list)" function: Just checking the walls...
19341 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019343f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019344{
19345 /* Used for unit testing. Change the code below to your liking. */
19346#if 0
19347 listitem_T *li;
19348 list_T *l;
19349 char_u *bad, *good;
19350
19351 if (argvars[0].v_type != VAR_LIST)
19352 return;
19353 l = argvars[0].vval.v_list;
19354 if (l == NULL)
19355 return;
19356 li = l->lv_first;
19357 if (li == NULL)
19358 return;
19359 bad = get_tv_string(&li->li_tv);
19360 li = li->li_next;
19361 if (li == NULL)
19362 return;
19363 good = get_tv_string(&li->li_tv);
19364 rettv->vval.v_number = test_edit_score(bad, good);
19365#endif
19366}
19367
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019368#ifdef FEAT_FLOAT
19369/*
19370 * "tan()" function
19371 */
19372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019373f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019374{
19375 float_T f;
19376
19377 rettv->v_type = VAR_FLOAT;
19378 if (get_float_arg(argvars, &f) == OK)
19379 rettv->vval.v_float = tan(f);
19380 else
19381 rettv->vval.v_float = 0.0;
19382}
19383
19384/*
19385 * "tanh()" function
19386 */
19387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019388f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019389{
19390 float_T f;
19391
19392 rettv->v_type = VAR_FLOAT;
19393 if (get_float_arg(argvars, &f) == OK)
19394 rettv->vval.v_float = tanh(f);
19395 else
19396 rettv->vval.v_float = 0.0;
19397}
19398#endif
19399
Bram Moolenaard52d9742005-08-21 22:20:28 +000019400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 * "tolower(string)" function
19402 */
19403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019404f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405{
19406 char_u *p;
19407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019408 p = vim_strsave(get_tv_string(&argvars[0]));
19409 rettv->v_type = VAR_STRING;
19410 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411
19412 if (p != NULL)
19413 while (*p != NUL)
19414 {
19415#ifdef FEAT_MBYTE
19416 int l;
19417
19418 if (enc_utf8)
19419 {
19420 int c, lc;
19421
19422 c = utf_ptr2char(p);
19423 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019424 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 /* TODO: reallocate string when byte count changes. */
19426 if (utf_char2len(lc) == l)
19427 utf_char2bytes(lc, p);
19428 p += l;
19429 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019430 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 p += l; /* skip multi-byte character */
19432 else
19433#endif
19434 {
19435 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19436 ++p;
19437 }
19438 }
19439}
19440
19441/*
19442 * "toupper(string)" function
19443 */
19444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019445f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019447 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019448 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449}
19450
19451/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019452 * "tr(string, fromstr, tostr)" function
19453 */
19454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019455f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019456{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019457 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019458 char_u *fromstr;
19459 char_u *tostr;
19460 char_u *p;
19461#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019462 int inlen;
19463 int fromlen;
19464 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019465 int idx;
19466 char_u *cpstr;
19467 int cplen;
19468 int first = TRUE;
19469#endif
19470 char_u buf[NUMBUFLEN];
19471 char_u buf2[NUMBUFLEN];
19472 garray_T ga;
19473
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019474 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019475 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19476 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019477
19478 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019479 rettv->v_type = VAR_STRING;
19480 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019481 if (fromstr == NULL || tostr == NULL)
19482 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019483 ga_init2(&ga, (int)sizeof(char), 80);
19484
19485#ifdef FEAT_MBYTE
19486 if (!has_mbyte)
19487#endif
19488 /* not multi-byte: fromstr and tostr must be the same length */
19489 if (STRLEN(fromstr) != STRLEN(tostr))
19490 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019491#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019492error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019493#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019494 EMSG2(_(e_invarg2), fromstr);
19495 ga_clear(&ga);
19496 return;
19497 }
19498
19499 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019500 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019501 {
19502#ifdef FEAT_MBYTE
19503 if (has_mbyte)
19504 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019505 inlen = (*mb_ptr2len)(in_str);
19506 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019507 cplen = inlen;
19508 idx = 0;
19509 for (p = fromstr; *p != NUL; p += fromlen)
19510 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019511 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019512 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019513 {
19514 for (p = tostr; *p != NUL; p += tolen)
19515 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019516 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019517 if (idx-- == 0)
19518 {
19519 cplen = tolen;
19520 cpstr = p;
19521 break;
19522 }
19523 }
19524 if (*p == NUL) /* tostr is shorter than fromstr */
19525 goto error;
19526 break;
19527 }
19528 ++idx;
19529 }
19530
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019531 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019532 {
19533 /* Check that fromstr and tostr have the same number of
19534 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019535 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019536 first = FALSE;
19537 for (p = tostr; *p != NUL; p += tolen)
19538 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019539 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019540 --idx;
19541 }
19542 if (idx != 0)
19543 goto error;
19544 }
19545
Bram Moolenaarcde88542015-08-11 19:14:00 +020019546 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019547 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019548 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019549
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019550 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019551 }
19552 else
19553#endif
19554 {
19555 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019556 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019557 if (p != NULL)
19558 ga_append(&ga, tostr[p - fromstr]);
19559 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019560 ga_append(&ga, *in_str);
19561 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019562 }
19563 }
19564
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019565 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019566 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019567 ga_append(&ga, NUL);
19568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019569 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019570}
19571
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019572#ifdef FEAT_FLOAT
19573/*
19574 * "trunc({float})" function
19575 */
19576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019577f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019578{
19579 float_T f;
19580
19581 rettv->v_type = VAR_FLOAT;
19582 if (get_float_arg(argvars, &f) == OK)
19583 /* trunc() is not in C90, use floor() or ceil() instead. */
19584 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19585 else
19586 rettv->vval.v_float = 0.0;
19587}
19588#endif
19589
Bram Moolenaar8299df92004-07-10 09:47:34 +000019590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591 * "type(expr)" function
19592 */
19593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019594f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019596 int n;
19597
19598 switch (argvars[0].v_type)
19599 {
19600 case VAR_NUMBER: n = 0; break;
19601 case VAR_STRING: n = 1; break;
19602 case VAR_FUNC: n = 2; break;
19603 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019604 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019605#ifdef FEAT_FLOAT
19606 case VAR_FLOAT: n = 5; break;
19607#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019608 case VAR_SPECIAL:
19609 if (argvars[0].vval.v_number == VVAL_FALSE
19610 || argvars[0].vval.v_number == VVAL_TRUE)
19611 n = 6;
19612 else
19613 n = 7;
19614 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019615 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19616 }
19617 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618}
19619
19620/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019621 * "undofile(name)" function
19622 */
19623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019624f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019625{
19626 rettv->v_type = VAR_STRING;
19627#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019628 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019629 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019630
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019631 if (*fname == NUL)
19632 {
19633 /* If there is no file name there will be no undo file. */
19634 rettv->vval.v_string = NULL;
19635 }
19636 else
19637 {
19638 char_u *ffname = FullName_save(fname, FALSE);
19639
19640 if (ffname != NULL)
19641 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19642 vim_free(ffname);
19643 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019644 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019645#else
19646 rettv->vval.v_string = NULL;
19647#endif
19648}
19649
19650/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019651 * "undotree()" function
19652 */
19653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019654f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019655{
19656 if (rettv_dict_alloc(rettv) == OK)
19657 {
19658 dict_T *dict = rettv->vval.v_dict;
19659 list_T *list;
19660
Bram Moolenaar730cde92010-06-27 05:18:54 +020019661 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019662 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019663 dict_add_nr_str(dict, "save_last",
19664 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019665 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19666 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019667 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019668
19669 list = list_alloc();
19670 if (list != NULL)
19671 {
19672 u_eval_tree(curbuf->b_u_oldhead, list);
19673 dict_add_list(dict, "entries", list);
19674 }
19675 }
19676}
19677
19678/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019679 * "values(dict)" function
19680 */
19681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019682f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019683{
19684 dict_list(argvars, rettv, 1);
19685}
19686
19687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 * "virtcol(string)" function
19689 */
19690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019691f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692{
19693 colnr_T vcol = 0;
19694 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019695 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019697 fp = var2fpos(&argvars[0], FALSE, &fnum);
19698 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19699 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 {
19701 getvvcol(curwin, fp, NULL, NULL, &vcol);
19702 ++vcol;
19703 }
19704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019705 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706}
19707
19708/*
19709 * "visualmode()" function
19710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019712f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 char_u str[2];
19715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019716 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 str[0] = curbuf->b_visual_mode_eval;
19718 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720
19721 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019722 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724}
19725
19726/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019727 * "wildmenumode()" function
19728 */
19729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019730f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019731{
19732#ifdef FEAT_WILDMENU
19733 if (wild_menu_showing)
19734 rettv->vval.v_number = 1;
19735#endif
19736}
19737
19738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 * "winbufnr(nr)" function
19740 */
19741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019742f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743{
19744 win_T *wp;
19745
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019746 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019748 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019750 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751}
19752
19753/*
19754 * "wincol()" function
19755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019757f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758{
19759 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761}
19762
19763/*
19764 * "winheight(nr)" function
19765 */
19766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019767f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768{
19769 win_T *wp;
19770
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019771 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019775 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776}
19777
19778/*
19779 * "winline()" function
19780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019782f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783{
19784 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786}
19787
19788/*
19789 * "winnr()" function
19790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019792f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793{
19794 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019795
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019797 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800}
19801
19802/*
19803 * "winrestcmd()" function
19804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019806f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807{
19808#ifdef FEAT_WINDOWS
19809 win_T *wp;
19810 int winnr = 1;
19811 garray_T ga;
19812 char_u buf[50];
19813
19814 ga_init2(&ga, (int)sizeof(char), 70);
19815 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19816 {
19817 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19818 ga_concat(&ga, buf);
19819# ifdef FEAT_VERTSPLIT
19820 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19821 ga_concat(&ga, buf);
19822# endif
19823 ++winnr;
19824 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019825 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019829 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832}
19833
19834/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019835 * "winrestview()" function
19836 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019838f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019839{
19840 dict_T *dict;
19841
19842 if (argvars[0].v_type != VAR_DICT
19843 || (dict = argvars[0].vval.v_dict) == NULL)
19844 EMSG(_(e_invarg));
19845 else
19846 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019847 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19848 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19849 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19850 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019851#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019852 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19853 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019854#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019855 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19856 {
19857 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19858 curwin->w_set_curswant = FALSE;
19859 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019860
Bram Moolenaar82c25852014-05-28 16:47:16 +020019861 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19862 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019863#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019864 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19865 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019866#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019867 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19868 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19869 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19870 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019871
19872 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019873 win_new_height(curwin, curwin->w_height);
19874# ifdef FEAT_VERTSPLIT
19875 win_new_width(curwin, W_WIDTH(curwin));
19876# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019877 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019878
Bram Moolenaarb851a962014-10-31 15:45:52 +010019879 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019880 curwin->w_topline = 1;
19881 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19882 curwin->w_topline = curbuf->b_ml.ml_line_count;
19883#ifdef FEAT_DIFF
19884 check_topfill(curwin, TRUE);
19885#endif
19886 }
19887}
19888
19889/*
19890 * "winsaveview()" function
19891 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019893f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019894{
19895 dict_T *dict;
19896
Bram Moolenaara800b422010-06-27 01:15:55 +020019897 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019898 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019899 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019900
19901 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19902 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19903#ifdef FEAT_VIRTUALEDIT
19904 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19905#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019906 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019907 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19908
19909 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19910#ifdef FEAT_DIFF
19911 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19912#endif
19913 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19914 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19915}
19916
19917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 * "winwidth(nr)" function
19919 */
19920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019921f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922{
19923 win_T *wp;
19924
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019925 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 else
19929#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933#endif
19934}
19935
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019937 * "wordcount()" function
19938 */
19939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019940f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019941{
19942 if (rettv_dict_alloc(rettv) == FAIL)
19943 return;
19944 cursor_pos_info(rettv->vval.v_dict);
19945}
19946
19947/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019948 * Write list of strings to file
19949 */
19950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019952{
19953 listitem_T *li;
19954 int c;
19955 int ret = OK;
19956 char_u *s;
19957
19958 for (li = list->lv_first; li != NULL; li = li->li_next)
19959 {
19960 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19961 {
19962 if (*s == '\n')
19963 c = putc(NUL, fd);
19964 else
19965 c = putc(*s, fd);
19966 if (c == EOF)
19967 {
19968 ret = FAIL;
19969 break;
19970 }
19971 }
19972 if (!binary || li->li_next != NULL)
19973 if (putc('\n', fd) == EOF)
19974 {
19975 ret = FAIL;
19976 break;
19977 }
19978 if (ret == FAIL)
19979 {
19980 EMSG(_(e_write));
19981 break;
19982 }
19983 }
19984 return ret;
19985}
19986
19987/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019988 * "writefile()" function
19989 */
19990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019991f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019992{
19993 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019994 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019995 char_u *fname;
19996 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019997 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019998
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019999 if (check_restricted() || check_secure())
20000 return;
20001
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020002 if (argvars[0].v_type != VAR_LIST)
20003 {
20004 EMSG2(_(e_listarg), "writefile()");
20005 return;
20006 }
20007 if (argvars[0].vval.v_list == NULL)
20008 return;
20009
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020010 if (argvars[2].v_type != VAR_UNKNOWN)
20011 {
20012 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20013 binary = TRUE;
20014 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20015 append = TRUE;
20016 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020017
20018 /* Always open the file in binary mode, library functions have a mind of
20019 * their own about CR-LF conversion. */
20020 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020021 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20022 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020023 {
20024 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20025 ret = -1;
20026 }
20027 else
20028 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020029 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20030 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020031 fclose(fd);
20032 }
20033
20034 rettv->vval.v_number = ret;
20035}
20036
20037/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020038 * "xor(expr, expr)" function
20039 */
20040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020041f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020042{
20043 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20044 ^ get_tv_number_chk(&argvars[1], NULL);
20045}
20046
20047
20048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020050 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 */
20052 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020053var2fpos(
20054 typval_T *varp,
20055 int dollar_lnum, /* TRUE when $ is last line */
20056 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020058 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020060 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061
Bram Moolenaara5525202006-03-02 22:52:09 +000020062 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020063 if (varp->v_type == VAR_LIST)
20064 {
20065 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020066 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020067 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020068 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020069
20070 l = varp->vval.v_list;
20071 if (l == NULL)
20072 return NULL;
20073
20074 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020075 pos.lnum = list_find_nr(l, 0L, &error);
20076 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020077 return NULL; /* invalid line number */
20078
20079 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020080 pos.col = list_find_nr(l, 1L, &error);
20081 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020082 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020083 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020084
20085 /* We accept "$" for the column number: last column. */
20086 li = list_find(l, 1L);
20087 if (li != NULL && li->li_tv.v_type == VAR_STRING
20088 && li->li_tv.vval.v_string != NULL
20089 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20090 pos.col = len + 1;
20091
Bram Moolenaara5525202006-03-02 22:52:09 +000020092 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020093 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020094 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020095 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020096
Bram Moolenaara5525202006-03-02 22:52:09 +000020097#ifdef FEAT_VIRTUALEDIT
20098 /* Get the virtual offset. Defaults to zero. */
20099 pos.coladd = list_find_nr(l, 2L, &error);
20100 if (error)
20101 pos.coladd = 0;
20102#endif
20103
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020104 return &pos;
20105 }
20106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020107 name = get_tv_string_chk(varp);
20108 if (name == NULL)
20109 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020110 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020112 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20113 {
20114 if (VIsual_active)
20115 return &VIsual;
20116 return &curwin->w_cursor;
20117 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020118 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020120 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20122 return NULL;
20123 return pp;
20124 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020125
20126#ifdef FEAT_VIRTUALEDIT
20127 pos.coladd = 0;
20128#endif
20129
Bram Moolenaar477933c2007-07-17 14:32:23 +000020130 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020131 {
20132 pos.col = 0;
20133 if (name[1] == '0') /* "w0": first visible line */
20134 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020135 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020136 pos.lnum = curwin->w_topline;
20137 return &pos;
20138 }
20139 else if (name[1] == '$') /* "w$": last visible line */
20140 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020141 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020142 pos.lnum = curwin->w_botline - 1;
20143 return &pos;
20144 }
20145 }
20146 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020148 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 {
20150 pos.lnum = curbuf->b_ml.ml_line_count;
20151 pos.col = 0;
20152 }
20153 else
20154 {
20155 pos.lnum = curwin->w_cursor.lnum;
20156 pos.col = (colnr_T)STRLEN(ml_get_curline());
20157 }
20158 return &pos;
20159 }
20160 return NULL;
20161}
20162
20163/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020164 * Convert list in "arg" into a position and optional file number.
20165 * When "fnump" is NULL there is no file number, only 3 items.
20166 * Note that the column is passed on as-is, the caller may want to decrement
20167 * it to use 1 for the first column.
20168 * Return FAIL when conversion is not possible, doesn't check the position for
20169 * validity.
20170 */
20171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020172list2fpos(
20173 typval_T *arg,
20174 pos_T *posp,
20175 int *fnump,
20176 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020177{
20178 list_T *l = arg->vval.v_list;
20179 long i = 0;
20180 long n;
20181
Bram Moolenaar493c1782014-05-28 14:34:46 +020020182 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20183 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020184 if (arg->v_type != VAR_LIST
20185 || l == NULL
20186 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020187 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020188 return FAIL;
20189
20190 if (fnump != NULL)
20191 {
20192 n = list_find_nr(l, i++, NULL); /* fnum */
20193 if (n < 0)
20194 return FAIL;
20195 if (n == 0)
20196 n = curbuf->b_fnum; /* current buffer */
20197 *fnump = n;
20198 }
20199
20200 n = list_find_nr(l, i++, NULL); /* lnum */
20201 if (n < 0)
20202 return FAIL;
20203 posp->lnum = n;
20204
20205 n = list_find_nr(l, i++, NULL); /* col */
20206 if (n < 0)
20207 return FAIL;
20208 posp->col = n;
20209
20210#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020211 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020212 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020213 posp->coladd = 0;
20214 else
20215 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020216#endif
20217
Bram Moolenaar493c1782014-05-28 14:34:46 +020020218 if (curswantp != NULL)
20219 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20220
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020221 return OK;
20222}
20223
20224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 * Get the length of an environment variable name.
20226 * Advance "arg" to the first character after the name.
20227 * Return 0 for error.
20228 */
20229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020230get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231{
20232 char_u *p;
20233 int len;
20234
20235 for (p = *arg; vim_isIDc(*p); ++p)
20236 ;
20237 if (p == *arg) /* no name found */
20238 return 0;
20239
20240 len = (int)(p - *arg);
20241 *arg = p;
20242 return len;
20243}
20244
20245/*
20246 * Get the length of the name of a function or internal variable.
20247 * "arg" is advanced to the first non-white character after the name.
20248 * Return 0 if something is wrong.
20249 */
20250 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252{
20253 char_u *p;
20254 int len;
20255
20256 /* Find the end of the name. */
20257 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020258 {
20259 if (*p == ':')
20260 {
20261 /* "s:" is start of "s:var", but "n:" is not and can be used in
20262 * slice "[n:]". Also "xx:" is not a namespace. */
20263 len = (int)(p - *arg);
20264 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20265 || len > 1)
20266 break;
20267 }
20268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 if (p == *arg) /* no name found */
20270 return 0;
20271
20272 len = (int)(p - *arg);
20273 *arg = skipwhite(p);
20274
20275 return len;
20276}
20277
20278/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020279 * Get the length of the name of a variable or function.
20280 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020282 * Return -1 if curly braces expansion failed.
20283 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 * If the name contains 'magic' {}'s, expand them and return the
20285 * expanded name in an allocated string via 'alias' - caller must free.
20286 */
20287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020288get_name_len(
20289 char_u **arg,
20290 char_u **alias,
20291 int evaluate,
20292 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293{
20294 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 char_u *p;
20296 char_u *expr_start;
20297 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298
20299 *alias = NULL; /* default to no alias */
20300
20301 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20302 && (*arg)[2] == (int)KE_SNR)
20303 {
20304 /* hard coded <SNR>, already translated */
20305 *arg += 3;
20306 return get_id_len(arg) + 3;
20307 }
20308 len = eval_fname_script(*arg);
20309 if (len > 0)
20310 {
20311 /* literal "<SID>", "s:" or "<SNR>" */
20312 *arg += len;
20313 }
20314
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020316 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020318 p = find_name_end(*arg, &expr_start, &expr_end,
20319 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 if (expr_start != NULL)
20321 {
20322 char_u *temp_string;
20323
20324 if (!evaluate)
20325 {
20326 len += (int)(p - *arg);
20327 *arg = skipwhite(p);
20328 return len;
20329 }
20330
20331 /*
20332 * Include any <SID> etc in the expanded string:
20333 * Thus the -len here.
20334 */
20335 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20336 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020337 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 *alias = temp_string;
20339 *arg = skipwhite(p);
20340 return (int)STRLEN(temp_string);
20341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342
20343 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020344 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 EMSG2(_(e_invexpr2), *arg);
20346
20347 return len;
20348}
20349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350/*
20351 * Find the end of a variable or function name, taking care of magic braces.
20352 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20353 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020354 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020355 * Return a pointer to just after the name. Equal to "arg" if there is no
20356 * valid name.
20357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020359find_name_end(
20360 char_u *arg,
20361 char_u **expr_start,
20362 char_u **expr_end,
20363 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020365 int mb_nest = 0;
20366 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020368 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020372 *expr_start = NULL;
20373 *expr_end = NULL;
20374 }
20375
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020376 /* Quick check for valid starting character. */
20377 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20378 return arg;
20379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380 for (p = arg; *p != NUL
20381 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020382 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020383 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020384 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020385 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020386 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020387 if (*p == '\'')
20388 {
20389 /* skip over 'string' to avoid counting [ and ] inside it. */
20390 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20391 ;
20392 if (*p == NUL)
20393 break;
20394 }
20395 else if (*p == '"')
20396 {
20397 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20398 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20399 if (*p == '\\' && p[1] != NUL)
20400 ++p;
20401 if (*p == NUL)
20402 break;
20403 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020404 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20405 {
20406 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020407 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020408 len = (int)(p - arg);
20409 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020410 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020411 break;
20412 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020414 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020416 if (*p == '[')
20417 ++br_nest;
20418 else if (*p == ']')
20419 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020422 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 if (*p == '{')
20425 {
20426 mb_nest++;
20427 if (expr_start != NULL && *expr_start == NULL)
20428 *expr_start = p;
20429 }
20430 else if (*p == '}')
20431 {
20432 mb_nest--;
20433 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20434 *expr_end = p;
20435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 }
20438
20439 return p;
20440}
20441
20442/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020443 * Expands out the 'magic' {}'s in a variable/function name.
20444 * Note that this can call itself recursively, to deal with
20445 * constructs like foo{bar}{baz}{bam}
20446 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20447 * "in_start" ^
20448 * "expr_start" ^
20449 * "expr_end" ^
20450 * "in_end" ^
20451 *
20452 * Returns a new allocated string, which the caller must free.
20453 * Returns NULL for failure.
20454 */
20455 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020456make_expanded_name(
20457 char_u *in_start,
20458 char_u *expr_start,
20459 char_u *expr_end,
20460 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020461{
20462 char_u c1;
20463 char_u *retval = NULL;
20464 char_u *temp_result;
20465 char_u *nextcmd = NULL;
20466
20467 if (expr_end == NULL || in_end == NULL)
20468 return NULL;
20469 *expr_start = NUL;
20470 *expr_end = NUL;
20471 c1 = *in_end;
20472 *in_end = NUL;
20473
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020474 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020475 if (temp_result != NULL && nextcmd == NULL)
20476 {
20477 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20478 + (in_end - expr_end) + 1));
20479 if (retval != NULL)
20480 {
20481 STRCPY(retval, in_start);
20482 STRCAT(retval, temp_result);
20483 STRCAT(retval, expr_end + 1);
20484 }
20485 }
20486 vim_free(temp_result);
20487
20488 *in_end = c1; /* put char back for error messages */
20489 *expr_start = '{';
20490 *expr_end = '}';
20491
20492 if (retval != NULL)
20493 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020494 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020495 if (expr_start != NULL)
20496 {
20497 /* Further expansion! */
20498 temp_result = make_expanded_name(retval, expr_start,
20499 expr_end, temp_result);
20500 vim_free(retval);
20501 retval = temp_result;
20502 }
20503 }
20504
20505 return retval;
20506}
20507
20508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020510 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 */
20512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020513eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020515 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20516}
20517
20518/*
20519 * Return TRUE if character "c" can be used as the first character in a
20520 * variable or function name (excluding '{' and '}').
20521 */
20522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020523eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020524{
20525 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526}
20527
20528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 * Set number v: variable to "val".
20530 */
20531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020532set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020534 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535}
20536
20537/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020538 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 */
20540 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020541get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020543 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544}
20545
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020546/*
20547 * Get string v: variable value. Uses a static buffer, can only be used once.
20548 */
20549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020550get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020551{
20552 return get_tv_string(&vimvars[idx].vv_tv);
20553}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020554
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020556 * Get List v: variable value. Caller must take care of reference count when
20557 * needed.
20558 */
20559 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020560get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020561{
20562 return vimvars[idx].vv_list;
20563}
20564
20565/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020566 * Set v:char to character "c".
20567 */
20568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020569set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020570{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020571 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020572
20573#ifdef FEAT_MBYTE
20574 if (has_mbyte)
20575 buf[(*mb_char2bytes)(c, buf)] = NUL;
20576 else
20577#endif
20578 {
20579 buf[0] = c;
20580 buf[1] = NUL;
20581 }
20582 set_vim_var_string(VV_CHAR, buf, -1);
20583}
20584
20585/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020586 * Set v:count to "count" and v:count1 to "count1".
20587 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 */
20589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020590set_vcount(
20591 long count,
20592 long count1,
20593 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020595 if (set_prevcount)
20596 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020597 vimvars[VV_COUNT].vv_nr = count;
20598 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599}
20600
20601/*
20602 * Set string v: variable to a copy of "val".
20603 */
20604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020605set_vim_var_string(
20606 int idx,
20607 char_u *val,
20608 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609{
Bram Moolenaara542c682016-01-31 16:28:04 +010020610 clear_tv(&vimvars[idx].vv_di.di_tv);
20611 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020613 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020615 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020617 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618}
20619
20620/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020621 * Set List v: variable to "val".
20622 */
20623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020624set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020625{
Bram Moolenaara542c682016-01-31 16:28:04 +010020626 clear_tv(&vimvars[idx].vv_di.di_tv);
20627 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020628 vimvars[idx].vv_list = val;
20629 if (val != NULL)
20630 ++val->lv_refcount;
20631}
20632
20633/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020634 * Set Dictionary v: variable to "val".
20635 */
20636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020637set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020638{
20639 int todo;
20640 hashitem_T *hi;
20641
Bram Moolenaara542c682016-01-31 16:28:04 +010020642 clear_tv(&vimvars[idx].vv_di.di_tv);
20643 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020644 vimvars[idx].vv_dict = val;
20645 if (val != NULL)
20646 {
20647 ++val->dv_refcount;
20648
20649 /* Set readonly */
20650 todo = (int)val->dv_hashtab.ht_used;
20651 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20652 {
20653 if (HASHITEM_EMPTY(hi))
20654 continue;
20655 --todo;
20656 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20657 }
20658 }
20659}
20660
20661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 * Set v:register if needed.
20663 */
20664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020665set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666{
20667 char_u regname;
20668
20669 if (c == 0 || c == ' ')
20670 regname = '"';
20671 else
20672 regname = c;
20673 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020674 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 set_vim_var_string(VV_REG, &regname, 1);
20676}
20677
20678/*
20679 * Get or set v:exception. If "oldval" == NULL, return the current value.
20680 * Otherwise, restore the value to "oldval" and return NULL.
20681 * Must always be called in pairs to save and restore v:exception! Does not
20682 * take care of memory allocations.
20683 */
20684 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020685v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686{
20687 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020688 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689
Bram Moolenaare9a41262005-01-15 22:18:47 +000020690 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691 return NULL;
20692}
20693
20694/*
20695 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20696 * Otherwise, restore the value to "oldval" and return NULL.
20697 * Must always be called in pairs to save and restore v:throwpoint! Does not
20698 * take care of memory allocations.
20699 */
20700 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020701v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702{
20703 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020704 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705
Bram Moolenaare9a41262005-01-15 22:18:47 +000020706 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 return NULL;
20708}
20709
20710#if defined(FEAT_AUTOCMD) || defined(PROTO)
20711/*
20712 * Set v:cmdarg.
20713 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20714 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20715 * Must always be called in pairs!
20716 */
20717 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020718set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719{
20720 char_u *oldval;
20721 char_u *newval;
20722 unsigned len;
20723
Bram Moolenaare9a41262005-01-15 22:18:47 +000020724 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020725 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020727 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020728 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020729 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 }
20731
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020732 if (eap->force_bin == FORCE_BIN)
20733 len = 6;
20734 else if (eap->force_bin == FORCE_NOBIN)
20735 len = 8;
20736 else
20737 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020738
20739 if (eap->read_edit)
20740 len += 7;
20741
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020742 if (eap->force_ff != 0)
20743 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20744# ifdef FEAT_MBYTE
20745 if (eap->force_enc != 0)
20746 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020747 if (eap->bad_char != 0)
20748 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020749# endif
20750
20751 newval = alloc(len + 1);
20752 if (newval == NULL)
20753 return NULL;
20754
20755 if (eap->force_bin == FORCE_BIN)
20756 sprintf((char *)newval, " ++bin");
20757 else if (eap->force_bin == FORCE_NOBIN)
20758 sprintf((char *)newval, " ++nobin");
20759 else
20760 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020761
20762 if (eap->read_edit)
20763 STRCAT(newval, " ++edit");
20764
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020765 if (eap->force_ff != 0)
20766 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20767 eap->cmd + eap->force_ff);
20768# ifdef FEAT_MBYTE
20769 if (eap->force_enc != 0)
20770 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20771 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020772 if (eap->bad_char == BAD_KEEP)
20773 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20774 else if (eap->bad_char == BAD_DROP)
20775 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20776 else if (eap->bad_char != 0)
20777 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020778# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020779 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020780 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781}
20782#endif
20783
20784/*
20785 * Get the value of internal variable "name".
20786 * Return OK or FAIL.
20787 */
20788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020789get_var_tv(
20790 char_u *name,
20791 int len, /* length of "name" */
20792 typval_T *rettv, /* NULL when only checking existence */
20793 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20794 int verbose, /* may give error message */
20795 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796{
20797 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020798 typval_T *tv = NULL;
20799 typval_T atv;
20800 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802
20803 /* truncate the name, so that we can use strcmp() */
20804 cc = name[len];
20805 name[len] = NUL;
20806
20807 /*
20808 * Check for "b:changedtick".
20809 */
20810 if (STRCMP(name, "b:changedtick") == 0)
20811 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020812 atv.v_type = VAR_NUMBER;
20813 atv.vval.v_number = curbuf->b_changedtick;
20814 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 }
20816
20817 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818 * Check for user-defined variables.
20819 */
20820 else
20821 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020822 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020824 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020825 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020826 if (dip != NULL)
20827 *dip = v;
20828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 }
20830
Bram Moolenaare9a41262005-01-15 22:18:47 +000020831 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020833 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020834 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 ret = FAIL;
20836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020837 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020838 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839
20840 name[len] = cc;
20841
20842 return ret;
20843}
20844
20845/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020846 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20847 * Also handle function call with Funcref variable: func(expr)
20848 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20849 */
20850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020851handle_subscript(
20852 char_u **arg,
20853 typval_T *rettv,
20854 int evaluate, /* do more than finding the end */
20855 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020856{
20857 int ret = OK;
20858 dict_T *selfdict = NULL;
20859 char_u *s;
20860 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020861 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020862
20863 while (ret == OK
20864 && (**arg == '['
20865 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020866 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020867 && !vim_iswhite(*(*arg - 1)))
20868 {
20869 if (**arg == '(')
20870 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020871 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020872 if (evaluate)
20873 {
20874 functv = *rettv;
20875 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020876
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020877 /* Invoke the function. Recursive! */
20878 s = functv.vval.v_string;
20879 }
20880 else
20881 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020882 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020883 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20884 &len, evaluate, selfdict);
20885
20886 /* Clear the funcref afterwards, so that deleting it while
20887 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020888 if (evaluate)
20889 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020890
20891 /* Stop the expression evaluation when immediately aborting on
20892 * error, or when an interrupt occurred or an exception was thrown
20893 * but not caught. */
20894 if (aborting())
20895 {
20896 if (ret == OK)
20897 clear_tv(rettv);
20898 ret = FAIL;
20899 }
20900 dict_unref(selfdict);
20901 selfdict = NULL;
20902 }
20903 else /* **arg == '[' || **arg == '.' */
20904 {
20905 dict_unref(selfdict);
20906 if (rettv->v_type == VAR_DICT)
20907 {
20908 selfdict = rettv->vval.v_dict;
20909 if (selfdict != NULL)
20910 ++selfdict->dv_refcount;
20911 }
20912 else
20913 selfdict = NULL;
20914 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20915 {
20916 clear_tv(rettv);
20917 ret = FAIL;
20918 }
20919 }
20920 }
20921 dict_unref(selfdict);
20922 return ret;
20923}
20924
20925/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020926 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020927 * value).
20928 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010020929 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020930alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020931{
Bram Moolenaar33570922005-01-25 22:26:29 +000020932 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020933}
20934
20935/*
20936 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 * The string "s" must have been allocated, it is consumed.
20938 * Return NULL for out of memory, the variable otherwise.
20939 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020940 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020941alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942{
Bram Moolenaar33570922005-01-25 22:26:29 +000020943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020945 rettv = alloc_tv();
20946 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020948 rettv->v_type = VAR_STRING;
20949 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 }
20951 else
20952 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020953 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954}
20955
20956/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020957 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020960free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961{
20962 if (varp != NULL)
20963 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020964 switch (varp->v_type)
20965 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020966 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020967 func_unref(varp->vval.v_string);
20968 /*FALLTHROUGH*/
20969 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020970 vim_free(varp->vval.v_string);
20971 break;
20972 case VAR_LIST:
20973 list_unref(varp->vval.v_list);
20974 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020975 case VAR_DICT:
20976 dict_unref(varp->vval.v_dict);
20977 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020978 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020979#ifdef FEAT_FLOAT
20980 case VAR_FLOAT:
20981#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020982 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010020983 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020984 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020985 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020986 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020987 break;
20988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 vim_free(varp);
20990 }
20991}
20992
20993/*
20994 * Free the memory for a variable value and set the value to NULL or 0.
20995 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020997clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998{
20999 if (varp != NULL)
21000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021001 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021003 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021004 func_unref(varp->vval.v_string);
21005 /*FALLTHROUGH*/
21006 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021007 vim_free(varp->vval.v_string);
21008 varp->vval.v_string = NULL;
21009 break;
21010 case VAR_LIST:
21011 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021012 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021013 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021014 case VAR_DICT:
21015 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021016 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021017 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021018 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021019 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021020 varp->vval.v_number = 0;
21021 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021022#ifdef FEAT_FLOAT
21023 case VAR_FLOAT:
21024 varp->vval.v_float = 0.0;
21025 break;
21026#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021027 case VAR_UNKNOWN:
21028 break;
21029 default:
21030 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021032 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 }
21034}
21035
21036/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021037 * Set the value of a variable to NULL without freeing items.
21038 */
21039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021040init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021041{
21042 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021043 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021044}
21045
21046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047 * Get the number value of a variable.
21048 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021049 * For incompatible types, return 0.
21050 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21051 * caller of incompatible types: it sets *denote to TRUE if "denote"
21052 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 */
21054 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021055get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021057 int error = FALSE;
21058
21059 return get_tv_number_chk(varp, &error); /* return 0L on error */
21060}
21061
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021062 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021063get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021064{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021065 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021067 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021069 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021070 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021071#ifdef FEAT_FLOAT
21072 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021073 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021074 break;
21075#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021076 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021077 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021078 break;
21079 case VAR_STRING:
21080 if (varp->vval.v_string != NULL)
21081 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021082 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021083 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021084 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021085 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021086 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021087 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021088 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021089 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021090 case VAR_SPECIAL:
21091 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21092 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021093 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021094 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021097 if (denote == NULL) /* useful for values that must be unsigned */
21098 n = -1;
21099 else
21100 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021101 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102}
21103
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021104#ifdef FEAT_FLOAT
21105 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021106get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021107{
21108 switch (varp->v_type)
21109 {
21110 case VAR_NUMBER:
21111 return (float_T)(varp->vval.v_number);
21112#ifdef FEAT_FLOAT
21113 case VAR_FLOAT:
21114 return varp->vval.v_float;
21115 break;
21116#endif
21117 case VAR_FUNC:
21118 EMSG(_("E891: Using a Funcref as a Float"));
21119 break;
21120 case VAR_STRING:
21121 EMSG(_("E892: Using a String as a Float"));
21122 break;
21123 case VAR_LIST:
21124 EMSG(_("E893: Using a List as a Float"));
21125 break;
21126 case VAR_DICT:
21127 EMSG(_("E894: Using a Dictionary as a Float"));
21128 break;
21129 default:
21130 EMSG2(_(e_intern2), "get_tv_float()");
21131 break;
21132 }
21133 return 0;
21134}
21135#endif
21136
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021138 * Get the lnum from the first argument.
21139 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021140 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 */
21142 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021143get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144{
Bram Moolenaar33570922005-01-25 22:26:29 +000021145 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 linenr_T lnum;
21147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021148 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 if (lnum == 0) /* no valid number, try using line() */
21150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021151 rettv.v_type = VAR_NUMBER;
21152 f_line(argvars, &rettv);
21153 lnum = rettv.vval.v_number;
21154 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 }
21156 return lnum;
21157}
21158
21159/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021160 * Get the lnum from the first argument.
21161 * Also accepts "$", then "buf" is used.
21162 * Returns 0 on error.
21163 */
21164 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021165get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021166{
21167 if (argvars[0].v_type == VAR_STRING
21168 && argvars[0].vval.v_string != NULL
21169 && argvars[0].vval.v_string[0] == '$'
21170 && buf != NULL)
21171 return buf->b_ml.ml_line_count;
21172 return get_tv_number_chk(&argvars[0], NULL);
21173}
21174
21175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 * Get the string value of a variable.
21177 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021178 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21179 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 * If the String variable has never been set, return an empty string.
21181 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021182 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21183 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 */
21185 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021186get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187{
21188 static char_u mybuf[NUMBUFLEN];
21189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021190 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191}
21192
21193 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021194get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021196 char_u *res = get_tv_string_buf_chk(varp, buf);
21197
21198 return res != NULL ? res : (char_u *)"";
21199}
21200
Bram Moolenaar7d647822014-04-05 21:28:56 +020021201/*
21202 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21203 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021204 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021205get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021206{
21207 static char_u mybuf[NUMBUFLEN];
21208
21209 return get_tv_string_buf_chk(varp, mybuf);
21210}
21211
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021212 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021213get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021214{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021215 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021217 case VAR_NUMBER:
21218 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21219 return buf;
21220 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021221 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021222 break;
21223 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021224 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021225 break;
21226 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021227 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021228 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021229#ifdef FEAT_FLOAT
21230 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021231 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021232 break;
21233#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021234 case VAR_STRING:
21235 if (varp->vval.v_string != NULL)
21236 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021237 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021238 case VAR_SPECIAL:
21239 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21240 return buf;
21241
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021242 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021243 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021246 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247}
21248
21249/*
21250 * Find variable "name" in the list of variables.
21251 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021252 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021253 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021254 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021256 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021257find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021260 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261
Bram Moolenaara7043832005-01-21 11:56:39 +000021262 ht = find_var_ht(name, &varname);
21263 if (htp != NULL)
21264 *htp = ht;
21265 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021267 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268}
21269
21270/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021271 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021272 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021274 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275find_var_in_ht(
21276 hashtab_T *ht,
21277 int htname,
21278 char_u *varname,
21279 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021280{
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 hashitem_T *hi;
21282
21283 if (*varname == NUL)
21284 {
21285 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021286 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021287 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021288 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021289 case 'g': return &globvars_var;
21290 case 'v': return &vimvars_var;
21291 case 'b': return &curbuf->b_bufvar;
21292 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021293#ifdef FEAT_WINDOWS
21294 case 't': return &curtab->tp_winvar;
21295#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021296 case 'l': return current_funccal == NULL
21297 ? NULL : &current_funccal->l_vars_var;
21298 case 'a': return current_funccal == NULL
21299 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 }
21301 return NULL;
21302 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021303
21304 hi = hash_find(ht, varname);
21305 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021306 {
21307 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021308 * worked find the variable again. Don't auto-load a script if it was
21309 * loaded already, otherwise it would be loaded every time when
21310 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021311 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021312 {
21313 /* Note: script_autoload() may make "hi" invalid. It must either
21314 * be obtained again or not used. */
21315 if (!script_autoload(varname, FALSE) || aborting())
21316 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021317 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021318 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021319 if (HASHITEM_EMPTY(hi))
21320 return NULL;
21321 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021322 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021323}
21324
21325/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021326 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021327 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021328 * Set "varname" to the start of name without ':'.
21329 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021330 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021331find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021333 hashitem_T *hi;
21334
Bram Moolenaar73627d02015-08-11 15:46:09 +020021335 if (name[0] == NUL)
21336 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 if (name[1] != ':')
21338 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021339 /* The name must not start with a colon or #. */
21340 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 return NULL;
21342 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021343
21344 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021345 hi = hash_find(&compat_hashtab, name);
21346 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021347 return &compat_hashtab;
21348
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021350 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021351 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 }
21353 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021354 if (*name == 'g') /* global variable */
21355 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021356 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21357 */
21358 if (vim_strchr(name + 2, ':') != NULL
21359 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021360 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021362 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021364 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021365#ifdef FEAT_WINDOWS
21366 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021367 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021368#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021369 if (*name == 'v') /* v: variable */
21370 return &vimvarht;
21371 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021372 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021373 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021374 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 if (*name == 's' /* script variable */
21376 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21377 return &SCRIPT_VARS(current_SID);
21378 return NULL;
21379}
21380
21381/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021382 * Get function call environment based on bactrace debug level
21383 */
21384 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021385get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021386{
21387 int i;
21388 funccall_T *funccal;
21389 funccall_T *temp_funccal;
21390
21391 funccal = current_funccal;
21392 if (debug_backtrace_level > 0)
21393 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021394 for (i = 0; i < debug_backtrace_level; i++)
21395 {
21396 temp_funccal = funccal->caller;
21397 if (temp_funccal)
21398 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021399 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021400 /* backtrace level overflow. reset to max */
21401 debug_backtrace_level = i;
21402 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021403 }
21404 return funccal;
21405}
21406
21407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021409 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 * Returns NULL when it doesn't exist.
21411 */
21412 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021413get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414{
Bram Moolenaar33570922005-01-25 22:26:29 +000021415 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021417 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 if (v == NULL)
21419 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021420 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421}
21422
21423/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021424 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 * sourcing this script and when executing functions defined in the script.
21426 */
21427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021428new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429{
Bram Moolenaara7043832005-01-21 11:56:39 +000021430 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021431 hashtab_T *ht;
21432 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021433
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21435 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021436 /* Re-allocating ga_data means that an ht_array pointing to
21437 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021438 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021439 for (i = 1; i <= ga_scripts.ga_len; ++i)
21440 {
21441 ht = &SCRIPT_VARS(i);
21442 if (ht->ht_mask == HT_INIT_SIZE - 1)
21443 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021444 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021445 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021446 }
21447
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 while (ga_scripts.ga_len < id)
21449 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021450 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021451 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021452 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 }
21455 }
21456}
21457
21458/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21460 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 */
21462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021463init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464{
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021466 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021467 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021468 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021469 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 dict_var->di_tv.vval.v_dict = dict;
21471 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021472 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21474 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475}
21476
21477/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021478 * Unreference a dictionary initialized by init_var_dict().
21479 */
21480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021481unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021482{
21483 /* Now the dict needs to be freed if no one else is using it, go back to
21484 * normal reference counting. */
21485 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21486 dict_unref(dict);
21487}
21488
21489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021491 * Frees all allocated variables and the value they contain.
21492 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 */
21494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021495vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021496{
21497 vars_clear_ext(ht, TRUE);
21498}
21499
21500/*
21501 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21502 */
21503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021504vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505{
Bram Moolenaara7043832005-01-21 11:56:39 +000021506 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021507 hashitem_T *hi;
21508 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509
Bram Moolenaar33570922005-01-25 22:26:29 +000021510 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021511 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021512 for (hi = ht->ht_array; todo > 0; ++hi)
21513 {
21514 if (!HASHITEM_EMPTY(hi))
21515 {
21516 --todo;
21517
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021519 * ht_array might change then. hash_clear() takes care of it
21520 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 v = HI2DI(hi);
21522 if (free_val)
21523 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021524 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021525 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021526 }
21527 }
21528 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021529 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530}
21531
Bram Moolenaara7043832005-01-21 11:56:39 +000021532/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 * Delete a variable from hashtab "ht" at item "hi".
21534 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021537delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538{
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021540
21541 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 clear_tv(&di->di_tv);
21543 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544}
21545
21546/*
21547 * List the value of one internal variable.
21548 */
21549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021550list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021552 char_u *tofree;
21553 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021554 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021556 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021558 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021559 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560}
21561
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021563list_one_var_a(
21564 char_u *prefix,
21565 char_u *name,
21566 int type,
21567 char_u *string,
21568 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569{
Bram Moolenaar31859182007-08-14 20:41:13 +000021570 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21571 msg_start();
21572 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 if (name != NULL) /* "a:" vars don't have a name stored */
21574 msg_puts(name);
21575 msg_putchar(' ');
21576 msg_advance(22);
21577 if (type == VAR_NUMBER)
21578 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021579 else if (type == VAR_FUNC)
21580 msg_putchar('*');
21581 else if (type == VAR_LIST)
21582 {
21583 msg_putchar('[');
21584 if (*string == '[')
21585 ++string;
21586 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021587 else if (type == VAR_DICT)
21588 {
21589 msg_putchar('{');
21590 if (*string == '{')
21591 ++string;
21592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 else
21594 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021595
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021597
21598 if (type == VAR_FUNC)
21599 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021600 if (*first)
21601 {
21602 msg_clr_eos();
21603 *first = FALSE;
21604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605}
21606
21607/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021608 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 * If the variable already exists, the value is updated.
21610 * Otherwise the variable is created.
21611 */
21612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021613set_var(
21614 char_u *name,
21615 typval_T *tv,
21616 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617{
Bram Moolenaar33570922005-01-25 22:26:29 +000021618 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021620 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021622 ht = find_var_ht(name, &varname);
21623 if (ht == NULL || *varname == NUL)
21624 {
21625 EMSG2(_(e_illvar), name);
21626 return;
21627 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021628 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021629
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021630 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21631 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021632
Bram Moolenaar33570922005-01-25 22:26:29 +000021633 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021636 if (var_check_ro(v->di_flags, name, FALSE)
21637 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021638 return;
21639 if (v->di_tv.v_type != tv->v_type
21640 && !((v->di_tv.v_type == VAR_STRING
21641 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021642 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021643 || tv->v_type == VAR_NUMBER))
21644#ifdef FEAT_FLOAT
21645 && !((v->di_tv.v_type == VAR_NUMBER
21646 || v->di_tv.v_type == VAR_FLOAT)
21647 && (tv->v_type == VAR_NUMBER
21648 || tv->v_type == VAR_FLOAT))
21649#endif
21650 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021651 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021652 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021653 return;
21654 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021655
21656 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021657 * Handle setting internal v: variables separately where needed to
21658 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 */
21660 if (ht == &vimvarht)
21661 {
21662 if (v->di_tv.v_type == VAR_STRING)
21663 {
21664 vim_free(v->di_tv.vval.v_string);
21665 if (copy || tv->v_type != VAR_STRING)
21666 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21667 else
21668 {
21669 /* Take over the string to avoid an extra alloc/free. */
21670 v->di_tv.vval.v_string = tv->vval.v_string;
21671 tv->vval.v_string = NULL;
21672 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021673 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021675 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021676 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021678 if (STRCMP(varname, "searchforward") == 0)
21679 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021680#ifdef FEAT_SEARCH_EXTRA
21681 else if (STRCMP(varname, "hlsearch") == 0)
21682 {
21683 no_hlsearch = !v->di_tv.vval.v_number;
21684 redraw_all_later(SOME_VALID);
21685 }
21686#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021687 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021688 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021689 else if (v->di_tv.v_type != tv->v_type)
21690 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021691 }
21692
21693 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 }
21695 else /* add a new variable */
21696 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021697 /* Can't add "v:" variable. */
21698 if (ht == &vimvarht)
21699 {
21700 EMSG2(_(e_illvar), name);
21701 return;
21702 }
21703
Bram Moolenaar92124a32005-06-17 22:03:40 +000021704 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021705 if (!valid_varname(varname))
21706 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021707
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021708 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21709 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021710 if (v == NULL)
21711 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021712 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021713 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021715 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 return;
21717 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021718 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021720
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021721 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021723 else
21724 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021725 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021726 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021727 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729}
21730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021731/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021732 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 * Also give an error message.
21734 */
21735 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021736var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021737{
21738 if (flags & DI_FLAGS_RO)
21739 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021740 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021741 return TRUE;
21742 }
21743 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21744 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021745 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021746 return TRUE;
21747 }
21748 return FALSE;
21749}
21750
21751/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021752 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21753 * Also give an error message.
21754 */
21755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021756var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021757{
21758 if (flags & DI_FLAGS_FIX)
21759 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021760 EMSG2(_("E795: Cannot delete variable %s"),
21761 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021762 return TRUE;
21763 }
21764 return FALSE;
21765}
21766
21767/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021768 * Check if a funcref is assigned to a valid variable name.
21769 * Return TRUE and give an error if not.
21770 */
21771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021772var_check_func_name(
21773 char_u *name, /* points to start of variable name */
21774 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021775{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021776 /* Allow for w: b: s: and t:. */
21777 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021778 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21779 ? name[2] : name[0]))
21780 {
21781 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21782 name);
21783 return TRUE;
21784 }
21785 /* Don't allow hiding a function. When "v" is not NULL we might be
21786 * assigning another function to the same var, the type is checked
21787 * below. */
21788 if (new_var && function_exists(name))
21789 {
21790 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21791 name);
21792 return TRUE;
21793 }
21794 return FALSE;
21795}
21796
21797/*
21798 * Check if a variable name is valid.
21799 * Return FALSE and give an error if not.
21800 */
21801 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021802valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021803{
21804 char_u *p;
21805
21806 for (p = varname; *p != NUL; ++p)
21807 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21808 && *p != AUTOLOAD_CHAR)
21809 {
21810 EMSG2(_(e_illvar), varname);
21811 return FALSE;
21812 }
21813 return TRUE;
21814}
21815
21816/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021817 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021818 * Also give an error message, using "name" or _("name") when use_gettext is
21819 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021820 */
21821 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021822tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021823{
21824 if (lock & VAR_LOCKED)
21825 {
21826 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021827 name == NULL ? (char_u *)_("Unknown")
21828 : use_gettext ? (char_u *)_(name)
21829 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021830 return TRUE;
21831 }
21832 if (lock & VAR_FIXED)
21833 {
21834 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021835 name == NULL ? (char_u *)_("Unknown")
21836 : use_gettext ? (char_u *)_(name)
21837 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021838 return TRUE;
21839 }
21840 return FALSE;
21841}
21842
21843/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021844 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021845 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021846 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021847 * It is OK for "from" and "to" to point to the same item. This is used to
21848 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021849 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021851copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021853 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021854 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021855 switch (from->v_type)
21856 {
21857 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021858 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021859 to->vval.v_number = from->vval.v_number;
21860 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021861#ifdef FEAT_FLOAT
21862 case VAR_FLOAT:
21863 to->vval.v_float = from->vval.v_float;
21864 break;
21865#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021866 case VAR_STRING:
21867 case VAR_FUNC:
21868 if (from->vval.v_string == NULL)
21869 to->vval.v_string = NULL;
21870 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021871 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021872 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021873 if (from->v_type == VAR_FUNC)
21874 func_ref(to->vval.v_string);
21875 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021876 break;
21877 case VAR_LIST:
21878 if (from->vval.v_list == NULL)
21879 to->vval.v_list = NULL;
21880 else
21881 {
21882 to->vval.v_list = from->vval.v_list;
21883 ++to->vval.v_list->lv_refcount;
21884 }
21885 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021886 case VAR_DICT:
21887 if (from->vval.v_dict == NULL)
21888 to->vval.v_dict = NULL;
21889 else
21890 {
21891 to->vval.v_dict = from->vval.v_dict;
21892 ++to->vval.v_dict->dv_refcount;
21893 }
21894 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021895 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021896 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897 break;
21898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899}
21900
21901/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021902 * Make a copy of an item.
21903 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021904 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21905 * reference to an already copied list/dict can be used.
21906 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021907 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021908 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021909item_copy(
21910 typval_T *from,
21911 typval_T *to,
21912 int deep,
21913 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021914{
21915 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021916 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021917
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021919 {
21920 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021921 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021922 }
21923 ++recurse;
21924
21925 switch (from->v_type)
21926 {
21927 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021928#ifdef FEAT_FLOAT
21929 case VAR_FLOAT:
21930#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021931 case VAR_STRING:
21932 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010021933 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000021934 copy_tv(from, to);
21935 break;
21936 case VAR_LIST:
21937 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021938 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021939 if (from->vval.v_list == NULL)
21940 to->vval.v_list = NULL;
21941 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21942 {
21943 /* use the copy made earlier */
21944 to->vval.v_list = from->vval.v_list->lv_copylist;
21945 ++to->vval.v_list->lv_refcount;
21946 }
21947 else
21948 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21949 if (to->vval.v_list == NULL)
21950 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021951 break;
21952 case VAR_DICT:
21953 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021954 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021955 if (from->vval.v_dict == NULL)
21956 to->vval.v_dict = NULL;
21957 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21958 {
21959 /* use the copy made earlier */
21960 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21961 ++to->vval.v_dict->dv_refcount;
21962 }
21963 else
21964 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21965 if (to->vval.v_dict == NULL)
21966 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021967 break;
21968 default:
21969 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021970 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971 }
21972 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021973 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021974}
21975
21976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 * ":echo expr1 ..." print each argument separated with a space, add a
21978 * newline at the end.
21979 * ":echon expr1 ..." print each argument plain.
21980 */
21981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021982ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983{
21984 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021985 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021986 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 char_u *p;
21988 int needclr = TRUE;
21989 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021990 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991
21992 if (eap->skip)
21993 ++emsg_skip;
21994 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021996 /* If eval1() causes an error message the text from the command may
21997 * still need to be cleared. E.g., "echo 22,44". */
21998 need_clr_eos = needclr;
21999
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022001 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 {
22003 /*
22004 * Report the invalid expression unless the expression evaluation
22005 * has been cancelled due to an aborting error, an interrupt, or an
22006 * exception.
22007 */
22008 if (!aborting())
22009 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022010 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 break;
22012 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022013 need_clr_eos = FALSE;
22014
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 if (!eap->skip)
22016 {
22017 if (atstart)
22018 {
22019 atstart = FALSE;
22020 /* Call msg_start() after eval1(), evaluating the expression
22021 * may cause a message to appear. */
22022 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022023 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022024 /* Mark the saved text as finishing the line, so that what
22025 * follows is displayed on a new line when scrolling back
22026 * at the more prompt. */
22027 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 }
22031 else if (eap->cmdidx == CMD_echo)
22032 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022033 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022034 if (p != NULL)
22035 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022037 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022039 if (*p != TAB && needclr)
22040 {
22041 /* remove any text still there from the command */
22042 msg_clr_eos();
22043 needclr = FALSE;
22044 }
22045 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 }
22047 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022048 {
22049#ifdef FEAT_MBYTE
22050 if (has_mbyte)
22051 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022052 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022053
22054 (void)msg_outtrans_len_attr(p, i, echo_attr);
22055 p += i - 1;
22056 }
22057 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022059 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022062 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022064 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 arg = skipwhite(arg);
22066 }
22067 eap->nextcmd = check_nextcmd(arg);
22068
22069 if (eap->skip)
22070 --emsg_skip;
22071 else
22072 {
22073 /* remove text that may still be there from the command */
22074 if (needclr)
22075 msg_clr_eos();
22076 if (eap->cmdidx == CMD_echo)
22077 msg_end();
22078 }
22079}
22080
22081/*
22082 * ":echohl {name}".
22083 */
22084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022085ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086{
22087 int id;
22088
22089 id = syn_name2id(eap->arg);
22090 if (id == 0)
22091 echo_attr = 0;
22092 else
22093 echo_attr = syn_id2attr(id);
22094}
22095
22096/*
22097 * ":execute expr1 ..." execute the result of an expression.
22098 * ":echomsg expr1 ..." Print a message
22099 * ":echoerr expr1 ..." Print an error
22100 * Each gets spaces around each argument and a newline at the end for
22101 * echo commands
22102 */
22103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105{
22106 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022107 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 int ret = OK;
22109 char_u *p;
22110 garray_T ga;
22111 int len;
22112 int save_did_emsg;
22113
22114 ga_init2(&ga, 1, 80);
22115
22116 if (eap->skip)
22117 ++emsg_skip;
22118 while (*arg != NUL && *arg != '|' && *arg != '\n')
22119 {
22120 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022121 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 {
22123 /*
22124 * Report the invalid expression unless the expression evaluation
22125 * has been cancelled due to an aborting error, an interrupt, or an
22126 * exception.
22127 */
22128 if (!aborting())
22129 EMSG2(_(e_invexpr2), p);
22130 ret = FAIL;
22131 break;
22132 }
22133
22134 if (!eap->skip)
22135 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022136 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 len = (int)STRLEN(p);
22138 if (ga_grow(&ga, len + 2) == FAIL)
22139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022140 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 ret = FAIL;
22142 break;
22143 }
22144 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 ga.ga_len += len;
22148 }
22149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022150 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 arg = skipwhite(arg);
22152 }
22153
22154 if (ret != FAIL && ga.ga_data != NULL)
22155 {
22156 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022159 out_flush();
22160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 else if (eap->cmdidx == CMD_echoerr)
22162 {
22163 /* We don't want to abort following commands, restore did_emsg. */
22164 save_did_emsg = did_emsg;
22165 EMSG((char_u *)ga.ga_data);
22166 if (!force_abort)
22167 did_emsg = save_did_emsg;
22168 }
22169 else if (eap->cmdidx == CMD_execute)
22170 do_cmdline((char_u *)ga.ga_data,
22171 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22172 }
22173
22174 ga_clear(&ga);
22175
22176 if (eap->skip)
22177 --emsg_skip;
22178
22179 eap->nextcmd = check_nextcmd(arg);
22180}
22181
22182/*
22183 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22184 * "arg" points to the "&" or '+' when called, to "option" when returning.
22185 * Returns NULL when no option name found. Otherwise pointer to the char
22186 * after the option name.
22187 */
22188 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022189find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190{
22191 char_u *p = *arg;
22192
22193 ++p;
22194 if (*p == 'g' && p[1] == ':')
22195 {
22196 *opt_flags = OPT_GLOBAL;
22197 p += 2;
22198 }
22199 else if (*p == 'l' && p[1] == ':')
22200 {
22201 *opt_flags = OPT_LOCAL;
22202 p += 2;
22203 }
22204 else
22205 *opt_flags = 0;
22206
22207 if (!ASCII_ISALPHA(*p))
22208 return NULL;
22209 *arg = p;
22210
22211 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22212 p += 4; /* termcap option */
22213 else
22214 while (ASCII_ISALPHA(*p))
22215 ++p;
22216 return p;
22217}
22218
22219/*
22220 * ":function"
22221 */
22222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022223ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224{
22225 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022226 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 int j;
22228 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022230 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231 char_u *name = NULL;
22232 char_u *p;
22233 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022234 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 garray_T newargs;
22236 garray_T newlines;
22237 int varargs = FALSE;
22238 int mustend = FALSE;
22239 int flags = 0;
22240 ufunc_T *fp;
22241 int indent;
22242 int nesting;
22243 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022244 dictitem_T *v;
22245 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022246 static int func_nr = 0; /* number for nameless function */
22247 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022248 hashtab_T *ht;
22249 int todo;
22250 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022251 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252
22253 /*
22254 * ":function" without argument: list functions.
22255 */
22256 if (ends_excmd(*eap->arg))
22257 {
22258 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022259 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022260 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022261 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022262 {
22263 if (!HASHITEM_EMPTY(hi))
22264 {
22265 --todo;
22266 fp = HI2UF(hi);
22267 if (!isdigit(*fp->uf_name))
22268 list_func_head(fp, FALSE);
22269 }
22270 }
22271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 eap->nextcmd = check_nextcmd(eap->arg);
22273 return;
22274 }
22275
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022276 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022277 * ":function /pat": list functions matching pattern.
22278 */
22279 if (*eap->arg == '/')
22280 {
22281 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22282 if (!eap->skip)
22283 {
22284 regmatch_T regmatch;
22285
22286 c = *p;
22287 *p = NUL;
22288 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22289 *p = c;
22290 if (regmatch.regprog != NULL)
22291 {
22292 regmatch.rm_ic = p_ic;
22293
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022294 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022295 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22296 {
22297 if (!HASHITEM_EMPTY(hi))
22298 {
22299 --todo;
22300 fp = HI2UF(hi);
22301 if (!isdigit(*fp->uf_name)
22302 && vim_regexec(&regmatch, fp->uf_name, 0))
22303 list_func_head(fp, FALSE);
22304 }
22305 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022306 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022307 }
22308 }
22309 if (*p == '/')
22310 ++p;
22311 eap->nextcmd = check_nextcmd(p);
22312 return;
22313 }
22314
22315 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022316 * Get the function name. There are these situations:
22317 * func normal function name
22318 * "name" == func, "fudi.fd_dict" == NULL
22319 * dict.func new dictionary entry
22320 * "name" == NULL, "fudi.fd_dict" set,
22321 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22322 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022323 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022324 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22325 * dict.func existing dict entry that's not a Funcref
22326 * "name" == NULL, "fudi.fd_dict" set,
22327 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022328 * s:func script-local function name
22329 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332 name = trans_function_name(&p, eap->skip, 0, &fudi);
22333 paren = (vim_strchr(p, '(') != NULL);
22334 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 {
22336 /*
22337 * Return on an invalid expression in braces, unless the expression
22338 * evaluation has been cancelled due to an aborting error, an
22339 * interrupt, or an exception.
22340 */
22341 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022343 if (!eap->skip && fudi.fd_newkey != NULL)
22344 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022345 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 else
22349 eap->skip = TRUE;
22350 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022351
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 /* An error in a function call during evaluation of an expression in magic
22353 * braces should not cause the function not to be defined. */
22354 saved_did_emsg = did_emsg;
22355 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356
22357 /*
22358 * ":function func" with only function name: list function.
22359 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022360 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 {
22362 if (!ends_excmd(*skipwhite(p)))
22363 {
22364 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022365 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 }
22367 eap->nextcmd = check_nextcmd(p);
22368 if (eap->nextcmd != NULL)
22369 *p = NUL;
22370 if (!eap->skip && !got_int)
22371 {
22372 fp = find_func(name);
22373 if (fp != NULL)
22374 {
22375 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022376 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022378 if (FUNCLINE(fp, j) == NULL)
22379 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 msg_putchar('\n');
22381 msg_outnum((long)(j + 1));
22382 if (j < 9)
22383 msg_putchar(' ');
22384 if (j < 99)
22385 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022386 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 out_flush(); /* show a line at a time */
22388 ui_breakcheck();
22389 }
22390 if (!got_int)
22391 {
22392 msg_putchar('\n');
22393 msg_puts((char_u *)" endfunction");
22394 }
22395 }
22396 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022397 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022399 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 }
22401
22402 /*
22403 * ":function name(arg1, arg2)" Define function.
22404 */
22405 p = skipwhite(p);
22406 if (*p != '(')
22407 {
22408 if (!eap->skip)
22409 {
22410 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022411 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 }
22413 /* attempt to continue by skipping some text */
22414 if (vim_strchr(p, '(') != NULL)
22415 p = vim_strchr(p, '(');
22416 }
22417 p = skipwhite(p + 1);
22418
22419 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22420 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22421
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022422 if (!eap->skip)
22423 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022424 /* Check the name of the function. Unless it's a dictionary function
22425 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022426 if (name != NULL)
22427 arg = name;
22428 else
22429 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022430 if (arg != NULL && (fudi.fd_di == NULL
22431 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022432 {
22433 if (*arg == K_SPECIAL)
22434 j = 3;
22435 else
22436 j = 0;
22437 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22438 : eval_isnamec(arg[j])))
22439 ++j;
22440 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022441 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022442 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022443 /* Disallow using the g: dict. */
22444 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22445 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022446 }
22447
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 /*
22449 * Isolate the arguments: "arg1, arg2, ...)"
22450 */
22451 while (*p != ')')
22452 {
22453 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22454 {
22455 varargs = TRUE;
22456 p += 3;
22457 mustend = TRUE;
22458 }
22459 else
22460 {
22461 arg = p;
22462 while (ASCII_ISALNUM(*p) || *p == '_')
22463 ++p;
22464 if (arg == p || isdigit(*arg)
22465 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22466 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22467 {
22468 if (!eap->skip)
22469 EMSG2(_("E125: Illegal argument: %s"), arg);
22470 break;
22471 }
22472 if (ga_grow(&newargs, 1) == FAIL)
22473 goto erret;
22474 c = *p;
22475 *p = NUL;
22476 arg = vim_strsave(arg);
22477 if (arg == NULL)
22478 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022479
22480 /* Check for duplicate argument name. */
22481 for (i = 0; i < newargs.ga_len; ++i)
22482 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22483 {
22484 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022485 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022486 goto erret;
22487 }
22488
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22490 *p = c;
22491 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 if (*p == ',')
22493 ++p;
22494 else
22495 mustend = TRUE;
22496 }
22497 p = skipwhite(p);
22498 if (mustend && *p != ')')
22499 {
22500 if (!eap->skip)
22501 EMSG2(_(e_invarg2), eap->arg);
22502 break;
22503 }
22504 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022505 if (*p != ')')
22506 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 ++p; /* skip the ')' */
22508
Bram Moolenaare9a41262005-01-15 22:18:47 +000022509 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 for (;;)
22511 {
22512 p = skipwhite(p);
22513 if (STRNCMP(p, "range", 5) == 0)
22514 {
22515 flags |= FC_RANGE;
22516 p += 5;
22517 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022518 else if (STRNCMP(p, "dict", 4) == 0)
22519 {
22520 flags |= FC_DICT;
22521 p += 4;
22522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 else if (STRNCMP(p, "abort", 5) == 0)
22524 {
22525 flags |= FC_ABORT;
22526 p += 5;
22527 }
22528 else
22529 break;
22530 }
22531
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022532 /* When there is a line break use what follows for the function body.
22533 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22534 if (*p == '\n')
22535 line_arg = p + 1;
22536 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 EMSG(_(e_trailing));
22538
22539 /*
22540 * Read the body of the function, until ":endfunction" is found.
22541 */
22542 if (KeyTyped)
22543 {
22544 /* Check if the function already exists, don't let the user type the
22545 * whole function before telling him it doesn't work! For a script we
22546 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022547 if (!eap->skip && !eap->forceit)
22548 {
22549 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22550 EMSG(_(e_funcdict));
22551 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022552 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022555 if (!eap->skip && did_emsg)
22556 goto erret;
22557
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 msg_putchar('\n'); /* don't overwrite the function name */
22559 cmdline_row = msg_row;
22560 }
22561
22562 indent = 2;
22563 nesting = 0;
22564 for (;;)
22565 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022566 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022567 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022568 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022569 saved_wait_return = FALSE;
22570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022572 sourcing_lnum_off = sourcing_lnum;
22573
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022574 if (line_arg != NULL)
22575 {
22576 /* Use eap->arg, split up in parts by line breaks. */
22577 theline = line_arg;
22578 p = vim_strchr(theline, '\n');
22579 if (p == NULL)
22580 line_arg += STRLEN(line_arg);
22581 else
22582 {
22583 *p = NUL;
22584 line_arg = p + 1;
22585 }
22586 }
22587 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 theline = getcmdline(':', 0L, indent);
22589 else
22590 theline = eap->getline(':', eap->cookie, indent);
22591 if (KeyTyped)
22592 lines_left = Rows - 1;
22593 if (theline == NULL)
22594 {
22595 EMSG(_("E126: Missing :endfunction"));
22596 goto erret;
22597 }
22598
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022599 /* Detect line continuation: sourcing_lnum increased more than one. */
22600 if (sourcing_lnum > sourcing_lnum_off + 1)
22601 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22602 else
22603 sourcing_lnum_off = 0;
22604
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 if (skip_until != NULL)
22606 {
22607 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22608 * don't check for ":endfunc". */
22609 if (STRCMP(theline, skip_until) == 0)
22610 {
22611 vim_free(skip_until);
22612 skip_until = NULL;
22613 }
22614 }
22615 else
22616 {
22617 /* skip ':' and blanks*/
22618 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22619 ;
22620
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022621 /* Check for "endfunction". */
22622 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022624 if (line_arg == NULL)
22625 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 break;
22627 }
22628
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022629 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 * at "end". */
22631 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22632 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022633 else if (STRNCMP(p, "if", 2) == 0
22634 || STRNCMP(p, "wh", 2) == 0
22635 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 || STRNCMP(p, "try", 3) == 0)
22637 indent += 2;
22638
22639 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022640 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022642 if (*p == '!')
22643 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022645 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22646 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022648 ++nesting;
22649 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 }
22651 }
22652
22653 /* Check for ":append" or ":insert". */
22654 p = skip_range(p, NULL);
22655 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22656 || (p[0] == 'i'
22657 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22658 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22659 skip_until = vim_strsave((char_u *)".");
22660
22661 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22662 arg = skipwhite(skiptowhite(p));
22663 if (arg[0] == '<' && arg[1] =='<'
22664 && ((p[0] == 'p' && p[1] == 'y'
22665 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22666 || (p[0] == 'p' && p[1] == 'e'
22667 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22668 || (p[0] == 't' && p[1] == 'c'
22669 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022670 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22671 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22673 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022674 || (p[0] == 'm' && p[1] == 'z'
22675 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 ))
22677 {
22678 /* ":python <<" continues until a dot, like ":append" */
22679 p = skipwhite(arg + 2);
22680 if (*p == NUL)
22681 skip_until = vim_strsave((char_u *)".");
22682 else
22683 skip_until = vim_strsave(p);
22684 }
22685 }
22686
22687 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022688 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022689 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022690 if (line_arg == NULL)
22691 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022693 }
22694
22695 /* Copy the line to newly allocated memory. get_one_sourceline()
22696 * allocates 250 bytes per line, this saves 80% on average. The cost
22697 * is an extra alloc/free. */
22698 p = vim_strsave(theline);
22699 if (p != NULL)
22700 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022701 if (line_arg == NULL)
22702 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022703 theline = p;
22704 }
22705
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022706 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22707
22708 /* Add NULL lines for continuation lines, so that the line count is
22709 * equal to the index in the growarray. */
22710 while (sourcing_lnum_off-- > 0)
22711 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022712
22713 /* Check for end of eap->arg. */
22714 if (line_arg != NULL && *line_arg == NUL)
22715 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 }
22717
22718 /* Don't define the function when skipping commands or when an error was
22719 * detected. */
22720 if (eap->skip || did_emsg)
22721 goto erret;
22722
22723 /*
22724 * If there are no errors, add the function
22725 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022726 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022727 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022728 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022730 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022731 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022732 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022733 goto erret;
22734 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022735
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022736 fp = find_func(name);
22737 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022739 if (!eap->forceit)
22740 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022741 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022742 goto erret;
22743 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022744 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022746 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022747 name);
22748 goto erret;
22749 }
22750 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022751 ga_clear_strings(&(fp->uf_args));
22752 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022753 vim_free(name);
22754 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 }
22757 else
22758 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022759 char numbuf[20];
22760
22761 fp = NULL;
22762 if (fudi.fd_newkey == NULL && !eap->forceit)
22763 {
22764 EMSG(_(e_funcdict));
22765 goto erret;
22766 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022767 if (fudi.fd_di == NULL)
22768 {
22769 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022770 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022771 goto erret;
22772 }
22773 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022774 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022775 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022776
22777 /* Give the function a sequential number. Can only be used with a
22778 * Funcref! */
22779 vim_free(name);
22780 sprintf(numbuf, "%d", ++func_nr);
22781 name = vim_strsave((char_u *)numbuf);
22782 if (name == NULL)
22783 goto erret;
22784 }
22785
22786 if (fp == NULL)
22787 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022788 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022789 {
22790 int slen, plen;
22791 char_u *scriptname;
22792
22793 /* Check that the autoload name matches the script name. */
22794 j = FAIL;
22795 if (sourcing_name != NULL)
22796 {
22797 scriptname = autoload_name(name);
22798 if (scriptname != NULL)
22799 {
22800 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022801 plen = (int)STRLEN(p);
22802 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022803 if (slen > plen && fnamecmp(p,
22804 sourcing_name + slen - plen) == 0)
22805 j = OK;
22806 vim_free(scriptname);
22807 }
22808 }
22809 if (j == FAIL)
22810 {
22811 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22812 goto erret;
22813 }
22814 }
22815
22816 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 if (fp == NULL)
22818 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022819
22820 if (fudi.fd_dict != NULL)
22821 {
22822 if (fudi.fd_di == NULL)
22823 {
22824 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022825 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022826 if (fudi.fd_di == NULL)
22827 {
22828 vim_free(fp);
22829 goto erret;
22830 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022831 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22832 {
22833 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022834 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022835 goto erret;
22836 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022837 }
22838 else
22839 /* overwrite existing dict entry */
22840 clear_tv(&fudi.fd_di->di_tv);
22841 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022842 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022843 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022844 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022845
22846 /* behave like "dict" was used */
22847 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022848 }
22849
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022851 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022852 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22853 {
22854 vim_free(fp);
22855 goto erret;
22856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022858 fp->uf_args = newargs;
22859 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022860#ifdef FEAT_PROFILE
22861 fp->uf_tml_count = NULL;
22862 fp->uf_tml_total = NULL;
22863 fp->uf_tml_self = NULL;
22864 fp->uf_profiling = FALSE;
22865 if (prof_def_func())
22866 func_do_profile(fp);
22867#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022868 fp->uf_varargs = varargs;
22869 fp->uf_flags = flags;
22870 fp->uf_calls = 0;
22871 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022872 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873
22874erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 ga_clear_strings(&newargs);
22876 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022877ret_free:
22878 vim_free(skip_until);
22879 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022882 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883}
22884
22885/*
22886 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022887 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022889 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022890 * TFN_INT: internal function name OK
22891 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022892 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 * Advances "pp" to just after the function name (if no error).
22894 */
22895 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022896trans_function_name(
22897 char_u **pp,
22898 int skip, /* only find the end, don't evaluate */
22899 int flags,
22900 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022902 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 char_u *start;
22904 char_u *end;
22905 int lead;
22906 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022908 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022909
22910 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022911 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022913
22914 /* Check for hard coded <SNR>: already translated function ID (from a user
22915 * command). */
22916 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22917 && (*pp)[2] == (int)KE_SNR)
22918 {
22919 *pp += 3;
22920 len = get_id_len(pp) + 3;
22921 return vim_strnsave(start, len);
22922 }
22923
22924 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22925 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022927 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022929
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022930 /* Note that TFN_ flags use the same values as GLV_ flags. */
22931 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022932 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022933 if (end == start)
22934 {
22935 if (!skip)
22936 EMSG(_("E129: Function name required"));
22937 goto theend;
22938 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022939 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022940 {
22941 /*
22942 * Report an invalid expression in braces, unless the expression
22943 * evaluation has been cancelled due to an aborting error, an
22944 * interrupt, or an exception.
22945 */
22946 if (!aborting())
22947 {
22948 if (end != NULL)
22949 EMSG2(_(e_invarg2), start);
22950 }
22951 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022952 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022953 goto theend;
22954 }
22955
22956 if (lv.ll_tv != NULL)
22957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022958 if (fdp != NULL)
22959 {
22960 fdp->fd_dict = lv.ll_dict;
22961 fdp->fd_newkey = lv.ll_newkey;
22962 lv.ll_newkey = NULL;
22963 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022965 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22966 {
22967 name = vim_strsave(lv.ll_tv->vval.v_string);
22968 *pp = end;
22969 }
22970 else
22971 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022972 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22973 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022974 EMSG(_(e_funcref));
22975 else
22976 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022977 name = NULL;
22978 }
22979 goto theend;
22980 }
22981
22982 if (lv.ll_name == NULL)
22983 {
22984 /* Error found, but continue after the function name. */
22985 *pp = end;
22986 goto theend;
22987 }
22988
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022989 /* Check if the name is a Funcref. If so, use the value. */
22990 if (lv.ll_exp_name != NULL)
22991 {
22992 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022993 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022994 if (name == lv.ll_exp_name)
22995 name = NULL;
22996 }
22997 else
22998 {
22999 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023000 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023001 if (name == *pp)
23002 name = NULL;
23003 }
23004 if (name != NULL)
23005 {
23006 name = vim_strsave(name);
23007 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023008 if (STRNCMP(name, "<SNR>", 5) == 0)
23009 {
23010 /* Change "<SNR>" to the byte sequence. */
23011 name[0] = K_SPECIAL;
23012 name[1] = KS_EXTRA;
23013 name[2] = (int)KE_SNR;
23014 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23015 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023016 goto theend;
23017 }
23018
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023019 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023020 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023021 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023022 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23023 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23024 {
23025 /* When there was "s:" already or the name expanded to get a
23026 * leading "s:" then remove it. */
23027 lv.ll_name += 2;
23028 len -= 2;
23029 lead = 2;
23030 }
23031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023032 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023033 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023034 /* skip over "s:" and "g:" */
23035 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023036 lv.ll_name += 2;
23037 len = (int)(end - lv.ll_name);
23038 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023039
23040 /*
23041 * Copy the function name to allocated memory.
23042 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23043 * Accept <SNR>123_name() outside a script.
23044 */
23045 if (skip)
23046 lead = 0; /* do nothing */
23047 else if (lead > 0)
23048 {
23049 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023050 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23051 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023052 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023053 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023054 if (current_SID <= 0)
23055 {
23056 EMSG(_(e_usingsid));
23057 goto theend;
23058 }
23059 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23060 lead += (int)STRLEN(sid_buf);
23061 }
23062 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023063 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023064 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023065 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023066 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023067 goto theend;
23068 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023069 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023070 {
23071 char_u *cp = vim_strchr(lv.ll_name, ':');
23072
23073 if (cp != NULL && cp < end)
23074 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023075 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023076 goto theend;
23077 }
23078 }
23079
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023080 name = alloc((unsigned)(len + lead + 1));
23081 if (name != NULL)
23082 {
23083 if (lead > 0)
23084 {
23085 name[0] = K_SPECIAL;
23086 name[1] = KS_EXTRA;
23087 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023088 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023089 STRCPY(name + 3, sid_buf);
23090 }
23091 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023092 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023093 }
23094 *pp = end;
23095
23096theend:
23097 clear_lval(&lv);
23098 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099}
23100
23101/*
23102 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23103 * Return 2 if "p" starts with "s:".
23104 * Return 0 otherwise.
23105 */
23106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023107eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023109 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23110 * the standard library function. */
23111 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23112 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 return 5;
23114 if (p[0] == 's' && p[1] == ':')
23115 return 2;
23116 return 0;
23117}
23118
23119/*
23120 * Return TRUE if "p" starts with "<SID>" or "s:".
23121 * Only works if eval_fname_script() returned non-zero for "p"!
23122 */
23123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023124eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125{
23126 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23127}
23128
23129/*
23130 * List the head of the function: "name(arg1, arg2)".
23131 */
23132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023133list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134{
23135 int j;
23136
23137 msg_start();
23138 if (indent)
23139 MSG_PUTS(" ");
23140 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023141 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 {
23143 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023144 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 }
23146 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023147 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023149 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 {
23151 if (j)
23152 MSG_PUTS(", ");
23153 msg_puts(FUNCARG(fp, j));
23154 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023155 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 {
23157 if (j)
23158 MSG_PUTS(", ");
23159 MSG_PUTS("...");
23160 }
23161 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023162 if (fp->uf_flags & FC_ABORT)
23163 MSG_PUTS(" abort");
23164 if (fp->uf_flags & FC_RANGE)
23165 MSG_PUTS(" range");
23166 if (fp->uf_flags & FC_DICT)
23167 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023168 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023169 if (p_verbose > 0)
23170 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171}
23172
23173/*
23174 * Find a function by name, return pointer to it in ufuncs.
23175 * Return NULL for unknown function.
23176 */
23177 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023178find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023180 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023182 hi = hash_find(&func_hashtab, name);
23183 if (!HASHITEM_EMPTY(hi))
23184 return HI2UF(hi);
23185 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186}
23187
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023188#if defined(EXITFREE) || defined(PROTO)
23189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023190free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023191{
23192 hashitem_T *hi;
23193
23194 /* Need to start all over every time, because func_free() may change the
23195 * hash table. */
23196 while (func_hashtab.ht_used > 0)
23197 for (hi = func_hashtab.ht_array; ; ++hi)
23198 if (!HASHITEM_EMPTY(hi))
23199 {
23200 func_free(HI2UF(hi));
23201 break;
23202 }
23203}
23204#endif
23205
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023207translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023208{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023209 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023210 return find_internal_func(name) >= 0;
23211 return find_func(name) != NULL;
23212}
23213
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023214/*
23215 * Return TRUE if a function "name" exists.
23216 */
23217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023218function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023219{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023220 char_u *nm = name;
23221 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023222 int n = FALSE;
23223
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023224 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23225 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023226 nm = skipwhite(nm);
23227
23228 /* Only accept "funcname", "funcname ", "funcname (..." and
23229 * "funcname(...", not "funcname!...". */
23230 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023231 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023232 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023233 return n;
23234}
23235
Bram Moolenaara1544c02013-05-30 12:35:52 +020023236 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023237get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023238{
23239 char_u *nm = name;
23240 char_u *p;
23241
23242 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23243
23244 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023245 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023246 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023247
Bram Moolenaara1544c02013-05-30 12:35:52 +020023248 vim_free(p);
23249 return NULL;
23250}
23251
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023252/*
23253 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023254 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23255 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023256 */
23257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023258builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023259{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023260 char_u *p;
23261
23262 if (!ASCII_ISLOWER(name[0]))
23263 return FALSE;
23264 p = vim_strchr(name, AUTOLOAD_CHAR);
23265 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023266}
23267
Bram Moolenaar05159a02005-02-26 23:04:13 +000023268#if defined(FEAT_PROFILE) || defined(PROTO)
23269/*
23270 * Start profiling function "fp".
23271 */
23272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023273func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023274{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023275 int len = fp->uf_lines.ga_len;
23276
23277 if (len == 0)
23278 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279 fp->uf_tm_count = 0;
23280 profile_zero(&fp->uf_tm_self);
23281 profile_zero(&fp->uf_tm_total);
23282 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023283 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023284 if (fp->uf_tml_total == NULL)
23285 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023286 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023287 if (fp->uf_tml_self == NULL)
23288 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023289 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023290 fp->uf_tml_idx = -1;
23291 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23292 || fp->uf_tml_self == NULL)
23293 return; /* out of memory */
23294
23295 fp->uf_profiling = TRUE;
23296}
23297
23298/*
23299 * Dump the profiling results for all functions in file "fd".
23300 */
23301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023302func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023303{
23304 hashitem_T *hi;
23305 int todo;
23306 ufunc_T *fp;
23307 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023308 ufunc_T **sorttab;
23309 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023310
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023311 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023312 if (todo == 0)
23313 return; /* nothing to dump */
23314
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023315 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023316
Bram Moolenaar05159a02005-02-26 23:04:13 +000023317 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23318 {
23319 if (!HASHITEM_EMPTY(hi))
23320 {
23321 --todo;
23322 fp = HI2UF(hi);
23323 if (fp->uf_profiling)
23324 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023325 if (sorttab != NULL)
23326 sorttab[st_len++] = fp;
23327
Bram Moolenaar05159a02005-02-26 23:04:13 +000023328 if (fp->uf_name[0] == K_SPECIAL)
23329 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23330 else
23331 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23332 if (fp->uf_tm_count == 1)
23333 fprintf(fd, "Called 1 time\n");
23334 else
23335 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23336 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23337 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23338 fprintf(fd, "\n");
23339 fprintf(fd, "count total (s) self (s)\n");
23340
23341 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23342 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023343 if (FUNCLINE(fp, i) == NULL)
23344 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023345 prof_func_line(fd, fp->uf_tml_count[i],
23346 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23348 }
23349 fprintf(fd, "\n");
23350 }
23351 }
23352 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023353
23354 if (sorttab != NULL && st_len > 0)
23355 {
23356 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23357 prof_total_cmp);
23358 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23359 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23360 prof_self_cmp);
23361 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23362 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023363
23364 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023365}
Bram Moolenaar73830342005-02-28 22:48:19 +000023366
23367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023368prof_sort_list(
23369 FILE *fd,
23370 ufunc_T **sorttab,
23371 int st_len,
23372 char *title,
23373 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023374{
23375 int i;
23376 ufunc_T *fp;
23377
23378 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23379 fprintf(fd, "count total (s) self (s) function\n");
23380 for (i = 0; i < 20 && i < st_len; ++i)
23381 {
23382 fp = sorttab[i];
23383 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23384 prefer_self);
23385 if (fp->uf_name[0] == K_SPECIAL)
23386 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23387 else
23388 fprintf(fd, " %s()\n", fp->uf_name);
23389 }
23390 fprintf(fd, "\n");
23391}
23392
23393/*
23394 * Print the count and times for one function or function line.
23395 */
23396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023397prof_func_line(
23398 FILE *fd,
23399 int count,
23400 proftime_T *total,
23401 proftime_T *self,
23402 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023403{
23404 if (count > 0)
23405 {
23406 fprintf(fd, "%5d ", count);
23407 if (prefer_self && profile_equal(total, self))
23408 fprintf(fd, " ");
23409 else
23410 fprintf(fd, "%s ", profile_msg(total));
23411 if (!prefer_self && profile_equal(total, self))
23412 fprintf(fd, " ");
23413 else
23414 fprintf(fd, "%s ", profile_msg(self));
23415 }
23416 else
23417 fprintf(fd, " ");
23418}
23419
23420/*
23421 * Compare function for total time sorting.
23422 */
23423 static int
23424#ifdef __BORLANDC__
23425_RTLENTRYF
23426#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023427prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023428{
23429 ufunc_T *p1, *p2;
23430
23431 p1 = *(ufunc_T **)s1;
23432 p2 = *(ufunc_T **)s2;
23433 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23434}
23435
23436/*
23437 * Compare function for self time sorting.
23438 */
23439 static int
23440#ifdef __BORLANDC__
23441_RTLENTRYF
23442#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023443prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023444{
23445 ufunc_T *p1, *p2;
23446
23447 p1 = *(ufunc_T **)s1;
23448 p2 = *(ufunc_T **)s2;
23449 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23450}
23451
Bram Moolenaar05159a02005-02-26 23:04:13 +000023452#endif
23453
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023454/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023455 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023456 * Return TRUE if a package was loaded.
23457 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023459script_autoload(
23460 char_u *name,
23461 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023462{
23463 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023464 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023465 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023466 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023467
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023468 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023469 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023470 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023471 return FALSE;
23472
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023473 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023474
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023475 /* Find the name in the list of previously loaded package names. Skip
23476 * "autoload/", it's always the same. */
23477 for (i = 0; i < ga_loaded.ga_len; ++i)
23478 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23479 break;
23480 if (!reload && i < ga_loaded.ga_len)
23481 ret = FALSE; /* was loaded already */
23482 else
23483 {
23484 /* Remember the name if it wasn't loaded already. */
23485 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23486 {
23487 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23488 tofree = NULL;
23489 }
23490
23491 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023492 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023493 ret = TRUE;
23494 }
23495
23496 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023497 return ret;
23498}
23499
23500/*
23501 * Return the autoload script name for a function or variable name.
23502 * Returns NULL when out of memory.
23503 */
23504 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023505autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023506{
23507 char_u *p;
23508 char_u *scriptname;
23509
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023510 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023511 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23512 if (scriptname == NULL)
23513 return FALSE;
23514 STRCPY(scriptname, "autoload/");
23515 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023516 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023517 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023518 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023519 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023520 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023521}
23522
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23524
23525/*
23526 * Function given to ExpandGeneric() to obtain the list of user defined
23527 * function names.
23528 */
23529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023530get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023532 static long_u done;
23533 static hashitem_T *hi;
23534 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535
23536 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023538 done = 0;
23539 hi = func_hashtab.ht_array;
23540 }
23541 if (done < func_hashtab.ht_used)
23542 {
23543 if (done++ > 0)
23544 ++hi;
23545 while (HASHITEM_EMPTY(hi))
23546 ++hi;
23547 fp = HI2UF(hi);
23548
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023549 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023550 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023551
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023552 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23553 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554
23555 cat_func_name(IObuff, fp);
23556 if (xp->xp_context != EXPAND_USER_FUNC)
23557 {
23558 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023559 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 STRCAT(IObuff, ")");
23561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 return IObuff;
23563 }
23564 return NULL;
23565}
23566
23567#endif /* FEAT_CMDL_COMPL */
23568
23569/*
23570 * Copy the function name of "fp" to buffer "buf".
23571 * "buf" must be able to hold the function name plus three bytes.
23572 * Takes care of script-local function names.
23573 */
23574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023575cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023577 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 {
23579 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023580 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 }
23582 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023583 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584}
23585
23586/*
23587 * ":delfunction {name}"
23588 */
23589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023590ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023592 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 char_u *p;
23594 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023595 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596
23597 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023598 name = trans_function_name(&p, eap->skip, 0, &fudi);
23599 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023601 {
23602 if (fudi.fd_dict != NULL && !eap->skip)
23603 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606 if (!ends_excmd(*skipwhite(p)))
23607 {
23608 vim_free(name);
23609 EMSG(_(e_trailing));
23610 return;
23611 }
23612 eap->nextcmd = check_nextcmd(p);
23613 if (eap->nextcmd != NULL)
23614 *p = NUL;
23615
23616 if (!eap->skip)
23617 fp = find_func(name);
23618 vim_free(name);
23619
23620 if (!eap->skip)
23621 {
23622 if (fp == NULL)
23623 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023624 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 return;
23626 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023627 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 {
23629 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23630 return;
23631 }
23632
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023633 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023635 /* Delete the dict item that refers to the function, it will
23636 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023637 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023639 else
23640 func_free(fp);
23641 }
23642}
23643
23644/*
23645 * Free a function and remove it from the list of functions.
23646 */
23647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023648func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023649{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023650 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023651
23652 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023653 ga_clear_strings(&(fp->uf_args));
23654 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023655#ifdef FEAT_PROFILE
23656 vim_free(fp->uf_tml_count);
23657 vim_free(fp->uf_tml_total);
23658 vim_free(fp->uf_tml_self);
23659#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023660
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023661 /* remove the function from the function hashtable */
23662 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23663 if (HASHITEM_EMPTY(hi))
23664 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023665 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023666 hash_remove(&func_hashtab, hi);
23667
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023668 vim_free(fp);
23669}
23670
23671/*
23672 * Unreference a Function: decrement the reference count and free it when it
23673 * becomes zero. Only for numbered functions.
23674 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023676func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677{
23678 ufunc_T *fp;
23679
23680 if (name != NULL && isdigit(*name))
23681 {
23682 fp = find_func(name);
23683 if (fp == NULL)
23684 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023685 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023686 {
23687 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023688 * when "uf_calls" becomes zero. */
23689 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023690 func_free(fp);
23691 }
23692 }
23693}
23694
23695/*
23696 * Count a reference to a Function.
23697 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023699func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023700{
23701 ufunc_T *fp;
23702
23703 if (name != NULL && isdigit(*name))
23704 {
23705 fp = find_func(name);
23706 if (fp == NULL)
23707 EMSG2(_(e_intern2), "func_ref()");
23708 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023709 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 }
23711}
23712
23713/*
23714 * Call a user function.
23715 */
23716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023717call_user_func(
23718 ufunc_T *fp, /* pointer to function */
23719 int argcount, /* nr of args */
23720 typval_T *argvars, /* arguments */
23721 typval_T *rettv, /* return value */
23722 linenr_T firstline, /* first line of range */
23723 linenr_T lastline, /* last line of range */
23724 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725{
Bram Moolenaar33570922005-01-25 22:26:29 +000023726 char_u *save_sourcing_name;
23727 linenr_T save_sourcing_lnum;
23728 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023729 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023730 int save_did_emsg;
23731 static int depth = 0;
23732 dictitem_T *v;
23733 int fixvar_idx = 0; /* index in fixvar[] */
23734 int i;
23735 int ai;
23736 char_u numbuf[NUMBUFLEN];
23737 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023738 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023739#ifdef FEAT_PROFILE
23740 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023741 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743
23744 /* If depth of calling is getting too high, don't execute the function */
23745 if (depth >= p_mfd)
23746 {
23747 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023748 rettv->v_type = VAR_NUMBER;
23749 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750 return;
23751 }
23752 ++depth;
23753
23754 line_breakcheck(); /* check for CTRL-C hit */
23755
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023756 fc = (funccall_T *)alloc(sizeof(funccall_T));
23757 fc->caller = current_funccal;
23758 current_funccal = fc;
23759 fc->func = fp;
23760 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023761 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023762 fc->linenr = 0;
23763 fc->returned = FALSE;
23764 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023766 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23767 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768
Bram Moolenaar33570922005-01-25 22:26:29 +000023769 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023770 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023771 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23772 * each argument variable and saves a lot of time.
23773 */
23774 /*
23775 * Init l: variables.
23776 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023777 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023778 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023779 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023780 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23781 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023782 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023783 name = v->di_key;
23784 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023785 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023786 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023787 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023788 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023789 v->di_tv.vval.v_dict = selfdict;
23790 ++selfdict->dv_refcount;
23791 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023792
Bram Moolenaar33570922005-01-25 22:26:29 +000023793 /*
23794 * Init a: variables.
23795 * Set a:0 to "argcount".
23796 * Set a:000 to a list with room for the "..." arguments.
23797 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023798 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023799 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023800 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023801 /* Use "name" to avoid a warning from some compiler that checks the
23802 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023803 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023804 name = v->di_key;
23805 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023806 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023807 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023808 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023809 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023810 v->di_tv.vval.v_list = &fc->l_varlist;
23811 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23812 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23813 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023814
23815 /*
23816 * Set a:firstline to "firstline" and a:lastline to "lastline".
23817 * Set a:name to named arguments.
23818 * Set a:N to the "..." arguments.
23819 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023820 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023821 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023822 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023823 (varnumber_T)lastline);
23824 for (i = 0; i < argcount; ++i)
23825 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023826 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023827 if (ai < 0)
23828 /* named argument a:name */
23829 name = FUNCARG(fp, i);
23830 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023832 /* "..." argument a:1, a:2, etc. */
23833 sprintf((char *)numbuf, "%d", ai + 1);
23834 name = numbuf;
23835 }
23836 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23837 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023838 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023839 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23840 }
23841 else
23842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023843 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23844 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023845 if (v == NULL)
23846 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023847 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023848 }
23849 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023850 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023851
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023852 /* Note: the values are copied directly to avoid alloc/free.
23853 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023854 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023855 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023856
23857 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23858 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023859 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23860 fc->l_listitems[ai].li_tv = argvars[i];
23861 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023862 }
23863 }
23864
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 /* Don't redraw while executing the function. */
23866 ++RedrawingDisabled;
23867 save_sourcing_name = sourcing_name;
23868 save_sourcing_lnum = sourcing_lnum;
23869 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023870 /* need space for function name + ("function " + 3) or "[number]" */
23871 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23872 + STRLEN(fp->uf_name) + 20;
23873 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 if (sourcing_name != NULL)
23875 {
23876 if (save_sourcing_name != NULL
23877 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023878 sprintf((char *)sourcing_name, "%s[%d]..",
23879 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 else
23881 STRCPY(sourcing_name, "function ");
23882 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23883
23884 if (p_verbose >= 12)
23885 {
23886 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023887 verbose_enter_scroll();
23888
Bram Moolenaar555b2802005-05-19 21:08:39 +000023889 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 if (p_verbose >= 14)
23891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023893 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023894 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023895 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896
23897 msg_puts((char_u *)"(");
23898 for (i = 0; i < argcount; ++i)
23899 {
23900 if (i > 0)
23901 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023902 if (argvars[i].v_type == VAR_NUMBER)
23903 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 else
23905 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023906 /* Do not want errors such as E724 here. */
23907 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023908 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023909 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023910 if (s != NULL)
23911 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023912 if (vim_strsize(s) > MSG_BUF_CLEN)
23913 {
23914 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23915 s = buf;
23916 }
23917 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023918 vim_free(tofree);
23919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 }
23921 }
23922 msg_puts((char_u *)")");
23923 }
23924 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023925
23926 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 --no_wait_return;
23928 }
23929 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023930#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023931 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023932 {
23933 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23934 func_do_profile(fp);
23935 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023936 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023937 {
23938 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023939 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023940 profile_zero(&fp->uf_tm_children);
23941 }
23942 script_prof_save(&wait_start);
23943 }
23944#endif
23945
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023947 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 save_did_emsg = did_emsg;
23949 did_emsg = FALSE;
23950
23951 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023952 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23954
23955 --RedrawingDisabled;
23956
23957 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023958 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023960 clear_tv(rettv);
23961 rettv->v_type = VAR_NUMBER;
23962 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 }
23964
Bram Moolenaar05159a02005-02-26 23:04:13 +000023965#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023966 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023967 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023968 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023969 profile_end(&call_start);
23970 profile_sub_wait(&wait_start, &call_start);
23971 profile_add(&fp->uf_tm_total, &call_start);
23972 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023973 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023974 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023975 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23976 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023977 }
23978 }
23979#endif
23980
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981 /* when being verbose, mention the return value */
23982 if (p_verbose >= 12)
23983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023985 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023988 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023989 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023990 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023991 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023994 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023995 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023996 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023997 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023998
Bram Moolenaar555b2802005-05-19 21:08:39 +000023999 /* The value may be very long. Skip the middle part, so that we
24000 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024001 * truncate it at the end. Don't want errors such as E724 here. */
24002 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024003 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024004 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024005 if (s != NULL)
24006 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024007 if (vim_strsize(s) > MSG_BUF_CLEN)
24008 {
24009 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24010 s = buf;
24011 }
24012 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024013 vim_free(tofree);
24014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 }
24016 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024017
24018 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 --no_wait_return;
24020 }
24021
24022 vim_free(sourcing_name);
24023 sourcing_name = save_sourcing_name;
24024 sourcing_lnum = save_sourcing_lnum;
24025 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024026#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024027 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024028 script_prof_restore(&wait_start);
24029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030
24031 if (p_verbose >= 12 && sourcing_name != NULL)
24032 {
24033 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024034 verbose_enter_scroll();
24035
Bram Moolenaar555b2802005-05-19 21:08:39 +000024036 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024038
24039 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 --no_wait_return;
24041 }
24042
24043 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024044 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024046
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024047 /* If the a:000 list and the l: and a: dicts are not referenced we can
24048 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024049 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24050 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24051 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24052 {
24053 free_funccal(fc, FALSE);
24054 }
24055 else
24056 {
24057 hashitem_T *hi;
24058 listitem_T *li;
24059 int todo;
24060
24061 /* "fc" is still in use. This can happen when returning "a:000" or
24062 * assigning "l:" to a global variable.
24063 * Link "fc" in the list for garbage collection later. */
24064 fc->caller = previous_funccal;
24065 previous_funccal = fc;
24066
24067 /* Make a copy of the a: variables, since we didn't do that above. */
24068 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24069 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24070 {
24071 if (!HASHITEM_EMPTY(hi))
24072 {
24073 --todo;
24074 v = HI2DI(hi);
24075 copy_tv(&v->di_tv, &v->di_tv);
24076 }
24077 }
24078
24079 /* Make a copy of the a:000 items, since we didn't do that above. */
24080 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24081 copy_tv(&li->li_tv, &li->li_tv);
24082 }
24083}
24084
24085/*
24086 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024087 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024088 */
24089 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024090can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024091{
24092 return (fc->l_varlist.lv_copyID != copyID
24093 && fc->l_vars.dv_copyID != copyID
24094 && fc->l_avars.dv_copyID != copyID);
24095}
24096
24097/*
24098 * Free "fc" and what it contains.
24099 */
24100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024101free_funccal(
24102 funccall_T *fc,
24103 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024104{
24105 listitem_T *li;
24106
24107 /* The a: variables typevals may not have been allocated, only free the
24108 * allocated variables. */
24109 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24110
24111 /* free all l: variables */
24112 vars_clear(&fc->l_vars.dv_hashtab);
24113
24114 /* Free the a:000 variables if they were allocated. */
24115 if (free_val)
24116 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24117 clear_tv(&li->li_tv);
24118
24119 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120}
24121
24122/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024123 * Add a number variable "name" to dict "dp" with value "nr".
24124 */
24125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024126add_nr_var(
24127 dict_T *dp,
24128 dictitem_T *v,
24129 char *name,
24130 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024131{
24132 STRCPY(v->di_key, name);
24133 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24134 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24135 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024136 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024137 v->di_tv.vval.v_number = nr;
24138}
24139
24140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141 * ":return [expr]"
24142 */
24143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024144ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145{
24146 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024147 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 int returning = FALSE;
24149
24150 if (current_funccal == NULL)
24151 {
24152 EMSG(_("E133: :return not inside a function"));
24153 return;
24154 }
24155
24156 if (eap->skip)
24157 ++emsg_skip;
24158
24159 eap->nextcmd = NULL;
24160 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024161 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 {
24163 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024164 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024166 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 }
24168 /* It's safer to return also on error. */
24169 else if (!eap->skip)
24170 {
24171 /*
24172 * Return unless the expression evaluation has been cancelled due to an
24173 * aborting error, an interrupt, or an exception.
24174 */
24175 if (!aborting())
24176 returning = do_return(eap, FALSE, TRUE, NULL);
24177 }
24178
24179 /* When skipping or the return gets pending, advance to the next command
24180 * in this line (!returning). Otherwise, ignore the rest of the line.
24181 * Following lines will be ignored by get_func_line(). */
24182 if (returning)
24183 eap->nextcmd = NULL;
24184 else if (eap->nextcmd == NULL) /* no argument */
24185 eap->nextcmd = check_nextcmd(arg);
24186
24187 if (eap->skip)
24188 --emsg_skip;
24189}
24190
24191/*
24192 * Return from a function. Possibly makes the return pending. Also called
24193 * for a pending return at the ":endtry" or after returning from an extra
24194 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024195 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024196 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 * FALSE when the return gets pending.
24198 */
24199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024200do_return(
24201 exarg_T *eap,
24202 int reanimate,
24203 int is_cmd,
24204 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205{
24206 int idx;
24207 struct condstack *cstack = eap->cstack;
24208
24209 if (reanimate)
24210 /* Undo the return. */
24211 current_funccal->returned = FALSE;
24212
24213 /*
24214 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24215 * not in its finally clause (which then is to be executed next) is found.
24216 * In this case, make the ":return" pending for execution at the ":endtry".
24217 * Otherwise, return normally.
24218 */
24219 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24220 if (idx >= 0)
24221 {
24222 cstack->cs_pending[idx] = CSTP_RETURN;
24223
24224 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024225 /* A pending return again gets pending. "rettv" points to an
24226 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024228 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 else
24230 {
24231 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024232 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024234 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024236 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 {
24238 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024239 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024240 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 else
24242 EMSG(_(e_outofmem));
24243 }
24244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024245 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246
24247 if (reanimate)
24248 {
24249 /* The pending return value could be overwritten by a ":return"
24250 * without argument in a finally clause; reset the default
24251 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024252 current_funccal->rettv->v_type = VAR_NUMBER;
24253 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 }
24255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024256 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 }
24258 else
24259 {
24260 current_funccal->returned = TRUE;
24261
24262 /* If the return is carried out now, store the return value. For
24263 * a return immediately after reanimation, the value is already
24264 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024265 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024267 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024268 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024270 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 }
24272 }
24273
24274 return idx < 0;
24275}
24276
24277/*
24278 * Free the variable with a pending return value.
24279 */
24280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024281discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282{
Bram Moolenaar33570922005-01-25 22:26:29 +000024283 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284}
24285
24286/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024287 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 * is an allocated string. Used by report_pending() for verbose messages.
24289 */
24290 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024291get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024293 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024294 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024295 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024297 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024298 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024299 if (s == NULL)
24300 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024301
24302 STRCPY(IObuff, ":return ");
24303 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24304 if (STRLEN(s) + 8 >= IOSIZE)
24305 STRCPY(IObuff + IOSIZE - 4, "...");
24306 vim_free(tofree);
24307 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308}
24309
24310/*
24311 * Get next function line.
24312 * Called by do_cmdline() to get the next line.
24313 * Returns allocated string, or NULL for end of function.
24314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024316get_func_line(
24317 int c UNUSED,
24318 void *cookie,
24319 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320{
Bram Moolenaar33570922005-01-25 22:26:29 +000024321 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024322 ufunc_T *fp = fcp->func;
24323 char_u *retval;
24324 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325
24326 /* If breakpoints have been added/deleted need to check for it. */
24327 if (fcp->dbg_tick != debug_tick)
24328 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024329 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 sourcing_lnum);
24331 fcp->dbg_tick = debug_tick;
24332 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024333#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024334 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024335 func_line_end(cookie);
24336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337
Bram Moolenaar05159a02005-02-26 23:04:13 +000024338 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024339 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24340 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024341 retval = NULL;
24342 else
24343 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024344 /* Skip NULL lines (continuation lines). */
24345 while (fcp->linenr < gap->ga_len
24346 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24347 ++fcp->linenr;
24348 if (fcp->linenr >= gap->ga_len)
24349 retval = NULL;
24350 else
24351 {
24352 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24353 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024354#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024355 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024356 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024357#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 }
24360
24361 /* Did we encounter a breakpoint? */
24362 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24363 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024366 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 sourcing_lnum);
24368 fcp->dbg_tick = debug_tick;
24369 }
24370
24371 return retval;
24372}
24373
Bram Moolenaar05159a02005-02-26 23:04:13 +000024374#if defined(FEAT_PROFILE) || defined(PROTO)
24375/*
24376 * Called when starting to read a function line.
24377 * "sourcing_lnum" must be correct!
24378 * When skipping lines it may not actually be executed, but we won't find out
24379 * until later and we need to store the time now.
24380 */
24381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024382func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024383{
24384 funccall_T *fcp = (funccall_T *)cookie;
24385 ufunc_T *fp = fcp->func;
24386
24387 if (fp->uf_profiling && sourcing_lnum >= 1
24388 && sourcing_lnum <= fp->uf_lines.ga_len)
24389 {
24390 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024391 /* Skip continuation lines. */
24392 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24393 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024394 fp->uf_tml_execed = FALSE;
24395 profile_start(&fp->uf_tml_start);
24396 profile_zero(&fp->uf_tml_children);
24397 profile_get_wait(&fp->uf_tml_wait);
24398 }
24399}
24400
24401/*
24402 * Called when actually executing a function line.
24403 */
24404 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024405func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024406{
24407 funccall_T *fcp = (funccall_T *)cookie;
24408 ufunc_T *fp = fcp->func;
24409
24410 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24411 fp->uf_tml_execed = TRUE;
24412}
24413
24414/*
24415 * Called when done with a function line.
24416 */
24417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024418func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024419{
24420 funccall_T *fcp = (funccall_T *)cookie;
24421 ufunc_T *fp = fcp->func;
24422
24423 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24424 {
24425 if (fp->uf_tml_execed)
24426 {
24427 ++fp->uf_tml_count[fp->uf_tml_idx];
24428 profile_end(&fp->uf_tml_start);
24429 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024430 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024431 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24432 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024433 }
24434 fp->uf_tml_idx = -1;
24435 }
24436}
24437#endif
24438
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439/*
24440 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024441 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 */
24443 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024444func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445{
Bram Moolenaar33570922005-01-25 22:26:29 +000024446 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447
24448 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24449 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024450 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024451 || fcp->returned);
24452}
24453
24454/*
24455 * return TRUE if cookie indicates a function which "abort"s on errors.
24456 */
24457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024458func_has_abort(
24459 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024461 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462}
24463
24464#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24465typedef enum
24466{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024467 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24468 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24469 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470} var_flavour_T;
24471
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024472static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473
24474 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024475var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476{
24477 char_u *p = varname;
24478
24479 if (ASCII_ISUPPER(*p))
24480 {
24481 while (*(++p))
24482 if (ASCII_ISLOWER(*p))
24483 return VAR_FLAVOUR_SESSION;
24484 return VAR_FLAVOUR_VIMINFO;
24485 }
24486 else
24487 return VAR_FLAVOUR_DEFAULT;
24488}
24489#endif
24490
24491#if defined(FEAT_VIMINFO) || defined(PROTO)
24492/*
24493 * Restore global vars that start with a capital from the viminfo file
24494 */
24495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024496read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497{
24498 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024499 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024500 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024501 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502
24503 if (!writing && (find_viminfo_parameter('!') != NULL))
24504 {
24505 tab = vim_strchr(virp->vir_line + 1, '\t');
24506 if (tab != NULL)
24507 {
24508 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024509 switch (*tab)
24510 {
24511 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024512#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024513 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024514#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024515 case 'D': type = VAR_DICT; break;
24516 case 'L': type = VAR_LIST; break;
24517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518
24519 tab = vim_strchr(tab, '\t');
24520 if (tab != NULL)
24521 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024522 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024523 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024524 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024526#ifdef FEAT_FLOAT
24527 else if (type == VAR_FLOAT)
24528 (void)string2float(tab + 1, &tv.vval.v_float);
24529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024531 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024532 if (type == VAR_DICT || type == VAR_LIST)
24533 {
24534 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24535
24536 if (etv == NULL)
24537 /* Failed to parse back the dict or list, use it as a
24538 * string. */
24539 tv.v_type = VAR_STRING;
24540 else
24541 {
24542 vim_free(tv.vval.v_string);
24543 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024544 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024545 }
24546 }
24547
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024548 /* when in a function use global variables */
24549 save_funccal = current_funccal;
24550 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024551 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024552 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024553
24554 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024555 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024556 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24557 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 }
24559 }
24560 }
24561
24562 return viminfo_readline(virp);
24563}
24564
24565/*
24566 * Write global vars that start with a capital to the viminfo file
24567 */
24568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024569write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570{
Bram Moolenaar33570922005-01-25 22:26:29 +000024571 hashitem_T *hi;
24572 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024573 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024574 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024575 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024576 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024577 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578
24579 if (find_viminfo_parameter('!') == NULL)
24580 return;
24581
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024582 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024583
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024584 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024585 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024587 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024589 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024590 this_var = HI2DI(hi);
24591 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024593 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024594 {
24595 case VAR_STRING: s = "STR"; break;
24596 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024597#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024598 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024599#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024600 case VAR_DICT: s = "DIC"; break;
24601 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024602 default: continue;
24603 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024604 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024605 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024606 if (p != NULL)
24607 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024608 vim_free(tofree);
24609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 }
24611 }
24612}
24613#endif
24614
24615#if defined(FEAT_SESSION) || defined(PROTO)
24616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024617store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618{
Bram Moolenaar33570922005-01-25 22:26:29 +000024619 hashitem_T *hi;
24620 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024621 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 char_u *p, *t;
24623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024624 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024625 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024627 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024629 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024630 this_var = HI2DI(hi);
24631 if ((this_var->di_tv.v_type == VAR_NUMBER
24632 || this_var->di_tv.v_type == VAR_STRING)
24633 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024634 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024635 /* Escape special characters with a backslash. Turn a LF and
24636 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024637 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024638 (char_u *)"\\\"\n\r");
24639 if (p == NULL) /* out of memory */
24640 break;
24641 for (t = p; *t != NUL; ++t)
24642 if (*t == '\n')
24643 *t = 'n';
24644 else if (*t == '\r')
24645 *t = 'r';
24646 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024647 this_var->di_key,
24648 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24649 : ' ',
24650 p,
24651 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24652 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024653 || put_eol(fd) == FAIL)
24654 {
24655 vim_free(p);
24656 return FAIL;
24657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 vim_free(p);
24659 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024660#ifdef FEAT_FLOAT
24661 else if (this_var->di_tv.v_type == VAR_FLOAT
24662 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24663 {
24664 float_T f = this_var->di_tv.vval.v_float;
24665 int sign = ' ';
24666
24667 if (f < 0)
24668 {
24669 f = -f;
24670 sign = '-';
24671 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024672 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024673 this_var->di_key, sign, f) < 0)
24674 || put_eol(fd) == FAIL)
24675 return FAIL;
24676 }
24677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678 }
24679 }
24680 return OK;
24681}
24682#endif
24683
Bram Moolenaar661b1822005-07-28 22:36:45 +000024684/*
24685 * Display script name where an item was last set.
24686 * Should only be invoked when 'verbose' is non-zero.
24687 */
24688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024689last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024690{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024691 char_u *p;
24692
Bram Moolenaar661b1822005-07-28 22:36:45 +000024693 if (scriptID != 0)
24694 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024695 p = home_replace_save(NULL, get_scriptname(scriptID));
24696 if (p != NULL)
24697 {
24698 verbose_enter();
24699 MSG_PUTS(_("\n\tLast set from "));
24700 MSG_PUTS(p);
24701 vim_free(p);
24702 verbose_leave();
24703 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024704 }
24705}
24706
Bram Moolenaard812df62008-11-09 12:46:09 +000024707/*
24708 * List v:oldfiles in a nice way.
24709 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024711ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024712{
24713 list_T *l = vimvars[VV_OLDFILES].vv_list;
24714 listitem_T *li;
24715 int nr = 0;
24716
24717 if (l == NULL)
24718 msg((char_u *)_("No old files"));
24719 else
24720 {
24721 msg_start();
24722 msg_scroll = TRUE;
24723 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24724 {
24725 msg_outnum((long)++nr);
24726 MSG_PUTS(": ");
24727 msg_outtrans(get_tv_string(&li->li_tv));
24728 msg_putchar('\n');
24729 out_flush(); /* output one line at a time */
24730 ui_breakcheck();
24731 }
24732 /* Assume "got_int" was set to truncate the listing. */
24733 got_int = FALSE;
24734
24735#ifdef FEAT_BROWSE_CMD
24736 if (cmdmod.browse)
24737 {
24738 quit_more = FALSE;
24739 nr = prompt_for_number(FALSE);
24740 msg_starthere();
24741 if (nr > 0)
24742 {
24743 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24744 (long)nr);
24745
24746 if (p != NULL)
24747 {
24748 p = expand_env_save(p);
24749 eap->arg = p;
24750 eap->cmdidx = CMD_edit;
24751 cmdmod.browse = FALSE;
24752 do_exedit(eap, NULL);
24753 vim_free(p);
24754 }
24755 }
24756 }
24757#endif
24758 }
24759}
24760
Bram Moolenaar53744302015-07-17 17:38:22 +020024761/* reset v:option_new, v:option_old and v:option_type */
24762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024763reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024764{
24765 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24766 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24767 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24768}
24769
24770
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771#endif /* FEAT_EVAL */
24772
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024774#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775
24776#ifdef WIN3264
24777/*
24778 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24779 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024780static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24781static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24782static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783
24784/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024785 * Get the short path (8.3) for the filename in "fnamep".
24786 * Only works for a valid file name.
24787 * When the path gets longer "fnamep" is changed and the allocated buffer
24788 * is put in "bufp".
24789 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24790 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791 */
24792 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024793get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024795 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024796 char_u *newbuf;
24797
24798 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799 l = GetShortPathName(*fnamep, *fnamep, len);
24800 if (l > len - 1)
24801 {
24802 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024803 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 newbuf = vim_strnsave(*fnamep, l);
24805 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024806 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807
24808 vim_free(*bufp);
24809 *fnamep = *bufp = newbuf;
24810
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024811 /* Really should always succeed, as the buffer is big enough. */
24812 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 }
24814
24815 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024816 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817}
24818
24819/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024820 * Get the short path (8.3) for the filename in "fname". The converted
24821 * path is returned in "bufp".
24822 *
24823 * Some of the directories specified in "fname" may not exist. This function
24824 * will shorten the existing directories at the beginning of the path and then
24825 * append the remaining non-existing path.
24826 *
24827 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024828 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024829 * bufp - Pointer to an allocated buffer for the filename.
24830 * fnamelen - Length of the filename pointed to by fname
24831 *
24832 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833 */
24834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024835shortpath_for_invalid_fname(
24836 char_u **fname,
24837 char_u **bufp,
24838 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024840 char_u *short_fname, *save_fname, *pbuf_unused;
24841 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024843 int old_len, len;
24844 int new_len, sfx_len;
24845 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846
24847 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024848 old_len = *fnamelen;
24849 save_fname = vim_strnsave(*fname, old_len);
24850 pbuf_unused = NULL;
24851 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024853 endp = save_fname + old_len - 1; /* Find the end of the copy */
24854 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024856 /*
24857 * Try shortening the supplied path till it succeeds by removing one
24858 * directory at a time from the tail of the path.
24859 */
24860 len = 0;
24861 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024863 /* go back one path-separator */
24864 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24865 --endp;
24866 if (endp <= save_fname)
24867 break; /* processed the complete path */
24868
24869 /*
24870 * Replace the path separator with a NUL and try to shorten the
24871 * resulting path.
24872 */
24873 ch = *endp;
24874 *endp = 0;
24875 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024876 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024877 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24878 {
24879 retval = FAIL;
24880 goto theend;
24881 }
24882 *endp = ch; /* preserve the string */
24883
24884 if (len > 0)
24885 break; /* successfully shortened the path */
24886
24887 /* failed to shorten the path. Skip the path separator */
24888 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889 }
24890
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024891 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024893 /*
24894 * Succeeded in shortening the path. Now concatenate the shortened
24895 * path with the remaining path at the tail.
24896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024898 /* Compute the length of the new path. */
24899 sfx_len = (int)(save_endp - endp) + 1;
24900 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024902 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024904 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024906 /* There is not enough space in the currently allocated string,
24907 * copy it to a buffer big enough. */
24908 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024910 {
24911 retval = FAIL;
24912 goto theend;
24913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 }
24915 else
24916 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024917 /* Transfer short_fname to the main buffer (it's big enough),
24918 * unless get_short_pathname() did its work in-place. */
24919 *fname = *bufp = save_fname;
24920 if (short_fname != save_fname)
24921 vim_strncpy(save_fname, short_fname, len);
24922 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024924
24925 /* concat the not-shortened part of the path */
24926 vim_strncpy(*fname + len, endp, sfx_len);
24927 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024929
24930theend:
24931 vim_free(pbuf_unused);
24932 vim_free(save_fname);
24933
24934 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935}
24936
24937/*
24938 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024939 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 */
24941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024942shortpath_for_partial(
24943 char_u **fnamep,
24944 char_u **bufp,
24945 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946{
24947 int sepcount, len, tflen;
24948 char_u *p;
24949 char_u *pbuf, *tfname;
24950 int hasTilde;
24951
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024952 /* Count up the path separators from the RHS.. so we know which part
24953 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024955 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956 if (vim_ispathsep(*p))
24957 ++sepcount;
24958
24959 /* Need full path first (use expand_env() to remove a "~/") */
24960 hasTilde = (**fnamep == '~');
24961 if (hasTilde)
24962 pbuf = tfname = expand_env_save(*fnamep);
24963 else
24964 pbuf = tfname = FullName_save(*fnamep, FALSE);
24965
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024966 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024968 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24969 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970
24971 if (len == 0)
24972 {
24973 /* Don't have a valid filename, so shorten the rest of the
24974 * path if we can. This CAN give us invalid 8.3 filenames, but
24975 * there's not a lot of point in guessing what it might be.
24976 */
24977 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024978 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24979 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980 }
24981
24982 /* Count the paths backward to find the beginning of the desired string. */
24983 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024984 {
24985#ifdef FEAT_MBYTE
24986 if (has_mbyte)
24987 p -= mb_head_off(tfname, p);
24988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024989 if (vim_ispathsep(*p))
24990 {
24991 if (sepcount == 0 || (hasTilde && sepcount == 1))
24992 break;
24993 else
24994 sepcount --;
24995 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 if (hasTilde)
24998 {
24999 --p;
25000 if (p >= tfname)
25001 *p = '~';
25002 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 }
25005 else
25006 ++p;
25007
25008 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25009 vim_free(*bufp);
25010 *fnamelen = (int)STRLEN(p);
25011 *bufp = pbuf;
25012 *fnamep = p;
25013
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025014 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015}
25016#endif /* WIN3264 */
25017
25018/*
25019 * Adjust a filename, according to a string of modifiers.
25020 * *fnamep must be NUL terminated when called. When returning, the length is
25021 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025022 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023 * When there is an error, *fnamep is set to NULL.
25024 */
25025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025026modify_fname(
25027 char_u *src, /* string with modifiers */
25028 int *usedlen, /* characters after src that are used */
25029 char_u **fnamep, /* file name so far */
25030 char_u **bufp, /* buffer for allocated file name or NULL */
25031 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032{
25033 int valid = 0;
25034 char_u *tail;
25035 char_u *s, *p, *pbuf;
25036 char_u dirname[MAXPATHL];
25037 int c;
25038 int has_fullname = 0;
25039#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025040 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 int has_shortname = 0;
25042#endif
25043
25044repeat:
25045 /* ":p" - full path/file_name */
25046 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25047 {
25048 has_fullname = 1;
25049
25050 valid |= VALID_PATH;
25051 *usedlen += 2;
25052
25053 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25054 if ((*fnamep)[0] == '~'
25055#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25056 && ((*fnamep)[1] == '/'
25057# ifdef BACKSLASH_IN_FILENAME
25058 || (*fnamep)[1] == '\\'
25059# endif
25060 || (*fnamep)[1] == NUL)
25061
25062#endif
25063 )
25064 {
25065 *fnamep = expand_env_save(*fnamep);
25066 vim_free(*bufp); /* free any allocated file name */
25067 *bufp = *fnamep;
25068 if (*fnamep == NULL)
25069 return -1;
25070 }
25071
25072 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025073 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 {
25075 if (vim_ispathsep(*p)
25076 && p[1] == '.'
25077 && (p[2] == NUL
25078 || vim_ispathsep(p[2])
25079 || (p[2] == '.'
25080 && (p[3] == NUL || vim_ispathsep(p[3])))))
25081 break;
25082 }
25083
25084 /* FullName_save() is slow, don't use it when not needed. */
25085 if (*p != NUL || !vim_isAbsName(*fnamep))
25086 {
25087 *fnamep = FullName_save(*fnamep, *p != NUL);
25088 vim_free(*bufp); /* free any allocated file name */
25089 *bufp = *fnamep;
25090 if (*fnamep == NULL)
25091 return -1;
25092 }
25093
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025094#ifdef WIN3264
25095# if _WIN32_WINNT >= 0x0500
25096 if (vim_strchr(*fnamep, '~') != NULL)
25097 {
25098 /* Expand 8.3 filename to full path. Needed to make sure the same
25099 * file does not have two different names.
25100 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25101 p = alloc(_MAX_PATH + 1);
25102 if (p != NULL)
25103 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025104 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025105 {
25106 vim_free(*bufp);
25107 *bufp = *fnamep = p;
25108 }
25109 else
25110 vim_free(p);
25111 }
25112 }
25113# endif
25114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 /* Append a path separator to a directory. */
25116 if (mch_isdir(*fnamep))
25117 {
25118 /* Make room for one or two extra characters. */
25119 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25120 vim_free(*bufp); /* free any allocated file name */
25121 *bufp = *fnamep;
25122 if (*fnamep == NULL)
25123 return -1;
25124 add_pathsep(*fnamep);
25125 }
25126 }
25127
25128 /* ":." - path relative to the current directory */
25129 /* ":~" - path relative to the home directory */
25130 /* ":8" - shortname path - postponed till after */
25131 while (src[*usedlen] == ':'
25132 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25133 {
25134 *usedlen += 2;
25135 if (c == '8')
25136 {
25137#ifdef WIN3264
25138 has_shortname = 1; /* Postpone this. */
25139#endif
25140 continue;
25141 }
25142 pbuf = NULL;
25143 /* Need full path first (use expand_env() to remove a "~/") */
25144 if (!has_fullname)
25145 {
25146 if (c == '.' && **fnamep == '~')
25147 p = pbuf = expand_env_save(*fnamep);
25148 else
25149 p = pbuf = FullName_save(*fnamep, FALSE);
25150 }
25151 else
25152 p = *fnamep;
25153
25154 has_fullname = 0;
25155
25156 if (p != NULL)
25157 {
25158 if (c == '.')
25159 {
25160 mch_dirname(dirname, MAXPATHL);
25161 s = shorten_fname(p, dirname);
25162 if (s != NULL)
25163 {
25164 *fnamep = s;
25165 if (pbuf != NULL)
25166 {
25167 vim_free(*bufp); /* free any allocated file name */
25168 *bufp = pbuf;
25169 pbuf = NULL;
25170 }
25171 }
25172 }
25173 else
25174 {
25175 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25176 /* Only replace it when it starts with '~' */
25177 if (*dirname == '~')
25178 {
25179 s = vim_strsave(dirname);
25180 if (s != NULL)
25181 {
25182 *fnamep = s;
25183 vim_free(*bufp);
25184 *bufp = s;
25185 }
25186 }
25187 }
25188 vim_free(pbuf);
25189 }
25190 }
25191
25192 tail = gettail(*fnamep);
25193 *fnamelen = (int)STRLEN(*fnamep);
25194
25195 /* ":h" - head, remove "/file_name", can be repeated */
25196 /* Don't remove the first "/" or "c:\" */
25197 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25198 {
25199 valid |= VALID_HEAD;
25200 *usedlen += 2;
25201 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025202 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025203 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 *fnamelen = (int)(tail - *fnamep);
25205#ifdef VMS
25206 if (*fnamelen > 0)
25207 *fnamelen += 1; /* the path separator is part of the path */
25208#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025209 if (*fnamelen == 0)
25210 {
25211 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25212 p = vim_strsave((char_u *)".");
25213 if (p == NULL)
25214 return -1;
25215 vim_free(*bufp);
25216 *bufp = *fnamep = tail = p;
25217 *fnamelen = 1;
25218 }
25219 else
25220 {
25221 while (tail > s && !after_pathsep(s, tail))
25222 mb_ptr_back(*fnamep, tail);
25223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025224 }
25225
25226 /* ":8" - shortname */
25227 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25228 {
25229 *usedlen += 2;
25230#ifdef WIN3264
25231 has_shortname = 1;
25232#endif
25233 }
25234
25235#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025236 /*
25237 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238 */
25239 if (has_shortname)
25240 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025241 /* Copy the string if it is shortened by :h and when it wasn't copied
25242 * yet, because we are going to change it in place. Avoids changing
25243 * the buffer name for "%:8". */
25244 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 {
25246 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025247 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248 return -1;
25249 vim_free(*bufp);
25250 *bufp = *fnamep = p;
25251 }
25252
25253 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025254 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 if (!has_fullname && !vim_isAbsName(*fnamep))
25256 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025257 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 return -1;
25259 }
25260 else
25261 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025262 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263
Bram Moolenaardc935552011-08-17 15:23:23 +020025264 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025266 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 return -1;
25268
25269 if (l == 0)
25270 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025271 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025273 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 return -1;
25275 }
25276 *fnamelen = l;
25277 }
25278 }
25279#endif /* WIN3264 */
25280
25281 /* ":t" - tail, just the basename */
25282 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25283 {
25284 *usedlen += 2;
25285 *fnamelen -= (int)(tail - *fnamep);
25286 *fnamep = tail;
25287 }
25288
25289 /* ":e" - extension, can be repeated */
25290 /* ":r" - root, without extension, can be repeated */
25291 while (src[*usedlen] == ':'
25292 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25293 {
25294 /* find a '.' in the tail:
25295 * - for second :e: before the current fname
25296 * - otherwise: The last '.'
25297 */
25298 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25299 s = *fnamep - 2;
25300 else
25301 s = *fnamep + *fnamelen - 1;
25302 for ( ; s > tail; --s)
25303 if (s[0] == '.')
25304 break;
25305 if (src[*usedlen + 1] == 'e') /* :e */
25306 {
25307 if (s > tail)
25308 {
25309 *fnamelen += (int)(*fnamep - (s + 1));
25310 *fnamep = s + 1;
25311#ifdef VMS
25312 /* cut version from the extension */
25313 s = *fnamep + *fnamelen - 1;
25314 for ( ; s > *fnamep; --s)
25315 if (s[0] == ';')
25316 break;
25317 if (s > *fnamep)
25318 *fnamelen = s - *fnamep;
25319#endif
25320 }
25321 else if (*fnamep <= tail)
25322 *fnamelen = 0;
25323 }
25324 else /* :r */
25325 {
25326 if (s > tail) /* remove one extension */
25327 *fnamelen = (int)(s - *fnamep);
25328 }
25329 *usedlen += 2;
25330 }
25331
25332 /* ":s?pat?foo?" - substitute */
25333 /* ":gs?pat?foo?" - global substitute */
25334 if (src[*usedlen] == ':'
25335 && (src[*usedlen + 1] == 's'
25336 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25337 {
25338 char_u *str;
25339 char_u *pat;
25340 char_u *sub;
25341 int sep;
25342 char_u *flags;
25343 int didit = FALSE;
25344
25345 flags = (char_u *)"";
25346 s = src + *usedlen + 2;
25347 if (src[*usedlen + 1] == 'g')
25348 {
25349 flags = (char_u *)"g";
25350 ++s;
25351 }
25352
25353 sep = *s++;
25354 if (sep)
25355 {
25356 /* find end of pattern */
25357 p = vim_strchr(s, sep);
25358 if (p != NULL)
25359 {
25360 pat = vim_strnsave(s, (int)(p - s));
25361 if (pat != NULL)
25362 {
25363 s = p + 1;
25364 /* find end of substitution */
25365 p = vim_strchr(s, sep);
25366 if (p != NULL)
25367 {
25368 sub = vim_strnsave(s, (int)(p - s));
25369 str = vim_strnsave(*fnamep, *fnamelen);
25370 if (sub != NULL && str != NULL)
25371 {
25372 *usedlen = (int)(p + 1 - src);
25373 s = do_string_sub(str, pat, sub, flags);
25374 if (s != NULL)
25375 {
25376 *fnamep = s;
25377 *fnamelen = (int)STRLEN(s);
25378 vim_free(*bufp);
25379 *bufp = s;
25380 didit = TRUE;
25381 }
25382 }
25383 vim_free(sub);
25384 vim_free(str);
25385 }
25386 vim_free(pat);
25387 }
25388 }
25389 /* after using ":s", repeat all the modifiers */
25390 if (didit)
25391 goto repeat;
25392 }
25393 }
25394
Bram Moolenaar26df0922014-02-23 23:39:13 +010025395 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25396 {
25397 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25398 if (p == NULL)
25399 return -1;
25400 vim_free(*bufp);
25401 *bufp = *fnamep = p;
25402 *fnamelen = (int)STRLEN(p);
25403 *usedlen += 2;
25404 }
25405
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 return valid;
25407}
25408
25409/*
25410 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25411 * "flags" can be "g" to do a global substitute.
25412 * Returns an allocated string, NULL for error.
25413 */
25414 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025415do_string_sub(
25416 char_u *str,
25417 char_u *pat,
25418 char_u *sub,
25419 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420{
25421 int sublen;
25422 regmatch_T regmatch;
25423 int i;
25424 int do_all;
25425 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025426 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427 garray_T ga;
25428 char_u *ret;
25429 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025430 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025431
25432 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25433 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025434 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435
25436 ga_init2(&ga, 1, 200);
25437
25438 do_all = (flags[0] == 'g');
25439
25440 regmatch.rm_ic = p_ic;
25441 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25442 if (regmatch.regprog != NULL)
25443 {
25444 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025445 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25447 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025448 /* Skip empty match except for first match. */
25449 if (regmatch.startp[0] == regmatch.endp[0])
25450 {
25451 if (zero_width == regmatch.startp[0])
25452 {
25453 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025454 i = MB_PTR2LEN(tail);
25455 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25456 (size_t)i);
25457 ga.ga_len += i;
25458 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025459 continue;
25460 }
25461 zero_width = regmatch.startp[0];
25462 }
25463
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 /*
25465 * Get some space for a temporary buffer to do the substitution
25466 * into. It will contain:
25467 * - The text up to where the match is.
25468 * - The substituted text.
25469 * - The text after the match.
25470 */
25471 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025472 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25474 {
25475 ga_clear(&ga);
25476 break;
25477 }
25478
25479 /* copy the text up to where the match is */
25480 i = (int)(regmatch.startp[0] - tail);
25481 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25482 /* add the substituted text */
25483 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25484 + ga.ga_len + i, TRUE, TRUE, FALSE);
25485 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025486 tail = regmatch.endp[0];
25487 if (*tail == NUL)
25488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 if (!do_all)
25490 break;
25491 }
25492
25493 if (ga.ga_data != NULL)
25494 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25495
Bram Moolenaar473de612013-06-08 18:19:48 +020025496 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497 }
25498
25499 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25500 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025501 if (p_cpo == empty_option)
25502 p_cpo = save_cpo;
25503 else
25504 /* Darn, evaluating {sub} expression changed the value. */
25505 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506
25507 return ret;
25508}
25509
25510#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */